File Coverage

blib/lib/Bitcoin/Crypto.pm
Criterion Covered Total %
statement 26 26 100.0
branch n/a
condition n/a
subroutine 14 14 100.0
pod 6 10 60.0
total 46 50 92.0


line stmt bran cond sub pod time code
1             package Bitcoin::Crypto;
2             $Bitcoin::Crypto::VERSION = '1.008_01'; # TRIAL
3             $Bitcoin::Crypto::VERSION = '1.00801';
4 6     6   366169 use v5.10;
  6         64  
5 6     6   34 use strict;
  6         12  
  6         121  
6 6     6   28 use warnings;
  6         15  
  6         177  
7 6     6   30 use Exporter qw(import);
  6         10  
  6         1982  
8              
9             our @EXPORT_OK = qw(btc_extprv btc_prv btc_extpub btc_pub btc_script);
10             our %EXPORT_TAGS = (all => [@EXPORT_OK]);
11              
12             sub btc_extprv
13             {
14 19     19 1 28168 require Bitcoin::Crypto::Key::ExtPrivate;
15 19         130 return 'Bitcoin::Crypto::Key::ExtPrivate';
16             }
17              
18             sub btc_prv
19             {
20 5     5 1 4641 require Bitcoin::Crypto::Key::Private;
21 5         42 return 'Bitcoin::Crypto::Key::Private';
22             }
23              
24             sub btc_extpub
25             {
26 7     7 1 59 require Bitcoin::Crypto::Key::ExtPublic;
27 7         42 return 'Bitcoin::Crypto::Key::ExtPublic';
28             }
29              
30             sub btc_pub
31             {
32 5     5 1 2981 require Bitcoin::Crypto::Key::Public;
33 5         35 return 'Bitcoin::Crypto::Key::Public';
34             }
35              
36             sub btc_script
37             {
38 1     1 1 7 require Bitcoin::Crypto::Script;
39 1         4 return 'Bitcoin::Crypto::Script';
40             }
41              
42             # OO interface
43              
44 1     1 0 889 sub extprv { goto \&btc_extprv }
45 1     1 0 112 sub prv { goto \&btc_prv }
46 1     1 0 548 sub extpub { goto \&btc_extpub }
47 1     1 0 568 sub pub { goto \&btc_pub }
48 1     1 1 926 sub script { goto \&btc_script }
49              
50             __END__
51              
52             =head1 NAME
53              
54             Bitcoin::Crypto - Bitcoin cryptography in Perl
55              
56             =head1 SYNOPSIS
57              
58             use Bitcoin::Crypto qw(btc_extprv);
59              
60             # extended keys are used for mnemonic generation and key derivation
61             my $mnemonic = btc_extprv->generate_mnemonic();
62             say "your mnemonic code is: $mnemonic";
63              
64             my $master_key = btc_extprv->from_mnemonic($mnemonic);
65             my $derived_key = $master_key->derive_key("m/0'");
66              
67             # basic keys are used for signatures and addresses
68             my $priv = $derived_key->get_basic_key();
69             my $pub = $priv->get_public_key();
70              
71             say 'private key: ' . $priv->to_wif();
72             say 'public key: ' . $pub->to_hex();
73             say 'address: ' . $pub->get_segwit_address();
74              
75             my $message = 'Hello CPAN';
76             my $signature = $priv->sign_message($message);
77              
78             if ($pub->verify_message($message, $signature)) {
79             say "successfully signed message '$message'";
80             say 'signature: ' . unpack 'H*', $signature;
81             }
82              
83             =head1 DESCRIPTION
84              
85             Cryptographic module for common Bitcoin-related tasks and key pair management.
86              
87             =head1 SCOPE
88              
89             This module allows you to do basic tasks for Bitcoin such as:
90              
91             =over 2
92              
93             =item * creating extended keys and utilizing bip32 key derivation
94              
95             =item * creating private key / public key pairs
96              
97             =item * address generation (in legacy, compatibility and segwit formats)
98              
99             =item * signature generation and verification
100              
101             =item * importing / exporting using popular mediums (WIF, mnemonic, hex)
102              
103             =item * using custom (non-Bitcoin) networks
104              
105             =back
106              
107             This module won't help you with:
108              
109             =over 2
110              
111             =item * serializing transactions
112              
113             =item * using any Bitcoin CLI tools / clients
114              
115             =item * connecting to Bitcoin network
116              
117             =back
118              
119             =head1 WHERE TO START?
120              
121             Documentation and examples in this module assume you're already familiar with the basics of Bitcoin protocol and asymmetric cryptography. If that's not the case, start with learning about those topics.
122              
123             If you like to learn by example, dive right into the examples directory.
124              
125             There are many things that you may want to achieve with this module. Common topics include:
126              
127             =over 2
128              
129             =item * create a key pair for signature or address generation
130              
131             Start with L<Bitcoin::Crypto::Key::Private> if you already have some data you want to use as a private key entropy (like Bitcoin's WIF format or hex data). If you'd like to generate a key and get a list of words (a mnemonic), L<Bitcoin::Crypto::Key::ExtPrivate> is what you want.
132              
133             =item * generate many keys at once
134              
135             L<Bitcoin::Crypto::Key::ExtPrivate> will allow you to derive as many keys as you want from a master key, so you won't have to store multiple private key seeds. L<Bitcoin::Crypto::Key::ExtPublic> can be then used to derive public keys lazily. I<(Note: storing extended public keys together with private keys in a hot storage will put your extended private key at risk!)>
136              
137             =item * work with other cryptocurrencies
138              
139             You can work with any cryptocurrency as long as it is based on the same fundamentals as Bitcoin. You have to register a network in L<Bitcoin::Crypto::Network> first, with the protocol data valid for your cryptocurrency.
140              
141             =item * serialize a Bitcoin script
142              
143             L<Bitcoin::Crypto::Script> will help you build and serialize a script, but not run it.
144              
145             =item * work with Bitcoin-related encodings
146              
147             See L<Bitcoin::Crypto::Base58> and L<Bitcoin::Crypto::Bech32>.
148              
149             =back
150              
151             =head1 HOW TO READ THE DOCUMENTATION?
152              
153             Most functions in this documentation have a code line showcasing the arguments used by the function.
154              
155             These lines are not meant to be valid perl. They're there for you to understand what arguments the function expects.
156              
157             Most packages in this module have the types of their thrown exceptions documented near the bottom of the document. The exceptions section may be useful to understand which types of exceptions can be thrown when using functions or methods from the package and what they mean. It is not meant to be a full list of exceptions a function can throw and unblessed errors may still be raised.
158              
159             =head1 SHORTCUT FUNCTIONS
160              
161             =head2 Exported interface
162              
163             This package exports the following functions when asked for them. They are shourtcut functions and will load needed packages and return their names. You can then use names of loaded packages to instantiate them however you want. You can also load all of them with the I<:all> tag in import. These functions can be used as follows:
164              
165             use Bitcoin::Crypto qw(btc_pub);
166              
167             # loads Bitcoin::Crypto::Key::Public and returns package name
168             # we can now use it to run its methods
169             my $public_key = btc_pub->from_hex($hex_data);
170              
171             =head3 btc_extprv
172              
173             Loads L<Bitcoin::Crypto::Key::ExtPrivate>
174              
175             =head3 btc_prv
176              
177             Loads L<Bitcoin::Crypto::Key::Private>
178              
179             =head3 btc_extpub
180              
181             Loads L<Bitcoin::Crypto::Key::ExtPublic>
182              
183             =head3 btc_pub
184              
185             Loads L<Bitcoin::Crypto::Key::Public>
186              
187             =head3 btc_script
188              
189             Loads L<Bitcoin::Crypto::Script>
190              
191             =head2 OO interface
192              
193             Alternatively, all function names mentioned above can be used without the C<btc_> prefix in OO style on the package's name.
194              
195             use Bitcoin::Crypto;
196              
197             # loads Bitcoin::Crypto::Key::Public and returns package name
198             # we can now use it to run its methods
199             my $public_key = Bitcoin::Crypto->pub->from_hex($hex_data);
200              
201             =head1 DISCLAIMER
202              
203             Although the module was written with an extra care and appropriate tests are in place asserting compatibility with many Bitcoin standards, due to complexity of the subject some bugs may still be present. In the world of digital money, a single bug may lead to losing funds. I encourage anyone to test the module themselves, review the test cases and use the module with care. Suggestions for improvements and more edge cases to test will be gladly accepted, but there is no warranty on your funds being manipulated by this module.
204              
205             =head1 SPEED
206              
207             Since most of the calculations are delegated to the XS (and further to libtommath and libtomcrypt) most tasks should be fairly quick to finish, in Perl definition of quick.
208             The module have a little bit of startup time because of Moo and Type::Tiny, measured in miliseconds. The biggest runtime bottleneck seem to be the key derivation mechanism, which imports a key once for every derivation path part. Some tasks, like signature generation and verification, should be very fast thanks to libtomcrypt doing all the heavy lifting. All in all, the module should be able to handle any task which does not require brute forcing (like vanity address generation).
209              
210             =head1 TODO
211              
212             I will gladly accept help working on these:
213              
214             =over 2
215              
216             =item * Taproot compatibility
217              
218             =item * Better error checking (subroutine inputs, edge cases etc.)
219              
220             =item * Detailed manual
221              
222             =item * Better test coverage
223              
224             =back
225              
226             =head1 SEE ALSO
227              
228             =over
229              
230             =item L<Bitcoin::RPC::Client>
231              
232             =item L<https://github.com/bitcoin/bips>
233              
234             =back
235              
236             =head1 AUTHOR
237              
238             Bartosz Jarzyna E<lt>bbrtj.pro@gmail.comE<gt>
239              
240             =head1 COPYRIGHT AND LICENSE
241              
242             Copyright (C) 2018 - 2023 by Bartosz Jarzyna
243              
244             This library is free software; you can redistribute it and/or modify
245             it under the same terms as Perl itself.
246              
247             =cut
248