Class Psbt

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

public final class Psbt extends Object

A Partially Signed Bitcoin Transaction (BIP-174, with the BIP-371 Taproot fields): the file hardware wallets, multisig coordinators and watch-only servers pass around until every signer has signed.

String fromCoordinator = "cHNidP8B...";     // the PSBT a coordinator sent
byte[] privateKey = SecureRandom.bytes(32);
Psbt psbt = Psbt.fromBase64(fromCoordinator);
psbt.sign(0, privateKey);           // any input this key can sign
psbt.finalizeAll();
String hex = psbt.extract().toHex();

sign(int, byte[]) handles P2PKH, P2WPKH, P2SH-P2WPKH, Taproot key-path, and P2SH/P2WSH multisig inputs; finalizeInput(int) builds the final scripts for the same kinds. Fields this class does not interpret are kept and written back unchanged.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    One key origin: a public key, the fingerprint of the master it descends from and the path from that master.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addBip32Derivation(int input, byte[] publicKey, int masterFingerprint, long[] path)
    Records the BIP-32 origin of a key an input needs, so a hardware wallet can find it.
    void
    addTapBip32Derivation(int input, byte[] xOnlyKey, int masterFingerprint, long[] path)
    Records the Taproot (BIP-371) origin of an input's x-only key, for a key-path spend.
    The signed network transaction.
    boolean
    Finalizes every input, answering whether all of them are final.
    boolean
    finalizeInput(int input)
    Builds the final scriptSig and witness of an input from its signatures, answering whether it could.
    static Psbt
    Reads a Base64 PSBT, the form wallets exchange.
    static Psbt
    A PSBT for an unsigned transaction; its scripts and witnesses are cleared.
    The BIP-32 origins recorded for an input's keys, both the ECDSA kind and the Taproot kind (BIP-371, whose public keys are 32-byte x-only).
    List<byte[][]>
    The signatures collected for an input: {publicKey, signature} pairs.
    getSpentOutput(int input)
    The output an input spends, from its witness or non-witness UTXO, or null.
    The unsigned transaction.
    static Psbt
    parse(byte[] data)
    Reads a binary PSBT.
    byte[]
    The binary PSBT.
    void
    setNonWitnessUtxo(int input, BitcoinTransaction previous)
    Records the whole previous transaction of an input, which legacy inputs need.
    void
    setRedeemScript(int input, Script redeem)
    Records the redeem script of a P2SH input.
    void
    setTapInternalKey(int input, byte[] xOnly)
    Records the x-only internal key of a Taproot input.
    void
    setWitnessScript(int input, Script witnessScript)
    Records the witness script of a P2WSH input.
    void
    Records the output a SegWit input spends.
    boolean
    sign(int input, byte[] privateKey)
    Signs one input with privateKey if the key belongs to it, answering whether it did.
    boolean
    sign(int input, BitcoinKey key)
    Signs one input with a key that may live elsewhere, such as a wallet or a hardware device; see [#sign(int, byte[])] for what is signed.
    int
    signAll(byte[] privateKey)
    Signs every input the key belongs to, answering how many.
    int
    Signs every input a key belongs to, answering how many.
    The Base64 PSBT.

    Methods inherited from class Object

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

    • fromTransaction

      public static Psbt fromTransaction(BitcoinTransaction unsigned)
      A PSBT for an unsigned transaction; its scripts and witnesses are cleared.
    • parse

      public static Psbt parse(byte[] data)

      Reads a binary PSBT.

      Throws
      • IllegalArgumentException: for a malformed PSBT, a duplicate key, or a global transaction that is not unsigned
    • fromBase64

      public static Psbt fromBase64(String base64)
      Reads a Base64 PSBT, the form wallets exchange.
    • serialize

      public byte[] serialize()
      The binary PSBT.
    • toBase64

      public String toBase64()
      The Base64 PSBT.
    • getTransaction

      public BitcoinTransaction getTransaction()
      The unsigned transaction.
    • setWitnessUtxo

      public void setWitnessUtxo(int input, BitcoinTransaction.Output spent)
      Records the output a SegWit input spends.
    • setNonWitnessUtxo

      public void setNonWitnessUtxo(int input, BitcoinTransaction previous)
      Records the whole previous transaction of an input, which legacy inputs need.
    • setRedeemScript

      public void setRedeemScript(int input, Script redeem)
      Records the redeem script of a P2SH input.
    • setWitnessScript

      public void setWitnessScript(int input, Script witnessScript)
      Records the witness script of a P2WSH input.
    • setTapInternalKey

      public void setTapInternalKey(int input, byte[] xOnly)
      Records the x-only internal key of a Taproot input.
    • addBip32Derivation

      public void addBip32Derivation(int input, byte[] publicKey, int masterFingerprint, long[] path)
      Records the BIP-32 origin of a key an input needs, so a hardware wallet can find it.
    • getSpentOutput

      public BitcoinTransaction.Output getSpentOutput(int input)
      The output an input spends, from its witness or non-witness UTXO, or null.
    • getBip32Derivations

      public List<Psbt.Derivation> getBip32Derivations(int input)
      The BIP-32 origins recorded for an input's keys, both the ECDSA kind and the Taproot kind (BIP-371, whose public keys are 32-byte x-only). A wallet signs the inputs whose master fingerprint is its own.
    • addTapBip32Derivation

      public void addTapBip32Derivation(int input, byte[] xOnlyKey, int masterFingerprint, long[] path)
      Records the Taproot (BIP-371) origin of an input's x-only key, for a key-path spend.
    • getPartialSignatures

      public List<byte[][]> getPartialSignatures(int input)
      The signatures collected for an input: {publicKey, signature} pairs.
    • sign

      public boolean sign(int input, byte[] privateKey)

      Signs one input with privateKey if the key belongs to it, answering whether it did. The sighash type is the input's own, or SIGHASH_ALL (Taproot: SIGHASH_DEFAULT).

      Throws
      • IllegalStateException: when the input lacks the UTXO (or the scripts) it needs
    • sign

      public boolean sign(int input, BitcoinKey key)
      Signs one input with a key that may live elsewhere, such as a wallet or a hardware device; see [#sign(int, byte[])] for what is signed.
    • signAll

      public int signAll(byte[] privateKey)
      Signs every input the key belongs to, answering how many.
    • signAll

      public int signAll(BitcoinKey key)
      Signs every input a key belongs to, answering how many.
    • finalizeInput

      public boolean finalizeInput(int input)
      Builds the final scriptSig and witness of an input from its signatures, answering whether it could. A multisig input finalizes once it holds m signatures.
    • finalizeAll

      public boolean finalizeAll()
      Finalizes every input, answering whether all of them are final.
    • extract

      public BitcoinTransaction extract()

      The signed network transaction.

      Throws
      • IllegalStateException: when an input is not final