The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Bitcoin::Crypto::Key::Public - Bitcoin public keys

SYNOPSIS

        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();

DESCRIPTION

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).

METHODS

from_bytes

        $key_object = $class->from_bytes($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.

new

        $key_object = $class->new($data)

This works exactly the same as from_bytes

to_bytes

        $bytestring = $object->to_bytes()

Does the opposite of from_bytes on a target object

from_hex

        $key_object = $class->from_hex($hex)

Use this method to create a public key instance from a hexadecimal number. Packs the number and runs it through from_bytes.

Returns class instance.

to_hex

        $hex_string = $object->to_hex()

Does the opposite of from_hex on a target object

set_compressed

        $key_object = $object->set_compressed($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.

set_network

        $key_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.

Returns current key instance.

verify_message

        $signature_valid = $object->verify_message($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.

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 string containing Bech32 encoded witness 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".

clear_purpose

        $object->clear_purpose;

Clears the purpose of this key instance, removing safety checks on address generation.

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)

SEE ALSO

Bitcoin::Crypto::Key::Private
Bitcoin::Crypto::Network
Bitcoin::Crypto::Base58
Bitcoin::Crypto::Bech32