The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Bitcoin::Crypto::Segwit - Segregated Witness version definitions

SYNOPSIS

        use Bitcoin::Crypto::Segwit qw(validate_program);

        my $program_version = validate_program($segwit_program);

DESCRIPTION

This module provides tools required to define and use a Segregated Witness version validator.

FUNCTIONS

validate_program

        $segwit_version = validate_program($program)

Performs a segwit program validation on $program, which is expected to be a byte string in which the first byte is a segwit version. Based on this version a validator is invoked, present in %Bitcoin::Crypto::Segwit::validators module hash. If the validator is not defined for a segwit version being validated, a warning is issued.

The function returns the detected segwit program version. Please note that it does not perform any more checks than ensuring the byte string is in correct format.

The current implementation defines a validator for segwit version 0. In the future (when another segwit program version is defined) it might be neccessary to define another one in the program until it's added to the library. This can be done like so:

        use Bitcoin::Crypto::Segwit;
        use Bitcoin::Crypto::Exception;

        $Bitcoin::Crypto::Segwit::validators{1} = sub {
                my ($data) = @_;

                # perform validation
                Bitcoin::Crypto::Exception::SegwitProgram->raise(
                        "validation of program version 1 failed"
                ) if ...;

                # if validation is successful just do nothing
                return;
        };

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:

  • SegwitProgram - a validation of a segwit program has failed

SEE ALSO

Bitcoin::Crypto::Exception
Bitcoin::Crypto::Bech32