psbt
Methods for working with BIP 174 PSBTs
Last updated 4 days ago by alexbosworth .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install psbt 
SYNC missed versions from official npm registry.

PSBTs

PSBTs are a format for communicating and collaboratively working with transactions.

The format is defined in BIP-0174: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki

Methods

combinePsbts

Combine multiple PSBTs

Values that describe a spend, such as UTXOs, scripts, key origins, MuSig2 participants, and sighash types, must match across PSBTs. For other values that differ, such as final scripts, MuSig2 public nonces and MuSig2 partial signatures, the earliest PSBT's value is used.

Signatures that differ for a key are checked against the transaction and the output that their input spends. When the input has a sighash type, only signatures of that sighash type are considered, if there are any. The earliest valid signature is used, or the earliest signature when none of them can be checked and found valid.

Signatures must use the sighash type of their input when it has one. A 64 byte taproot signature uses the default sighash type.

{
  psbts: [<BIP 174 Encoded PSBT Hex String>]
}

@throws
<Combine PSBT Error>

@returns
{
  psbt: <BIP 174 Encoded PSBT Hex String>
}

createPsbt

Create a PSBT

When a timelock is set, inputs without a sequence use a sequence that lets the timelock take effect

{
  outputs: [{
    script: <Output ScriptPub Hex String>
    tokens: <Sending Tokens Number>
  }]
  [timelock]: <Set Lock Time on Transaction To Number>
  utxos: [{
    id: <Transaction Id Hex String>
    [sequence]: <Sequence Number>
    vout: <Output Index Number>
  }]
  [version]: <Transaction Version Number>
}

@throws
<Error>

@returns
{
  psbt: <Partially Signed Bitcoin Transaction Hex Encoded String>
}

decodePsbt

Decode a BIP 174 encoded PSBT

Public keys are returned exactly as they are encoded in the PSBT. The PSBT version, global extended public keys and proprietary fields are checked and returned as unrecognized attributes. Hash preimages and MuSig2 fields are checked and returned with their keys and values.

A final script witness is returned as its length prefixed elements, without the count of elements that comes before them in the PSBT.

{
  psbt: <Hex Encoded Partially Signed Bitcoin Transaction String>
}

@throws
<Invalid PSBT Error>

