The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Bitcoin::Crypto::PSBT::Field - Single field of a PSBT

SYNOPSIS

        use Bitcoin::Crypto::PSBT::Field;

        my $field = Bitcoin::Crypto::PSBT::Field->new(
                type => 'PSBT_IN_OUTPUT_INDEX',
                value => 1,
        );

        $psbt->add_field($field, 1);

DESCRIPTION

This is a helper class which represents a single PSBT field.

While fields hold bytestring data, Bitcoin::Crypto defines some serializers and deserializers to make it easier to handle the keys and values. These try to DWIM and should be pretty straightforward, for example PSBT_GLOBAL_UNSIGNED_TX deserializes into an object of Bitcoin::Crypto::Transaction. Serializers are not currently documented, but fields key_data and value_data of Bitcoin::Crypto::PSBT::FieldType holds the descriptions of what is the type after deserialization (and before serialization). Reading the source of Bitcoin::Crypto::PSBT::FieldType may be required if it isn't clear how they are implemented for a specific field.

Reading the value through "raw_value" will return a bytestring, but reading thourgh value will use the deserializer. Calling set_value will use the serializer to update "raw_value". The field only holds raw data and uses serializers to update it as a convenience.

INTERFACE

Attributes

map

The Bitcoin::Crypto::PSBT::Map object this field belongs to. Field can only belong to a single map at a time. There is no need to set it manually, it will be set when adding the field to a map.

writer: set_map

type

Required in the constructor. The type of the field. Must be an instance of Bitcoin::Crypto::PSBT::FieldType. Can be coerced from a PSBT_* field name.

raw_key

Available in the constructor. Raw bytestring keydata for this field. Only valid for field types which actually define key data.

To use a dedicated serializer for a key, use key (constructor key), key (reader method) or set_key (writer method).

writer: set_raw_key

raw_value

Available in the constructor. Raw bytestring valuedata for this field.

To use a dedicated serializer for a value, use value (constructor key), value (reader method) or set_value (writer method).

writer: set_raw_value

Methods

new

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

This is a standard Moo constructor, which can be used to create the object. It takes arguments specified in "Attributes".

Returns class instance.

validate

        $object = $object->validate()

Performs a validation of this field. Will throw an exception if the validation fails. This method is called automatically when a field is added to a map.

serialized_key

        $bytestring = $object->serialized_key()

Returns a key in the serialized form (compactsize type + key). Used to sort the keys for the serialized PSBT map.

to_serialized

        $serialized = $object->to_serialized()

Serializes a field into a bytestring.

from_serialized

        $object = $class->from_serialized($data, %params)

Deserializes the bytestring $data into a field.

%params can be any of:

  • map_type

    A constant for map type - required.

  • pos

    Position for partial string decoding. Optional. If passed, must be a scalar reference to an integer value.

    This integer will mark the starting position of $bytestring from which to start decoding. It will be set to the next byte after end of input stream.

dump

        $text = $object->dump()

Returns a readable description of this field.

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