File Coverage

blib/lib/Blockchain/Ethereum/ABI/Type/String.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 28 28 100.0


line stmt bran cond sub pod time code
1 4     4   3304 use v5.26;
  4         17  
2 4     4   26 use Object::Pad;
  4         16  
  4         26  
3              
4             package Blockchain::Ethereum::ABI::Type::String 0.012;
5             class Blockchain::Ethereum::ABI::Type::String
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::String - Interface for solidity string type
14              
15             =head1 SYNOPSIS
16              
17             Allows you to define and instantiate a solidity string type:
18              
19             my $type = Blockchain::Ethereum::ABI::String->new(
20             signature => $signature,
21             data => $value
22             );
23              
24             $type->encode();
25             ...
26              
27             In most cases you don't want to use this directly, use instead:
28              
29             =over 4
30              
31             =item * B: L
32              
33             =item * B: L
34              
35             =back
36              
37             =cut
38              
39 5     5   17 method _configure { return }
  5         33  
40              
41             =head2 encode
42              
43             Encodes the given data to the type of the signature
44              
45             Usage:
46              
47             encode() -> encoded string
48              
49             =over 4
50              
51             =back
52              
53             ABI encoded hex string
54              
55             =cut
56              
57 11     11 1 21 method encode {
58              
59 11 100       28 return $self->_encoded if $self->_encoded;
60              
61 4         14 my $hex = unpack("H*", $self->data);
62              
63             # for dynamic length basic types the length must be included
64 4         27 $self->_push_dynamic($self->_encode_length(length(pack("H*", $hex))));
65 4         19 $self->_push_dynamic($self->pad_right($hex));
66              
67 4         11 return $self->_encoded;
68             }
69              
70             =head2 decode
71              
72             Decodes the given data to the type of the signature
73              
74             Usage:
75              
76             decoded() -> string
77              
78             =over 4
79              
80             =back
81              
82             String
83              
84             =cut
85              
86 1     1 1 2 method decode {
87              
88 1         5 my @data = $self->data->@*;
89              
90 1         3 my $size = hex shift @data;
91 1         4 my $string_data = join('', @data);
92 1         16 my $packed_string = pack("H*", $string_data);
93 1         6 return substr($packed_string, 0, $size);
94             }
95              
96             1;
97              
98             __END__