File Coverage

blib/lib/Blockchain/Ethereum/Keystore/Address.pm
Criterion Covered Total %
statement 24 24 100.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 2 4 50.0
total 36 39 92.3


line stmt bran cond sub pod time code
1 4     4   105014 use v5.26;
  4         24  
2 4     4   654 use Object::Pad;
  4         11589  
  4         24  
3              
4             package Blockchain::Ethereum::Keystore::Address;
5             class Blockchain::Ethereum::Keystore::Address;
6              
7             our $AUTHORITY = 'cpan:REFECO'; # AUTHORITY
8             our $VERSION = '0.007'; # VERSION
9              
10 4     4   1495 use Carp;
  4         8  
  4         264  
11 4     4   531 use Crypt::Digest::Keccak256 qw(keccak256_hex);
  4         4613  
  4         2733  
12              
13 90     90 0 169 field $address :reader :writer :param;
  90     18 0 542  
  18         48  
  18         224  
14              
15             ADJUST {
16              
17             my $unprefixed = $self->address =~ s/^0x//r;
18              
19             croak 'Invalid address format' unless length($unprefixed) == 40;
20              
21             my @hashed_chars = split //, keccak256_hex(lc $unprefixed);
22             my @address_chars = split //, $unprefixed;
23             my $checksummed_chars = '';
24              
25             $checksummed_chars .= hex $hashed_chars[$_] >= 8 ? uc $address_chars[$_] : lc $address_chars[$_] for 0 .. length($unprefixed) - 1;
26              
27             $self->set_address($checksummed_chars);
28             }
29              
30 18     18 1 51 method no_prefix {
31              
32 18         31 my $unprefixed = $self->address =~ s/^0x//r;
33 18         89 return $unprefixed;
34             }
35              
36             use overload
37 4         51 fallback => 1,
38 4     4   1365 '""' => \&to_string;
  4         1044  
39              
40 27     27 1 7304 method to_string {
41              
42 27 50       89 return $self->address if $self->address =~ /^0x/;
43 27         77 return '0x' . $self->address;
44             }
45              
46             1;
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             Blockchain::Ethereum::Keystore::Address
57              
58             =head1 VERSION
59              
60             version 0.007
61              
62             =head1 SYNOPSIS
63              
64             Import an existing address:
65              
66             my $address = Blockchain::Ethereum::Address->new(0x...);
67             # print checksummed address
68             print $address;
69              
70             Generate a new address:
71              
72             my $key = Blockchain::Ethereum::Key->new;
73             my $address = $key->address;
74              
75             =head1 METHODS
76              
77             =head2 no_prefix
78              
79             Returns the checksummed address without the 0x prefix
80              
81             =head2 to_string
82              
83             Returns the checksummed 0x prefixed address
84              
85             This function will be called as the default stringification method
86              
87             =head1 AUTHOR
88              
89             Reginaldo Costa <refeco@cpan.org>
90              
91             =head1 COPYRIGHT AND LICENSE
92              
93             This software is Copyright (c) 2023 by REFECO.
94              
95             This is free software, licensed under:
96              
97             The MIT (X11) License
98              
99             =cut