Class Psbt
java.lang.Object
com.codename1.crypto.bitcoin.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(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 ClassesModifier and TypeClassDescriptionstatic final classOne key origin: a public key, the fingerprint of the master it descends from and the path from that master. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBip32Derivation(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.voidaddTapBip32Derivation(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.extract()The signed network transaction.booleanFinalizes every input, answering whether all of them are final.booleanfinalizeInput(int input) Builds the finalscriptSigand witness of an input from its signatures, answering whether it could.static PsbtfromBase64(String base64) Reads a Base64 PSBT, the form wallets exchange.static PsbtfromTransaction(BitcoinTransaction unsigned) A PSBT for an unsigned transaction; its scripts and witnesses are cleared.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).List<byte[][]> getPartialSignatures(int input) The signatures collected for an input:{publicKey, signature}pairs.getSpentOutput(int input) The output an input spends, from its witness or non-witness UTXO, ornull.The unsigned transaction.static Psbtparse(byte[] data) Reads a binary PSBT.byte[]The binary PSBT.voidsetNonWitnessUtxo(int input, BitcoinTransaction previous) Records the whole previous transaction of an input, which legacy inputs need.voidsetRedeemScript(int input, Script redeem) Records the redeem script of a P2SH input.voidsetTapInternalKey(int input, byte[] xOnly) Records the x-only internal key of a Taproot input.voidsetWitnessScript(int input, Script witnessScript) Records the witness script of a P2WSH input.voidsetWitnessUtxo(int input, BitcoinTransaction.Output spent) Records the output a SegWit input spends.booleansign(int input, byte[] privateKey) Signs one input withprivateKeyif the key belongs to it, answering whether it did.booleansign(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.intsignAll(byte[] privateKey) Signs every input the key belongs to, answering how many.intsignAll(BitcoinKey key) Signs every input a key belongs to, answering how many.toBase64()The Base64 PSBT.
-
Method Details
-
fromTransaction
A PSBT for an unsigned transaction; its scripts and witnesses are cleared. -
parse
Reads a binary PSBT.
Throws
IllegalArgumentException: for a malformed PSBT, a duplicate key, or a global transaction that is not unsigned
-
fromBase64
-
serialize
public byte[] serialize()The binary PSBT. -
toBase64
The Base64 PSBT. -
getTransaction
The unsigned transaction. -
setWitnessUtxo
Records the output a SegWit input spends. -
setNonWitnessUtxo
Records the whole previous transaction of an input, which legacy inputs need. -
setRedeemScript
Records the redeem script of a P2SH input. -
setWitnessScript
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
The output an input spends, from its witness or non-witness UTXO, ornull. -
getBip32Derivations
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
The signatures collected for an input:{publicKey, signature}pairs. -
sign
public boolean sign(int input, byte[] privateKey) Signs one input with
privateKeyif the key belongs to it, answering whether it did. The sighash type is the input's own, orSIGHASH_ALL(Taproot:SIGHASH_DEFAULT).Throws
IllegalStateException: when the input lacks the UTXO (or the scripts) it needs
-
sign
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
Signs every input a key belongs to, answering how many. -
finalizeInput
public boolean finalizeInput(int input) Builds the finalscriptSigand witness of an input from its signatures, answering whether it could. A multisig input finalizes once it holdsmsignatures. -
finalizeAll
public boolean finalizeAll()Finalizes every input, answering whether all of them are final. -
extract
The signed network transaction.
Throws
IllegalStateException: when an input is not final
-