File Coverage

blib/lib/Blockchain/Ethereum/Keystore/Seed.pm
Criterion Covered Total %
statement 39 43 90.7
branch n/a
condition n/a
subroutine 13 15 86.6
pod 0 8 0.0
total 52 66 78.7


line stmt bran cond sub pod time code
1 1     1   103081 use v5.26;
  1         12  
2 1     1   599 use Object::Pad;
  1         10676  
  1         4  
3              
4             package Blockchain::Ethereum::Keystore::Seed;
5             class Blockchain::Ethereum::Keystore::Seed;
6              
7             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
8             our $VERSION = '0.007'; # VERSION
9              
10 1     1   428 use Carp;
  1         3  
  1         62  
11 1     1   459 use Crypt::PRNG qw(random_bytes);
  1         3527  
  1         70  
12 1     1   520 use Bitcoin::Crypto qw(btc_extprv);
  1         430  
  1         69  
13              
14 1     1   471 use Blockchain::Ethereum::Keystore::Key;
  1         3  
  1         1111  
15              
16             field $seed :reader :writer :param //= undef;
17 5     5 0 24 field $mnemonic :reader :writer :param //= undef;
  5     1 0 31  
  1         72  
  1         5  
18 3     3 0 480567 field $salt :reader :writer :param //= undef;
  3     0 0 17  
  0         0  
  0         0  
19              
20 10     10 0 34 field $_hdw_handler :reader(_hdw_handler) :writer(set_hdw_handler);
  10     1 0 56  
  1     3 0 4  
  1     0   6  
  3         39614  
  3         20  
  0         0  
  0         0  
21              
22             ADJUST {
23             if ($self->seed) {
24             $self->set_hdw_handler(btc_extprv->from_hex_seed(unpack "H*", $self->seed));
25             } elsif ($self->mnemonic) {
26             $self->set_hdw_handler(btc_extprv->from_mnemonic($self->mnemonic, $self->salt));
27             }
28              
29             unless ($self->_hdw_handler) {
30             # if the seed is not given, generate a new one
31             $self->set_seed(random_bytes(64));
32             $self->set_hdw_handler(btc_extprv->from_hex_seed(unpack "H*", $self->seed));
33             }
34             }
35              
36 7     7 0 49 method derive_key ($index, $account = 0, $purpose = 44, $coin_type = 60, $change = 0) {
  7         21  
  7         17  
  7         17  
  7         16  
  7         20  
  7         18  
  7         17  
37              
38 7         172 my $path = Bitcoin::Crypto::BIP44->new(
39             index => $index,
40             purpose => $purpose,
41             coin_type => $coin_type,
42             account => $account,
43             change => $change,
44             );
45              
46 7         7151 return Blockchain::Ethereum::Keystore::Key->new(
47             private_key => pack "H*",
48             $self->_hdw_handler->derive_key($path)->get_basic_key->to_hex
49             );
50              
51             }
52              
53             1;
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             Blockchain::Ethereum::Keystore::Seed
64              
65             =head1 VERSION
66              
67             version 0.007
68              
69             =head1 SYNOPSIS
70              
71             Creating a new seed and derivating the key from it:
72              
73             my $seed = Blockchain::Ethereum::Seed->new;
74             my $key = $seed->deriv_key(2); # Blockchain::Ethereum::Keystore::Key
75             print $key->address;
76              
77             Importing a mnemonic:
78              
79             my $seed = Blockchain::Ethereum::Seed->new(mnemonic => 'your mnemonic here');
80              
81             Importing seed bytes:
82              
83             my $hex_seed = '...';
84             my $seed = Blockchain::Ethereum::Seed->new(seed => pack("H*", $hex_seed));
85              
86             =head1 OVERVIEW
87              
88             If instantiated without a seed or mnemonic, this module uses L<Crypt::PRNG> for the random seed generation
89              
90             =head1 METHODS
91              
92             =head2 deriv_key
93              
94             Derivates a L<Blockchain::Ethereum::Keystore::Key> for the given index
95              
96             =over 4
97              
98             =item * C<$index> key index
99              
100             =item * C<$account> [optional, default 0] account index
101              
102             =item * C<$purpose> [optional, default 44] improvement proposal
103              
104             =item * C<$coin_type> [optional, default 60] coin type code
105              
106             =back
107              
108             L<Blockchain::Ethereum::Keystore::Key>
109              
110             =head1 AUTHOR
111              
112             Reginaldo Costa <refeco@cpan.org>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is Copyright (c) 2023 by REFECO.
117              
118             This is free software, licensed under:
119              
120             The MIT (X11) License
121              
122             =cut