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 0 4 0.0
total 34 39 87.1


line stmt bran cond sub pod time code
1 5     5   109458 use v5.26;
  5         30  
2 5     5   750 use Object::Pad;
  5         11477  
  5         50  
3              
4             package Blockchain::Ethereum::Keystore::Address 0.005;
5             class Blockchain::Ethereum::Keystore::Address;
6              
7             =encoding utf8
8              
9             =head1 NAME
10              
11             Blockchain::Ethereum::Keystore::Address
12              
13             =head1 SYNOPSIS
14              
15             Address utilities
16              
17             my $address = Blockchain::Ethereum::Address->new(0x...);
18             print $address;
19             ...
20              
21             =cut
22              
23 5     5   1742 use Carp;
  5         13  
  5         396  
24 5     5   587 use Crypt::Digest::Keccak256 qw(keccak256_hex);
  5         4842  
  5         3416  
25              
26 90     90 0 154 field $address :reader :writer :param;
  90     18 0 536  
  18         45  
  18         210  
27              
28             ADJUST {
29              
30             my $unprefixed = $self->address =~ s/^0x//r;
31              
32             croak 'Invalid address format' unless length($unprefixed) == 40;
33              
34             my @hashed_chars = split //, keccak256_hex(lc $unprefixed);
35             my @address_chars = split //, $unprefixed;
36             my $checksummed_chars = '';
37              
38             $checksummed_chars .= hex $hashed_chars[$_] >= 8 ? uc $address_chars[$_] : lc $address_chars[$_] for 0 .. length($unprefixed) - 1;
39              
40             $self->set_address($checksummed_chars);
41             }
42              
43 18     18 0 57 method no_prefix {
44              
45 18         39 my $unprefixed = $self->address =~ s/^0x//r;
46 18         121 return $unprefixed;
47             }
48              
49             use overload
50 5         53 fallback => 1,
51 5     5   1416 '""' => \&to_string;
  5         1579  
52              
53 27     27 0 6968 method to_string {
54              
55 27 50       85 return $self->address if $self->address =~ /^0x/;
56 27         95 return '0x' . $self->address;
57             }
58              
59             1;
60              
61             __END__
62              
63             =head1 AUTHOR
64              
65             Reginaldo Costa, C<< <refeco at cpan.org> >>
66              
67             =head1 BUGS
68              
69             Please report any bugs or feature requests to L<https://github.com/refeco/perl-ethereum-keystore>
70              
71             =head1 LICENSE AND COPYRIGHT
72              
73             This software is Copyright (c) 2023 by REFECO.
74              
75             This is free software, licensed under:
76              
77             The MIT License
78              
79             =cut
80