File Coverage

blib/lib/Blockchain/Ethereum/Transaction.pm
Criterion Covered Total %
statement 48 60 80.0
branch 2 2 100.0
condition n/a
subroutine 35 68 51.4
pod 2 60 3.3
total 87 190 45.7


line stmt bran cond sub pod time code
1 4     4   105642 use v5.26;
  4         16  
2 4     4   685 use Object::Pad ':experimental(init_expr)';
  4         11678  
  4         29  
3              
4             package Blockchain::Ethereum::Transaction 0.006;
5             role Blockchain::Ethereum::Transaction;
6              
7             =encoding utf8
8              
9             =head1 NAME
10              
11             Blockchain::Ethereum::Transaction - Ethereum transaction abstraction
12              
13             =head1 SYNOPSIS
14              
15             In most cases you don't want to use this directly, use instead:
16              
17             =over 4
18              
19             =item * B<Legacy>: L<Blockchain::Ethereum::Transaction::Legacy>
20              
21             =item * B<EIP1559>: L<Blockchain::Ethereum::Transaction::EIP1559>
22              
23             =back
24              
25             =cut
26              
27 4     4   1812 use Carp;
  4         21  
  4         293  
28 4     4   2026 use Digest::Keccak qw(keccak_256);
  4         6249  
  4         285  
29              
30 4     4   2017 use Blockchain::Ethereum::RLP;
  4         11760  
  4         5686  
31              
32 8     8 0 22 field $chain_id :reader :writer :param;
  8     8 0 56  
  0     0 0 0  
  0     0 0 0  
        0 0    
        0 0    
33 8     8 0 24 field $nonce :reader :writer :param;
  8     8 0 48  
  0     0 0 0  
  0     0 0 0  
        0 0    
        0 0    
34 8     8 0 21 field $gas_limit :reader :writer :param;
  8     8 0 38  
  0     0 0 0  
  0     0 0 0  
        0 0    
        0 0    
35 8     8 0 18 field $to :reader :writer :param //= '';
        8 0    
        0 0    
36 8     0 0 38 field $value :reader :writer :param //= '0x0';
  0     0 0 0  
  0     0 0 0  
  8     8 0 26  
        8 0    
        0 0    
37 8     8 0 40 field $data :reader :writer :param //= '';
  8     8 0 21  
  0     0 0 0  
  0     0 0 0  
        0 0    
        0 0    
38 8     0 0 39 field $v :reader :writer :param = undef;
  0     0 0 0  
  0     0 0 0  
39 3     3 0 11 field $r :reader :writer :param = undef;
  3     3 0 12  
  11     0 0 27  
  11     11 0 74  
        11 0    
        0 0    
40 6     6 0 17 field $s :reader :writer :param = undef;
  6     6 0 40  
  3     0 0 4492805  
  3     3 0 18  
        3 0    
        0 0    
41              
42 8     8 0 19 field $rlp :reader = Blockchain::Ethereum::RLP->new();
  6     8 0 14  
  6     0 0 39  
  3     6 0 92  
  3     6 0 14  
        0 0    
        3 0    
        3 0    
        0 0    
43              
44 8         51 =head2 serialize
45              
46             To be implemented by the child classes, encodes the given transaction parameters to RLP
47              
48             Usage:
49              
50             serialize() -> RLP encoded transaction bytes
51              
52             =over 4
53              
54             =back
55              
56             Returns the RLP encoded transaction bytes
57              
58             =cut
59              
60             method serialize;
61              
62             =head2 generate_v
63              
64             Generate the transaction v field using the given y-parity
65              
66             Usage:
67              
68             generate_v($y_parity) -> hexadecimal v
69              
70             =over 4
71              
72             =item * C<$y_parity> y-parity
73              
74             =back
75              
76             Returns the v hexadecimal value also sets the v fields from transaction
77              
78             =cut
79              
80             method generate_v;
81              
82             =head2 hash
83              
84             SHA3 Hash the serialized transaction object
85              
86             Usage:
87              
88             hash() -> SHA3 transaction hash
89              
90             =over 4
91              
92             =back
93              
94             Returns the SHA3 transaction hash bytes
95              
96             =cut
97              
98 3     3 1 136957 method hash {
        3 1    
        0 0    
99              
100 3         21 return keccak_256($self->serialize);
101             }
102              
103             # In case of Math::BigInt given for any params, get the hex value
104 8     8   18 method _equalize_params ($params) {
  8     8   17  
  8     0   20  
  8         14  
105              
106 8 100       29 return [map { ref $_ eq 'Math::BigInt' ? $_->as_hex : $_ } $params->@*];
  66         1583  
107             }
108              
109             1;
110              
111             __END__
112              
113             =head1 AUTHOR
114              
115             Reginaldo Costa, C<< <refeco at cpan.org> >>
116              
117             =head1 BUGS
118              
119             Please report any bugs or feature requests to L<https://github.com/refeco/perl-ethereum-transaction>
120              
121             =head1 LICENSE AND COPYRIGHT
122              
123             This software is Copyright (c) 2023 by REFECO.
124              
125             This is free software, licensed under:
126              
127             The MIT License
128              
129             =cut