File Coverage

blib/lib/Blockchain/Ethereum/ABI/Type/Bytes.pm
Criterion Covered Total %
statement 31 32 96.8
branch 8 10 80.0
condition 1 5 20.0
subroutine 6 6 100.0
pod 2 2 100.0
total 48 55 87.2


line stmt bran cond sub pod time code
1 5     5   3035 use v5.26;
  5         20  
2 5     5   29 use Object::Pad;
  5         11  
  5         28  
3              
4             package Blockchain::Ethereum::ABI::Type::Bytes 0.012;
5             class Blockchain::Ethereum::ABI::Type::Bytes
6             :isa(Blockchain::Ethereum::ABI::Type)
7             :does(Blockchain::Ethereum::ABI::TypeRole);
8              
9             =encoding utf8
10              
11             =head1 NAME
12              
13             Blockchain::Ethereum::ABI::Bytes - Interface for solidity bytes type
14              
15             =head1 SYNOPSIS
16              
17             Allows you to define and instantiate a solidity bytes type:
18              
19             my $type = Blockchain::Ethereum::ABI::Bytes->new(
20             signature => $signature,
21             data => $value
22             );
23              
24             $type->encode();
25              
26             In most cases you don't want to use this directly, use instead:
27              
28             =over 4
29              
30             =item * B: L
31              
32             =item * B: L
33              
34             =back
35              
36             =cut
37              
38 5     5   1693 use Carp;
  5         25  
  5         5189  
39              
40 21     21   52 method _configure { return }
  21         124  
41              
42             =head2 encode
43              
44             Encodes the given data to the type of the signature
45              
46             Usage:
47              
48             encode() -> encoded string
49              
50             =over 4
51              
52             =back
53              
54             ABI encoded hex string
55              
56             =cut
57              
58 28     28 1 58 method encode {
59              
60 28 100       65 return $self->_encoded if $self->_encoded;
61             # remove 0x and validates the hexadecimal value
62 12 50 0     39 croak 'Invalid hexadecimal value ' . $self->data // 'undef'
63             unless $self->data =~ /^(?:0x|0X)?([a-fA-F0-9]+)$/;
64 12         40 my $hex = $1;
65              
66 12         63 my $data_length = length(pack("H*", $hex));
67 12 100       41 unless ($self->fixed_length) {
68             # for dynamic length basic types the length must be included
69 6         29 $self->_push_dynamic($self->_encode_length($data_length));
70 6         29 $self->_push_dynamic($self->pad_right($hex));
71             } else {
72 6 50 33     16 croak "Invalid data length, signature: @{[$self->fixed_length]}, data length: $data_length"
  0         0  
73             if $self->fixed_length && $data_length != $self->fixed_length;
74 6         48 $self->_push_static($self->pad_right($hex));
75             }
76              
77 12         39 return $self->_encoded;
78             }
79              
80             =head2 decode
81              
82             Decodes the given data to the type of the signature
83              
84             Usage:
85              
86             decoded() -> hexadecimal encoded bytes
87              
88             =over 4
89              
90             =back
91              
92             hexadecimal encoded bytes string
93              
94             =cut
95              
96 9     9 1 17 method decode {
97              
98 9         22 my @data = $self->data->@*;
99              
100 9         14 my $hex_data;
101 9         26 my $size = $self->fixed_length;
102 9 100       23 unless ($self->fixed_length) {
103 4         13 $size = hex shift @data;
104              
105 4         14 $hex_data = join('', @data);
106             } else {
107 5         12 $hex_data = $data[0];
108             }
109              
110 9         45 my $bytes = substr(pack("H*", $hex_data), 0, $size);
111 9         78 return sprintf "0x%s", unpack("H*", $bytes);
112             }
113              
114             1;
115              
116             __END__