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

NAME

Bitcoin::Crypto::Script - Bitcoin script representations

SYNOPSIS

        use Bitcoin::Crypto::Script;

        my $script = Bitcoin::Crypto::Script->new
                ->add_operation("OP_1")
                ->add_operation("OP_TRUE")
                ->add_operation("OP_EQUAL");

        # getting serialized script
        my $serialized = $script->get_script();

        # getting address from script (p2wsh)
        my $address = $script->get_segwit_adress();

DESCRIPTION

This class allows you to create a bitcoin script representations

You can use a script object to:

  • create a script from opcodes

  • serialize script into byte string

  • create legacy (p2sh), compat (p2sh(p2wsh)) and segwit (p2wsh) adresses

METHODS

new

        $script_object = $class->new($data)

A constructor. Returns new script instance

add_operation

        $script_object = $object->add_operation($opcode)

Adds a new opcode at the end of a script. Returns the object instance for chaining.

Throws an exception for unknown opcodes.

add_raw

        $script_object = $object->add_raw($bytes)

Adds $bytes at the end of a script. Useful when you need a value in a script that shouldn't be pushed to the execution stack, like the first four bytes after PUSHDATA4.

Returns the object instance for chaining.

push_bytes

        $script_object = $object->push_bytes($bytes)

Pushes $bytes to the execution stack at the end of a script, using a minimal push opcode.

For example, running $script-push_bytes("\x03")> will have the same effect as $script-add_operation("OP_3")>.

Throws an exception for data exceeding a 4 byte number in length.

Returns the object instance for chaining.

get_script

        $bytestring = $object->get_script()

Returns a serialized script as byte string.

get_script_hash

        $bytestring = $object->get_script_hash()

Returns a serialized script parsed with HASH160 (ripemd160 of sha256).

set_network

        $script_object = $object->set_network($val)

Change key's network state to $val. It can be either network name present in Bitcoin::Crypto::Network package or an instance of this class.

Returns current object instance.

get_legacy_address

        $address = $object->get_legacy_address()

Returns string containing Base58Check encoded script hash (p2sh address)

get_compat_address

        $address = $object->get_compat_address()

Returns string containing Base58Check encoded script hash containing a witness program for compatibility purposes (p2sh(p2wsh) address)

get_segwit_address

        $address = $object->get_segwit_address()

Returns string containing Bech32 encoded witness program (p2wsh address)

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:

  • ScriptOpcode - unknown opcode was specified

  • ScriptPush - data pushed to the execution stack is invalid

  • NetworkConfig - incomplete or corrupted network configuration

SEE ALSO

Bitcoin::Crypto::PrivateKey
Bitcoin::Crypto::Network