org.bitcoinj.script.ScriptChunk Java Examples

The following examples show how to use org.bitcoinj.script.ScriptChunk. 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 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 #2
Source File: DefaultRiskAnalysis.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Checks if the given input passes some of the AreInputsStandard checks. Not complete.
 */
public static RuleViolation isInputStandard(TransactionInput input) {
    for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
        if (chunk.data != null && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
        if (chunk.isPushData()) {
            ECDSASignature signature;
            try {
                signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
            } catch (IllegalArgumentException x) {
                // Doesn't look like a signature.
                signature = null;
            }
            if (signature != null) {
                if (!TransactionSignature.isEncodingCanonical(chunk.data))
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
                if (!signature.isCanonical())
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
            }
        }
    }
    return RuleViolation.NONE;
}
 
Example #3
Source File: BisqRiskAnalysis.java    From bisq-core with GNU Affero General Public License v3.0 6 votes vote down vote up
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
    for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
        if (chunk.data != null && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
        if (chunk.isPushData()) {
            ECDSASignature signature;
            try {
                signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
            } catch (RuntimeException x) {
                // Doesn't look like a signature.
                signature = null;
            }
            if (signature != null) {
                if (!TransactionSignature.isEncodingCanonical(chunk.data))
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
                if (!signature.isCanonical())
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
            }
        }
    }
    return RuleViolation.NONE;
}
 
Example #4
Source File: BisqRiskAnalysis.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
    for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
        if (chunk.data != null && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
        if (chunk.isPushData()) {
            ECDSASignature signature;
            try {
                signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
            } catch (RuntimeException x) {
                // Doesn't look like a signature.
                signature = null;
            }
            if (signature != null) {
                if (!TransactionSignature.isEncodingCanonical(chunk.data))
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
                if (!signature.isCanonical())
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
            }
        }
    }
    return RuleViolation.NONE;
}
 
Example #5
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 #6
Source File: DefaultRiskAnalysis.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
    for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
        if (chunk.data != null && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
        if (chunk.isPushData()) {
            ECDSASignature signature;
            try {
                signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
            } catch (RuntimeException x) {
                // Doesn't look like a signature.
                signature = null;
            }
            if (signature != null) {
                if (!TransactionSignature.isEncodingCanonical(chunk.data))
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
                if (!signature.isCanonical())
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
            }
        }
    }
    return RuleViolation.NONE;
}
 
Example #7
Source File: DefaultRiskAnalysis.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
    for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
        if (chunk.data != null && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
        if (chunk.isPushData()) {
            ECDSASignature signature;
            try {
                signature = ECKey.ECDSASignature.decodeFromDER(chunk.data);
            } catch (RuntimeException x) {
                // Doesn't look like a signature.
                signature = null;
            }
            if (signature != null) {
                if (!TransactionSignature.isEncodingCanonical(chunk.data))
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
                if (!signature.isCanonical())
                    return RuleViolation.SIGNATURE_CANONICAL_ENCODING;
            }
        }
    }
    return RuleViolation.NONE;
}
 
Example #8
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 #9
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 5 votes vote down vote up
private Script substituteAllBinding() {

        ScriptBuilder sb = new ScriptBuilder();

        for (ScriptChunk chunk : getChunks()) {

            if (isVariable(chunk)) {
                String name = getVariableName(chunk);
                Object obj = getValue(name);
                Class<?> expectedClass = getType(name);

                if (expectedClass.isInstance(obj)) {
                    for (ScriptChunk ch : BitcoinUtils.toScript(obj).getChunks()) {
                        sb.addChunk(ch);
                    }
                }
                else
                    throw new IllegalArgumentException(
                        "expected class " + expectedClass.getName() + ", got " + obj.getClass().getName());
            }
            else {
                sb.addChunk(chunk);
            }
        }

        return sb.build();
    }
 
