Java Code Examples for org.bitcoinj.script.Script#getChunks()

The following examples show how to use org.bitcoinj.script.Script#getChunks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenerateLowSTests.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Convert a script to a string format that suits the style expected in
 * tx_valid.json and tx_invalid.json.
 */
private static String scriptToString(Script scriptPubKey) {
    final StringBuilder buf = new StringBuilder();
    for (ScriptChunk chunk: scriptPubKey.getChunks()) {
        if (buf.length() > 0) {
            buf.append(" ");
        }
        if (chunk.isOpCode()) {
            buf.append(getOpCodeName(chunk.opcode));
        } else if (chunk.data != null) {
            // Data chunk
            buf.append("0x")
                .append(Integer.toString(chunk.opcode, 16)).append(" 0x")
                .append(Utils.HEX.encode(chunk.data));
        } else {
            buf.append(chunk.toString());
        }
    }
    return buf.toString();
}
 
Example 2
Source File: GenerateLowSTests.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Convert a script to a string format that suits the style expected in
 * tx_valid.json and tx_invalid.json.
 */
private static String scriptToString(Script scriptPubKey) {
    final StringBuilder buf = new StringBuilder();
    for (ScriptChunk chunk: scriptPubKey.getChunks()) {
        if (buf.length() > 0) {
            buf.append(" ");
        }
        if (chunk.isOpCode()) {
            buf.append(getOpCodeName(chunk.opcode));
        } else if (chunk.data != null) {
            // Data chunk
            buf.append("0x")
                .append(Integer.toString(chunk.opcode, 16)).append(" 0x")
                .append(Utils.HEX.encode(chunk.data));
        } else {
            buf.append(chunk.toString());
        }
    }
    return buf.toString();
}
 
Example 3
Source File: TXUtil.java    From jelectrum with MIT License 6 votes vote down vote up
public static byte[] getPubKey(TransactionInput in) throws ScriptException
{
Script script = in.getScriptSig();
List<ScriptChunk> chunks = script.getChunks();

// Copied from Bitcoinj release 0.14 Script.java getPubKey
  // Should handle old style things fine.  Probably.

      if (chunks.size() != 2) {
          throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script not of right size, expecting 2 but got " + chunks.size());
      }
      final ScriptChunk chunk0 = chunks.get(0);
      final byte[] chunk0data = chunk0.data;
      final ScriptChunk chunk1 = chunks.get(1);
      final byte[] chunk1data = chunk1.data;
      if (chunk0data != null && chunk0data.length > 2 && chunk1data != null && chunk1data.length > 2) {
          // If we have two large constants assume the input to a pay-to-address output.
          return chunk1data;
      } else if (chunk1.equalsOpCode(OP_CHECKSIG) && chunk0data != null && chunk0data.length > 2) {
          // A large constant followed by an OP_CHECKSIG is the key.
          return chunk0data;
      } else {
          throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script did not match expected form: " + script);
      }
}
 
Example 4
Source File: ScriptTools.java    From thunder with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean testScript (byte[] script, byte[] template, byte[]... parameters) {

        Script s = new Script(script);
        List<ScriptChunk> chunks = s.getChunks();

        int parameter = 0;

        for (int i = 0; i < chunks.size(); i++) {
            boolean correct = false;
            ScriptChunk chunk = chunks.get(i);
            byte templateChunk = template[i];

            int op = templateChunk;
            if (op < 0) {
                op = op + 256;
            }
            if (op == 255) {
                if (chunk.isPushData()) {
                    if (Arrays.equals(chunk.data, parameters[parameter])) {
                        parameter++;
                        correct = true;
                    }
                }
            } else {
                if (chunk.opcode == op) {
                    correct = true;
                }
            }
            if (!correct) {
                return false;
            }

        }

        return true;
    }
 
