NAME

Bitcoin::Crypto::Key::ExtPublic - Bitcoin extended public keys

SYNOPSIS

use Bitcoin::Crypto qw(btc_extprv);
use Bitcoin::Crypto::Util qw(generate_mnemonic to_format)

my $mnemonic = generate_mnemonic;
my $key = btc_extprv->from_mnemonic($mnemonic)->get_public_key;

# derive child public key
my $path = "M/0";
my $child_key = $key->derive_key($path);
my $ser_child_key = to_format [base58 => $child_key->to_serialized];
print "Your exported $path child key is: $ser_child_key";

# create basic public key
my $basic_public = $child_key->get_basic_key;

DESCRIPTION

This class allows you to create an extended public key instance. They are public counterparts to extended keys.

You can use an extended public key to:

  • derive extended keys using a path (only public keys, no hardened paths)

  • export and restore keys from the serialized format

Extended public keys pose a security risk: if the attacker obtains an extended public key and a single private key derived from the extended private key associated with the public key, they can obtain every private key on the same derivation path. For this reason, it is not recommended to share extended public keys.

INTERFACE

Attributes

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

depth

Integer - depth of derivation. Default: 0 (master key)

parent_fingerprint

Bytestring of length 4 - fingerprint of the parent key. Default: four zero bytes

child_number

Integer - sequence number of the key on the current "depth". Default: 0

chain_code

Bytestring of length 32 - chain code of the extended key.

Methods

new

Constructor is reserved for internal and advanced use only. Use "from_serialized" instead.

to_serialized

$serialized_key = $object->to_serialized()

Returns the key serialized in format specified in BIP32 as byte string.

from_serialized

$key_object = $class->from_serialized($serialized, $network = undef)

Tries to unserialize byte string $serialized with format specified in BIP32.

Dies on errors. If multiple networks match serialized data specify $network manually (id of the network) to avoid exception.

get_basic_key

$basic_key_object = $object->get_basic_key()

Returns the key in basic format: Bitcoin::Crypto::Key::Public

derive_key

$derived_key_object = $object->derive_key($path)

Performs extended key derivation as specified in BIP32 on the current key with $path. Dies on error.

See BIP32 document for details on derivation paths and methods.

Note that public keys cannot derive private keys and your derivation path must start with M (capital m).

Returns a new extended key instance - result of a derivation.

derive_key_bip44

$derived_key_object = $object->derive_key_bip44(%data)

A helper that constructs a Bitcoin::Crypto::BIP44 path from %data and calls "derive_key" with it. In extended public keys, bip44 is always constructed with public setting - it will always derive starting from account, effectively only using change and index attributes.

get_fingerprint

$fingerprint = $object->get_fingerprint($len = 4)

Returns a fingerprint of the extended key of $len length (byte string)

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:

  • KeyDerive - key couldn't be derived correctly

  • KeyCreate - key couldn't be created correctly

  • NetworkConfig - incomplete or corrupted network configuration

SEE ALSO

Bitcoin::Crypto::Key::ExtPrivate
Bitcoin::Crypto::Network