Crypto/Key/ExtPrivate.pm

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