Search in sources :

Example 1 with OutputScript

use of it.unica.tcs.lib.script.OutputScript in project balzac by balzac-lang.

the class TransactionBuilder method toTransaction.

@Override
public Transaction toTransaction(ECKeyStore keystore) {
    checkState(this.isReady(), "the transaction and all its ancestors are not ready");
    Transaction tx = new Transaction(params);
    // set version
    tx.setVersion(2);
    // inputs
    for (Input input : inputs) {
        if (!input.hasParentTx()) {
            // coinbase transaction
            // script will be set later
            byte[] script = new byte[] {};
            TransactionInput txInput = new TransactionInput(params, tx, script);
            tx.addInput(txInput);
            checkState(txInput.isCoinBase(), "'txInput' is expected to be a coinbase");
        } else {
            ITransactionBuilder parentTransaction2 = input.getParentTx();
            Transaction parentTransaction = parentTransaction2.toTransaction(keystore);
            TransactionOutPoint outPoint = new TransactionOutPoint(params, input.getOutIndex(), parentTransaction);
            // script will be set later
            byte[] script = new byte[] {};
            TransactionInput txInput = new TransactionInput(params, tx, script, outPoint);
            // set checksequenseverify (relative locktime)
            if (input.getLocktime() == UNSET_LOCKTIME) {
                // see BIP-0065
                if (this.locktime != UNSET_LOCKTIME)
                    txInput.setSequenceNumber(TransactionInput.NO_SEQUENCE - 1);
            } else {
                txInput.setSequenceNumber(input.getLocktime());
            }
            // txInput.setScriptSig(input.script.build());
            tx.addInput(txInput);
        }
    }
    // outputs
    for (Output output : outputs) {
        // bind free variables
        OutputScript sb = output.getScript();
        for (String freeVarName : getVariables()) {
            if (sb.hasVariable(freeVarName) && sb.isFree(freeVarName)) {
                sb.bindVariable(freeVarName, this.getValue(freeVarName));
            }
        }
        checkState(sb.isReady(), "script cannot have free variables: " + sb.toString());
        checkState(sb.signatureSize() == 0);
        Script outScript;
        if (sb instanceof P2SHOutputScript) {
            // P2SH
            outScript = ((P2SHOutputScript) sb).getOutputScript();
        } else {
            outScript = sb.build();
        }
        Coin value = Coin.valueOf(output.getValue());
        tx.addOutput(value, outScript);
    }
    // set checklocktime (absolute locktime)
    if (locktime != UNSET_LOCKTIME) {
        tx.setLockTime(locktime);
    }
    // set all the signatures within the input scripts (which are never part of the signature)
    for (int i = 0; i < tx.getInputs().size(); i++) {
        TransactionInput txInput = tx.getInputs().get(i);
        InputScript sb = inputs.get(i).getScript();
        // bind free variables
        for (String freeVarName : getVariables()) {
            if (sb.hasVariable(freeVarName) && sb.isFree(freeVarName)) {
                sb.bindVariable(freeVarName, this.getValue(freeVarName));
            }
        }
        checkState(sb.isReady(), "script cannot have free variables: " + sb.toString());
        byte[] outScript;
        if (txInput.isCoinBase()) {
            outScript = new byte[] {};
        } else {
            if (txInput.getOutpoint().getConnectedOutput().getScriptPubKey().isPayToScriptHash()) {
                checkState(sb instanceof P2SHInputScript, "why not?");
                P2SHInputScript p2shInput = (P2SHInputScript) sb;
                outScript = p2shInput.getRedeemScript().build().getProgram();
            } else
                outScript = txInput.getOutpoint().getConnectedPubKeyScript();
        }
        try {
            sb.setAllSignatures(keystore, tx, i, outScript);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        }
        checkState(sb.signatureSize() == 0, "all the signatures should have been set");
        // update scriptSig
        txInput.setScriptSig(sb.build());
    }
    return tx;
}
Also used : P2SHInputScript(it.unica.tcs.lib.script.P2SHInputScript) OutputScript(it.unica.tcs.lib.script.OutputScript) InputScript(it.unica.tcs.lib.script.InputScript) Script(org.bitcoinj.script.Script) P2SHOutputScript(it.unica.tcs.lib.script.P2SHOutputScript) P2SHInputScript(it.unica.tcs.lib.script.P2SHInputScript) InputScript(it.unica.tcs.lib.script.InputScript) P2SHOutputScript(it.unica.tcs.lib.script.P2SHOutputScript) KeyStoreException(java.security.KeyStoreException) TransactionInput(org.bitcoinj.core.TransactionInput) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) P2SHInputScript(it.unica.tcs.lib.script.P2SHInputScript) Coin(org.bitcoinj.core.Coin) TransactionInput(org.bitcoinj.core.TransactionInput) Transaction(org.bitcoinj.core.Transaction) OutputScript(it.unica.tcs.lib.script.OutputScript) P2SHOutputScript(it.unica.tcs.lib.script.P2SHOutputScript) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Aggregations

InputScript (it.unica.tcs.lib.script.InputScript)1 OutputScript (it.unica.tcs.lib.script.OutputScript)1 P2SHInputScript (it.unica.tcs.lib.script.P2SHInputScript)1 P2SHOutputScript (it.unica.tcs.lib.script.P2SHOutputScript)1 KeyStoreException (java.security.KeyStoreException)1 Coin (org.bitcoinj.core.Coin)1 Transaction (org.bitcoinj.core.Transaction)1 TransactionInput (org.bitcoinj.core.TransactionInput)1 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)1 Script (org.bitcoinj.script.Script)1