NAME

Bitcoin::Crypto::Script - class for 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

  • (work in progress) run script and get resulting stack

METHODS

new

        sig: new($class, $data)

This works exactly the same as from_bytes

add_operation

        sig: add_operation($self, $opcode)

Adds a new opcode at the end of a script. Returns $self for chaining. Throws an exception for unknown opcodes.

add_raw

        sig: add_raw($self, $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 $self for chaining.

push_bytes

        sig: push_bytes($self, $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 $self for chaining.

get_script

        sig: get_script($self)

Returns a serialized script as byte string.

get_script_hash

        sig: get_script_hash($self)

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

set_network

        sig: set_network($self, $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 key instance.

get_legacy_address

        sig: get_legacy_address($self)

Returns string containing Base58Check encoded script hash (p2sh address)

get_compat_address

        sig: get_compat_address($self)

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

get_segwit_address

        sig: get_segwit_address($self)

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