Example #10
Source File: AbstractScriptBuilder.java    From balzac with Apache License 2.0 5 votes vote down vote up
private static ImmutableList<ScriptChunk> optimize(ImmutableList<ScriptChunk> scriptChunks) {
    if (scriptChunks.size() == 0 || scriptChunks.size() == 1) {
        return scriptChunks;
    }

    ScriptChunk ch1 = scriptChunks.get(0);
    ScriptChunk ch2 = scriptChunks.get(1);

    if (ch1.equalsOpCode(ScriptOpCodes.OP_TOALTSTACK) && ch2.equalsOpCode(ScriptOpCodes.OP_FROMALTSTACK)) {
        return optimize(scriptChunks.subList(2, scriptChunks.size()));
    }

    return ImmutableList.<ScriptChunk>builder().add(scriptChunks.get(0))
        .addAll(optimize(scriptChunks.subList(1, scriptChunks.size()))).build();
}
 
Example #11
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 5 votes vote down vote up
/**
 * Extract a string representation of this builder.
 * 
 * @return a string representing this builder
 */
public String serialize() {
    StringBuilder str = new StringBuilder();
    for (ScriptChunk ch : getChunks()) {
        str.append(serializeChunk(ch)).append(" ");
    }
    return str.toString().trim();
}
 
Example #12
Source File: BisqRiskAnalysis.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Checks the output to see if the script violates a standardness rule. Not complete.
 */
