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::Transaction::UTXO - Unspent transaction output instance

SYNOPSIS

        use Bitcoin::Crypto qw(btc_utxo);

        # register the utxos automatically from the serialized transaction
        btc_utxo->extract($serialized_tx);

        # create the utxo manually
        my $utxo = btc_utxo->new(
                txid => [hex => '94e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457'],
                output_index => 1,
                output => {
                        locking_script => [P2PKH => '1HrfeGdVP4d1uAdbSknzeaFpDFQVJyVpLu'],
                        value => 1_02119131,
                }
        );

        # register
        $utxo->register;

        # find the utxo
        btc_utxo->get([hex => '94e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457'], 1);

        # unregister
        $utxo->unregister;

DESCRIPTION

UTXO is a transaction output which hasn't been spent yet. All transaction inputs must be UTXOs. Bitcoin::Crypto requires you to register UTXOs before you can create a transaction.

INTERFACE

Attributes

txid

A bytestring - id of the source transaction.

Available in the constructor.

output_index

A positive or zero integer which is the index of the output in the source transaction.

Available in the constructor.

block

Optional instance of Bitcoin::Crypto::Block.

Available in the constructor.

output

Instance of Bitcoin::Crypto::Transaction::Output. A hash reference will be coerced into an object by passing it to the constructor.

Available in the constructor.

Methods

new

        $tx = $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.

register

        $object = $object->register()

Registers the given UTXO. It will be held in memory and will be available to fetch using "get".

unregister

        $object = $object->unregister()

Does the opposite of "register".

get

        $utxo = $object->get($txid, $output_index);

Returns the UTXO registered with given txid and output index. Throws an exception if it cannot be found or loaded.

set_loader

        $class->set_loader(sub { ... })
        $class->set_loader(undef)

Replaces an UTXO loader.

The subroutine should accept the same parameters as "get" and return a constructed UTXO object. If possible, the loader should not return the same UTXO twice in a single runtime of the script.

Returns nothing. Passing undef disables the loader.

extract

        $class->extract($serialized_tx)

Extracts all outputs from the $serialized_tx (a bytestring).

Returns nothing.

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:

  • UTXO - UTXO was not found

SEE ALSO

Bitcoin::Crypto::Transaction