@returns
{
  inputs: [{
    [bip32_derivations]: [{
      fingerprint: <Public Key Fingerprint Hex String>
      [leaf_hashes]: [<Taproot Leaf Hash Hex String>]
      [path]: <BIP 32 Child/HardenedChild/Index Derivation Path String>
      public_key: <Public Key Hex String>
    }]
    [final_scriptsig]: <Final ScriptSig Hex String>
    [final_scriptwitness]: <Witness Elements Without Count Hex String>
    [musig2_partial_signatures]: [{
      aggregate_key: <Aggregate Public Key Hex String>
      [leaf_hash]: <Taproot Leaf Hash Hex String>
      partial_signature: <Partial Signature Hex String>
      participant_key: <Participant Public Key Hex String>
    }]
    [musig2_participants]: [{
      aggregate_key: <Aggregate Public Key Hex String>
      participant_keys: [<Participant Public Key Hex String>]
    }]
    [musig2_public_nonces]: [{
      aggregate_key: <Aggregate Public Key Hex String>
      [leaf_hash]: <Taproot Leaf Hash Hex String>
      participant_key: <Participant Public Key Hex String>
      public_nonce: <Public Nonce Hex String>
    }]
    [non_witness_utxo]: <Non-Witness Hex Encoded Transaction String>
    [partial_sig]: [{
      hash_type: <Signature Hash Type Number>
      public_key: <Public Key Hex String>
      signature: <Signature Hex String>
    }]
    [preimages]: [{
      hash: <Hash Hex String>
      hash_function: <Hash Function Name String>
      preimage: <Preimage Hex String>
    }]
    [redeem_script]: <Hex Encoded Redeem Script String>
    [sighash_type]: <Sighash Type Number>
    [taproot_control_block]: <Taproot Control Block Hex String>
    [taproot_leaf_hash]: <Taproot Leaf Hash Hex String>
    [taproot_leaf_public_key]: <Leaf Script X Only Public Key Hex String>
    [taproot_leaf_script]: <Taproot Leaf Spend Script Hex String>
    [taproot_leaf_scripts]: [{
      control_block: <Taproot Control Block Hex String>
      script: <Taproot Leaf Spend Script Hex String>
      version: <Taproot Leaf Spend Script Version Number>
    }]
    [taproot_leaf_version]: <Taproot Leaf Spend Script Version Number>
    [taproot_internal_key]: <X Only Taproot Internal Public Key Hex String>
    [taproot_key_spend_sig]: <Taproot Key Spend Signature Hex String>
    [taproot_key_spend_sig_hash_type]: <Key Spend Signature Sighash Number>
    [taproot_root_hash]: <Taproot Merkle Root Hash Hex String>
    [taproot_script_signature]: <Taproot Script Spend Signature Hex String>
    [taproot_script_signatures]: [{
      hash_type: <Signature Sighash Type Number>
      leaf_hash: <Taproot Leaf Hash Hex String>
      public_key: <Leaf Script X Only Public Key Hex String>
      signature: <Taproot Script Spend Signature Hex String>
    }]
    [unrecognized_attributes]: [{
      type: <Key Type Hex String>
      value: <Value Hex String>
    }]
    [witness_script]: <Witness Script Hex String>
    [witness_utxo]: {
      script_pub: <UTXO ScriptPub Hex String>
      tokens: <Tokens Number>
    }
  }]
  outputs: [{
    [bip32_derivation]: {
      fingerprint: <Public Key Fingerprint Hex String>
      [leaf_hashes]: [<Taproot Leaf Hash Hex String>]
      [path]: <BIP 32 Child/HardenedChild/Index Derivation Path String>
      public_key: <Public Key Hex String>
    }
    [bip32_derivations]: [{
      fingerprint: <Public Key Fingerprint Hex String>
      [leaf_hashes]: [<Taproot Leaf Hash Hex String>]
      [path]: <BIP 32 Child/HardenedChild/Index Derivation Path String>
      public_key: <Public Key Hex String>
    }]
    [musig2_participants]: [{
      aggregate_key: <Aggregate Public Key Hex String>
      participant_keys: [<Participant Public Key Hex String>]
    }]
    [redeem_script]: <Hex Encoded Redeem Script>
    [taproot_internal_key]: <X Only Taproot Internal Public Key Hex String>
    [taproot_script_tree]: [{
      depth: <Tree Depth Number>
      script: <Leaf Script Hex String>
      version: <Leaf Script Version Number>
    }]
    [unrecognized_attributes]: [{
      type: <Key Type Hex String>
      value: <Value Hex String>
    }]
    [witness_script]: <Hex Encoded Witness Script>
  }]
  pairs: [{
    type: <Key Type Hex String>
    value: <Value Hex String>
  }]
  [unrecognized_attributes]: [{
    type: <Global Key Type Hex String>
    value: <Global Value Hex String>
  }]
  unsigned_transaction: <Unsigned Transaction Hex String>
}

encodePsbt

Encode a Partially Signed Bitcoin Transaction

Every key starts with a complete, minimally encoded type number, and a key can only be used once within a section. A separator ends a section. The pairs must encode a valid PSBT.

{
  pairs: [{
    [separator]: <Is Separator Bool>
    [type]: <Type Buffer Object>
    [value]: <Value Buffer Object>
  }]
}

@throws
<Failed To Encode Error>

@returns
{
  psbt: <Hex Encoded Partially Signed Bitcoin Transaction String>
}

extendPsbt

Extend a PSBT with additional input fields

Existing values are kept, and added values replace existing values that have the same key.

Signatures must use the sighash type of their input when it has one. A 64 byte taproot signature uses the default sighash type.

A final script witness is given as it is encoded in a PSBT: the count of witness elements, then each length prefixed element.

{
  inputs: [{
    [bip32_derivations]: [{
      fingerprint: <Public Key Fingerprint Hex String>
      [leaf_hashes]: [<Taproot Leaf Hash Hex String>]
      [path]: <BIP 32 Child/HardenedChild/Index Derivation Path String>
      public_key: <Public Key Hex String>
    }]
    [final_scriptsig]: <Final ScriptSig Hex String>
    [final_scriptwitness]: <Witness Elements With Count Hex String>
    [non_witness_utxo]: <Non-Witness Hex Encoded Transaction String>
    [partial_sig]: [{
      hash_type: <Signature Hash Type Number>
      public_key: <Public Key Hex String>
      signature: <ECDSA Signature Hex String>
    }]
    [redeem_script]: <Hex Encoded Redeem Script String>
    [sighash_type]: <Sighash Type Number>
    [taproot_key_spend_sig]: <Taproot Key Spend Signature Hex String>
    [witness_script]: <Witness Script Hex String>
    [witness_utxo]: {
      script_pub: <UTXO ScriptPub Hex String>
      tokens: <Tokens Number>
    }
  }]
  psbt: <BIP 174 Encoded PSBT String>
}

