Libdogecoin/FFI.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 100% 74 / 74 CC 4 SCAR 3.2
CC
4
Cov
100%
generate_key_pair
2.0
generate_hd_master_pub_key_pair
2.0
verify_master_priv_pub_keypair
2.0
CRAP 1.4
↑ Summary Libdogecoin.pm »
1 package Finance::Libdogecoin::FFI;
2 # ABSTRACT: Use features from libdogecoin via FFI
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use FFI::Platypus;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use FFI::CheckLib 0.28 'find_lib_or_die';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use FFI::Platypus::Buffer qw( buffer_to_scalar grow set_used_length );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Alien::Libdogecoin;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 {
14 1 my $ffi = FFI::Platypus->new(
15 api => 1,
16 lib => [
17 find_lib_or_die lib => 'dogecoin',
18 alien => [ 'Alien::Libdogecoin' ],
19 ],
20 );
21  
22 1 ▶ while (my $sig = <DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $sig = readline DATA)) 8 1 9
23 8 chomp $sig;
24 8 my ($return, $name, @args) = split /,/, $sig;
25 8 $ffi->attach( $name => \@args => $return );
26 }
27  
28 1 *context_start = \&dogecoin_ecc_start;
29 1 *context_stop = \&dogecoin_ecc_stop;
30 }
31  
32 sub _call_with_buffer_pair {
33 7 ▶ my ($is_testnet, $priv_buffer_len, $pub_buffer_len, $function) = @_;
Subroutine _call_with_buffer_pair: called CC=1 SCAR=0.0
34  
35 7 grow( (my $privkeybuf), $priv_buffer_len );
36 7 grow( (my $pubkeybuf), $pub_buffer_len + 1);
37  
38 7 $function->($privkeybuf, $pubkeybuf, $is_testnet);
39  
40 7 return( substr($privkeybuf, 0, $priv_buffer_len), substr($pubkeybuf, 0, $pub_buffer_len) );
41 }
42  
43 sub generate_key_pair {
44 3 ▶ my $is_testnet = shift ? 1 : 0;
Subroutine generate_key_pair: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 1 2 3
45 3 return _call_with_buffer_pair( $is_testnet, 53, 34, \&generatePrivPubKeypair );
46 }
47  
48 sub verify_key_pair {
49 6 ▶ my ($priv_key, $pub_key, $is_testnet) = @_;
Subroutine verify_key_pair: called CC=1 SCAR=0.0
Pod documented
50  
51 6 return verifyPrivPubKeypair( $priv_key, $pub_key, !!$is_testnet );
52 }
53  
54 sub verify_p2pkh_address {
55 3 ▶ my $address = shift;
Subroutine verify_p2pkh_address: called CC=1 SCAR=0.0
Pod documented
56 3 return !! verifyP2pkhAddress( $address, length($address) );
57 }
58  
59 sub generate_hd_master_pub_key_pair {
60 4 ▶ my $chaincode = shift ? 1 : 0;
Subroutine generate_hd_master_pub_key_pair: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 2 2 4
61 4 return _call_with_buffer_pair( $chaincode, 128, 35, \&generateHDMasterPubKeypair );
62 }
63  
64 sub generate_derived_hd_pub_key {
65 2 ▶ my $master_priv_key = shift;
Subroutine generate_derived_hd_pub_key: called CC=1 SCAR=0.0
Pod documented
66 2 grow my $pubkeybuf, 128;
67 2 generateDerivedHDPubkey($master_priv_key, $pubkeybuf);
68  
69 2 return substr($pubkeybuf, 0, 128);
70 }
71  
72 sub verify_master_priv_pub_keypair {
73 8 ▶ my ($priv_key, $pub_key, $chaincode) = @_;
Subroutine verify_master_priv_pub_keypair: called CC=2 SCAR=6.9
Pod documented
74 8 ▶ $chaincode = $chaincode ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$chaincode ? : 4 4 8
75  
76 8 return verifyHDMasterPubKeypair( $priv_key, $pub_key, $chaincode );
77 }
78  
79 1 'much wow';
80  
81 =pod
82  
83 =encoding UTF-8
84  
85 =head1 NAME
86  
87 Finance::Libdogecoin::FFI - Use features from libdogecoin via FFI
88  
89 =head1 VERSION
90  
91 version 1.20220814.2215
92  
93 =head2 SYNOPSIS
94  
95 To generate and verify a private/public keypair:
96  
97 # call this before all key-manipulation functions
98 Finance::Libdogecoin::FFI::context_start();
99  
100 my ($priv_key, $pub_key) = Finance::Libdogecoin::FFI::generate_key_pair();
101  
102 if (Finance::Libdogecoin::FFI::verify_key_pair( $priv_key, $pub_key )) {
103 # ... key pair is valid
104 }
105  
106 # call this after all key-manipulation functions
107 Finance::Libdogecoin::FFI::context_stop();
108  
109 From this example be aware of three things:
110  
111 =over 4
112  
113 =item * First, call C<context_start()> before and C<context_stop()> after any
114 use of the key-manipulation functions. These start and finish the cryptographic
115 context used in the underlying C library.
116  
117 =item * Second, these function are available from the
118 C<Finance::Libdogecoin::FFI> namespace and are not exported by default.
119  
120 =item * Third, these functions are minimally Perlish. A nicer interface should/will exist.
121  
122 =back
123  
124 =head2 DESCRIPTION
125  
126 This module provides a minimal FFI interface to C<libdogecoin> functions. It
127 uses L<Alien::Libdogecoin> to use a local installation of this library or the
128 system version.
129  
130 See
131 L<https://github.com/dogecoinfoundation/libdogecoin/blob/main/doc/address.md>
132 for full documentation of the C<libdogecoin> library, including the principles
133 behind the design. The API of this module follows the example of the Python
134 bindings, though the C function calls are also available in this namespace.
135  
136 =head2 FUNCTIONS
137  
138 =head3 C<context_start()>
139  
140 Initializes the C<libdogecoin> cryptography mechanism. Call this before using
141 any other key-management functions.
142  
143 =head3 C<context_start()>
144  
145 Finishes the C<libdogecoin> cryptography mechanism. Call this when you no
146 longer need to use any other key-management functions.
147  
148 =head3 C<generate_key_pair( $is_testnet )>
149  
150 The boolean C<$is_testnet> argument is optional; the default value is false.
151  
152 Generates and returns a private/public key pair for the network. The return value
153 is two strings in base-58 encoding. Do not share the private key. Do not lose
154 the private key.
155  
156 The public key is a valid P2PKH address.
157  
158 =head3 C<verify_key_pair( $private_key, $public_key, $is_testnet )>
159  
160 The boolean C<$is_testnet> argument is optional; the default value is false.
161  
162 Given two strings in base-58 encoding, returns a boolean value to indicate
163 whether the pair is valid for the given network.
164  
165 =head3 C<verify_p2pkh_address( $address )>
166  
167 Given a public key, returns a boolean to indicate whether the address passes
168 validation checks. This doesn't guarantee that an address I<is> legitimate, but
169 it can help decide whether it's worth sending to it at all.
170  
171 =head3 C<generate_hd_master_pub_key_pair( $is_testnet )>
172  
173 The boolean C<$is_testnet> argument is optional; the default value is false.
174  
175 Generates and returns a private/public key hierarchical deterministic pair for
176 the network. The return value is two strings in base-58 encoding. Do not share
177 the private key. Do not lose the private key.
178  
179 =head3 C<generate_derived_hd_pub_key( $master_private_key )>
180  
181 Given a master key hierarchical deterministic key, derive and return a public
182 key associated with the master.
183  
184 =head3 C<verify_master_priv_pub_keypair( $master_private_key, $master_public_key, $is_testnet )>
185  
186 The boolean C<$is_testnet> argument is optional; the default value is false.
187  
188 Given two strings representing hierarchical deterministic keys (a master
189 public key and a master private key) in base-58 encoding, returns a boolean
190 value to indicate whether the pair both match and are valid for the given
191 network. In other words, for the given network, can the public key be derived
192 from the private key?
193  
194 =head2 AUTHOR, COPYRIGHT, and LICENSE
195  
196 Copyright (c) 2022, chromatic. Interface based on and derived from the C<libdogecoin> Python bindings.
197  
198 =head1 AUTHOR
199  
200 chromatic <chromatic@wgz.org>
201  
202 =head1 COPYRIGHT AND LICENSE
203  
204 This software is Copyright (c) 2022 by chromatic.
205  
206 This is free software, licensed under:
207  
208 The MIT (X11) License
209  
210 =cut
211  
212 __DATA__
↑ Summary Libdogecoin.pm »