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';
3 13     13   155890 use v5.10;
  13         80  
4 13     13   70 use strict;
  13         32  
  13         253  
5 13     13   69 use warnings;
  13         24  
  13         447  
6 13     13   102 use Exporter qw(import);
  13         28  
  13         526  
7 13     13   1973 use Crypt::Misc qw(encode_b58b decode_b58b);
  13         37928  
  13         897  
8              
9 13     13   3162 use Bitcoin::Crypto::Helpers qw(new_bigint hash256 verify_bytestring);
  13         38  
  13         1331  
10 13     13   101 use Bitcoin::Crypto::Exception;
  13         77  
  13         4820  
11              
12             our @EXPORT_OK = qw(
13             encode_base58
14             encode_base58check
15             decode_base58
16             decode_base58check
17             );
18              
19             our %EXPORT_TAGS = (all => [@EXPORT_OK]);
20              
21             my $CHECKSUM_SIZE = 4;
22              
23             sub encode_base58
24             {
25 168     168 1 354 my ($bytes) = @_;
26 168         528 verify_bytestring($bytes);
27              
28 168         656 return encode_b58b($bytes);
29             }
30              
31             sub encode_base58check
32             {
33 168     168 1 809 my ($bytes) = @_;
34 168         581 my $checksum = pack("a" . $CHECKSUM_SIZE, hash256($bytes));
35 168         2556 return encode_base58($bytes . $checksum);
36             }
37              
38             sub decode_base58
39             {
40 66     66 1 4433 my ($base58encoded) = @_;
41 66         217 my $decoded = decode_b58b($base58encoded);
42 66 100       8164 Bitcoin::Crypto::Exception::Base58InputFormat->raise(
43             "illegal characters in base58 string"
44             ) unless defined $decoded;
45              
46 63         168 return $decoded;
47             }
48              
49             sub verify_checksum
50             {
51 58     58 0 145 my ($decoded) = @_;
52 58         177 my $encoded_val = substr $decoded, 0, -$CHECKSUM_SIZE;
53 58         137 my $checksum = substr $decoded, -$CHECKSUM_SIZE;
54 58         244 return unpack("a" . $CHECKSUM_SIZE, hash256($encoded_val)) eq $checksum;
55             }
56              
57             sub decode_base58check
58             {
59 60     60 1 7112 my ($base58encoded) = @_;
60 60         157 my $decoded = decode_base58($base58encoded);
61              
62 58 100       186 Bitcoin::Crypto::Exception::Base58InputChecksum->raise(
63             "incorrect base58check checksum"
64             ) unless verify_checksum($decoded);
65              
66 55         1264 return substr $decoded, 0, -$CHECKSUM_SIZE;
67             }
68              
69             1;
70              
71             __END__
72             =head1 NAME
73              
74             Bitcoin::Crypto::Base58 - Bitcoin's Base58 helpers
75              
76             =head1 SYNOPSIS
77              
78             # none exported by default
79             use Bitcoin::Crypto::Base58 qw(
80             encode_base58
81             decode_base58
82             encode_base58check
83             decode_base58check
84             );
85              
86             my $b58str = encode_base58check(pack "A*", "hello");
87             my $bytestr = decode_base58check($b58str);
88              
89             =head1 DESCRIPTION
90              
91             Implementation of Base58Check algorithm and alias to CryptX C<encode_b58b> / C<decode_b58b>
92              
93             =head1 FUNCTIONS
94              
95             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.
96              
97             =head2 encode_base58
98              
99             =head2 decode_base58
100              
101             Basic base58 encoding / decoding.
102              
103             Encoding takes one argument which is byte string.
104              
105             Decoding takes base58-encoded string
106              
107             These two functions are just aliases to L<Crypt::Misc/encode_b58b> and
108             L<Crypt::Misc/decode_b58b> with some error checking.
109              
110             =head2 encode_base58check
111              
112             =head2 decode_base58check
113              
114             Base58 with checksum validation. These functions are used with Bitcoin legacy /
115             compatibility addresses.
116              
117             Arguments are the same as base functions mentioned above.
118              
119             Additional errors (other than illegal characters) are thrown.
120              
121             =head1 EXCEPTIONS
122              
123             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:
124              
125             =over 2
126              
127             =item * Base58InputFormat - input was not suitable for base58 operations due to invalid format
128              
129             =item * Base58InputChecksum - checksum validation has failed
130              
131             =back
132              
133             =head1 SEE ALSO
134              
135             =over 2
136              
137             =item L<Crypt::Misc>
138              
139             =item L<Bitcoin::Crypto::Key::Private>
140              
141             =item L<Bitcoin::Crypto::Key::Public>
142              
143             =back
144              
145             =cut
146