@throws
<Error>

@returns
{
  psbt: <Extended PSBT Hex String>
}

extractTransaction

Extract a transaction from a finalized PSBT

Every input needs a final scriptSig or a final script witness, either of which can be empty. A transaction needs inputs and outputs to be extracted.

The transaction is serialized with witnesses when an input has a final script witness with at least one element, even if the element is empty.

Final scripts are checked against the output that their input spends, when it is known and it is a kind of output that finalizing supports. Scripts must be the scripts that the output pays to, and signatures must be valid signatures that nodes relay, with a low s value. Scripts that only push data are run, and so are the rules that make a script fail whatever it runs on, such as its size, its pushes and disabled opcodes. A scriptSig that always fails is rejected even when its spent output is not known. Other final scripts, and taproot spends with an annex, are not checked.

When every input has a UTXO, the fee is checked: outputs cannot spend more than the inputs, and the fee rate cannot be over the maximum fee rate, which is 25,000 tokens per vbyte unless another maximum is given. A given maximum fee rate needs the UTXO of every input to check it.

A given maximum fee rate also needs the full previous transaction of each input, unless a valid final taproot signature signs every input. Other signatures, such as version 0 witness signatures and taproot signatures with SIGHASH_ANYONECANPAY, only commit to the amount of their own input, so a witness UTXO alone could claim less than its input spends. The default maximum fee rate only catches gross mistakes, such as a large input without its change output, but it trusts the amount of a witness UTXO, so it does not protect against a PSBT that misstates an amount. A maximum fee rate of Infinity does not limit the fee.

{
  [max_fee_tokens_per_vbyte]: <Maximum Fee Tokens Per Virtual Byte Number>
  psbt: <BIP 174 Encoded PSBT Hex String>
}

@throws
<Extract Transaction Error>

@returns
{
  transaction: <Hex Serialized Transaction String>
}

finalizePsbt

Finalize the inputs of a PSBT

Inputs are finalized for the output that they spend, so they need a UTXO. The output must pay to a public key, a public key hash, or a multisig, directly or through a redeem or witness script, or be a taproot output with a key spend signature or a leaf script that can be satisfied.

Signatures of the keys that a script pays to are checked against the transaction and the outputs that they spend, and an invalid signature stops its input from being finalized. ECDSA signatures need a low s value. A taproot signature that signs every input needs the output that each input spends. A key spend signature that cannot be checked without them is not used when a leaf script can spend the input instead.

A taproot output without a valid key spend signature is spent with the smallest witness of a leaf script that is a miniscript of keys, k of n keys (multi_a), timelocks (older and after) and hashlocks (sha256, hash256, ripemd160 and hash160) joined with and_v. The leaf script is satisfied with valid signatures, exactly k of them for k of n keys, preimages that the PSBT has for its hashlocks, and a sequence and lock time that meet its timelocks.

An input that is already final keeps its final scripts. Its final scripts are checked the same way as the final scripts of a transaction being extracted.

A P2SH redeem script can be at most 520 bytes, the most that a scriptSig can push. Witness scripts are not limited this way.

Final inputs keep their UTXO and the fields not used for signing, such as unknown and proprietary fields.

When finalizing is partial, inputs that cannot be finalized, such as inputs that are not signed yet, are left as they are instead of stopping the PSBT from being finalized.

{
  [is_partial]: <Finalize Only the Inputs That Can Be Finalized Bool>
  psbt: <BIP 174 Encoded PSBT Hex String>
}

@throws
<Finalize PSBT Error>

@returns
{
  psbt: <BIP 174 Encoded PSBT Hex String>
}

signPsbt

Update a PSBT with signatures

Inputs are signed by the signing keys that their scripts pay to. A script must pay to a public key, to a public key hash, or to a multisig of public keys. Inputs with other scripts, finalized inputs, and keys that already signed an input are skipped.

