Bitcoin::Crypto::Script - Bitcoin script representations
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();
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
$script_object = $class->new($data)
A constructor. Returns new script instance
$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.
$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.
$bytes
PUSHDATA4
Returns the object instance for chaining.
$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")>.
$script-
Throws an exception for data exceeding a 4 byte number in length.
$bytestring = $object->get_script()
Returns a serialized script as byte string.
$bytestring = $object->get_script_hash()
Returns a serialized script parsed with HASH160 (ripemd160 of sha256).
HASH160
$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.
$val
Returns current object instance.
$address = $object->get_legacy_address()
Returns string containing Base58Check encoded script hash (p2sh address)
$address = $object->get_compat_address()
Returns string containing Base58Check encoded script hash containing a witness program for compatibility purposes (p2sh(p2wsh) address)
$address = $object->get_segwit_address()
Returns string containing Bech32 encoded witness program (p2wsh address)
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
To install Bitcoin::Crypto, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Bitcoin::Crypto
CPAN shell
perl -MCPAN -e shell install Bitcoin::Crypto
For more information on module installation, please visit the detailed CPAN module installation guide.