File Coverage

blib/lib/Blockchain/Ethereum/Seed.pm
Criterion Covered Total %
statement 42 42 100.0
branch 12 16 75.0
condition n/a
subroutine 13 13 100.0
pod 0 5 0.0
total 67 76 88.1


line stmt bran cond sub pod time code
1             package Blockchain::Ethereum::Seed;
2              
3 2     2   392852 use v5.26;
  2         9  
4 2     2   15 use strict;
  2         5  
  2         73  
5 2     2   13 use warnings;
  2         5  
  2         221  
6              
7             # ABSTRACT: Seed abstraction
8             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
9             our $VERSION = '0.021'; # VERSION
10              
11 2     2   15 use Carp;
  2         5  
  2         245  
12 2     2   1167 use Crypt::PRNG qw(random_bytes);
  2         10346  
  2         203  
13 2     2   1232 use Bitcoin::Crypto qw(btc_extprv);
  2         3486  
  2         169  
14              
15 2     2   1085 use Blockchain::Ethereum::Key;
  2         8  
  2         1067  
16              
17             sub new {
18 3     3 0 311095 my ($class, %params) = @_;
19              
20 3         11 my $self = bless {}, $class;
21 3         12 foreach (qw(seed mnemonic salt)) {
22 9 100       39 $self->{$_} = $params{$_} if exists $params{$_};
23             }
24              
25 3 100       12 if ($self->seed) {
    100          
26 1         7 $self->{hdw_handler} = btc_extprv->from_seed($self->seed);
27             } elsif ($self->mnemonic) {
28 1         9 $self->{hdw_handler} = btc_extprv->from_mnemonic($self->mnemonic, $self->salt);
29             }
30              
31 3 100       53058 unless ($self->_hdw_handler) {
32             # if the seed is not given, generate a new one
33 1         8 $self->{seed} = random_bytes(64);
34 1         26 $self->{hdw_handler} = btc_extprv->from_seed($self->seed);
35             }
36              
37 3         1032 return $self;
38             }
39              
40             sub seed {
41 5     5 0 46 shift->{seed};
42             }
43              
44             sub mnemonic {
45 3     3 0 2231611 shift->{mnemonic};
46             }
47              
48             sub salt {
49 1     1 0 7 shift->{salt};
50             }
51              
52             sub _hdw_handler {
53 10     10   135 shift->{hdw_handler};
54             }
55              
56             sub derive_key {
57 7     7 0 141 my ($self, $index, $account, $purpose, $coin_type, $change) = @_;
58              
59 7 50       32 $account = 0 unless $account;
60 7 50       23 $purpose = 44 unless $purpose;
61 7 50       21 $coin_type = 60 unless $coin_type;
62 7 50       25 $change = 0 unless $change;
63              
64 7         279 my $path = Bitcoin::Crypto::BIP44->new(
65             index => $index,
66             purpose => $purpose,
67             coin_type => $coin_type,
68             account => $account,
69             change => $change,
70             );
71              
72 7         8992 return Blockchain::Ethereum::Key->new(private_key => $self->_hdw_handler->derive_key($path)->get_basic_key->to_serialized);
73             }
74              
75             1;
76              
77             __END__