NAME
Bitcoin::Crypto::BIP44 - BIP44 (multi-account hierarchy) implementation
SYNOPSIS
use Bitcoin::Crypto::BIP44;
my $path = Bitcoin::Crypto::BIP44->new(
coin_type => Bitcoin::Crypto::Network->get('bitcoin_testnet'), # can also be a number or a key instance
index => 43,
# account => 0,
# change => 0,
);
# stringifies automatically
say "$path";
# can be used in key derivation
$ext_private_key->derive_key($path);
DESCRIPTION
This class is a helper for constructing BIP44-compilant key derivation paths. BIP44 describes the mechanism the HD (Hierarchical Deterministic) wallets use to decide derivation paths for coins. BIP49 and BIP84 are constructed the same way, but used for compat and segwit addresses respectively.
Each coin has its own coin_type
constant, a list of which is maintained here: https://github.com/satoshilabs/slips/blob/master/slip-0044.md. Bitcoin::Crypto::Network instances hold these constants under the bip44_coin
property.
BIP44 objects stringify automatically and can be directly used in "derive_key" in Bitcoin::Crypto::Key::ExtPrivate method. In return, any key object can be used as coin_type
in "new", which will automatically fetch coin_type number from the key's current network.
INTERFACE
Attributes
Refer to https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki for details of those attributes.
All of these attributes can be fetched using a method with the same name.
purpose
Purpose contains the BIP document number that you wish to use. Can be either 44
, 49
or 84
.
By default, number 44
will be used.
coin_type
Needs to be a non-negative integer number. It should be less than 2^31
(but will not check for that).
Will also accept key objects and network objects, as it is possible to fetch the constant for them. In this case, it might raise an exception if the network does not contain the bip44_coin
constant.
This value should be in line with the table of BIP44 constants mentioned above.
By default, the value defined in the current default network will be used: see Bitcoin::Crypto::Network.
account
Needs to be a non-negative integer number. It should be less than 2^31
(but will not check for that).
By default, the value 0
is used.
change
Needs to be a number 1
(for addresses to be used as change outputs) or 0
(for addresses that are to be used only internally).
By default, the value 0
is used.
index
Needs to be a non-negative integer number. It should be less than 2^31
(but will not check for that).
By default, the value 0
is used.
public
Use public
if you want to derive a public extended key. This is a must when deriving BIP44 from extended public keys.
Since it isn't possible to derive full BIP44 path with public derivation scheme, this will assume "get_from_account".
get_account
If passed 1
, the resulting derivation path will only go as far as to the account part. "index" and "change" values will be ignored. Use this to get extended key for the account.
By default, you will get the full derivation path.
get_from_account
If passed 1
, the resulting derivation path will start after the account part. "purpose", "coin_type" and "account" values will be ignored. Use this to further derive key that was only derived up to the account part.
By default, you will get the full derivation path.
Methods
new
$bip_object = $class->new(%data)
This is a standard Moo constructor, which can be used to create the object. It takes arguments specified in "Attributes".
Returns class instance.
as_string
$path = $object->as_string()
Stringifies the object as BIP44-compilant key derivation path. Can be used indirectly by just stringifying the object.
get_derivation_path
my $path_obj = $object->get_derivation_path()
Returns an object of class Bitcoin::Crypto::DerivationPath.
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:
NetworkConfig - incomplete or corrupted network configuration
SEE ALSO
Bitcoin::Crypto::Key::ExtPrivate
https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki