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
sig: new($class, $data)
This works exactly the same as from_bytes
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.
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.
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.
$script-
sig: get_script($self)
Returns a serialized script as byte string.
sig: get_script_hash($self)
Returns a serialized script parsed with HASH160 (ripemd160 of sha256).
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.
sig: get_legacy_address($self)
Returns string containing Base58Check encoded script hash (p2sh address)
sig: get_compat_address($self)
Returns string containing Base58Check encoded script hash containing a witness program for compatibility purposes (p2sh(p2wsh) address)
sig: get_segwit_address($self)
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.