Class BitcoinTransaction
A Bitcoin transaction: inputs spending previous outputs, new outputs, and the witness data SegWit inputs carry.
Mutable while it is built and signed; serialize() writes the network form (with the
witness when any input has one) and parse(byte[]) reads it back. The three signature hash
algorithms are here -- legacy (legacySighash(int, Script, int)), SegWit v0 BIP-143
(segwitV0Sighash(int, Script, long, int)) and Taproot BIP-341 (taprootSighash(int, List, int)) -- together with
one-call signers for the common single-key inputs:
String prevTxid = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
byte[] privateKey = SecureRandom.bytes(32);
BitcoinTransaction tx = new BitcoinTransaction();
tx.addInput(prevTxid, 0);
tx.addOutput(50000, BitcoinAddress.parse("bc1q...", BitcoinNetwork.MAINNET).getScriptPubKey());
tx.signP2wpkh(0, privateKey, 60000);
String hex = tx.toHex(); // broadcast
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classOne input: the outpoint it spends, its unlocking script, sequence and witness.static final classOne output: an amount in satoshis and the script that locks it. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longThe sequence that disables relative lock time and replace-by-fee signalling.static final longThe sequence an input signals BIP-125 replace-by-fee with.static final intSign every input and output.static final intCombined with another type: sign only this input.static final intTaproot's default, which commits likeSIGHASH_ALLand adds no byte to the signature.static final intSign every input and no output.static final intSign every input and the output with the same index. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintintAdds an input with an explicit sequence.intaddOutput(long value, BitcoinAddress address) Adds an output paying an address.intAdds an output.static byte[]ecdsa(byte[] sighash, byte[] privateKey, int sighashType) A DER ECDSA signature with the sighash byte appended, as legacy and SegWit v0 inputs carry it.static BitcoinTransactionparse(byte[])from hex.The inputs, live: changes to anInputchange the transaction.longThe lock time.The outputs.getTxid()The transaction id in display hex: the reversed double SHA-256 of the form without witness.longThe version, 2 by default.longThe virtual size in vbytes, which fees are priced in.getWtxid()The witness transaction id in display hex.booleanTells whether any input carries witness data.byte[]legacySighash(int index, Script scriptCode, int sighashType) The legacy signature hash of inputindexagainstscriptCode(the spent output script, or the redeem script of P2SH).static BitcoinTransactionparse(byte[] raw) Reads a serialized transaction, with or without witness.static BitcoinTransactionparse(byte[] raw, boolean allowWitness) Reads a serialized transaction.byte[]segwitV0Sighash(int index, Script scriptCode, long amount, int sighashType) The BIP-143 signature hash of a SegWit v0 input.byte[]The network serialization, with the witness when any input has one.voidsetLockTime(long lockTime) Sets the lock time (a block height below 500000000, a Unix time above).voidsetVersion(long version) Sets the version.voidsignP2pkh(int index, byte[] privateKey, boolean compressed) Signs a P2PKH input withSIGHASH_ALLand sets itsscriptSig.voidsignP2shP2wpkh(int index, byte[] privateKey, long amount) Signs a BIP-49 P2SH-P2WPKH input withSIGHASH_ALL.voidsignP2trKeyPath(int index, byte[] privateKey, byte[] merkleRoot, List<BitcoinTransaction.Output> spent, int sighashType) Signs a Taproot key-path input, committing to a script tree's Merkle root.voidsignP2trKeyPath(int index, byte[] privateKey, List<BitcoinTransaction.Output> spent) Signs a BIP-86 Taproot key-path input withSIGHASH_DEFAULTand sets its witness.voidsignP2wpkh(int index, byte[] privateKey, long amount) Signs a P2WPKH input withSIGHASH_ALLand sets its witness.byte[]taprootSighash(int index, List<BitcoinTransaction.Output> spent, int sighashType) The BIP-341 signature hash of a Taproot input, key path, no annex.byte[]taprootSighash(int index, List<BitcoinTransaction.Output> spent, int sighashType, byte[] leafHash) The BIP-341 signature hash of a Taproot script-path spend ofleafHash(BIP-342 extension, no annex, code separator position none);nullfor the key path.toHex()serialize()in hex, the formsendrawtransactionand every broadcast API take.
-
Field Details
-
SIGHASH_ALL
public static final int SIGHASH_ALLSign every input and output.- See Also:
-
SIGHASH_NONE
public static final int SIGHASH_NONESign every input and no output.- See Also:
-
SIGHASH_SINGLE
public static final int SIGHASH_SINGLESign every input and the output with the same index.- See Also:
-
SIGHASH_ANYONECANPAY
public static final int SIGHASH_ANYONECANPAYCombined with another type: sign only this input.- See Also:
-
SIGHASH_DEFAULT
public static final int SIGHASH_DEFAULTTaproot's default, which commits likeSIGHASH_ALLand adds no byte to the signature.- See Also:
-
SEQUENCE_FINAL
public static final long SEQUENCE_FINALThe sequence that disables relative lock time and replace-by-fee signalling.- See Also:
-
SEQUENCE_RBF
public static final long SEQUENCE_RBFThe sequence an input signals BIP-125 replace-by-fee with.- See Also:
-
-
Constructor Details
-
BitcoinTransaction
public BitcoinTransaction()
-
-
Method Details
-
addInput
Adds an input spending outputindexof the transactionprevTxid(display hex), withSEQUENCE_RBF. Returns its index. -
addInput
Adds an input with an explicit sequence. Returns its index. -
addOutput
Adds an output. Returns its index. -
addOutput
Adds an output paying an address. Returns its index. -
getInputs
The inputs, live: changes to anInputchange the transaction. -
getOutputs
The outputs. -
getVersion
public long getVersion()The version, 2 by default. -
setVersion
public void setVersion(long version) Sets the version. -
getLockTime
public long getLockTime()The lock time. -
setLockTime
public void setLockTime(long lockTime) Sets the lock time (a block height below 500000000, a Unix time above). -
hasWitness
public boolean hasWitness()Tells whether any input carries witness data. -
serialize
public byte[] serialize()The network serialization, with the witness when any input has one. -
toHex
serialize()in hex, the formsendrawtransactionand every broadcast API take. -
getTxid
The transaction id in display hex: the reversed double SHA-256 of the form without witness. -
getWtxid
The witness transaction id in display hex. -
getVirtualSize
public long getVirtualSize()The virtual size in vbytes, which fees are priced in. -
parse
Reads a serialized transaction, with or without witness.
Throws
IllegalArgumentException: for truncated or trailing data
-
parse
Reads a serialized transaction. With
allowWitnessfalse the SegWit marker is not recognized, which is how a PSBT's unsigned transaction is read: a transaction with no inputs would otherwise begin with the marker byte.Throws
- : for truncated or trailing data
-
fromHex
parse(byte[])from hex. -
legacySighash
The legacy signature hash of inputindexagainstscriptCode(the spent output script, or the redeem script of P2SH). -
segwitV0Sighash
The BIP-143 signature hash of a SegWit v0 input.
Parameters
-
scriptCode: for P2WPKH the P2PKH script of the key hash; for P2WSH the witness script -
amount: the value of the spent output in satoshis, which the signature commits to
-
-
taprootSighash
The BIP-341 signature hash of a Taproot input, key path, no annex.
Parameters
spent: the output every input spends, in input order -- Taproot signatures commit to all their amounts and scripts
-
taprootSighash
public byte[] taprootSighash(int index, List<BitcoinTransaction.Output> spent, int sighashType, byte[] leafHash) The BIP-341 signature hash of a Taproot script-path spend ofleafHash(BIP-342 extension, no annex, code separator position none);nullfor the key path. -
signP2pkh
public void signP2pkh(int index, byte[] privateKey, boolean compressed) Signs a P2PKH input withSIGHASH_ALLand sets itsscriptSig. -
signP2wpkh
public void signP2wpkh(int index, byte[] privateKey, long amount) Signs a P2WPKH input withSIGHASH_ALLand sets its witness. -
signP2shP2wpkh
public void signP2shP2wpkh(int index, byte[] privateKey, long amount) Signs a BIP-49 P2SH-P2WPKH input withSIGHASH_ALL. -
signP2trKeyPath
Signs a BIP-86 Taproot key-path input with
SIGHASH_DEFAULTand sets its witness.Parameters
-
privateKey: the untweaked internal private key -
spent: the output every input spends, in input order
-
-
signP2trKeyPath
public void signP2trKeyPath(int index, byte[] privateKey, byte[] merkleRoot, List<BitcoinTransaction.Output> spent, int sighashType) Signs a Taproot key-path input, committing to a script tree's Merkle root. -
ecdsa
public static byte[] ecdsa(byte[] sighash, byte[] privateKey, int sighashType) A DER ECDSA signature with the sighash byte appended, as legacy and SegWit v0 inputs carry it.
-