Crypto/Role/Key.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 92.3% 24 / 26 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 1 totaltotal 97.1% 138 / 142 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.005';
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 2436 ▶{
Subroutine _run_with_key: called CC=2 SCAR=6.9
37 2436 my ($self, $sub_ref) = @_;
38  
39 2436 ▶ if ($self->_is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_private) { } 1614 822 2436
40 1614 return $self->_key_instance->unmask_to($sub_ref);
41 }
42 else {
43 822 return $sub_ref->($self->_key_instance);
44 }
45 }
46  
47 sub _validate_key
48 1328 ▶{
Subroutine _validate_key: called CC=5 SCAR=16.2
49 1328 state $sig = signature(method => !!1, positional => [ByteStr]);
50 1328 my ($self, $entropy) = $sig->(@_);
51 1328 my $is_private = get_key_type $entropy;
52  
53 1328 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_private 1 1327 1328
54 'invalid entropy data passed to key creation method'
55 ) unless defined $is_private;
56  
57 1327 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $is_private == $self->_is_private 6 1321 1327
58 'trying to create key from unknown key data'
59 ) unless $is_private == $self->_is_private;
60  
61 1321 ▶ if ($is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_private) { } 504 817 1321
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 817 try {
68  
69 # keep public keys in compressed form always
70 817 $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 1328 ▶{
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
82 1328 my ($self) = @_;
83 1328 state $type_secret = BitcoinSecret;
84  
85 1328 ▶ $self->_set_key_instance($type_secret->assert_coerce($self->_key_instance))
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_private 506 822 1328
86 if $self->_is_private;
87  
88 $self->_run_with_key(
89 1328 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 1328 $self->_validate_key($_[0]);
91 }
92 1328 );
93 }
94  
95 35 signature_for has_purpose => (
96 method => !!1,
97 positional => [BIP44Purpose],
98 );
99  
100 sub has_purpose
101 191 ▶{
Subroutine has_purpose: called CC=2 SCAR=6.9
102 191 my ($self, $purpose) = @_;
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 1108 ▶{
Subroutine __full_private: called CC=1 SCAR=0.0
115 1108 state $sig = signature(positional => [ByteStr]);
116 1108 my ($key) = $sig->(@_);
117 1108 return ensure_length $key, KEY_MAX_LENGTH;
118 }
119  
120 sub __private_to_public
121 632 ▶{
Subroutine __private_to_public: called CC=1 SCAR=0.0
122 632 my ($key) = @_;
123 632 return ecc->create_public_key(__full_private($key));
124 }
125  
126 sub raw_key
127 1818 ▶{
Subroutine raw_key: called CC=11 SCAR=24.0
128 1823 my ($self, $type) = @_;
129 1823 my $is_private = $self->_is_private;
130  
131 1823 ▶ $type //= $is_private ? 'private' : 'public';
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 442 327 769
Condition100%2/2
$type //= $is_private ? "private" : "public"
l!l
1049769
MC/DC100%1/1
$type //= $is_private ? "private" : "public"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
132 1823 ▶ if ($type eq 'public' && (!$self->can('compressed') || $self->compressed)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "public" and !$self->can("compressed") || $self->compressed) 634 1184 1818
Condition100%6/6
$type eq "public" and !$self->can("compressed") || $self->compressed
!ll&&!rl&&r
114440634
not $self->can("compressed") or $self->compressed
l!l&&r!l&&!r
12051440
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 639 $type = 'public_compressed';
134 }
135  
136 1823 ▶ if ($type eq 'private') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'private') { } 476 1342 1818
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 1342 ▶ my $key = $is_private ? $self->_run_with_key(\&__private_to_public) : $self->_key_instance;
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 632 710 1342
146 1342 ▶ if ($type eq 'public_xonly') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'public_xonly') { } 196 1146 1342
147 196 return ecc->xonly_public_key($key);
148 }
149 else {
150 1146 ▶ return $key if $type eq 'public_compressed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "public_compressed" 1106 40 1146
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 »