Crypto/Key/ExtBase.pm

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