Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class BitcoinTransaction
- Object
- 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 reads it back. The three signature hash
algorithms are here – legacy (legacySighash), SegWit v0 BIP-143
(segwitV0Sighash) and Taproot BIP-341 (taprootSighash) – 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 types
class BitcoinTransaction.Input | One input: the outpoint it spends, its unlocking script, sequence and witness. |
class BitcoinTransaction.Output | One output: an amount in satoshis and the script that locks it. |
Fields
public static final int SIGHASH_ALL = 1 | Sign every input and output. |
public static final int SIGHASH_NONE = 2 | Sign every input and no output. |
public static final int SIGHASH_SINGLE = 3 | Sign every input and the output with the same index. |
public static final int SIGHASH_ANYONECANPAY = 128 | Combined with another type: sign only this input. |
public static final int SIGHASH_DEFAULT = 0 | Taproot’s default, which commits like SIGHASH_ALL and adds no byte to the signature. |
public static final long SEQUENCE_FINAL = 4294967295L | The sequence that disables relative lock time and replace-by-fee signalling. |
public static final long SEQUENCE_RBF = 4294967293L | The sequence an input signals BIP-125 replace-by-fee with. |
Constructors
public BitcoinTransaction() |
Methods
Inherited methods
Field details
SIGHASH_ALL
public static final int SIGHASH_ALL = 1Sign every input and output.
SIGHASH_NONE
public static final int SIGHASH_NONE = 2Sign every input and no output.
SIGHASH_SINGLE
public static final int SIGHASH_SINGLE = 3Sign every input and the output with the same index.
SIGHASH_ANYONECANPAY
public static final int SIGHASH_ANYONECANPAY = 128Combined with another type: sign only this input.
SIGHASH_DEFAULT
public static final int SIGHASH_DEFAULT = 0Taproot’s default, which commits like
SIGHASH_ALL and adds no byte to the signature.SEQUENCE_FINAL
public static final long SEQUENCE_FINAL = 4294967295LThe sequence that disables relative lock time and replace-by-fee signalling.
SEQUENCE_RBF
public static final long SEQUENCE_RBF = 4294967293LThe sequence an input signals BIP-125 replace-by-fee with.
Constructor details
BitcoinTransaction
public BitcoinTransaction()Method details
addInput
public int addInput(String prevTxid, long index)Adds an input spending output
index of the transaction prevTxid (display hex), with
SEQUENCE_RBF. Returns its index.addInput
public int addInput(String prevTxid, long index, long sequence)Adds an input with an explicit sequence. Returns its index.
addOutput
public int addOutput(long value, Script scriptPubKey)Adds an output. Returns its index.
addOutput
public int addOutput(long value, BitcoinAddress address)Adds an output paying an address. Returns its index.
getInputs
public List<BitcoinTransaction.Input> getInputs()The inputs, live: changes to an
Input change the transaction.getOutputs
public List<BitcoinTransaction.Output> 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
public String toHex()serialize in hex, the form sendrawtransaction and every broadcast API take.getTxid
public String getTxid()The transaction id in display hex: the reversed double SHA-256 of the form without
witness.
getWtxid
public String getWtxid()The witness transaction id in display hex.
getVirtualSize
public long getVirtualSize()The virtual size in vbytes, which fees are priced in.
parse
public static BitcoinTransaction parse(byte[] raw)Reads a serialized transaction, with or without witness.
Throws
IllegalArgumentException- for truncated or trailing data
parse
public static BitcoinTransaction parse(byte[] raw, boolean allowWitness)Reads a serialized transaction. With
allowWitness false 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
public static BitcoinTransaction fromHex(String hex)parse from hex.legacySighash
public byte[] legacySighash(int index, Script scriptCode, int sighashType)The legacy signature hash of input
index against scriptCode (the spent output
script, or the redeem script of P2SH).segwitV0Sighash
public byte[] segwitV0Sighash(int index, Script scriptCode, long amount, int sighashType)The BIP-143 signature hash of a SegWit v0 input.
Parameters
indexint- Not documented.
scriptCodeScript- for P2WPKH the P2PKH script of the key hash; for P2WSH the witness script
amountlong- the value of the spent output in satoshis, which the signature commits to
sighashTypeint- Not documented.
taprootSighash
public byte[] taprootSighash(int index, List<BitcoinTransaction.Output> spent, int sighashType)The BIP-341 signature hash of a Taproot input, key path, no annex.
Parameters
indexint- Not documented.
spentList<BitcoinTransaction.Output>- the output every input spends, in input order – Taproot signatures commit to all their amounts and scripts
sighashTypeint- Not documented.
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 of
leafHash (BIP-342
extension, no annex, code separator position none); null for the key path.signP2pkh
public void signP2pkh(int index, byte[] privateKey, boolean compressed)Signs a P2PKH input with
SIGHASH_ALL and sets its scriptSig.signP2wpkh
public void signP2wpkh(int index, byte[] privateKey, long amount)Signs a P2WPKH input with
SIGHASH_ALL and sets its witness.signP2shP2wpkh
public void signP2shP2wpkh(int index, byte[] privateKey, long amount)Signs a BIP-49 P2SH-P2WPKH input with
SIGHASH_ALL.signP2trKeyPath
public void signP2trKeyPath(int index, byte[] privateKey, List<BitcoinTransaction.Output> spent)Signs a BIP-86 Taproot key-path input with
SIGHASH_DEFAULT and sets its witness.Parameters
indexint- Not documented.
privateKeybyte[]- the untweaked internal private key
spentList<BitcoinTransaction.Output>- 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.