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 Psbt

  1. Object
  2. Psbt

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 handles P2PKH, P2WPKH, P2SH-P2WPKH, Taproot key-path, and P2SH/P2WSH multisig inputs; finalizeInput builds the final scripts for the same kinds. Fields this class does not interpret are kept and written back unchanged.

Nested types

class Psbt.DerivationOne key origin: a public key, the fingerprint of the master it descends from and the path from that master.

Methods

public static Psbt fromTransaction(BitcoinTransaction unsigned)A PSBT for an unsigned transaction; its scripts and witnesses are cleared.
public static Psbt parse(byte[] data)Reads a binary PSBT.
public static Psbt fromBase64(String base64)Reads a Base64 PSBT, the form wallets exchange.
public byte[] serialize()The binary PSBT.
public String toBase64()The Base64 PSBT.
public BitcoinTransaction getTransaction()The unsigned transaction.
public void setWitnessUtxo(int input, BitcoinTransaction.Output spent)Records the output a SegWit input spends.
public void setNonWitnessUtxo(int input, BitcoinTransaction previous)Records the whole previous transaction of an input, which legacy inputs need.
public void setRedeemScript(int input, Script redeem)Records the redeem script of a P2SH input.
public void setWitnessScript(int input, Script witnessScript)Records the witness script of a P2WSH input.
public void setTapInternalKey(int input, byte[] xOnly)Records the x-only internal key of a Taproot input.
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.
public BitcoinTransaction.Output getSpentOutput(int input)The output an input spends, from its witness or non-witness UTXO, or null.
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).
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.
public List<byte[][]> getPartialSignatures(int input)The signatures collected for an input: {publicKey, signature} pairs.
public boolean sign(int input, byte[] privateKey)Signs one input with privateKey if the key belongs to it, answering whether it did.
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.
public int signAll(byte[] privateKey)Signs every input the key belongs to, answering how many.
public int signAll(BitcoinKey key)Signs every input a key belongs to, answering how many.
public boolean finalizeInput(int input)Builds the final scriptSig and witness of an input from its signatures, answering whether it could.
public boolean finalizeAll()Finalizes every input, answering whether all of them are final.
public BitcoinTransaction extract()The signed network transaction.

Inherited methods

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