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;
}
Aggregations