Bitcoin-Crypto

 view release on metacpan or  search on metacpan

lib/Bitcoin/Crypto/BIP44.pm  view on Meta::CPAN

	isa => Bool,
	default => !!0,
);

has param 'get_from_account' => (
	isa => Bool,
	default => !!0,
);

has param 'public' => (
	isa => Bool,
	default => !!0,
);

use overload
	q{""} => 'as_string',
	fallback => 1;

sub as_string
{
	my ($self) = @_;

	# https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
	# m / purpose' / coin_type' / account' / change / address_index

	my $path = $self->public ? 'M' : 'm';

	$path = sprintf "%s/%u'/%u'/%u'",
		$path, $self->purpose, $self->coin_type, $self->account
		if !$self->public && !$self->get_from_account;

	return $path
		if $self->get_account;

	return sprintf "%s/%u/%u",
		$path, $self->change, $self->index;
}

1;

__END__
=head1 NAME

Bitcoin::Crypto::BIP44 - BIP44 implementation

=head1 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);

=head1 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 C<coin_type> constant, a list of which is maintained
here: L<https://github.com/satoshilabs/slips/blob/master/slip-0044.md>.
L<Bitcoin::Crypto::Network> instances hold these constants under the
C<bip44_coin> property.

BIP44 objects stringify automatically and can be directly used in
L<Bitcoin::Crypto::Key::ExtPrivate/derive_key> method. In return, any key
object can be used as C<coin_type> in L</new>, which will automatically fetch
coin_type number from the key's current network.

=head1 INTERFACE

=head2 Attributes

Refer to L<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.

=head3 purpose

Purpose contains the BIP document number that you wish to use. Can be either
C<44>, C<49> or C<84>.

By default, number C<44> will be used.

=head3 coin_type

Needs to be a non-negative integer number. It should be less than C<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 C<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
L<Bitcoin::Crypto::Network>.

=head3 account

Needs to be a non-negative integer number. It should be less than C<2^31> (but
will not check for that).

By default, the value C<0> is used.

=head3 change

Needs to be a number C<1> (for addresses to be used as change outputs) or C<0>
(for addresses that are to be used only internally).

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.801 second using v1.00-cache-2.02-grep-82fe00e-cpan-9e6bc14194b )