File Coverage

blib/lib/Bitcoin/Crypto.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 5 5 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Bitcoin::Crypto;
2             $Bitcoin::Crypto::VERSION = '1.008';
3 6     6   359389 use v5.10;
  6         68  
4 6     6   32 use strict;
  6         16  
  6         120  
5 6     6   39 use warnings;
  6         13  
  6         185  
6 6     6   32 use Exporter qw(import);
  6         10  
  6         1418  
7              
8             our @EXPORT_OK = qw(btc_extprv btc_prv btc_extpub btc_pub btc_script);
9             our %EXPORT_TAGS = (all => [@EXPORT_OK]);
10              
11             sub btc_extprv
12             {
13 19     19 1 27490 require Bitcoin::Crypto::Key::ExtPrivate;
14 19         119 return "Bitcoin::Crypto::Key::ExtPrivate";
15             }
16              
17             sub btc_prv
18             {
19 5     5 1 4424 require Bitcoin::Crypto::Key::Private;
20 5         34 return "Bitcoin::Crypto::Key::Private";
21             }
22              
23             sub btc_extpub
24             {
25 7     7 1 1336 require Bitcoin::Crypto::Key::ExtPublic;
26 7         36 return "Bitcoin::Crypto::Key::ExtPublic";
27             }
28              
29             sub btc_pub
30             {
31 5     5 1 2938 require Bitcoin::Crypto::Key::Public;
32 5         30 return "Bitcoin::Crypto::Key::Public";
33             }
34              
35             sub btc_script
36             {
37 1     1 1 826 require Bitcoin::Crypto::Script;
38 1         3 return "Bitcoin::Crypto::Script";
39             }
40              
41             __END__
42             =head1 NAME
43              
44             Bitcoin::Crypto - Bitcoin cryptography in Perl
45              
46             =head1 SYNOPSIS
47              
48             use Bitcoin::Crypto qw(btc_extprv);
49              
50             # extended keys are used for mnemonic generation and key derivation
51             my $mnemonic = btc_extprv->generate_mnemonic();
52             say "your mnemonic code is: $mnemonic";
53              
54             my $master_key = btc_extprv->from_mnemonic($mnemonic);
55             my $derived_key = $master_key->derive_key("m/0'");
56              
57             # basic keys are used for signatures and addresses
58             my $priv = $derived_key->get_basic_key();
59             my $pub = $priv->get_public_key();
60              
61             say "private key: " . $priv->to_wif();
62             say "public key: " . $pub->to_hex();
63             say "address: " . $pub->get_segwit_address();
64              
65             my $message = "Hello CPAN";
66             my $signature = $priv->sign_message($message);
67              
68             if ($pub->verify_message($message, $signature)) {
69             say "successfully signed message '$message'";
70             say "signature: " . unpack "H*", $signature;
71             }
72              
73             =head1 DESCRIPTION
74              
75             Cryptographic module for common Bitcoin-related tasks and key pair management.
76              
77             =head1 SCOPE
78              
79             This module allows you to do basic tasks for Bitcoin such as:
80              
81             =over 2
82              
83             =item * creating extended keys and utilizing bip32 key derivation
84              
85             =item * creating private key / public key pairs
86              
87             =item * address generation (in legacy, compatibility and segwit formats)
88              
89             =item * signature generation and verification
90              
91             =item * importing / exporting using popular mediums (WIF, mnemonic, hex)
92              
93             =item * using custom (non-Bitcoin) networks
94              
95             =back
96              
97             This module won't help you with:
98              
99             =over 2
100              
101             =item * serializing transactions
102              
103             =item * using any Bitcoin CLI tools / clients
104              
105             =item * connecting to Bitcoin network
106              
107             =back
108              
109             =head1 WHERE TO START?
110              
111             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.
112              
113             If you like to learn by example, dive right into the examples directory.
114              
115             There are many things that you may want to achieve with this module. Common topics include:
116              
117             =over 2
118              
119             =item * create a key pair for signature or address generation
120              
121             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.
122              
123             =item * generate many keys at once
124              
125             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!)>
126              
127             =item * work with other cryptocurrencies
128              
129             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.
130              
131             =item * serialize a Bitcoin script
132              
133             L<Bitcoin::Crypto::Script> will help you build and serialize a script, but not run it.
134              
135             =item * work with Bitcoin-related encodings
136              
137             See L<Bitcoin::Crypto::Base58> and L<Bitcoin::Crypto::Bech32>.
138              
139             =back
140              
141             =head1 HOW TO READ THE DOCUMENTATION?
142              
143             Most functions in this documentation have a code line showcasing the arguments used by the function.
144              
145             These lines are not meant to be valid perl. They're there for you to understand what arguments the function expects.
146              
147             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.
148              
149             =head1 SHORTCUT FUNCTIONS
150              
151             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:
152              
153             use Bitcoin::Crypto qw(btc_pub);
154              
155             # loads Bitcoin::Crypto::Key::Public and returns package name
156             # we can now use it to run its methods
157             my $public_key = btc_pub->from_hex($hex_data);
158              
159             =head2 btc_extprv
160              
161             Loads L<Bitcoin::Crypto::Key::ExtPrivate>
162              
163             =head2 btc_prv
164              
165             Loads L<Bitcoin::Crypto::Key::Private>
166              
167             =head2 btc_extpub
168              
169             Loads L<Bitcoin::Crypto::Key::ExtPublic>
170              
171             =head2 btc_pub
172              
173             Loads L<Bitcoin::Crypto::Key::Public>
174              
175             =head2 btc_script
176              
177             Loads L<Bitcoin::Crypto::Script>
178              
179             =head1 DISCLAIMER
180              
181             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.
182              
183             =head1 SPEED
184              
185             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.
186             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).
187              
188             =head1 TODO
189              
190             I will gladly accept help working on these:
191              
192             =over 2
193              
194             =item * Taproot compatibility
195              
196             =item * Better error checking (subroutine inputs, edge cases etc.)
197              
198             =item * Detailed manual
199              
200             =item * Better test coverage
201              
202             =back
203              
204             =head1 SEE ALSO
205              
206             =over
207              
208             =item L<Bitcoin::RPC::Client>
209              
210             =item L<https://github.com/bitcoin/bips>
211              
212             =back
213              
214             =head1 AUTHOR
215              
216             Bartosz Jarzyna E<lt>bbrtj.pro@gmail.comE<gt>
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             Copyright (C) 2018 - 2023 by Bartosz Jarzyna
221              
222             This library is free software; you can redistribute it and/or modify
223             it under the same terms as Perl itself.
224              
225             =cut
226