public static RuleViolation isOutputStandard(TransactionOutput output) {
    // OP_RETURN has usually output value zero, so we exclude that from the MIN_ANALYSIS_NONDUST_OUTPUT check
    if (!output.getScriptPubKey().isOpReturn()
            && output.getValue().compareTo(MIN_ANALYSIS_NONDUST_OUTPUT) < 0)
        return RuleViolation.DUST;
    for (ScriptChunk chunk : output.getScriptPubKey().getChunks()) {
        if (chunk.isPushData() && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
    }
    return RuleViolation.NONE;
}
 
Example #13
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 #14
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 5 votes vote down vote up
protected String serializeChunk(ScriptChunk ch) {

        StringBuilder str = new StringBuilder();

        if (isSignature(ch)) {
            String mapKey = getMapKey(ch);
            str.append("[");
            str.append("sig");
            str.append(",");
            str.append(this.signatures.get(mapKey).keyID);
            str.append(",");
            str.append(encodeModifier(this.signatures.get(mapKey).hashType, this.signatures.get(mapKey).anyoneCanPay));
            str.append("]");
            str.append(" ");
        }
        else if (isVariable(ch)) {
            String name = getVariableName(ch);
            str.append("[");
            str.append("var");
            str.append(",");
            str.append(name);
            str.append(",");
            str.append(getType(name).getCanonicalName());
            str.append("]");
            str.append(" ");
        }
        else if (ch.isOpCode()) {
            str.append(getOpCodeName(ch.opcode));
        }
        else if (ch.data != null) {
            // Data chunk
            str.append("PUSHDATA").append("[").append(Utils.HEX.encode(ch.data)).append("]");
        }
        else {
            // Small num
            str.append(decodeFromOpN(ch.opcode));
        }
        return str.toString();
    }
 
Example #15
Source File: DefaultRiskAnalysis.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks the output to see if the script violates a standardness rule. Not complete.
 */
public static RuleViolation isOutputStandard(TransactionOutput output) {
    if (output.getValue().compareTo(MIN_ANALYSIS_NONDUST_OUTPUT) < 0)
        return RuleViolation.DUST;
    for (ScriptChunk chunk : output.getScriptPubKey().getChunks()) {
        if (chunk.isPushData() && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
    }
    return RuleViolation.NONE;
}
 
Example #16
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 5 votes vote down vote up
private void removeVariableChunk(String name) {
    ListIterator<ScriptChunk> it = getChunks().listIterator();

    while (it.hasNext()) {
        ScriptChunk next = it.next();

        if (isVariable(next, name)) {
            it.remove();
        }
    }
}
 
Example #17
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 #18
Source File: AbstractScriptBuilderWithVar.java    From balzac with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public T signaturePlaceholder(String keyID, SigHash hashType, boolean anyoneCanPay) {
    checkNotNull(keyID, "'keyID' cannot be null");
    checkNotNull(hashType, "'hashType' cannot be null");
    SignatureUtil sig = new SignatureUtil(keyID, hashType, anyoneCanPay);
    String mapKey = sig.getUniqueKey();
    byte[] data = (SIGNATURE_PREFIX + mapKey).getBytes();
    checkState(data.length < 256, "data too long: " + data.length);
    ScriptChunk chunk = new ScriptChunk(OP_PUSHDATA1, data);
    super.addChunk(chunk);
    this.signatures.put(mapKey, sig);
    return (T) this;
}
 
Example #19
Source File: AbstractScriptBuilder.java    From balzac with Apache License 2.0 5 votes vote down vote up
private static ImmutableList<ScriptChunk> iterateOptimize(ImmutableList<ScriptChunk> scriptChunks) {
    ImmutableList<ScriptChunk> value = scriptChunks;
    ImmutableList<ScriptChunk> newValue = optimize(scriptChunks);

    while (!newValue.equals(value)) {
        value = newValue;
        newValue = optimize(value);
    }
    ;

    return newValue;
}
 
Example #20
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 #21
Source File: DefaultRiskAnalysis.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks the output to see if the script violates a standardness rule. Not complete.
 */
public static RuleViolation isOutputStandard(TransactionOutput output) {
    if (output.getValue().compareTo(MIN_ANALYSIS_NONDUST_OUTPUT) < 0)
        return RuleViolation.DUST;
    for (ScriptChunk chunk : output.getScriptPubKey().getChunks()) {
        if (chunk.isPushData() && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
    }
    return RuleViolation.NONE;
}
 
Example #22
Source File: ContractBuilder.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public Script createMethodScript(String abiParams, int gasLimitInt, int gasPriceInt, String _contractAddress) throws RuntimeException {
    byte[] version = Hex.decode("04000000");
    byte[] arrayGasLimit = org.spongycastle.util.Arrays.reverse((new BigInteger(String.valueOf(gasLimitInt))).toByteArray());
    byte[] gasLimit = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
    System.arraycopy(arrayGasLimit, 0, gasLimit, 0, arrayGasLimit.length);
    byte[] arrayGasPrice = org.spongycastle.util.Arrays.reverse((new BigInteger(String.valueOf(gasPriceInt))).toByteArray());
    byte[] gasPrice = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
    System.arraycopy(arrayGasPrice, 0, gasPrice, 0, arrayGasPrice.length);
    byte[] data = Hex.decode(abiParams);
    byte[] contractAddress = Hex.decode(_contractAddress);
    byte[] program;
    ScriptChunk versionChunk = new ScriptChunk(OP_PUSHDATA_4, version);
    ScriptChunk gasLimitChunk = new ScriptChunk(OP_PUSHDATA_8, gasLimit);
    ScriptChunk gasPriceChunk = new ScriptChunk(OP_PUSHDATA_8, gasPrice);
    ScriptChunk dataChunk = new ScriptChunk(ScriptOpCodes.OP_PUSHDATA2, data);
    ScriptChunk contactAddressChunk = new ScriptChunk(ScriptOpCodes.OP_PUSHDATA2, contractAddress);
    ScriptChunk opExecChunk = new ScriptChunk(OP_EXEC_ASSIGN, null);
    List<ScriptChunk> chunkList = new ArrayList<>();
    chunkList.add(versionChunk);
    chunkList.add(gasLimitChunk);
    chunkList.add(gasPriceChunk);
    chunkList.add(dataChunk);
    chunkList.add(contactAddressChunk);
    chunkList.add(opExecChunk);
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        for (ScriptChunk chunk : chunkList) {
            chunk.write(bos);
        }
        program = bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return new Script(program);
}
 
Example #23
Source File: BisqRiskAnalysis.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Checks the output to see if the script violates a standardness rule. Not complete.
 */
public static RuleViolation isOutputStandard(TransactionOutput output) {
    // OP_RETURN has usually output value zero, so we exclude that from the MIN_ANALYSIS_NONDUST_OUTPUT check
    if (!output.getScriptPubKey().isOpReturn()
            && output.getValue().compareTo(MIN_ANALYSIS_NONDUST_OUTPUT) < 0)
        return RuleViolation.DUST;
    for (ScriptChunk chunk : output.getScriptPubKey().getChunks()) {
        if (chunk.isPushData() && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
    }
    return RuleViolation.NONE;
}
 
Example #24
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void customTransactionSpending() throws Exception {
    // We'll set up a wallet that receives a coin, then sends a coin of lesser value and keeps the change.
    Coin v1 = valueOf(3, 0);
    sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, v1);
    assertEquals(v1, wallet.getBalance());
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.UNSPENT));
    assertEquals(1, wallet.getTransactions(true).size());

    Coin v2 = valueOf(0, 50);
    Coin v3 = valueOf(0, 75);
    Coin v4 = valueOf(1, 25);

    Transaction t2 = new Transaction(UNITTEST);
    t2.addOutput(v2, OTHER_ADDRESS);
    t2.addOutput(v3, OTHER_ADDRESS);
    t2.addOutput(v4, OTHER_ADDRESS);
    SendRequest req = SendRequest.forTx(t2);
    wallet.completeTx(req);

    // Do some basic sanity checks.
    assertEquals(1, t2.getInputs().size());
    List<ScriptChunk> scriptSigChunks = t2.getInput(0).getScriptSig().getChunks();
    // check 'from address' -- in a unit test this is fine
    assertEquals(2, scriptSigChunks.size());
    assertEquals(myAddress, LegacyAddress.fromPubKeyHash(UNITTEST, Utils.sha256hash160(scriptSigChunks.get(1).data)));
    assertEquals(TransactionConfidence.ConfidenceType.UNKNOWN, t2.getConfidence().getConfidenceType());

    // We have NOT proven that the signature is correct!
    wallet.commitTx(t2);
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.PENDING));
    assertEquals(1, wallet.getPoolSize(WalletTransaction.Pool.SPENT));
    assertEquals(2, wallet.getTransactions(true).size());
}
 
