Class BitcoinTransaction

java.lang.Object
com.codename1.crypto.bitcoin.BitcoinTransaction

public final class BitcoinTransaction extends Object

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 Classes
    Modifier and Type
    Class
    Description
    static final class 
    One input: the outpoint it spends, its unlocking script, sequence and witness.
    static final class 
    One output: an amount in satoshis and the script that locks it.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The sequence that disables relative lock time and replace-by-fee signalling.
    static final long
    The sequence an input signals BIP-125 replace-by-fee with.
    static final int
    Sign every input and output.
    static final int
    Combined with another type: sign only this input.
    static final int
    Taproot's default, which commits like SIGHASH_ALL and adds no byte to the signature.
    static final int
    Sign every input and no output.
    static final int
    Sign every input and the output with the same index.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    addInput(String prevTxid, long index)
    Adds an input spending output index of the transaction prevTxid (display hex), with SEQUENCE_RBF.
    int
    addInput(String prevTxid, long index, long sequence)
    Adds an input with an explicit sequence.
    int
    addOutput(long value, BitcoinAddress address)
    Adds an output paying an address.
    int
    addOutput(long value, Script scriptPubKey)
    Adds 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.
    parse(byte[]) from hex.
    The inputs, live: changes to an Input change the transaction.
    long
    The lock time.
    The outputs.
    The transaction id in display hex: the reversed double SHA-256 of the form without witness.
    long
    The version, 2 by default.
    long
    The virtual size in vbytes, which fees are priced in.
    The witness transaction id in display hex.
    boolean
    Tells whether any input carries witness data.
    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).
    parse(byte[] raw)
    Reads a serialized transaction, with or without witness.
    parse(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.
    void
    setLockTime(long lockTime)
    Sets the lock time (a block height below 500000000, a Unix time above).
    void
    setVersion(long version)
    Sets the version.
    void
    signP2pkh(int index, byte[] privateKey, boolean compressed)
    Signs a P2PKH input with SIGHASH_ALL and sets its scriptSig.
    void
    signP2shP2wpkh(int index, byte[] privateKey, long amount)
    Signs a BIP-49 P2SH-P2WPKH input with SIGHASH_ALL.
    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.
    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.
    void
    signP2wpkh(int index, byte[] privateKey, long amount)
    Signs a P2WPKH input with SIGHASH_ALL and 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 of leafHash (BIP-342 extension, no annex, code separator position none); null for the key path.
    serialize() in hex, the form sendrawtransaction and every broadcast API take.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SIGHASH_ALL

      public static final int SIGHASH_ALL
      Sign every input and output.
      See Also:
    • SIGHASH_NONE

      public static final int SIGHASH_NONE
      Sign every input and no output.
      See Also:
    • SIGHASH_SINGLE

      public static final int SIGHASH_SINGLE
      Sign every input and the output with the same index.
      See Also:
    • SIGHASH_ANYONECANPAY

      public static final int SIGHASH_ANYONECANPAY
      Combined with another type: sign only this input.
      See Also:
    • SIGHASH_DEFAULT

      public static final int SIGHASH_DEFAULT
      Taproot's default, which commits like SIGHASH_ALL and adds no byte to the signature.
      See Also:
    • SEQUENCE_FINAL

      public static final long SEQUENCE_FINAL
      The sequence that disables relative lock time and replace-by-fee signalling.
      See Also:
    • SEQUENCE_RBF

      public static final long SEQUENCE_RBF
      The sequence an input signals BIP-125 replace-by-fee with.
      See Also:
  • 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(byte[]) 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
      • 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

      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
      • 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 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
      • 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.