A taproot input gets a key spend signature from the key of its output, or from its internal key tweaked with the merkle root of its script tree. A signing key is also tried as the internal key of an output without a script tree. When no signing key signs for the output key, leaf scripts get script spend signatures from the signing keys that they check, when they are a miniscript of keys, k of n keys, timelocks and hashlocks joined with and_v. Taproot inputs without a sighash type are signed with the default sighash type, and taproot inputs with a key spend signature are skipped.

Signatures commit to the whole transaction by default. Signing an input whose sighash type is not SIGHASH_ALL, or the default of a taproot input, fails unless that sighash type is allowed, as BIP 174 requires.

ECDSA signatures have a low R value, as Bitcoin Core makes them, so that they encode one byte shorter. Taproot signatures are made with new random data each time, as BIP 340 advises, so signing again gives another signature.

Version 0 witness inputs are only signed with compressed public keys.

Legacy inputs need their full previous transaction and witness inputs need a UTXO to be signed. A taproot signature that signs every input needs the UTXO of every input.

When every input has a UTXO, the fee is checked, and no signatures are returned when the check fails: outputs cannot spend more than the inputs, and the fee rate cannot be over the maximum fee rate, which is 25,000 tokens per vbyte unless another maximum is given. A given maximum fee rate needs the UTXO of every input.

A given maximum fee rate also needs the full previous transaction of each input, unless every signature that this makes is a taproot signature of every input. Other signatures, such as ECDSA signatures and taproot signatures with SIGHASH_ANYONECANPAY, do not commit to the amount of every input, so a witness UTXO alone could claim less than its input spends. The default maximum fee rate only catches gross mistakes, such as a large input without its change output, but it trusts the amount of a witness UTXO, so it does not protect against a PSBT that misstates an amount. A maximum fee rate of Infinity does not limit the fee.

The fee rate is measured with the size of the transaction without its signatures, which is never more than its signed virtual size. With the amounts that the PSBT gives, a signed transaction never pays over the maximum fee rate, but a fee rate that is just within the maximum for the signed transaction can be rejected: the measured fee rate is a little higher for witness inputs, and up to a few times higher for legacy inputs.

The network is bitcoin, regtest, signet, testnet, or testnet4. An unrecognized network name is taken to be the bitcoin network.

{
  [allowed_sighash_types]: [<Also Allow Signing With Sighash Type Number>]
  [max_fee_tokens_per_vbyte]: <Maximum Fee Tokens Per Virtual Byte Number>
  network: <Network Name String>
  psbt: <BIP 174 Encoded PSBT Hex String>
  signing_keys: [<WIF Encoded Private Key String>]
}

@throws
<Sign PSBT Error>

@returns
{
  psbt: <BIP 174 Encoded PSBT Hex String>
}

transactionAsPsbt

Convert a signed transaction to a signed PSBT

Inputs can spend P2PKH, P2WPKH, and nested P2WPKH outputs, and P2SH, P2WSH, nested P2WSH, and bare outputs with a script that pays to a public key, to a public key hash, or to a multisig. Taproot inputs can be key spends, or spends of a leaf script that is a miniscript of keys, k of n keys, timelocks and hashlocks joined with and_v. Other spends are rejected, as is a conversion that would not finalize to the same transaction.

The PSBT keeps the exact locktime and input sequences of the transaction.

{
  spending: [<Spending Transaction Hex String>]
  transaction: <Hex Encoded Transaction String>
}

@throws
<Error>

@returns
{
  psbt: <Signed PSBT String>
}

unextractTransaction

Convert a raw transaction into a finalized PSBT ready for extraction

An input that spends a legacy output needs the full previous transaction that it spends from. An input that spends a witness output, including a P2SH output spent with a push of the witness program that it hashes, can instead have a UTXO reference to the output that it spends. A UTXO reference given along with the previous transaction must describe the same output.

{
  spending: [<Spending Transaction Hex String>]
  transaction: <Hex Encoded Transaction String>
  [utxos]: [{
    script_pub: <Output Script Hex String>
    tokens: <Output Tokens Number>
    vin: <Input Index Number>
  }]
}

@throws
<Error>

@returns
{
  psbt: <Finalized PSBT Hex String>
}

updatePsbt

Update a PSBT

Everything already in the PSBT is kept. Supplied values are added, and they replace an existing value that has the same key.

Scripts and derivations are matched to the output that each input spends and to each output. Derivations are only added for keys that the scripts check signatures with.

