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

The following examples show how to use org.bitcoinj.script.Script#getScriptSigWithSignature() . 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: CustomTransactionSigner.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) {
    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 (!ScriptPattern.isPayToScriptHash(scriptPubKey)) {
            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 2
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 3
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;
}