File Coverage

blib/lib/Steemit/Base58.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 4 50.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 36 40 90.0


line stmt bran cond sub pod time code
1             package Steemit::Base58;
2 2     2   1506 use Modern::Perl;
  2         7427  
  2         14  
3              
4             =head1 NAME
5              
6             Steemit::Base58 - perl library for base58 encoding the bitcoin way
7              
8             =head1 SYNOPSIS
9              
10             use Steemit::Base58
11              
12             my $binary = Steemit::Base58::decode_base58( $base58_string )
13             my $base58 = Steemit::Base58::encode_base58( $binary )
14              
15             =cut
16              
17 2     2   2094 use Math::BigInt try => 'GMP,Pari';
  2         42711  
  2         11  
18 2     2   40586 use Carp;
  2         5  
  2         725  
19              
20             # except 0 O D / 1 l I
21             my $chars = [qw(
22             1 2 3 4 5 6 7 8 9
23              
24             A B C D E F G H J
25             K L M N P Q R S T
26             U V W X Y Z
27              
28             a b c d e f g h i
29             j k m n o p q r s
30             t u v w x y z
31              
32             )];
33             my $test = qr/^[@{[ join "", @$chars ]}]+$/;
34              
35             my $map = do {
36             my $i = 0;
37             +{ map { $_ => $i++ } @$chars };
38             };
39              
40             sub encode_base58 {
41 2     2 0 2848 my ($binary) = @_;
42 2 50       6 return $chars->[0] unless $binary;
43              
44 2         13 my $bigint = Math::BigInt->from_bytes($binary);
45 2         1968 my $base58 = '';
46 2         4 my $base = @$chars;
47              
48 2         7 while ($bigint->is_pos) {
49 70         2172 my ($quotient, $rest ) = $bigint->bdiv($base);
50 70         16054 $base58 = $chars->[$rest] . $base58;
51             }
52              
53 2         68 return $base58;
54             }
55              
56             sub decode_base58 {
57 5     5 0 1137 my $base58 = shift;
58 5         13 $base58 =~ tr/0OlI/DD11/;
59 5 50       41 $base58 =~ $test or croak "Invalid Base58";
60              
61 5         22 my $decoded = Math::BigInt->new(0);
62 5         584 my $multi = Math::BigInt->new(1);
63 5         163 my $base = @$chars;
64              
65 5         15 while (length $base58 > 0) {
66 223         25484 my $digit = chop $base58;
67 223         443 $decoded->badd($multi->copy->bmul($map->{$digit}));
68 223         42219 $multi->bmul($base);
69             }
70              
71 5         614 return $decoded->to_bytes;
72             }
73              
74              
75             1;
76              
77              
78             =head1 REPOSITORY
79              
80             L
81              
82              
83             =head1 AUTHOR
84              
85             snkoehn, C<< >>
86              
87             =head1 BUGS
88              
89             Please report any bugs or feature requests to C, or through
90             the web interface at L. I will be notified, and then you'll
91             automatically be notified of progress on your bug as I make changes.
92              
93              
94              
95              
96             =head1 SUPPORT
97              
98             You can find documentation for this module with the perldoc command.
99              
100             perldoc Steemit::WsClient
101              
102              
103             You can also look for information at:
104              
105             =over 4
106              
107             =item * RT: CPAN's request tracker (report bugs here)
108              
109             L
110              
111             =item * AnnoCPAN: Annotated CPAN documentation
112              
113             L
114              
115             =item * CPAN Ratings
116              
117             L
118              
119             =item * Search CPAN
120              
121             L
122              
123             =back
124              
125              
126             =head1 ACKNOWLEDGEMENTS
127              
128              
129             =head1 LICENSE AND COPYRIGHT
130              
131             Copyright 2018 snkoehn.
132              
133             This program is free software; you can redistribute it and/or modify it
134             under the terms of the the Artistic License (2.0). You may obtain a
135             copy of the full license at:
136              
137             L
138              
139             Any use, modification, and distribution of the Standard or Modified
140             Versions is governed by this Artistic License. By using, modifying or
141             distributing the Package, you accept this license. Do not use, modify,
142             or distribute the Package, if you do not accept this license.
143              
144             If your Modified Version has been derived from a Modified Version made
145             by someone other than you, you are nevertheless required to ensure that
146             your Modified Version complies with the requirements of this license.
147              
148             This license does not grant you the right to use any trademark, service
149             mark, tradename, or logo of the Copyright Holder.
150              
151             This license includes the non-exclusive, worldwide, free-of-charge
152             patent license to make, have made, use, offer to sell, sell, import and
153             otherwise transfer the Package with respect to any patent claims
154             licensable by the Copyright Holder that are necessarily infringed by the
155             Package. If you institute patent litigation (including a cross-claim or
156             counterclaim) against any party alleging that the Package constitutes
157             direct or contributory patent infringement, then this Artistic License
158             to you shall terminate on the date that such litigation is filed.
159              
160             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
161             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
162             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
163             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
164             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
165             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
166             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
167             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168              
169              
170             =cut
171