Crypto/Role/Key.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 92.3% 24 / 26 Conditioncond 91.6% 11 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 1 totaltotal 95.1% 137 / 144 CC 18 SCAR 15.7
CC
18
Cov
97%
raw_key
11.0
_validate_key
5.0
_run_with_key
2.0
CRAP 4.8
« Crypto/Script/Opcode.pm ↑ Summary Crypto/Script/Tree.pm »
1 package Bitcoin::Crypto::Role::Key;
2 35 $Bitcoin::Crypto::Role::Key::VERSION = '4.004';
3 40 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 40 ▶use Mooish::Base -standard, -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Bitcoin::Crypto::Constants qw(:key);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Bitcoin::Crypto::Util::Internal qw(get_key_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Bitcoin::Crypto::Helpers qw(ensure_length ecc);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Bitcoin::Crypto::Secret;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ByteStr or BitcoinSecret or SecretBuffer
18 35 has param '_key_instance' => (
19 writer => 1,
20 );
21  
22 35 has param 'purpose' => (
23 isa => BIP44Purpose,
24 writer => 1,
25 clearer => 1,
26 required => 0,
27 );
28  
29 35 with qw(Bitcoin::Crypto::Role::Network);
30  
31 35 requires qw(
32 _is_private
33 );
34  
35 sub _run_with_key
36 {
37 2431 ▶ my ($self, $sub_ref) = @_;
Subroutine _run_with_key: called CC=2 SCAR=6.9
38  
39 2431 ▶ if ($self->_is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_private) { } 1614 817 2431
40 1614 return $self->_key_instance->unmask_to($sub_ref);
41 }
42 else {
43 817 return $sub_ref->($self->_key_instance);
44 }
45 }
46  
47 sub _validate_key
48 {
49 1323 ▶ state $sig = signature(method => !!1, positional => [ByteStr]);
Subroutine _validate_key: called CC=5 SCAR=16.2
50 1323 my ($self, $entropy) = $sig->(@_);
51 1323 my $is_private = get_key_type $entropy;
52  
53 1323 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_private 1 1322 1323
54 'invalid entropy data passed to key creation method'
55 ) unless defined $is_private;
56  
57 1322 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $is_private == $self->_is_private 6 1316 1322
58 'trying to create key from unknown key data'
59 ) unless $is_private == $self->_is_private;
60  
61 1316 ▶ if ($is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_private) { } 504 812 1316
62 504 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless ecc()->verify_private_key(ensure_length($entropy, 32)) 0 504 504
63 'private key is not valid'
64 ) unless ecc->verify_private_key(ensure_length $entropy, KEY_MAX_LENGTH);
65 }
66 else {
67 812 try {
68  
69 # keep public keys in compressed form always
70 812 $self->_set_key_instance(ecc->compress_public_key($entropy));
71 }
72 catch ($e) {
73 0 Bitcoin::Crypto::Exception::KeyCreate->raise(
74 'public key is not valid'
75 );
76 }
77 }
78 }
79  
80 sub BUILD
81 {
82 1323 ▶ my ($self) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
83 1323 state $type_secret = BitcoinSecret;
84  
85 1323 ▶ $self->_set_key_instance($type_secret->assert_coerce($self->_key_instance))
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_private 506 817 1323
86 if $self->_is_private;
87  
88 $self->_run_with_key(
89 sub {
90 1323 ▶ $self->_validate_key($_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
91 }
92 1323 );
93 }
94  
95 35 signature_for has_purpose => (
96 method => !!1,
97 positional => [BIP44Purpose],
98 );
99  
100 sub has_purpose
101 {
102 191 ▶ my ($self, $purpose) = @_;
Subroutine has_purpose: called CC=2 SCAR=6.9
103  
104 191 ▶ return !$self->purpose || $self->purpose == $purpose;
Condition100%3/3
!$self->purpose || $self->purpose == $purpose
l!l&&r!l&&!r
935642
MC/DC100%2/2
!$self->purpose || $self->purpose == $purpose
!$self->purpose $self->purpose == $purpose
Truth table100%3/3
not(A) || B
A= !$self->purposeB= $self->purpose == $purpose
ABresult
1X1
011
000
105 }
106  
107 35 signature_for raw_key => (
108 method => !!1,
109 positional => [Maybe [Enum [qw(private public public_compressed public_xonly)]], {default => undef}],
110 );
111  
112 # helpers for raw_key
113 sub __full_private
114 {
115 1108 ▶ state $sig = signature(positional => [ByteStr]);
Subroutine __full_private: called CC=1 SCAR=0.0
116 1108 my ($key) = $sig->(@_);
117 1108 return ensure_length $key, KEY_MAX_LENGTH;
118 }
119  
120 sub __private_to_public
121 {
122 632 ▶ my ($key) = @_;
Subroutine __private_to_public: called CC=1 SCAR=0.0
123 632 return ecc->create_public_key(__full_private($key));
124 }
125  
126 sub raw_key
127 {
128 1818 ▶ my ($self, $type) = @_;
Subroutine raw_key: called CC=11 SCAR=24.0
129 1818 my $is_private = $self->_is_private;
130  
131 1818 ▶ $type //= $is_private ? 'private' : 'public';
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 442 327 769
Condition66%2/3
$type //= $is_private ? "private" : "public"
l!l&&r!l&&!r
10447690
MC/DC0%0/2
$type //= $is_private ? "private" : "public"
$type $is_private ? "private" : "public"
Truth table66%2/3
A //= B
A= $typeB= $is_private ? "private" : "public"
ABresult
1X1
011
000
132 1818 ▶ if ($type eq 'public' && (!$self->can('compressed') || $self->compressed)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "public" and !$self->can("compressed") || $self->compressed) 629 1184 1813
Condition100%6/6
$type eq "public" and !$self->can("compressed") || $self->compressed
!ll&&!rl&&r
114499570
not $self->can("compressed") or $self->compressed
l!l&&r!l&&!r
12050940
MC/DC100%3/3
$type eq "public" and !$self->can("compressed") || $self->compressed
$type eq "public" not $self->can("compressed") $self->compressed
Truth table100%4/4
A and not(B) or C
A= $type eq "public"B= not $self->can("compressed")C= $self->compressed
ABCresult
0XX0
1000
11X1
1011
133 634 $type = 'public_compressed';
134 }
135  
136 1818 ▶ if ($type eq 'private') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'private') { } 476 1337 1813
137 481 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $is_private 0 476 476
138 'cannot create private key from a public key'
139 ) unless $is_private;
140  
141 # CAUTION: exposing the secret!
142 481 return $self->_run_with_key(\&__full_private);
143 }
144 else {
145 1337 ▶ my $key = $is_private ? $self->_run_with_key(\&__private_to_public) : $self->_key_instance;
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 632 705 1337
146 1337 ▶ if ($type eq 'public_xonly') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'public_xonly') { } 196 1141 1337
147 196 return ecc->xonly_public_key($key);
148 }
149 else {
150 1141 ▶ return $key if $type eq 'public_compressed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "public_compressed" 1101 40 1141
151 40 return ecc->compress_public_key($key, !!0);
152 }
153 }
154 }
155  
156 35 1;
157  
« Crypto/Script/Opcode.pm ↑ Summary Crypto/Script/Tree.pm »