Exemple #1
0
  private Script parseScriptString(String string) throws Exception {
    String[] words = string.split("[ \\t\\n]");

    UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream();

    for (String w : words) {
      if (w.equals("")) continue;
      if (w.matches("^-?[0-9]*$")) {
        // Number
        long val = Long.parseLong(w);
        if (val >= -1 && val <= 16) out.write(Script.encodeToOpN((int) val));
        else
          Script.writeBytes(
              out, Utils.reverseBytes(Utils.encodeMPI(BigInteger.valueOf(val), false)));
      } else if (w.matches("^0x[0-9a-fA-F]*$")) {
        // Raw hex data, inserted NOT pushed onto stack:
        out.write(Hex.decode(w.substring(2)));
      } else if (w.length() >= 2 && w.startsWith("'") && w.endsWith("'")) {
        // Single-quoted string, pushed as data. NOTE: this is poor-man's
        // parsing, spaces/tabs/newlines in single-quoted strings won't work.
        Script.writeBytes(out, w.substring(1, w.length() - 1).getBytes(Charset.forName("UTF-8")));
      } else if (ScriptOpCodes.getOpCode(w) != OP_INVALIDOPCODE) {
        // opcode, e.g. OP_ADD or OP_1:
        out.write(ScriptOpCodes.getOpCode(w));
      } else if (w.startsWith("OP_")
          && ScriptOpCodes.getOpCode(w.substring(3)) != OP_INVALIDOPCODE) {
        // opcode, e.g. OP_ADD or OP_1:
        out.write(ScriptOpCodes.getOpCode(w.substring(3)));
      } else {
        throw new RuntimeException("Invalid Data");
      }
    }

    return new Script(out.toByteArray());
  }