Crypto/Role/Key.pm

filter: Statementstmt 100% 81 / 81 Branchbran 92.8% 26 / 28 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 1 totaltotal 97.8% 138 / 141 CC 18 SCAR 15.6
CC
18
Cov
98%
raw_key
11.0
_validate_key
5.0
_run_with_key
2.0
CRAP 4.8
« Crypto/Script/Opcode.pm ↑ Summary Crypto/Bech32.pm »
1 package Bitcoin::Crypto::Role::Key;
2 37 $Bitcoin::Crypto::Role::Key::VERSION = '4.006';
3 43 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 43 ▶use Mooish::Base -standard, -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 43 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 43 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 43 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 43 ▶use Bitcoin::Crypto::Constants qw(:key);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 43 ▶use Bitcoin::Crypto::Util::Internal qw(get_key_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 43 ▶use Bitcoin::Crypto::Helpers qw(ensure_length ecc);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 43 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 43 ▶use Bitcoin::Crypto::Secret;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ByteStr or BitcoinSecret or SecretBuffer
18 37 has param '_key_instance' => (
19 writer => 1,
20 );
21  
22 37 has param 'purpose' => (
23 isa => BIP44Purpose,
24 writer => 1,
25 clearer => 1,
26 required => 0,
27 );
28  
29 37 with qw(Bitcoin::Crypto::Role::Network);
30  
31 37 requires qw(
32 _is_private
33 );
34  
35 sub _run_with_key
36 2848 ▶{
Subroutine _run_with_key: called CC=2 SCAR=6.9
37 2848 my ($self, $sub_ref) = @_;
38  
39 2848 ▶ if ($self->_is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_private) { } 1799 1049 2848
40 1799 return $self->_key_instance->unmask_to($sub_ref);
41 }
42 else {
43 1049 return $sub_ref->($self->_key_instance);
44 }
45 }
46  
47 sub _validate_key
48 1582 ▶{
Subroutine _validate_key: called CC=5 SCAR=16.1
49 1582 state $sig = signature(method => !!1, positional => [ByteStr]);
50 1582 my ($self, $entropy) = $sig->(@_);
51 1582 my $is_private = get_key_type $entropy;
52  
53 1582 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_private 4 1578 1582
54 'invalid entropy data passed to key creation method'
55 ) unless defined $is_private;
56  
57 1578 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $is_private == $self->_is_private 4 1574 1578
58 'trying to create key from unknown key data'
59 ) unless $is_private == $self->_is_private;
60  
61 1574 ▶ if ($is_private) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_private) { } 531 1043 1574
62 531 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless ecc()->verify_private_key(ensure_length($entropy, 32)) 0 531 531
63 'private key is not valid'
64 ) unless ecc->verify_private_key(ensure_length $entropy, KEY_MAX_LENGTH);
65 }
66 else {
67 1043 try {
68  
69 # keep public keys in compressed form always
70 1043 $self->_set_key_instance(ecc->compress_public_key($entropy));
71 }
72 catch ($e) {
73 1 Bitcoin::Crypto::Exception::KeyCreate->raise(
74 'public key is not valid'
75 );
76 }
77 }
78 }
79  
80 sub BUILD
81 1582 ▶{
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
82 1582 my ($self) = @_;
83 1582 state $type_secret = BitcoinSecret;
84  
85 1582 ▶ $self->_set_key_instance($type_secret->assert_coerce($self->_key_instance))
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_private 533 1049 1582
86 if $self->_is_private;
87  
88 $self->_run_with_key(
89 1582 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 1582 $self->_validate_key($_[0]);
91 }
92 1582 );
93 }
94  
95 37 signature_for has_purpose => (
96 method => !!1,
97 positional => [BIP44Purpose, Bool, {default => !!0}],
98 );
99  
100 sub has_purpose
101 220 ▶{
Subroutine has_purpose: called CC=2 SCAR=6.9
102 220 my ($self, $wanted_purpose, $force_defined) = @_;
103  
104 220 my $purpose = $self->purpose;
105 220 ▶ return !$force_defined if !defined $purpose;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $purpose 122 98 220
106 98 return $purpose == $wanted_purpose;
107 }
108  
109 37 signature_for raw_key => (
110 method => !!1,
111 positional => [Maybe [Enum [qw(private public public_compressed public_xonly)]], {default => undef}],
112 );
113  
114 # helpers for raw_key
115 sub __full_private
116 1266 ▶{
Subroutine __full_private: called CC=1 SCAR=0.0
117 1266 state $sig = signature(positional => [ByteStr]);
118 1266 my ($key) = $sig->(@_);
119 1266 return ensure_length $key, KEY_MAX_LENGTH;
120 }
121  
122 sub __private_to_public
123 750 ▶{
Subroutine __private_to_public: called CC=1 SCAR=0.0
124 750 my ($key) = @_;
125 750 return ecc->create_public_key(__full_private($key));
126 }
127  
128 sub raw_key
129 2200 ▶{
Subroutine raw_key: called CC=11 SCAR=24.0
130 2206 my ($self, $type) = @_;
131 2206 my $is_private = $self->_is_private;
132  
133 2206 ▶ $type //= $is_private ? 'private' : 'public';
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 462 448 910
Condition100%2/2
$type //= $is_private ? "private" : "public"
l!l
1290910
MC/DC100%1/1
$type //= $is_private ? "private" : "public"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
134 2206 ▶ if ($type eq 'public' && (!$self->can('compressed') || $self->compressed)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "public" and !$self->can("compressed") || $self->compressed) 887 1313 2200
Condition100%6/6
$type eq "public" and !$self->can("compressed") || $self->compressed
!ll&&!rl&&r
127241887
not $self->can("compressed") or $self->compressed
l!l&&r!l&&!r
12076741
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
135 893 $type = 'public_compressed';
136 }
137  
138 2206 ▶ if ($type eq 'private') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'private') { } 516 1684 2200
139 522 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $is_private 0 516 516
140 'cannot create private key from a public key'
141 ) unless $is_private;
142  
143 # CAUTION: exposing the secret!
144 522 return $self->_run_with_key(\&__full_private);
145 }
146 else {
147 1684 ▶ my $key = $is_private ? $self->_run_with_key(\&__private_to_public) : $self->_key_instance;
Branch: 1 branch
BranchTrueFalseTotal
$is_private ? : 750 934 1684
148 1684 ▶ if ($type eq 'public_xonly') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'public_xonly') { } 282 1402 1684
149 282 return ecc->xonly_public_key($key);
150 }
151 else {
152 1402 ▶ return $key if $type eq 'public_compressed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "public_compressed" 1361 41 1402
153 41 return ecc->compress_public_key($key, !!0);
154 }
155 }
156 }
157  
158 37 1;
159  
« Crypto/Script/Opcode.pm ↑ Summary Crypto/Bech32.pm »