Taproot key origins are added for the internal key of a taproot input or output, and for keys that its leaf scripts check signatures with, listing those leaves. A derivation key that tweaks to the key of a taproot output without a script tree is added as its internal key.

When the output being spent is known, taproot input fields are only added for a taproot output, and an internal key, merkle root and leaf scripts must commit to its output key. Leaf scripts must be in the script tree of the internal key and merkle root, and give the internal key and merkle root of their control blocks when these are not otherwise known.

A preimage is added to its input keyed by its hash, made with its hash function: hash160, hash256, ripemd160 or sha256.

A supplied funding transaction is added as the witness UTXO of a witness output, and as the full transaction of a legacy or version 0 witness output, so that signers can check the amount being spent.

The sighash type of an input is chosen once every change to the input is merged, including additional attributes. An input with a sighash type after the changes keeps it, and all of its signatures must use it. Otherwise, supplied signatures give the input the sighash type that all of its signatures share, or no sighash type when they use different ones. Taproot signatures do not set a sighash type.

{
  [additional_attributes]: [{
    type: <Type Hex String>
    value: <Value Hex String>
    [vin]: <Input Index Number>
    [vout]: <Output Index Number>
  }]
  [bip32_derivations]: [{
    fingerprint: <BIP 32 Fingerprint of Parent's Key Hex String>
    [path]: <BIP 32 Derivation Path String>
    public_key: <Public Key Hex String>
  }]
  [preimages]: [{
    hash_function: <Hash Function Name String>
    preimage: <Preimage Hex String>
    vin: <Input Index Number>
  }]
  psbt: <BIP 174 Encoded PSBT String>
  [redeem_scripts]: [<Hex Encoded Redeem Script String>]
  [sighashes]: [{
    id: <Transaction Id String>
    sighash: <Sighash Flag Number>
    vout: <Spending Output Index Number>
  }]
  [signatures]: [{
    [hash_type]: <Signature Hash Type Number>
    public_key: <Public Key Hex String>
    signature: <Signature Hex String>
    vin: <Signature Input Index Number>
  }]
  [taproot_inputs]: [{
    [internal_key]: <Taproot Internal X Only Public Key Hex String>
    [key_spend_sig]: <Taproot Key Spend Signature Hex String>
    [leaf_scripts]: [{
      control_block: <Taproot Control Block Hex String>
      script: <Taproot Leaf Script Hex String>
    }]
    [root_hash]: <Taproot Merkle Root Hash Hex String>
    [script_signatures]: [{
      leaf_hash: <Taproot Leaf Hash Hex String>
      public_key: <Leaf Script X Only Public Key Hex String>
      signature: <Taproot Script Spend Signature Hex String>
    }]
    vin: <Input Index Number>
  }]
  [transactions]: [<Hex Encoded Transaction String>]
  [witness_scripts]: [<Witness Script Hex String>]
}

@throws
<Update PSBT Error>

@returns
{
  psbt: <Hex Encoded Partially Signed Bitcoin Transaction String>
}

Current Tags

  • 8.0.0                                ...           latest (4 days ago)

41 Versions

  • 8.0.0                                ...           4 days ago
  • 7.0.1                                ...           a month ago
  • 7.0.0                                ...           2 months ago
  • 6.0.4                                ...           2 months ago
  • 6.0.3                                ...           3 months ago
  • 6.0.2                                ...           3 months ago
  • 6.0.1                                ...           3 months ago
  • 6.0.0                                ...           3 months ago
  • 5.0.0                                ...           9 months ago
  • 4.0.0                                ...           2 years ago
  • 3.0.0                                ...           3 years ago
  • 2.7.2                                ...           4 years ago
  • 2.7.1                                ...           4 years ago
  • 2.7.0                                ...           4 years ago
  • 2.6.0                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.0                                ...           4 years ago
  • 2.3.0                                ...           4 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.1.11                                ...           5 years ago
  • 1.1.10                                ...           6 years ago
  • 1.1.9                                ...           6 years ago
  • 1.1.8                                ...           6 years ago
  • 1.1.7                                ...           6 years ago
  • 1.1.6                                ...           6 years ago
  • 1.1.5                                ...           6 years ago
  • 1.1.4                                ...           6 years ago
  • 1.1.3                                ...           6 years ago
  • 1.1.2                                ...           7 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.6                                ...           7 years ago
  • 1.0.5                                ...           7 years ago
  • 1.0.4                                ...           7 years ago
  • 1.0.3                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE