Crypto/BIP85.pm

filter: Statementstmt 100% 73 / 73 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 97.9% 97 / 99 CC 5 SCAR 3.8
CC
5
Cov
98%
derive_entropy
3.0
derive_mnemonic
2.0
_adjust_length
2.0
CRAP 1.5
« Crypto/Role/Network.pm ↑ Summary Crypto/Key/ExtPrivate.pm »
1 package Bitcoin::Crypto::BIP85;
2 1 $Bitcoin::Crypto::BIP85::VERSION = '4.005';
3 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::Mac::HMAC qw(hmac);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Crypt::Digest::SHAKE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Bitcoin::Crypto qw(btc_prv btc_extprv);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Bitcoin::Crypto::Util::Internal qw(mnemonic_from_entropy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 has param 'key' => (
18 isa => InstanceOf ['Bitcoin::Crypto::Key::ExtPrivate'],
19 );
20  
21 sub _adjust_length
22 11 ▶{
Subroutine _adjust_length: called CC=2 SCAR=6.9
23 11 my ($self, $bytes, $bytelength) = @_;
24  
25 11 ▶ if ($bytelength <= length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytelength <= length $bytes) { } 10 1 11
26 10 return substr $bytes, 0, $bytelength;
27 }
28 else {
29 1 my $shake = Crypt::Digest::SHAKE->new(256);
30 1 $shake->add($bytes);
31 1 return $shake->done($bytelength);
32 }
33 }
34  
35 1 signature_for derive_entropy => (
36 method => !!1,
37 positional => [DerivationPath, Maybe [PositiveInt], {default => undef}],
38 );
39  
40 sub derive_entropy
41 15 ▶{
Subroutine derive_entropy: called CC=3 SCAR=11.0
42 15 my ($self, $path_info, $bytelength) = @_;
43  
44 Bitcoin::Crypto::Exception::KeyDerive->raise(
45 'seed derivation path must be fully hardened'
46 15 ▶ ) unless all { !!$_->[1] } @{$path_info->get_path_hardened};
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { not !$_->[1]; } , @{$path_info->get_path_hardened;}) 0 15 15
47  
48 15 my $key = $self->key->derive_key($path_info);
49  
50 15 my $seed = hmac('SHA512', 'bip-entropy-from-k', $key->raw_key('private'));
51 15 ▶ $seed = $self->_adjust_length($seed, $bytelength)
Branch: 1 branch
BranchTrueFalseTotal
if $bytelength 11 4 15
52 if $bytelength;
53  
54 15 return $seed;
55 }
56  
57 1 signature_for derive_mnemonic => (
58 method => !!1,
59 named => [
60 words => Enum [12, 18, 24],
61 {default => 24},
62 language => Str,
63 {default => 'en'},
64 index => PositiveOrZeroInt,
65 {default => 0},
66 ],
67 bless => !!0,
68 );
69  
70 sub derive_mnemonic
71 4 ▶{
Subroutine derive_mnemonic: called CC=2 SCAR=7.0
72 4 my ($self, $args) = @_;
73  
74 4 my %language_map = (
75 'en' => 0,
76 'ja' => 1,
77 'ko' => 2,
78 'es' => 3,
79 'zh-simplified' => 4,
80 'zh-traditional' => 5,
81 'fr' => 6,
82 'it' => 7,
83  
84 # NOTE: czech has no BIP39 wordlist module, but add it for sake of completeness
85 'cz' => 8,
86 );
87  
88 4 my $language_index = $language_map{$args->{language}};
89 4 ▶ Bitcoin::Crypto::Exception::MnemonicGenerate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $language_index 0 4 4
90 "unknown mnemonic language code $args->{language}"
91 ) unless defined $language_index;
92  
93 4 my $spec_path = "m/83696968'/39'/$language_index'/$args->{words}'/$args->{index}'";
94 4 my $length = $args->{words} / 3 * 4;
95  
96 4 my $entropy = $self->derive_entropy($spec_path, $length);
97 4 return mnemonic_from_entropy($entropy, $args->{language});
98 }
99  
100 1 signature_for derive_prv => (
101 method => !!1,
102 named => [
103 index => PositiveOrZeroInt,
104 {default => 0},
105 ],
106 bless => !!0,
107 );
108  
109 sub derive_prv
110 2 ▶{
Subroutine derive_prv: called CC=1 SCAR=0.0
111 2 my ($self, $args) = @_;
112  
113 2 my $spec_path = "m/83696968'/2'/$args->{index}'";
114  
115 2 my $entropy = $self->derive_entropy($spec_path, 32);
116 2 return btc_prv->from_serialized($entropy);
117 }
118  
119 1 signature_for derive_extprv => (
120 method => !!1,
121 named => [
122 index => PositiveOrZeroInt,
123 {default => 0},
124 ],
125 bless => !!0,
126 );
127  
128 sub derive_extprv
129 2 ▶{
Subroutine derive_extprv: called CC=1 SCAR=0.0
130 2 my ($self, $args) = @_;
131  
132 2 my $spec_path = "m/83696968'/32'/$args->{index}'";
133  
134 2 my $entropy = $self->derive_entropy($spec_path);
135 2 return btc_extprv->new(
136 _key_instance => substr($entropy, 32, 32),
137 chain_code => substr($entropy, 0, 32),
138 );
139 }
140  
141 1 signature_for derive_bytes => (
142 method => !!1,
143 named => [
144 bytes => Int->where(q{$_ >= 16 && $_ <= 64}),
145 {default => 64},
146 index => PositiveOrZeroInt,
147 {default => 0},
148 ],
149 bless => !!0,
150 );
151  
152 sub derive_bytes
153 3 ▶{
Subroutine derive_bytes: called CC=1 SCAR=0.0
154 3 my ($self, $args) = @_;
155  
156 3 my $spec_path = "m/83696968'/128169'/$args->{bytes}'/$args->{index}'";
157  
158 3 return $self->derive_entropy($spec_path, $args->{bytes});
159 }
160  
161 1 1;
162  
163 __END__
« Crypto/Role/Network.pm ↑ Summary Crypto/Key/ExtPrivate.pm »