NAME

Bitcoin::Crypto::PSBT - Partially Signed Bitcoin Transactions

SYNOPSIS

use Bitcoin::Crypto qw(btc_psbt);

# import PSBT from a serialized form
my $psbt = btc_psbt->from_serialized([base64 => $psbt_string]);

# dump in readable format
print $psbt->dump;

# get a single PSBT field
my $field = $psbt->get_field('PSBT_GLOBAL_TX_VERSION');

# get decoded field key and value
my $key = $field->key;
my $value = $field->value;

# get all PSBT fields of a given type
my @fields = $psbt->get_all_fields('PSBT_GLOBAL_PROPRIETARY');

DESCRIPTION

This is a class implementing the PSBT format as described in BIP174 and BIP370. It currently supports versions 0 and 2 of the spec. It allows serialization, deserialization, validation and access to PSBT fields. It currently does not support the roles defined by the PSBT documents, so all the operations on PSBTs (like adding inputs or creating a final transaction out of it) must be done manually.

PSBT consists of a number of maps: one global, one for each transaction input and one for each transaction output. Each map holds a number of fields. Each field has a value and can optionally have extra key data.

INTERFACE

Attributes

maps

Not assignable in the constructor

An array reference of PSBT internal maps - objects of class Bitcoin::Crypto::PSBT::Map. It should seldom be handled manually - use "get_field", "get_all_fields" and "add_field" to access fields of specific map.

Methods

new

$psbt = $class->new(%args)

This is a standard Moo constructor, which can be used to create the object.

Returns class instance.

version

$version = $object->version()

Returns the version of the PSBT (0 or 2).

input_count

$int = $object->input_count()

Returns the number of inputs the PSBT defines.

output_count

$int = $object->output_count()

Returns the number of outputs the PSBT defines.

get_field

$field = $object->get_field($field_type_name, $map_index = undef, $key = undef)

Tries to get a field of $field_type_name as defined in BIP174, for example PSBT_GLOBAL_UNSIGNED_TX. If the field is from input or output maps, it also requires $map_index to be passed (0-based index of the input or output). Optional $key (a bytestring) can be provided if this PSBT field defines keys.

Returns an instance of Bitcoin::Crypto::PSBT::Field, which you can use to access key and value data.

If there isn't exactly one field with this type in the map, it will throw an exception. This allows you to write the following without checking the return value of the function:

my $output_index_0 = $object->get_field('PSBT_IN_OUTPUT_INDEX', 0)->value;

See "get_all_fields" for a variant which does not check the field count.

get_all_fields

@fields = $object->get_all_fields($field_type_name, $map_index = undef, $key = undef)

Same as "get_field", but will return all the fields of given type from a given map. It may be used if the field exists, or to get multiple fields with different key data.

The return value is a list (not an array), so using it in scalar context will get the last found field (as opposed to a field count).

add_field

$object = $object->add_field(%field_data)
$object = $object->add_field($field_object, $map_index = undef)

Adds a new field to the PSBT. It can be run either with %field_data (a hash arguments for the "new" in Bitcoin::Crypto::PSBT::Field) or with $field_object (constructed Bitcoin::Crypto::PSBT::Field) and $map_index.

If passing %field_data hash, it can contain an additional index key to represent $map_index. The field will be constructed and added to the map. Adding the index to a map triggers its validations, so it must be complete enough to pass them. For this reason, sometimes it could be more preferable to construct and fill the field by hand before adding it to the PSBT.

Note that a field cannot be used in more than one map at a time.

list_fields

@list = $object->list_fields()

This method lists all fields present in the PSBT. It returns a list of array references, each array reference has two elements: field type and map index. These are the same elements that are needed in "get_all_fields" calls, so you may use this data to loop through PSBT fields.

check

$object = $object->check()

Checks the internal state of PSBT fields and throws an exception if it is invalid. Returns the object itself.

to_serialized

$serialized = $object->to_serialized()

Serializes a PSBT into a bytestring. "check" is called automatically before serializing.

from_serialized

$object = $class->from_serialized($data)

Deserializes the bytestring $data into a PSBT object. "check" is called automatically after deserializing.

dump

$text = $object->dump()

Returns a readable description of all the maps in the PSBT.

FIELD REFERENCE

Just for reference, here is a list of all PSBT fields which are currently supported by this module. This list is auto-generated from "key_data" in Bitcoin::Crypto::PSBT::FieldType and Bitcoin::Crypto::PSBT::FieldType/value_data (which contain short strings with description of field content).

For a list of PSBT fields, see BIP174.

Global map

  • PSBT_GLOBAL_UNSIGNED_TX

    Key data: <none>

    Value data: Bitcoin::Crypto::Transaction object

  • PSBT_GLOBAL_XPUB

    Key data: Bitcoin::Crypto::Key::ExtPublic object

    Value data: Array reference, where the first item is a fingerprint and the second item is Bitcoin::Crypto::DerivationPath

  • PSBT_GLOBAL_TX_VERSION

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_GLOBAL_FALLBACK_LOCKTIME

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_GLOBAL_INPUT_COUNT

    Key data: <none>

    Value data: Positive integer value

  • PSBT_GLOBAL_OUTPUT_COUNT

    Key data: <none>

    Value data: Positive integer value

  • PSBT_GLOBAL_TX_MODIFIABLE

    Key data: <none>

    Value data: Hash reference with flags: inputs_modifiable, outputs_modifiable, has_sighash_single

