File Coverage

blib/lib/Bitcoin/Crypto/Base58.pm
Criterion Covered Total %
statement 38 38 100.0
branch 4 4 100.0
condition n/a
subroutine 12 12 100.0
pod 4 5 80.0
total 58 59 98.3


line stmt bran cond sub pod time code
1             package Bitcoin::Crypto::Base58;
2             $Bitcoin::Crypto::Base58::VERSION = '1.008_01'; # TRIAL
3             $Bitcoin::Crypto::Base58::VERSION = '1.00801';
4 13     13   153439 use v5.10;
  13         71  
5 13     13   137 use strict;
  13         64  
  13         340  
6 13     13   69 use warnings;
  13         28  
  13         421  
7 13     13   73 use Exporter qw(import);
  13         25  
  13         493  
8 13     13   1969 use Crypt::Misc qw(encode_b58b decode_b58b);
  13         38825  
  13         909  
9              
10 13     13   2791 use Bitcoin::Crypto::Helpers qw(new_bigint hash256 verify_bytestring);
  13         49  
  13         951  
11 13     13   99 use Bitcoin::Crypto::Exception;
  13         36  
  13         5342  
12              
13             our @EXPORT_OK = qw(
14             encode_base58
15             encode_base58check
16             decode_base58
17             decode_base58check
18             );
19              
20             our %EXPORT_TAGS = (all => [@EXPORT_OK]);
21              
22             my $CHECKSUM_SIZE = 4;
23              
24             sub encode_base58
25             {
26 168     168 1 362 my ($bytes) = @_;
27 168         549 verify_bytestring($bytes);
28              
29 168         705 return encode_b58b($bytes);
30             }
31              
32             sub encode_base58check
33             {
34 168     168 1 762 my ($bytes) = @_;
35 168         594 my $checksum = pack('a' . $CHECKSUM_SIZE, hash256($bytes));
36 168         2622 return encode_base58($bytes . $checksum);
37             }
38              
39             sub decode_base58
40             {
41 66     66 1 5011 my ($base58encoded) = @_;
42 66         253 my $decoded = decode_b58b($base58encoded);
43 66 100       8371 Bitcoin::Crypto::Exception::Base58InputFormat->raise(
44             'illegal characters in base58 string'
45             ) unless defined $decoded;
46              
47 63         171 return $decoded;
48             }
49              
50             sub verify_checksum
51             {
52 58     58 0 138 my ($decoded) = @_;
53 58         160 my $encoded_val = substr $decoded, 0, -$CHECKSUM_SIZE;
54 58         153 my $checksum = substr $decoded, -$CHECKSUM_SIZE;
55 58         248 return unpack('a' . $CHECKSUM_SIZE, hash256($encoded_val)) eq $checksum;
56             }
57              
58             sub decode_base58check
59             {
60 60     60 1 7396 my ($base58encoded) = @_;
61 60         156 my $decoded = decode_base58($base58encoded);
62              
63 58 100       193 Bitcoin::Crypto::Exception::Base58InputChecksum->raise(
64             'incorrect base58check checksum'
65             ) unless verify_checksum($decoded);
66              
67 55         1169 return substr $decoded, 0, -$CHECKSUM_SIZE;
68             }
69              
70             1;
71              
72             __END__
73             =head1 NAME
74              
75             Bitcoin::Crypto::Base58 - Bitcoin's Base58 helpers
76              
77             =head1 SYNOPSIS
78              
79             # none exported by default
80             use Bitcoin::Crypto::Base58 qw(
81             encode_base58
82             decode_base58
83             encode_base58check
84             decode_base58check
85             );
86              
87             my $b58str = encode_base58check(pack 'A*', 'hello');
88             my $bytestr = decode_base58check($b58str);
89              
90             =head1 DESCRIPTION
91              
92             Implementation of Base58Check algorithm and alias to CryptX C<encode_b58b> / C<decode_b58b>
93              
94             =head1 FUNCTIONS
95              
96             This module is based on Exporter. None of the functions are exported by default. C<:all> tag exists that exports all the functions at once.
97              
98             =head2 encode_base58
99              
100             =head2 decode_base58
101              
102             Basic base58 encoding / decoding.
103              
104             Encoding takes one argument which is byte string.
105              
106             Decoding takes base58-encoded string
107              
108             These two functions are just aliases to L<Crypt::Misc/encode_b58b> and
109             L<Crypt::Misc/decode_b58b> with some error checking.
110              
111             =head2 encode_base58check
112              
113             =head2 decode_base58check
114              
115             Base58 with checksum validation. These functions are used with Bitcoin legacy /
116             compatibility addresses.
117              
118             Arguments are the same as base functions mentioned above.
119              
120             Additional errors (other than illegal characters) are thrown.
121              
122             =head1 EXCEPTIONS
123              
124             This module throws an instance of L<Bitcoin::Crypto::Exception> if it encounters an error. It can produce the following error types from the L<Bitcoin::Crypto::Exception> namespace:
125              
126             =over 2
127              
128             =item * Base58InputFormat - input was not suitable for base58 operations due to invalid format
129              
130             =item * Base58InputChecksum - checksum validation has failed
131              
132             =back
133              
134             =head1 SEE ALSO
135              
136             =over 2
137              
138             =item L<Crypt::Misc>
139              
140             =item L<Bitcoin::Crypto::Key::Private>
141              
142             =item L<Bitcoin::Crypto::Key::Public>
143              
144             =back
145              
146             =cut
147