Bitcoin::Crypto::Key::Public - Bitcoin public keys
use Bitcoin::Crypto::Key::Public; $pub = Bitcoin::Crypto::Key::Public->from_hex($asn_hex); # verify signature (it has to be byte string, see perlpacktut) $pub->verify_message(pack("a*", "Hello world"), $sig); # getting address from public key (p2wpkh) my $address = $pub->get_segwit_address();
This class allows you to create a public key instance.
You can use a public key to:
verify messages
create addresses: legacy (p2pkh), compatibility (p2sh(p2wpkh)) and segwit (p2wpkh).
sig: from_bytes($class, $data)
Use this method to create a PublicKey instance from a byte string. Data $data must represent a public key in ASN X9.62 format.
Returns class instance.
sig: new($class, $data)
This works exactly the same as from_bytes
sig: to_bytes($self)
Does the opposite of from_bytes on a target object
sig: from_hex($class, $hex)
Use this method to create a public key instance from a hexadecimal number. Packs the number and runs it through from_bytes.
sig: to_hex($self)
Does the opposite of from_hex on a target object
sig: set_compressed($self, $val)
Change key's compression state to $val (1/0). This will change the address. If $val is omitted it is set to 1.
Returns current key instance.
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.
sig: verify_message($self, $message, $signature, $algo = "sha256")
Verifies $signature against digest of $message (with $algo digest algorithm) using public key. $algo must be available in Digest package.
Returns boolean.
sig: get_legacy_address($self)
Returns string containing Base58Check encoded public key hash (p2pkh address)
sig: get_compat_address($self)
Returns string containing Base58Check encoded script hash containing a witness program for compatibility purposes (p2sh(p2wpkh) address)
sig: get_segwit_address($self)
Returns string containing Bech32 encoded witness program (p2wpkh 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:
KeyCreate - key couldn't be created correctly
Verify - couldn't verify the message correctly
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.