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 2     2   111200 use v5.26;
  2         33  
2 2     2   639 use Object::Pad;
  2         11944  
  2         11  
3              
4             package Blockchain::Ethereum::Keystore::Seed 0.005;
5             class Blockchain::Ethereum::Keystore::Seed;
6              
7             =encoding utf8
8              
9             =head1 NAME
10              
11             Blockchain::Ethereum::Keystore::Seed
12              
13             =head1 SYNOPSIS
14              
15             If instantiated without a seed or mnemonic, this module uses L<Crypt::PRNG> for the random seed generation
16              
17             my $seed = Blockchain::Ethereum::Seed->new;
18             my $key = $seed->deriv_key(2);
19             print $key->address;
20             ...
21              
22             =cut
23              
24 2     2   768 use Carp;
  2         4  
  2         161  
25 2     2   536 use Crypt::PRNG qw(random_bytes);
  2         3857  
  2         143  
26 2     2   1167 use Bitcoin::Crypto qw(btc_extprv);
  2         729  
  2         138  
27              
28 2     2   519 use Blockchain::Ethereum::Keystore::Key;
  2         6  
  2         2182  
29              
30             field $seed :reader :writer :param //= undef;
31 5     5 0 29 field $mnemonic :reader :writer :param //= undef;
  5     1 0 39  
  1         77  
  1         5  
32 3     3 0 496171 field $salt :reader :writer :param //= undef;
  3     0 0 17  
  0         0  
  0         0  
33              
34 10     10 0 31 field $_hdw_handler :reader(_hdw_handler) :writer(set_hdw_handler);
  10     1 0 74  
  1     3 0 4  
  1     0   6  
  3         39730  
  3         29  
  0         0  
  0         0  
35              
36             ADJUST {
37             if ($self->seed) {
38             $self->set_hdw_handler(btc_extprv->from_hex_seed(unpack "H*", $self->seed));
39             } elsif ($self->mnemonic) {
40             $self->set_hdw_handler(btc_extprv->from_mnemonic($self->mnemonic, $self->salt));
41             }
42              
43             unless ($self->_hdw_handler) {
44             # if the seed is not given, generate a new one
45             $self->set_seed(random_bytes(64));
46             $self->set_hdw_handler(btc_extprv->from_hex_seed(unpack "H*", $self->seed));
47             }
48             }
49              
50 7     7 0 57 method derive_key ($index, $account = 0, $purpose = 44, $coin_type = 60, $change = 0) {
  7         15  
  7         22  
  7         16  
  7         14  
  7         11  
  7         18  
  7         12  
51              
52 7         172 my $path = Bitcoin::Crypto::BIP44->new(
53             index => $index,
54             purpose => $purpose,
55             coin_type => $coin_type,
56             account => $account,
57             change => $change,
58             );
59              
60 7         7568 return Blockchain::Ethereum::Keystore::Key->new(
61             private_key => pack "H*",
62             $self->_hdw_handler->derive_key($path)->get_basic_key->to_hex
63             );
64              
65             }
66              
67             1;
68              
69             __END__
70              
71             =head1 AUTHOR
72              
73             Reginaldo Costa, C<< <refeco at cpan.org> >>
74              
75             =head1 BUGS
76              
77             Please report any bugs or feature requests to L<https://github.com/refeco/perl-ethereum-keystore>
78              
79             =head1 LICENSE AND COPYRIGHT
80              
81             This software is Copyright (c) 2023 by REFECO.
82              
83             This is free software, licensed under:
84              
85             The MIT License
86              
87             =cut
88