File Coverage

blib/lib/Blockchain/Ethereum/Transaction/Legacy.pm
Criterion Covered Total %
statement 24 26 92.3
branch 2 2 100.0
condition 4 6 66.6
subroutine 6 7 85.7
pod 2 4 50.0
total 38 45 84.4


line stmt bran cond sub pod time code
1 2     2   107422 use v5.26;
  2         22  
2 2     2   652 use Object::Pad;
  2         11140  
  2         12  
3              
4             package Blockchain::Ethereum::Transaction::Legacy 0.008;
5             class Blockchain::Ethereum::Transaction::Legacy
6 1     1   604 :does(Blockchain::Ethereum::Transaction);
  1         3  
  1         84  
7              
8             =encoding utf8
9              
10             =head1 NAME
11              
12             Blockchain::Ethereum::Transaction::Legacy - Ethereum Legacy transaction abstraction
13              
14             =head1 SYNOPSIS
15              
16             Transaction abstraction for Legacy transactions
17              
18             my $transaction = Blockchain::Ethereum::Transaction::Legacy->new(
19             nonce => '0x9',
20             gas_price => '0x4A817C800',
21             gas_limit => '0x5208',
22             to => '0x3535353535353535353535353535353535353535',
23             value => '0xDE0B6B3A7640000',
24             chain_id => '0x1'
25              
26             # github.com/refeco/perl-ethereum-keystore
27             my $key = Blockchain::Ethereum::Keystore::Key->new(
28             private_key => pack "H*",
29             '4646464646464646464646464646464646464646464646464646464646464646'
30             );
31              
32             $key->sign_transaction($transaction);
33              
34             my $raw_transaction = $transaction->serialize;
35              
36             =cut
37              
38 2     2 0 7 field $gas_price :reader :writer :param;
  2     0 0 12  
  0         0  
  0         0  
39              
40             =head2 serialize
41              
42             Encodes the given transaction parameters to RLP
43              
44             Usage:
45              
46             serialize -> RLP encoded transaction bytes
47              
48             =over 4
49              
50             =back
51              
52             Returns the RLP encoded transaction bytes
53              
54             =cut
55              
56 2     2 1 20 method serialize {
57              
58 2         11 my @params = (
59             $self->nonce, #
60             $self->gas_price,
61             $self->gas_limit,
62             $self->to,
63             $self->value,
64             $self->data,
65             );
66              
67 2         14 @params = $self->_equalize_params(\@params)->@*;
68              
69 2 100 66     15 if ($self->v && $self->r && $self->s) {
      66        
70 1         4 push(@params, $self->v, $self->r, $self->s);
71             } else {
72 1         8 push(@params, $self->chain_id, '0x', '0x');
73             }
74              
75 2         16 return $self->rlp->encode(\@params);
76             }
77              
78             =head2 generate_v
79              
80             Generate the transaction v field using the given y-parity
81              
82             Usage:
83              
84             generate_v($y_parity) -> hexadecimal v
85              
86             =over 4
87              
88             =item * C<$y_parity> y-parity
89              
90             =back
91              
92             Returns the v hexadecimal value also sets the v fields from transaction
93              
94             =cut
95              
96 1     1 1 12 method generate_v ($y_parity) {
  1         3  
  1         4  
  1         2  
97              
98 1         9 my $v = sprintf("0x%x", (hex $self->chain_id) * 2 + 35 + $y_parity);
99              
100 1         7 $self->set_v($v);
101 1         2 return $v;
102             }
103              
104             1;
105              
106             __END__
107              
108             =head1 AUTHOR
109              
110             Reginaldo Costa, C<< <refeco at cpan.org> >>
111              
112             =head1 BUGS
113              
114             Please report any bugs or feature requests to L<https://github.com/refeco/perl-ethereum-transaction>
115              
116             =head1 LICENSE AND COPYRIGHT
117              
118             This software is Copyright (c) 2023 by REFECO.
119              
120             This is free software, licensed under:
121              
122             The MIT License
123              
124             =cut