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

The following examples show how to use org.bitcoinj.script.Script#isPayToScriptHash() . 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: ElectrumMultiWallet.java    From java-stratum with Apache License 2.0 6 votes vote down vote up
private void doMarkKeysAsUsed(Transaction tx, KeyChainGroup keychain) {
    for (TransactionOutput o : tx.getOutputs()) {
        try {
            Script script = o.getScriptPubKey();
            if (script.isSentToRawPubKey()) {
                byte[] pubkey = script.getPubKey();
                keychain.markPubKeyAsUsed(pubkey);
            } else if (script.isSentToAddress()) {
                byte[] pubkeyHash = script.getPubKeyHash();
                keychain.markPubKeyHashAsUsed(pubkeyHash);
            } else if (script.isPayToScriptHash()) {
                Address a = Address.fromP2SHScript(tx.getParams(), script);
                keychain.markP2SHAddressAsUsed(a);
            }
        } catch (ScriptException e) {
            // Just means we didn't understand the output of this transaction: ignore it.
            log.warn("Could not parse tx output script: {}", e.toString());
        }
    }
}
 
Example 2
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private ListenableFuture<Boolean> verifyP2SHSpendableBy(final Script scriptHash, final int subAccount, final Integer pointer) {
    if (!scriptHash.isPayToScriptHash())
        return Futures.immediateFuture(false);
    final byte[] gotP2SH = scriptHash.getPubKeyHash();

    return mExecutor.submit(new Callable<Boolean>() {
        @Override
        public Boolean call() {
            final byte[] multisig = createOutScript(subAccount, pointer);
            if (Arrays.equals(gotP2SH, Wally.hash160(getSegWitScript(multisig))))
                return true;
            return Arrays.equals(gotP2SH, Wally.hash160(multisig));
        }
    });
}
 
Example 3
Source File: MissingSigResolutionSigner.java    From green_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 (scriptPubKey.isPayToScriptHash() || scriptPubKey.isSentToMultiSig()) {
            int sigSuffixCount = scriptPubKey.isPayToScriptHash() ? 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;
}
 
Example 4
Source File: CustomTransactionSigner.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    Transaction tx = propTx.partialTx;
    int numInputs = tx.getInputs().size();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = tx.getInput(i);
        TransactionOutput txOut = txIn.getConnectedOutput();
        if (txOut == null) {
            continue;
        }
        Script scriptPubKey = txOut.getScriptPubKey();
        if (!scriptPubKey.isPayToScriptHash()) {
            log.warn("CustomTransactionSigner works only with P2SH transactions");
            return false;
        }

        Script inputScript = checkNotNull(txIn.getScriptSig());

        try {
            // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when
            // we sign missing pieces (to check this would require either assuming any signatures are signing
            // standard output types or a way to get processed signatures out of script execution)
            txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey());
            log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i);
            continue;
        } catch (ScriptException e) {
            // Expected.
        }

        RedeemData redeemData = txIn.getConnectedRedeemData(keyBag);
        if (redeemData == null) {
            log.warn("No redeem data found for input {}", i);
            continue;
        }

        Sha256Hash sighash = tx.hashForSignature(i, redeemData.redeemScript, Transaction.SigHash.ALL, false);
        SignatureAndKey sigKey = getSignature(sighash, propTx.keyPaths.get(scriptPubKey));
        TransactionSignature txSig = new TransactionSignature(sigKey.sig, Transaction.SigHash.ALL, false);
        int sigIndex = inputScript.getSigInsertionIndex(sighash, sigKey.pubKey);
        inputScript = scriptPubKey.getScriptSigWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex);
        txIn.setScriptSig(inputScript);
    }
    return true;
}
 
Example 5
Source File: MissingSigResolutionSigner.java    From GreenBits 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 (scriptPubKey.isPayToScriptHash() || scriptPubKey.isSentToMultiSig()) {
            int sigSuffixCount = scriptPubKey.isPayToScriptHash() ? 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;
}
 
Example 6
Source File: CustomTransactionSigner.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    Transaction tx = propTx.partialTx;
    int numInputs = tx.getInputs().size();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = tx.getInput(i);
        TransactionOutput txOut = txIn.getConnectedOutput();
        if (txOut == null) {
            continue;
        }
        Script scriptPubKey = txOut.getScriptPubKey();
        if (!scriptPubKey.isPayToScriptHash()) {
            log.warn("CustomTransactionSigner works only with P2SH transactions");
            return false;
        }

        Script inputScript = checkNotNull(txIn.getScriptSig());

        try {
            // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when
            // we sign missing pieces (to check this would require either assuming any signatures are signing
            // standard output types or a way to get processed signatures out of script execution)
            txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey());
            log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i);
            continue;
        } catch (ScriptException e) {
            // Expected.
        }

        RedeemData redeemData = txIn.getConnectedRedeemData(keyBag);
        if (redeemData == null) {
            log.warn("No redeem data found for input {}", i);
            continue;
        }

        Sha256Hash sighash = tx.hashForSignature(i, redeemData.redeemScript, Transaction.SigHash.ALL, false);
        SignatureAndKey sigKey = getSignature(sighash, propTx.keyPaths.get(scriptPubKey));
        TransactionSignature txSig = new TransactionSignature(sigKey.sig, Transaction.SigHash.ALL, false);
        int sigIndex = inputScript.getSigInsertionIndex(sighash, sigKey.pubKey);
        inputScript = scriptPubKey.getScriptSigWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex);
        txIn.setScriptSig(inputScript);
    }
    return true;
}