Crypto/Key/ExtBase.pm

filter: Statementstmt 97.6% 123 / 126 Branchbran 93.7% 45 / 48 Conditioncond 92.5% 25 / 27 MC/DCmcdc 88.8% 16 / 18 Subroutinesub 92.0% 23 / 25 Podpod 0.0% 0 / 2 totaltotal 94.3% 232 / 246 CC 36 SCAR 21.2
CC
36
Cov
96%
from_serialized
19.1
_get_network_extkey_version
8.0
_get_purpose_from_BIP44
5.0
CRAP 8.3
« Crypto/Script/Recognition.pm ↑ Summary Crypto/Role/PSBT/Finalizer.pm »
1 package Bitcoin::Crypto::Key::ExtBase;
2 10 $Bitcoin::Crypto::Key::ExtBase::VERSION = '4.006';
3 13 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Bitcoin::Crypto::Key::Private;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Bitcoin::Crypto::Key::Public;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Bitcoin::Crypto::Constants qw(:bip44 :key);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Bitcoin::Crypto::Util::Internal qw(hash160 to_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Bitcoin::Crypto::Helpers qw(ensure_length);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Bitcoin::Crypto::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 has param 'depth' => (
21 isa => IntMaxBits [8],
22 default => 0
23 );
24  
25 10 has param 'parent_fingerprint' => (
26 coerce => ByteStrLen [4],
27 default => (pack 'x4'),
28 );
29  
30 10 has param 'child_number' => (
31 isa => IntMaxBits [32],
32 default => 0
33 );
34  
35 10 has param 'chain_code' => (
36 coerce => ByteStrLen [32],
37 );
38  
39 10 with qw(Bitcoin::Crypto::Role::Key);
40  
41 sub _is_private
42 0 ▶{
Subroutine _is_private: not called CC=1 SCAR=6.9
43 0 die __PACKAGE__ . '::_is_private is unimplemented';
44 }
45  
46 sub _derive_key_partial
47 0 ▶{
Subroutine _derive_key_partial: not called CC=1 SCAR=6.9
48 0 die __PACKAGE__ . '::_derive_key_partial is unimplemented';
49 }
50  
51 sub _get_network_extkey_version
52 682 ▶{
Subroutine _get_network_extkey_version: called CC=8 SCAR=20.8
53 682 my ($self, $network, $purpose) = @_;
54 682 ▶ $network = $self->network if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 127 555 682
55 682 ▶ $purpose = $self->purpose if @_ < 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 127 555 682
56  
57 682 my $name = 'ext';
58 682 ▶ $name .= $self->_is_private ? 'prv' : 'pub';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_private ? : 326 356 682
59 682 ▶ $name .= '_compat' if $purpose && $purpose eq BIP44_COMPAT_PURPOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $purpose and $purpose eq 49 90 592 682
Condition100%3/3
$purpose and $purpose eq 49
!ll&&!rl&&r
49210090
MC/DC100%2/2
$purpose and $purpose eq 49
$purpose $purpose eq 49
Truth table100%3/3
A and B
A= $purposeB= $purpose eq 49
ABresult
0X0
100
111
60 682 ▶ $name .= '_segwit' if $purpose && $purpose eq BIP44_SEGWIT_PURPOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $purpose and $purpose eq 84 67 615 682
Condition100%3/3
$purpose and $purpose eq 84
!ll&&!rl&&r
49212367
MC/DC100%2/2
$purpose and $purpose eq 84
$purpose $purpose eq 84
Truth table100%3/3
A and B
A= $purposeB= $purpose eq 84
ABresult
0X0
100
111
61 682 $name .= '_version';
62  
63 682 return $network->$name;
64 }
65  
66 sub to_serialized
67 127 ▶{
Subroutine to_serialized: called CC=2 SCAR=6.9
Pod undocumented
68 127 my ($self) = @_;
69  
70 127 my $version = $self->_get_network_extkey_version;
71  
72 # network field is not required, lazy check for completeness
73 127 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 127 127
74 'no extended key version found in network configuration'
75 ) unless defined $version;
76  
77 # version number (4B)
78 127 my $serialized = pack('N', $version);
79  
80 # depth (1B)
81 127 $serialized .= pack('C', $self->depth);
82  
83 # parent's fingerprint (4B) - ensured
84 127 $serialized .= $self->parent_fingerprint;
85  
86 # child number (4B)
87 127 $serialized .= pack('N', $self->child_number);
88  
89 # chain code (32B)
90 127 $serialized .= $self->chain_code;
91  
92 # key entropy (1 + 32B)
93 127 $serialized .= ensure_length $self->raw_key, KEY_MAX_LENGTH + 1;
94  
95 127 return $serialized;
96 }
97  
98 10 signature_for from_serialized => (
99 method => !!1,
100 positional => [ByteStr, Maybe [Str], {default => undef}],
101 );
102  
103 sub from_serialized
104 82 ▶{
Subroutine from_serialized: called CC=19 SCAR=29.5
105 82 my ($class, $serialized, $network) = @_;
106  
107 # expected length is 78
108 82 ▶ if (defined $serialized && length $serialized == 78) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $serialized and length $serialized == 78) { } 82 0 82
Condition33%1/3
defined $serialized and length $serialized == 78
!ll&&!rl&&r
0082
MC/DC0%0/2
defined $serialized and length $serialized == 78
defined $serialized length $serialized == 78
Truth table33%1/3
A and B
A= defined $serializedB= length $serialized == 78
ABresult
0X0
100
111
109 82 my ($version, $depth, $fingerprint, $number, $chain_code, $data) =
110 unpack 'a4Ca4Na32a33', $serialized;
111  
112 82 my $is_private = pack('x') eq substr $data, 0, 1;
113  
114 82 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 2 branches
BranchTrueFalseTotal
if $is_private != $class->_is_private 6 76 82
$is_private ? : 6 0 6
115 'invalid class used, key is ' . ($is_private ? 'private' : 'public')
116 ) if $is_private != $class->_is_private;
117  
118 76 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 and $fingerprint ne "\0\0\0\0" 1 75 76
Condition100%3/3
$depth == 0 and $fingerprint ne "\0\0\0\0"
!ll&&!rl&&r
45301
MC/DC100%2/2
$depth == 0 and $fingerprint ne "\0\0\0\0"
$depth == 0 $fingerprint ne "\0\0\0\0"
Truth table100%3/3
A and B
A= $depth == 0B= $fingerprint ne "\0\0\0\0"
ABresult
0X0
100
111
119 'parent fingerprint encountered for key of depth 0'
120 ) if $depth == 0 && $fingerprint ne pack 'x4';
121  
122 75 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 and $number != 0 1 74 75
Condition100%3/3
$depth == 0 and $number != 0
!ll&&!rl&&r
45291
MC/DC100%2/2
$depth == 0 and $number != 0
$depth == 0 $number != 0
Truth table100%3/3
A and B
A= $depth == 0B= $number != 0
ABresult
0X0
100
111
123 'child number encountered for key of depth 0'
124 ) if $depth == 0 && $number != 0;
125  
126 74 ▶ $data = substr $data, 1, KEY_MAX_LENGTH
Branch: 1 branch
BranchTrueFalseTotal
if $is_private 32 42 74
127 if $is_private;
128  
129 74 $version = unpack 'N', $version;
130  
131 74 my $purpose;
132 my @found_networks;
133  
134 74 for my $check_purpose (
135 undef,
136 BIP44_COMPAT_PURPOSE,
137 BIP44_SEGWIT_PURPOSE
138 )
139 {
140 @found_networks = Bitcoin::Crypto::Network->find(
141 555 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
142 555 my ($inst) = @_;
143 555 my $this_version = $class->_get_network_extkey_version($inst, $check_purpose);
144 555 ▶ return $this_version && $this_version eq $version;
Condition100%3/3
$this_version && $this_version eq $version
!ll&&!rl&&r
9638376
MC/DC100%2/2
$this_version && $this_version eq $version
$this_version $this_version eq $version
Truth table100%3/3
A && B
A= $this_versionB= $this_version eq $version
ABresult
0X0
100
111
145 }
146 98 );
147  
148 98 ▶ @found_networks = grep { $_ eq $network } @found_networks
Branch: 1 branch
BranchTrueFalseTotal
if defined $network 5 93 98
149 if defined $network;
150  
151 98 ▶ if (@found_networks > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_networks > 0) 68 30 98
152 68 $purpose = $check_purpose;
153 68 last;
154 }
155 }
156  
157 74 ▶ if (@found_networks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_networks > 1) 5 69 74
158 5 my $default_network = Bitcoin::Crypto::Network->get->id;
159  
160 Bitcoin::Crypto::Exception::KeyCreate->raise(
161 'found multiple networks possible for given serialized key: ' . join ', ', @found_networks
162 5 ▶ ) if none { $_ eq $default_network } @found_networks;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $default_network; } , @found_networks) 1 4 5
163  
164 4 @found_networks = ($default_network);
165 }
166  
167 Bitcoin::Crypto::Exception::KeyCreate->raise(
168 73 ▶ "network name $network cannot be used for given serialized key"
Branch: 1 branch
BranchTrueFalseTotal
if @found_networks == 0 and defined $network 1 72 73
Condition100%3/3
@found_networks == 0 and defined $network
!ll&&!rl&&r
6751
MC/DC100%2/2
@found_networks == 0 and defined $network
@found_networks == 0 defined $network
Truth table100%3/3
A and B
A= @found_networks == 0B= defined $network
ABresult
0X0
100
111
169 ) if @found_networks == 0 && defined $network;
170  
171 72 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
if @found_networks == 0 5 67 72
172 "couldn't find network for serialized key version $version"
173 ) if @found_networks == 0;
174  
175 67 ▶ my $key = $class->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $purpose ? : 8 59 67
176 _key_instance => $data,
177 chain_code => $chain_code,
178 child_number => $number,
179 parent_fingerprint => $fingerprint,
180 depth => $depth,
181 network => $found_networks[0],
182 (defined $purpose ? (purpose => $purpose) : ()),
183 );
184  
185 63 return $key;
186 }
187 else {
188 0 Bitcoin::Crypto::Exception::KeyCreate->raise(
189 'input data does not look like a valid serialized extended key'
190 );
191 }
192 }
193  
194 sub get_basic_key
195 76 ▶{
Subroutine get_basic_key: called CC=2 SCAR=6.9
Pod undocumented
196 76 my ($self) = @_;
197 76 ▶ my $base_class = 'Bitcoin::Crypto::Key::' . ($self->_is_private ? 'Private' : 'Public');
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_private ? : 48 28 76
198 76 my $basic_key = $base_class->new(
199 _key_instance => $self->_key_instance,
200 network => $self->network,
201 purpose => $self->purpose,
202 );
203  
204 76 return $basic_key;
205 }
206  
207 10 signature_for get_fingerprint => (
208 method => !!1,
209 positional => [PositiveInt, {default => 4}],
210 );
211  
212 sub get_fingerprint
213 323 ▶{
Subroutine get_fingerprint: called CC=1 SCAR=0.0
214 323 my ($self, $len) = @_;
215  
216 323 my $pubkey = $self->raw_key('public_compressed');
217 323 my $identifier = hash160($pubkey);
218 323 return substr $identifier, 0, $len;
219 }
220  
221 sub _get_purpose_from_BIP44
222 102 ▶{
Subroutine _get_purpose_from_BIP44: called CC=5 SCAR=16.1
223 102 my ($self, $path) = @_;
224  
225 # NOTE: only handles BIP44 correctly when it is constructed with Bitcoin::Crypto::BIP44
226 # NOTE: when deriving new keys, we do not care about previous state:
227 # - if BIP44 is further derived, it is not BIP44 anymore
228 # - if BIP44 is derived as a new BIP44, the old one is like the new master key
229 # because of that, set purpose to undef if path is not BIP44
230  
231 return undef
232 102 ▶ unless blessed $path && $path->isa('Bitcoin::Crypto::BIP44');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($path) and $path->isa("Bitcoin::Crypto::BIP44") 55 47 102
Condition100%3/3
builtin::blessed($path) and $path->isa("Bitcoin::Crypto::BIP44")
!ll&&!rl&&r
401547
MC/DC100%2/2
builtin::blessed($path) and $path->isa("Bitcoin::Crypto::BIP44")
builtin::blessed($path) $path->isa("Bitcoin::Crypto::BIP44")
Truth table100%3/3
A and B
A= builtin::blessed($path)B= $path->isa("Bitcoin::Crypto::BIP44")
ABresult
0X0
100
111
233  
234 47 ▶ return $self->purpose
Branch: 1 branch
BranchTrueFalseTotal
if $path->get_from_account or $path->public 19 28 47
Condition100%3/3
$path->get_from_account or $path->public
l!l&&r!l&&!r
14528
MC/DC100%2/2
$path->get_from_account or $path->public
$path->get_from_account $path->public
Truth table100%3/3
A or B
A= $path->get_from_accountB= $path->public
ABresult
1X1
011
000
235 if $path->get_from_account || $path->public;
236  
237 28 return $path->purpose;
238 }
239  
240 10 signature_for derive_key => (
241 method => !!1,
242 positional => [Defined],
243 );
244  
245 sub derive_key
246 112 ▶{
Subroutine derive_key: called CC=4 SCAR=13.9
247 115 my ($self, $path) = @_;
248  
249 # $path must be remembered for BIP44 operation below
250 115 my $path_info = DerivationPath->assert_coerce($path);
251  
252 113 ▶ Bitcoin::Crypto::Exception::KeyDerive->raise(
Branch: 1 branch
BranchTrueFalseTotal
if !!$self->_is_private ne !!$path_info->private 1 109 110
253 'cannot derive key: key type mismatch'
254 ) if !!$self->_is_private ne !!$path_info->private;
255  
256 112 my $key = $self;
257 112 for my $child_num (@{$path_info->path}) {
258 323 my $hardened = $child_num >= MAX_CHILD_KEYS;
259  
260 # dies if hardened-from-public requested
261 # dies if key is invalid
262 323 $key = $key->_derive_key_partial($child_num, $hardened);
263 }
264  
265 # do not apply these tweaks if no derivation actually happened (path 'm') -
266 # this would remove purpose from the key
267 112 ▶ if ($key != $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key != $self) 102 7 109
268 105 $key->set_network($self->network);
269 102 $key->set_purpose($self->_get_purpose_from_BIP44($path));
270 }
271  
272 109 return $key;
273 }
274  
275 10 1;
276  
277 # Internal use only
278  
« Crypto/Script/Recognition.pm ↑ Summary Crypto/Role/PSBT/Finalizer.pm »