NAME
Bitcoin::Crypto::Key::Public - Bitcoin public keys
SYNOPSIS
use Bitcoin::Crypto::Key::Public;
$pub = Bitcoin::Crypto::Key::Public->from_serialized([hex => $asn_hex]);
# verify signature of custom message
# (it has to be byte string, see perlpacktut)
$pub->verify_message('Hello world', $sig);
# getting address from public key (p2wpkh)
my $address = $pub->get_segwit_address();
DESCRIPTION
This class allows you to create a public key instance.
You can use a public key to verify messages and get addresses.
INTERFACE
Attributes
compressed
Boolean value indicating if this ECC key should be compressed. Default: true.
writer: set_compressed
network
Instance of Bitcoin::Crypto::Network - current network for this key. Can be coerced from network id. Default: current default network.
writer: set_network
purpose
BIP44 purpose which was used to obtain this key. Filled automatically when deriving an extended key. If the key was not obtained through BIP44 derivation, this attribute is undef.
writer: set_purpose
clearer: clear_purpose
taproot_output
Boolean value indicating if this key was obtained through taproot tweaking. Taproot output keys are used to sign and verify schnorr signatures in P2TR outputs. Default: false
writer: set_taproot_output
Methods
new
Constructor is reserved for internal and advanced use only. Use "from_serialized" instead.
from_serialized
$key_object = $class->from_serialized($serialized)
This creates a new key from string data. Argument $serialized is a formatable bytestring which must represent a public key in ASN X9.62 format.
Returns a new key object instance.
to_serialized
$serialized = $key_object->to_serialized()
This returns a public key in ASN X9.62 format. The result is a bytestring which can be further formated with to_format utility.
The result will vary depending on compression state: see "compressed"
get_hash
$bytestr = $object->get_hash()
Returns hash160 of the serialized public key.
witness_program
$script = $object->witness_program($version, \%args = {})
Builds a witness program for given witness $version as Bitcoin::Crypto::Script instance. %args depends on witness version:
For witness version 0 (segwit), no arguments are used.
For witness version 1 (taproot),
tweak_suffixoptional bytestring argument can be passed.
get_taproot_output_key
$pub = $object->get_taproot_output_key($tweak_suffix = undef)
Returns a new public key instance that represents an output taproot key. Optional $tweak_suffix can be passed as bytestring.
get_xonly_key
$bytestring = $object->get_xonly_key()
Returns a 32-byte bytestring containing the xonly key for this public key.
verify_message
$signature_valid = $object->verify_message($message, $signature, %params)
Verifies $signature against digest of $message (digesting it with double sha256) using public key.
%params can be any of:
flagsAn instance of Bitcoin::Crypto::Transaction::Flags. If not passed, full set of consensus flags will be assumed (same as calling "new" in Bitcoin::Crypto::Transaction::Flags with no arguments).
Returns boolean.
Character encoding note: $message should be encoded in the proper encoding before passing it to this method. Passing Unicode string will cause the function to fail. You can encode like this (for UTF-8):
use Encode qw(encode);
$message = encode('UTF-8', $message);
get_legacy_address
$address_string = $object->get_legacy_address()
Returns string containing Base58Check encoded public key hash (p2pkh address).
If the public key was obtained through BIP44 derivation scheme, this method will check whether the purpose was 44 and raise an exception otherwise. If you wish to generate this address anyway, call clear_purpose.
get_compat_address
$address_string = $object->get_compat_address()
Returns string containing Base58Check encoded script hash containing a witness program for compatibility purposes (p2sh(p2wpkh) address)
If the public key was obtained through BIP44 derivation scheme, this method will check whether the purpose was 49 and raise an exception otherwise. If you wish to generate this address anyway, call clear_purpose.
get_segwit_address
$address_string = $object->get_segwit_address()
Returns a string containing Bech32 encoded witness version 0 program (p2wpkh address)
If the public key was obtained through BIP44 derivation scheme, this method will check whether the purpose was 84 and raise an exception otherwise. If you wish to generate this address anyway, call clear_purpose.
get_taproot_address
$address_string = $object->get_taproot_address($script_tree = undef)
Returns a string containing Bech32m encoded witness version 1 program (p2tr address)
Optional $script_tree can be passed as Bitcoin::Crypto::Script::Tree object. Passing this argument will generate an address that can be spent using script path spend as well as key path spend. If this argument is not passed, an unspendable script path will be used according to BIP341.
If the public key was obtained through BIP44 derivation scheme, this method will check whether the purpose was 86 and raise an exception otherwise. If you wish to generate this address anyway, call clear_purpose.
get_address
$address_string = $object->get_address()
Returns a string containing the address. Tries to guess which address type is most fitting:
If the key has a BIP44 purpose set, generates type of address which matches the purpose
If the key doesn't have a purpose but the network supports segwit, returns a taproot address (same as
get_taproot_address, but does not accept a script tree)If the network doesn't support segwit, returns legacy address
NOTE: The rules this function uses to choose the address type will change when more up-to-date address types are implemented. Use other address functions if this is not what you want.
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:
KeyCreate - key couldn't be created correctly
Verify - couldn't verify the message correctly
NetworkConfig - incomplete or corrupted network configuration
AddressGenerate - address could not be generated (see BIP44 constraint notes)