| 1 | package Bitcoin::Crypto::Key::ExtPrivate; | ||||||||||
| 2 | 11 | $Bitcoin::Crypto::Key::ExtPrivate::VERSION = '4.005'; | |||||||||
| 3 | 11 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 11 | ▶ | use Mooish::Base -standard; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 11 | ▶ | use Crypt::Mac::HMAC qw(hmac); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 11 | ▶ | use Bitcoin::BIP39 qw(bip39_mnemonic_to_entropy); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 11 | ▶ | use Types::Common -sigs; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 11 | ▶ | use Carp qw(carp); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 11 | ▶ | use Bitcoin::Crypto::BIP44; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 11 | ▶ | use Bitcoin::Crypto::Key::ExtPublic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 11 | ▶ | use Bitcoin::Crypto::Constants; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 11 | ▶ | use Bitcoin::Crypto::Helpers qw(ensure_length ecc die_no_trace); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 11 | ▶ | use Bitcoin::Crypto::Util::Internal qw(mnemonic_to_seed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 11 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 11 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 11 | extends qw(Bitcoin::Crypto::Key::ExtBase); | |||||||||
| 21 | |||||||||||
| 22 | 3319 | ▶ | sub _is_private { 1 } | ||||||||
|
Subroutine
_is_private: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 11 | signature_for from_serialized => ( | |||||||||
| 25 | method => !!1, | ||||||||||
| 26 | positional => [BitcoinSecret, Maybe [Str], {default => undef}], | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | sub from_serialized | ||||||||||
| 30 | 30 | ▶ | { | ||||||||
|
Subroutine
from_serialized: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 30 | my ($class, $secret, $network) = @_; | |||||||||
| 32 | |||||||||||
| 33 | return $secret->unmask_to( | ||||||||||
| 34 | 30 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 30 | return $class->SUPER::from_serialized(shift, $network); | |||||||||
| 36 | } | ||||||||||
| 37 | 30 | ); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 11 | signature_for from_mnemonic => ( | |||||||||
| 41 | method => !!1, | ||||||||||
| 42 | positional => [BitcoinSecret, Maybe [BitcoinSecret], {default => ''}, Maybe [Str], {default => undef}], | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | sub from_mnemonic | ||||||||||
| 46 | 35 | ▶ | { | ||||||||
|
Subroutine
from_mnemonic: called CC=3 SCAR=11.0
| |||||||||||
| 47 | 35 | my ($class, $secret_mnemonic, $secret_password, $lang) = @_; | |||||||||
| 48 | |||||||||||
| 49 | 35 | ▶ | my $mnemonic = $secret_mnemonic->unmask_to(sub { shift }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | my $password = defined $secret_password | ||||||||||
| 51 | 31 | ▶ | ? $secret_password->unmask_to(sub { shift }) | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 35 | ▶ | : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 35 | ▶ | if (defined $lang) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | # make sure no whitespace gets in the way (only when $lang is specified | ||||||||||
| 57 | # to make it possible to recover key imported with such error) | ||||||||||
| 58 | 17 | $mnemonic = join ' ', grep { length $_ } split /\s+/, $mnemonic; | |||||||||
| 59 | |||||||||||
| 60 | # checks validity of seed in given language | ||||||||||
| 61 | # requires Wordlist::LANG::BIP39 module for given LANG | ||||||||||
| 62 | Bitcoin::Crypto::Exception::MnemonicCheck->trap_into( | ||||||||||
| 63 | 17 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 17 | bip39_mnemonic_to_entropy( | |||||||||
| 65 | mnemonic => $mnemonic, | ||||||||||
| 66 | language => $lang | ||||||||||
| 67 | ); | ||||||||||
| 68 | } | ||||||||||
| 69 | 17 | ); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 35 | return $class->from_seed(mnemonic_to_seed($mnemonic, $password)); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 11 | signature_for from_seed => ( | |||||||||
| 76 | method => !!1, | ||||||||||
| 77 | positional => [BitcoinSecret], | ||||||||||
| 78 | ); | ||||||||||
| 79 | |||||||||||
| 80 | sub from_seed | ||||||||||
| 81 | 50 | ▶ | { | ||||||||
|
Subroutine
from_seed: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 50 | my ($class, $secret) = @_; | |||||||||
| 83 | 50 | state $sig = signature(positional => [ByteStr]); | |||||||||
| 84 | |||||||||||
| 85 | return $secret->unmask_to( | ||||||||||
| 86 | 50 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 87 | 50 | my ($seed) = $sig->(@_); | |||||||||
| 88 | |||||||||||
| 89 | 50 | my $bytes = hmac('SHA512', 'Bitcoin seed', $seed); | |||||||||
| 90 | 50 | my $key = substr $bytes, 0, 32; | |||||||||
| 91 | 50 | my $cc = substr $bytes, 32, 32; | |||||||||
| 92 | |||||||||||
| 93 | 50 | return $class->new( | |||||||||
| 94 | _key_instance => $key, | ||||||||||
| 95 | chain_code => $cc, | ||||||||||
| 96 | ); | ||||||||||
| 97 | } | ||||||||||
| 98 | 50 | ); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | sub get_public_key | ||||||||||
| 102 | 61 | ▶ | { | ||||||||
|
Subroutine
get_public_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 103 | 61 | my ($self) = @_; | |||||||||
| 104 | |||||||||||
| 105 | 61 | my $public = Bitcoin::Crypto::Key::ExtPublic->new( | |||||||||
| 106 | _key_instance => $self->raw_key('public'), | ||||||||||
| 107 | chain_code => $self->chain_code, | ||||||||||
| 108 | child_number => $self->child_number, | ||||||||||
| 109 | parent_fingerprint => $self->parent_fingerprint, | ||||||||||
| 110 | depth => $self->depth, | ||||||||||
| 111 | network => $self->network, | ||||||||||
| 112 | purpose => $self->purpose, | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | 61 | return $public; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | sub derive_key_bip44 | ||||||||||
| 119 | 41 | ▶ | { | ||||||||
|
Subroutine
derive_key_bip44: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 120 | 41 | my ($self, %data) = @_; | |||||||||
| 121 | 41 | my $path = Bitcoin::Crypto::BIP44->new( | |||||||||
| 122 | %data, | ||||||||||
| 123 | coin_type => $self, | ||||||||||
| 124 | ); | ||||||||||
| 125 | |||||||||||
| 126 | 41 | return $self->derive_key($path); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | sub _derive_key_partial | ||||||||||
| 130 | 295 | ▶ | { | ||||||||
|
Subroutine
_derive_key_partial: called CC=2 SCAR=6.9
| |||||||||||
| 131 | 295 | my ($self, $child_num, $hardened) = @_; | |||||||||
| 132 | 295 | my $key = $self->raw_key; | |||||||||
| 133 | |||||||||||
| 134 | 295 | my $hmac_data; | |||||||||
| 135 | 295 | ▶ | if ($hardened) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | |||||||||||
| 137 | # zero byte + key data - 32 bytes | ||||||||||
| 138 | 169 | $hmac_data = "\x00" . $key; | |||||||||
| 139 | } | ||||||||||
| 140 | else { | ||||||||||
| 141 | |||||||||||
| 142 | # public key data - SEC compressed form | ||||||||||
| 143 | 126 | $hmac_data = $self->raw_key('public_compressed'); | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | # child number - 4 bytes | ||||||||||
| 147 | 295 | $hmac_data .= ensure_length pack('N', $child_num), 4; | |||||||||
| 148 | |||||||||||
| 149 | 295 | my $data = hmac('SHA512', $self->chain_code, $hmac_data); | |||||||||
| 150 | 295 | my $tweak = substr $data, 0, 32; | |||||||||
| 151 | 295 | my $chain_code = substr $data, 32, 32; | |||||||||
| 152 | |||||||||||
| 153 | Bitcoin::Crypto::Exception::KeyDerive->trap_into( | ||||||||||
| 154 | 295 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||
| 155 | 295 | $key = ecc->add_private_key($key, $tweak); | |||||||||
| 156 | 295 | ▶ | die_no_trace 'verification failed' unless ecc->verify_private_key($key); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | }, | ||||||||||
| 158 | 295 | "key $child_num in sequence was found invalid" | |||||||||
| 159 | ); | ||||||||||
| 160 | |||||||||||
| 161 | 295 | return $self->new( | |||||||||
| 162 | _key_instance => $key, | ||||||||||
| 163 | chain_code => $chain_code, | ||||||||||
| 164 | child_number => $child_num, | ||||||||||
| 165 | parent_fingerprint => $self->get_fingerprint, | ||||||||||
| 166 | depth => $self->depth + 1, | ||||||||||
| 167 | ); | ||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 11 | 1; | |||||||||
| 171 | |||||||||||
| 172 | __END__ | ||||||||||