Example #25
Source File: DefaultRiskAnalysis.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks the output to see if the script violates a standardness rule. Not complete.
 */
public static RuleViolation isOutputStandard(TransactionOutput output) {
    if (output.getValue().compareTo(MIN_ANALYSIS_NONDUST_OUTPUT) < 0)
        return RuleViolation.DUST;
    for (ScriptChunk chunk : output.getScriptPubKey().getChunks()) {
        if (chunk.isPushData() && !chunk.isShortestPossiblePushData())
            return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
    }
    return RuleViolation.NONE;
}
 
Example #26
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 #27
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;}


}
 
Example #28
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 #29
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 #30
Source File: MissingSigResolutionSigner.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    if (missingSigsMode == Wallet.MissingSigsMode.USE_OP_ZERO)
        return true;

    int numInputs = propTx.partialTx.getInputs().size();
    byte[] dummySig = TransactionSignature.dummy().encodeToBitcoin();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = propTx.partialTx.getInput(i);
        if (txIn.getConnectedOutput() == null) {
            log.warn("Missing connected output, assuming input {} is already signed.", i);
            continue;
        }

        Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
        Script inputScript = txIn.getScriptSig();
        if (ScriptPattern.isPayToScriptHash(scriptPubKey) || ScriptPattern.isSentToMultisig(scriptPubKey)) {
            int sigSuffixCount = ScriptPattern.isPayToScriptHash(scriptPubKey) ? 1 : 0;
            // all chunks except the first one (OP_0) and the last (redeem script) are signatures
            for (int j = 1; j < inputScript.getChunks().size() - sigSuffixCount; j++) {
                ScriptChunk scriptChunk = inputScript.getChunks().get(j);
                if (scriptChunk.equalsOpCode(0)) {
                    if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                        throw new MissingSignatureException();
                    } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                        txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, j - 1));
                    }
                }
            }
        } else {
            if (inputScript.getChunks().get(0).equalsOpCode(0)) {
                if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                    throw new ECKey.MissingPrivateKeyException();
                } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                    txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, 0));
                }
            }
        }
        // TODO handle non-P2SH multisig
    }
    return true;
}