Input map

  • PSBT_IN_NON_WITNESS_UTXO

    Key data: <none>

    Value data: Bitcoin::Crypto::Transaction object

  • PSBT_IN_WITNESS_UTXO

    Key data: <none>

    Value data: Bitcoin::Crypto::Transaction::Output object

  • PSBT_IN_PARTIAL_SIG

    Key data: Bitcoin::Crypto::Key::Public object

    Value data: Bytestring value

  • PSBT_IN_SIGHASH_TYPE

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_IN_REDEEM_SCRIPT

    Key data: <none>

    Value data: Bitcoin::Crypto::Script object

  • PSBT_IN_WITNESS_SCRIPT

    Key data: <none>

    Value data: Bitcoin::Crypto::Script object

  • PSBT_IN_BIP32_DERIVATION

    Key data: Bitcoin::Crypto::Key::Public object

    Value data: Array reference, where the first item is a fingerprint and the second item is Bitcoin::Crypto::DerivationPath

  • PSBT_IN_FINAL_SCRIPTSIG

    Key data: <none>

    Value data: Bitcoin::Crypto::Script object

  • PSBT_IN_FINAL_SCRIPTWITNESS

    Key data: <none>

    Value data: Bytestring value

  • PSBT_IN_POR_COMMITMENT

    Key data: <none>

    Value data: Bytestring value

  • PSBT_IN_RIPEMD160

    Key data: Bytestring value

    Value data: Bytestring value

  • PSBT_IN_SHA256

    Key data: Bytestring value

    Value data: Bytestring value

  • PSBT_IN_HASH160

    Key data: Bytestring value

    Value data: Bytestring value

  • PSBT_IN_HASH256

    Key data: Bytestring value

    Value data: Bytestring value

  • PSBT_IN_PREVIOUS_TXID

    Key data: <none>

    Value data: Bytestring value

  • PSBT_IN_OUTPUT_INDEX

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_IN_SEQUENCE

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_IN_REQUIRED_TIME_LOCKTIME

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_IN_REQUIRED_HEIGHT_LOCKTIME

    Key data: <none>

    Value data: 32-bit positive integer value

  • PSBT_IN_TAP_KEY_SIG

    Key data: <none>

    Value data: Bytestring value

  • PSBT_IN_TAP_SCRIPT_SIG

    Key data: Array reference, where the first item is Bitcoin::Crypto::Key::Public and second element is a leaf hash bytestring

    Value data: Bytestring value

  • PSBT_IN_TAP_LEAF_SCRIPT

    Key data: Instance of Bitcoin::Crypto::Transaction::ControlBlock

    Value data: Array reference, where the first item is Bitcoin::Crypto::Script and second element is a leaf version number

  • PSBT_IN_TAP_BIP32_DERIVATION

    Key data: Bitcoin::Crypto::Key::Public object

    Value data: Array reference, where first item is an array of leaf hashes, second element is a fingerprint and the third element is Bitcoin::Crypto::DerivationPath

  • PSBT_IN_TAP_INTERNAL_KEY

    Key data: <none>

    Value data: Bitcoin::Crypto::Key::Public object

  • PSBT_IN_TAP_MERKLE_ROOT

    Key data: <none>

    Value data: Bytestring value

Output map

  • PSBT_OUT_REDEEM_SCRIPT

    Key data: <none>

    Value data: Bitcoin::Crypto::Script object

  • PSBT_OUT_WITNESS_SCRIPT

    Key data: <none>

    Value data: Bitcoin::Crypto::Script object

  • PSBT_OUT_BIP32_DERIVATION

    Key data: Bitcoin::Crypto::Key::Public object

    Value data: Array reference, where the first item is a fingerprint and the second item is Bitcoin::Crypto::DerivationPath

  • PSBT_OUT_AMOUNT

    Key data: <none>

    Value data: Math::BigInt object

  • PSBT_OUT_SCRIPT

    Key data: <none>

    Value data: Bitcoin::Crypto::Script object

  • PSBT_OUT_TAP_INTERNAL_KEY

    Key data: <none>

    Value data: Bitcoin::Crypto::Key::Public object

  • PSBT_OUT_TAP_TREE

    Key data: <none>

    Value data: Bitcoin::Crypto::Script::Tree instance

  • PSBT_OUT_TAP_BIP32_DERIVATION

    Key data: Bitcoin::Crypto::Key::Public object

    Value data: Array reference, where first item is an array of leaf hashes, second element is a fingerprint and the third element is Bitcoin::Crypto::DerivationPath

EXCEPTIONS

This module throws an instance of Bitcoin::Crypto::Exception if it encounters an error. It can produce the following error types from the Bitcoin::Crypto::Exception namespace:

  • PSBT - general error with the PSBT

SEE ALSO

Bitcoin::Crypto::PSBT::Field
Bitcoin::Crypto::Transaction