Example 5
Source File: ScriptTools.java    From thundernetwork with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean testScript (byte[] script, byte[] template, byte[]... parameters) {

        Script s = new Script(script);
        List<ScriptChunk> chunks = s.getChunks();

        int parameter = 0;

        for (int i = 0; i < chunks.size(); i++) {
            boolean correct = false;
            ScriptChunk chunk = chunks.get(i);
            byte templateChunk = template[i];

            int op = templateChunk;
            if (op < 0) {
                op = op + 256;
            }
            if (op == 255) {
                if (chunk.isPushData()) {
                    if (Arrays.equals(chunk.data, parameters[parameter])) {
                        parameter++;
                        correct = true;
                    }
                }
            } else {
                if (chunk.opcode == op) {
                    correct = true;
                }
            }
            if (!correct) {
                return false;
            }

        }

        return true;
    }
 
Example 6
Source File: TXUtil.java    From jelectrum with MIT License 5 votes vote down vote up
public static ByteString parseInputScript(Script script)
{
  List<ScriptChunk> chunks = script.getChunks();


  if (chunks.size() == 1)
  if (chunks.get(0) != null)
  if (chunks.get(0).data != null)
  if (chunks.get(0).data.length == 22)
  {
    try (TimeRecordAuto tra = new TimeRecordAuto("txutil_parse_input_single"))
    {
      ByteString h = ByteString.copyFrom(chunks.get(0).data);
      h = Util.SHA256BIN(h);
      h = Util.RIPEMD160(h);
      ByteString prefix = HexUtil.hexStringToBytes("a914");
      ByteString suffix = HexUtil.hexStringToBytes("87");

      ByteString out_script = prefix.concat(h).concat(suffix);

      return Util.reverse(Util.SHA256BIN(out_script));
    }

  }

  return null;


}
 
Example 7
Source File: DumpTxList.java    From jelectrum with MIT License 4 votes vote down vote up
public static void main(String args[]) throws Exception
{
  Jelectrum jelly = new Jelectrum(new Config(args[0]));

  Scanner scan = new Scanner(new FileInputStream(args[1]));

  PrintStream pout = new PrintStream(new FileOutputStream(args[2], false));

  TXUtil txutil = new TXUtil(jelly.getDB(), jelly.getNetworkParameters());

  while(scan.hasNext())
  {
    String hash = scan.next();
    Transaction tx = jelly.getDB().getTransaction(new Sha256Hash(hash)).getTx(jelly.getNetworkParameters());


    int in_idx =0;
    for(TransactionInput in : tx.getInputs())
    {
      Address addr = in.getFromAddress();

      byte[] h160 = addr.getHash160();

      pout.println("txin:" + hash + ":" + in_idx + ":" + Hex.encodeHexString(h160));

      in_idx++;

      /*System.out.println("Input: " + in);
      Script script = in.getScriptSig();
      for(ScriptChunk chunk : script.getChunks())
      {
        if (chunk.isOpCode())
        {
          System.out.println("    op " + chunk.opcode);
        }
        if (chunk.isPushData() && (chunk.data != null))
        {
          System.out.println("    data " + chunk.data.length);
        }

      }*/
    }
    pout.println("tx:" + hash + ":" + txutil.getTXBlockHeight(tx, jelly.getBlockChainCache()));

  for(TransactionOutput out : tx.getOutputs())
  {
    int idx = out.getIndex();
    Script script = out.getScriptPubKey();

    for(ScriptChunk chunk : script.getChunks())
    {
      if (chunk.isOpCode())
      {
        //System.out.println("    op " + chunk.opcode);
      }
      if (chunk.isPushData() && (chunk.data != null))
      {
        pout.println("txout:" + hash + ":" + idx + ":" + Hex.encodeHexString(chunk.data));
      }

    }


  }

  }

  pout.flush();
  pout.close();



}
 
Example 8
Source File: DumpTxList.java    From jelectrum with MIT License 4 votes vote down vote up
public static boolean hasStrangeData(Transaction tx)
{
  try
  {
  boolean hasStrange = false;
  /*for(TransactionInput in : tx.getInputs())
  {
    Script script = in.getScriptSig();
    int data_in = 0;
     for(ScriptChunk chunk : script.getChunks())
    {
      if (chunk.isOpCode())
      {
      }
      if (chunk.isPushData() && (chunk.data != null))
      {
        data_in += chunk.data.length;
      }

    }
    if (data_in > 20) return true;

  }*/
  int extra_data = 0;

  for(TransactionOutput out : tx.getOutputs())
  {
    int data_out = 0;
    Script script = out.getScriptPubKey();

    for(ScriptChunk chunk : script.getChunks())
    {
      if (chunk.isOpCode())
      {
      }
      if (chunk.isPushData() && (chunk.data != null))
      {
        data_out += chunk.data.length;
      }

    }
    if (data_out > 20) extra_data += data_out;


  }

  if (extra_data > 20) return true;

  return false;
  }

  catch(Throwable t){return true;}


}
 
Example 9
Source File: UtxoTrieMgr.java    From jelectrum with MIT License 4 votes vote down vote up
public static byte[] figureOutStrange(Script script, TransactionOutput out, NetworkParameters params)
{
  if (script == null) return null;

  //org.bitcoinj.core.Utils.sha256hash160 
  List<ScriptChunk> chunks = script.getChunks();
  /*System.out.println("STRANGE: " + out.getParentTransaction().getHash() + " - has strange chunks " + chunks.size());
  for(int i =0; i<chunks.size(); i++)
  {
    System.out.print("Chunk " + i + " ");
    System.out.print(Hex.encodeHex(chunks.get(i).data)); 
    System.out.println(" " + getOpCodeName(chunks.get(i).data[0]));
  }*/

  //Remember, java bytes are signed because hate
  //System.out.println(out);
  //System.out.println(script);
  if (chunks.size() == 6)
  {
    if (chunks.get(0).equalsOpCode(OP_DUP))
    if (chunks.get(1).equalsOpCode(OP_HASH160))
    if (chunks.get(3).equalsOpCode(OP_EQUALVERIFY))
    if (chunks.get(4).equalsOpCode(OP_CHECKSIG))
    if (chunks.get(5).equalsOpCode(OP_NOP))
    if (chunks.get(2).isPushData())
    if (chunks.get(2).data.length == 20)
    {
      return chunks.get(2).data;
    }

  }
  
  /*if ((chunks.size() == 6) && (chunks.get(2).data.length == 20))
  {
    for(int i=0; i<6; i++)
    {
      if (chunks.get(i) == null) return null;
      if (chunks.get(i).data == null) return null;
      if (i != 2)
      {
        if (chunks.get(i).data.length != 1) return null;
      }
    }
    if (chunks.get(0).data[0] == OP_DUP)
    if ((int)(chunks.get(1).data[0] & 0xFF) == OP_HASH160)
    if ((int)(chunks.get(3).data[0] & 0xFF) == OP_EQUALVERIFY)
    if ((int)(chunks.get(4).data[0] & 0xFF) == OP_CHECKSIG)
    if (chunks.get(5).data[0] == OP_NOP)
    {
      return chunks.get(2).data;
    }
  }*/
  return null;

}
 
Example 10
Source File: DumpTx.java    From jelectrum with MIT License 4 votes vote down vote up
public static boolean hasStrangeData(Transaction tx)
{
  try
  {
  boolean hasStrange = false;
  /*for(TransactionInput in : tx.getInputs())
  {
    Script script = in.getScriptSig();
    int data_in = 0;
     for(ScriptChunk chunk : script.getChunks())
    {
      if (chunk.isOpCode())
      {
      }
      if (chunk.isPushData() && (chunk.data != null))
      {
        data_in += chunk.data.length;
      }

    }
    if (data_in > 20) return true;

  }*/
  int extra_data = 0;

  for(TransactionOutput out : tx.getOutputs())
  {
    int data_out = 0;
    Script script = out.getScriptPubKey();

    for(ScriptChunk chunk : script.getChunks())
    {
      if (chunk.isOpCode())
      {
      }
      if (chunk.isPushData() && (chunk.data != null))
      {
        data_out += chunk.data.length;
      }

    }
    if (data_out > 20) extra_data += data_out;


  }

  if (extra_data > 20) return true;

  return false;
  }

  catch(Throwable t){return true;}


}