File Coverage

blib/lib/Blockchain/Ethereum/Keystore/Key.pm
Criterion Covered Total %
statement 42 54 77.7
branch 0 2 0.0
condition n/a
subroutine 15 17 88.2
pod 3 6 50.0
total 60 79 75.9


line stmt bran cond sub pod time code
1 3     3   101022 use v5.26;
  3         26  
2 3     3   629 use Object::Pad;
  3         10912  
  3         20  
3              
4             package Blockchain::Ethereum::Keystore::Key;
5             class Blockchain::Ethereum::Keystore::Key;
6              
7             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
8             our $VERSION = '0.007'; # VERSION
9              
10 3     3   1290 use Carp;
  3         23  
  3         215  
11 3     3   1930 use Crypt::PK::ECC;
  3         39593  
  3         179  
12 3     3   1588 use Crypt::Perl::ECDSA::Parse;
  3         318464  
  3         193  
13 3     3   33 use Crypt::Perl::ECDSA::Utils;
  3         9  
  3         121  
14 3     3   1543 use Crypt::Digest::Keccak256 qw(keccak256);
  3         2226  
  3         227  
15 3     3   23 use Crypt::PRNG qw(random_bytes);
  3         7  
  3         163  
16              
17 3     3   1500 use Blockchain::Ethereum::Keystore::Key::PKUtil;
  3         59  
  3         136  
18 3     3   1309 use Blockchain::Ethereum::Keystore::Address;
  3         8  
  3         3255  
19              
20             field $private_key :reader :writer :param //= undef;
21 9     9 0 22 field $_ecc_handler :reader(_ecc_handler) :writer(set_ecc_handler);
  9     32 0 71  
  32     13 0 96  
  32     0   74795  
  13         503466  
  13         266  
  0         0  
  0         0  
22              
23             ADJUST {
24             # if the private key is not set, generate a new one
25             $self->set_private_key(random_bytes(32)) unless defined $self->private_key;
26              
27             my $importer = Crypt::PK::ECC->new();
28             $importer->import_key_raw($self->private_key, 'secp256k1');
29              
30             # Crypt::PK::ECC does not provide support for deterministic keys
31             $self->set_ecc_handler(bless Crypt::Perl::ECDSA::Parse::private($importer->export_key_der('private')),
32             'Blockchain::Ethereum::Keystore::Key::PKUtil');
33              
34             }
35              
36 0     0 1 0 method sign_transaction ($transaction) {
  0         0  
  0         0  
  0         0  
37              
38 0 0       0 croak "transaction must be a reference from Blockchain::Ethereum::Transaction"
39             unless ref($transaction) =~ /^\QBlockchain::Ethereum::Transaction/;
40              
41             # _sign is overriden by Blockchain::ethereum::Keystore::Key::PKUtil
42             # to include the y_parity as part of the response
43 0         0 my ($r, $s, $y_parity) = $self->_ecc_handler->_sign($transaction->hash);
44              
45 0         0 $transaction->set_r($r->as_hex);
46 0         0 $transaction->set_s($s->as_hex);
47 0         0 $transaction->generate_v($y_parity);
48              
49 0         0 return $transaction;
50             }
51              
52 9     9 1 40 method address {
53              
54 9         52 my ($x, $y) = Crypt::Perl::ECDSA::Utils::split_G_or_public($self->_ecc_handler->_decompress_public_point);
55              
56             # address is the hash of the concatenated value of x and y
57 9         446 my $address = substr(keccak256($x . $y), -20);
58 9         211 my $hex_address = unpack("H*", $address);
59              
60 9         164 return Blockchain::Ethereum::Keystore::Address->new(address => "0x$hex_address");
61             }
62              
63 6     6 1 21 method export {
64              
65 6         29 return $self->private_key;
66             }
67              
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Blockchain::Ethereum::Keystore::Key
79              
80             =head1 VERSION
81              
82             version 0.007
83              
84             =head1 SYNOPSIS
85              
86             Generate a new key:
87              
88             my $key = Blockchain::Ethereum::Key->new;
89             $key->sign_transaction($transaction); # Blockchain::Ethereum::Transaction
90              
91             Import existent key:
92              
93             my $key = Blockchain::Ethereum::Key->new(private_key => $private_key); # private key bytes
94             $key->sign_transaction($transaction); # Blockchain::Ethereum::Transaction
95              
96             =head1 OVERVIEW
97              
98             This is a private key abstraction
99              
100             If instantiated without a private key, this module uses L<Crypt::PRNG> for the random key generation
101              
102             =head1 METHODS
103              
104             =head2 sign_transaction
105              
106             Sign a L<Blockchain::Ethereum::Transaction> object
107              
108             =over 4
109              
110             =item * C<$transaction> - L<Blockchain::Ethereum::Transaction> subclass
111              
112             =back
113              
114             self
115              
116             =head2 address
117              
118             Export the L<Blockchain::Ethereum::Keystore::Address> from the imported/generated private key
119              
120             =over 4
121              
122             =back
123              
124             L<Blockchain::Ethereum::Keystore::Address>
125              
126             =head2 export
127              
128             Export the source/new private key
129              
130             =over 4
131              
132             =back
133              
134             Private key bytes
135              
136             =head1 AUTHOR
137              
138             Reginaldo Costa <refeco@cpan.org>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is Copyright (c) 2023 by REFECO.
143              
144             This is free software, licensed under:
145              
146             The MIT (X11) License
147              
148             =cut