Crypto/Key/Private.pm

filter: Statementstmt 100% 82 / 82 Branchbran 93.7% 15 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 99.2% 125 / 126 CC 10 SCAR 12.4
CC
10
Cov
99%
__ANON__
9.0
to_wif
2.0
from_wif
1.0
CRAP 3.5
« Crypto/Script.pm ↑ Summary Crypto/Transaction/Signer.pm »
1 package Bitcoin::Crypto::Key::Private;
2 29 $Bitcoin::Crypto::Key::Private::VERSION = '4.006';
3 29 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 29 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Bitcoin::BIP39 qw(bip39_mnemonic_to_entropy entropy_to_bip39_mnemonic);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 29 ▶use Bitcoin::Crypto::Key::Public;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use Bitcoin::Crypto::Base58 qw(encode_base58check decode_base58check);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Bitcoin::Crypto::Constants qw(:key);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 29 ▶use Bitcoin::Crypto::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 29 ▶use Bitcoin::Crypto::Util::Internal qw(validate_wif);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 29 ▶use Bitcoin::Crypto::Helpers qw(ensure_length);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 29 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 29 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 29 extends qw(Bitcoin::Crypto::Key::Base);
21  
22 1385 ▶sub _is_private { 1 }
Subroutine _is_private: called CC=1 SCAR=0.0
23  
24 sub to_wif
25 21 ▶{
Subroutine to_wif: called CC=2 SCAR=6.9
Pod documented
26 21 my ($self) = @_;
27 21 my $bytes = $self->to_serialized();
28  
29 # wif network - 1B
30 21 my $wifdata = $self->network->wif_byte;
31  
32 # key entropy - 32B
33 21 $wifdata .= ensure_length $bytes, KEY_MAX_LENGTH;
34  
35 # additional byte for compressed key - 1B
36 21 ▶ $wifdata .= WIF_COMPRESSED_BYTE if $self->compressed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->compressed 20 1 21
37  
38 21 return encode_base58check($wifdata);
39 }
40  
41 29 signature_for from_serialized => (
42 method => !!1,
43 positional => [BitcoinSecret],
44 );
45  
46 29 signature_for from_wif => (
47 method => !!1,
48 positional => [BitcoinSecret, Maybe [Str], {default => undef}],
49 );
50  
51 sub from_wif
52 18 ▶{
Subroutine from_wif: called CC=1 SCAR=0.0
53 18 my ($class, $secret, $network) = @_;
54  
55 return $secret->unmask_to(
56 18 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
57 18 my ($wif) = @_;
58  
59 18 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless validate_wif($wif) 0 18 18
60 'base58 string is not valid WIF'
61 ) unless validate_wif($wif);
62  
63 18 my $decoded = decode_base58check($wif);
64 18 my $private = substr $decoded, 1;
65  
66 18 my $compressed = 0;
67 18 ▶ if (length($private) > KEY_MAX_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $private > 32) 9 9 18
68 9 chop $private;
69 9 $compressed = 1;
70 }
71  
72 18 my $wif_network_byte = substr $decoded, 0, 1;
73 my @found_networks =
74 18 ▶ Bitcoin::Crypto::Network->find(sub { shift->wif_byte eq $wif_network_byte });
Subroutine __ANON__: called CC=1 SCAR=0.0
75 18 ▶ @found_networks = grep { $_ eq $network } @found_networks
Branch: 1 branch
BranchTrueFalseTotal
if defined $network 2 16 18
76 if defined $network;
77  
78 18 ▶ if (@found_networks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_networks > 1) 8 10 18
79 8 my $default_network = Bitcoin::Crypto::Network->get->id;
80  
81 Bitcoin::Crypto::Exception::KeyCreate->raise(
82 'found multiple networks possible for given WIF: ' . join ', ', @found_networks
83 8 ▶ ) 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 7 8
84  
85 7 @found_networks = ($default_network);
86 }
87  
88 Bitcoin::Crypto::Exception::KeyCreate->raise(
89 17 ▶ "network name $network cannot be used for given WIF"
Branch: 1 branch
BranchTrueFalseTotal
if @found_networks == 0 and defined $network 1 16 17
Condition100%3/3
@found_networks == 0 and defined $network
!ll&&!rl&&r
1511
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
90 ) if @found_networks == 0 && defined $network;
91  
92 16 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
if @found_networks == 0 1 15 16
93 "couldn't find network for WIF byte $wif_network_byte"
94 ) if @found_networks == 0;
95  
96 15 my $instance = $class->from_serialized($private);
97 15 $instance->set_compressed($compressed);
98 15 $instance->set_network(@found_networks);
99 15 return $instance;
100 }
101 18 );
102 }
103  
104 sub get_public_key
105 262 ▶{
Subroutine get_public_key: called CC=1 SCAR=0.0
Pod documented
106 262 my ($self) = @_;
107  
108 262 my $public = Bitcoin::Crypto::Key::Public->new(
109 _key_instance => $self->raw_key('public'),
110 compressed => $self->compressed,
111 network => $self->network,
112 purpose => $self->purpose,
113 taproot_output => $self->taproot_output,
114 );
115  
116 262 return $public;
117 }
118  
119 29 1;
120  
121 __END__
« Crypto/Script.pm ↑ Summary Crypto/Transaction/Signer.pm »