blib/lib/Bitcoin/RPC/Client.pm

filter: Statementstmt 63.2% 50 / 79 Branchbran 33.3% 10 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 1 totaltotal 53.6% 67 / 125 CC 17 SCAR 31.9
CC
17
Cov
54%
AUTOLOAD
36.2
isa_cookie
30.0
__ANON__
2.0
CRAP 24.2
↑ Summary
1 package Bitcoin::RPC::Client;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::RPC::Legacy::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.12';
12  
13 1 ▶has jsonrpc => (is => "lazy", default => sub { "JSON::RPC::Legacy::Client"->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
14 2 has user => (is => 'ro');
15 2 has password => (is => 'ro');
16 2 has cookie => (is => 'ro', isa => \&isa_cookie);
17 2 has host => (is => 'ro');
18 2 has wallet => (is => 'ro');
19 2 has port => (is => "lazy", default => 8332);
20 2 has timeout => (is => "lazy", default => 20);
21 2 has debug => (is => "lazy", default => 0);
22  
23 # SSL constructor options
24 # OpenSSL support has been removed from Bitcoin Core as of v0.12.0
25 # but should work with older versions
26 2 has ssl => (is => 'ro', default => 0);
27 2 has verify_hostname => (is => 'ro', default => 1);
28  
29 2 my $DEBUG_DUMPED = 0;
30  
31 2 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=13 SCAR=35.9
32 2 my $self = shift;
33 2 my $method = $Bitcoin::RPC::Client::AUTOLOAD;
34  
35 2 $method =~ s/.*:://;
36  
37 2 ▶ return if ($method eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 1 1 2
38  
39 # Build request URL
40 1 my $url = "";
41  
42 # Are we using SSL?
43 1 my $uri = "http://";
44 1 ▶ if ($self->ssl eq 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssl eq 1) 0 1 1
45 0 $uri = "https://";
46 }
47  
48 # Cookie will take precedence over user/password
49 1 ▶ if ($self->cookie) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->cookie) { } 0 1 1
elsif ($self->user) { } 1 0 1
50 # If cookie is defined its contents become user:password
51 0 $url = $uri . $self->cookie . "\@" . $self->host . ":" . $self->port;
52 } elsif ($self->user) {
53 1 $url = $uri . $self->user . ":" . $self->password . "\@" . $self->host . ":" . $self->port;
54 } else {
55 0 die "An RPC user or RPC cookie file must be defined\n";
56 }
57  
58 # Tack on a specific wallet name if given
59 1 ▶ if ($self->wallet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wallet) 0 1 1
60 0 $url .= "/wallet/" . $self->wallet;
61 }
62  
63 1 my $client = $self->jsonrpc;
64  
65 # Set timeout because bitcoin is slow
66 1 $client->ua->timeout($self->timeout);
67  
68 # Set Agent, let them know who we be
69 1 $client->ua->agent("Bitcoin::RPC::Client/" . $VERSION);
70  
71 # Turn on debugging for LWP::UserAgent
72 1 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 1 1
73 0 ▶ if (!$DEBUG_DUMPED) { # We only want to set this up once
Branch: 1 branch
BranchTrueFalseTotal
unless ($DEBUG_DUMPED) 0 0 0
74 0 ▶ $client->ua->add_handler("request_send", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ $client->ua->add_handler("response_done", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $DEBUG_DUMPED = 1;
77 }
78 }
79  
80 # For self signed certs
81 1 ▶ if ($self->verify_hostname eq 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_hostname eq 0) 0 1 1
82 0 $client->ua->ssl_opts( verify_hostname => 0,
83 SSL_verify_mode => 0 );
84 }
85  
86 # Build the RPC
87 1 ▶ my $obj = {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
88 method => $method,
89 params => (ref $_[0] ? $_[0] : [@_]),
90 };
91 1 my $res;
92 eval {
93 # Attempt the RPC
94 1 $res = $client->call( $url, $obj );
95 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $res = $client->call($url, $obj) } } or do { my $tr = $client->ua->post($url); die $tr->status_line } 0 1 1
96 # Test the HTTP service and die with usable output if above RPC call fails
97 1 my $tr = $client->ua->post($url);
98 1 die $tr->status_line;
99 };
100 0 ▶ if($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
101 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
102 0 my $content = $res->content;
103 0 die sprintf("error code: %d, error message: %s (%s)\n", $content->{error}->{code}, $content->{error}->{message}, $method);
104 }
105  
106 0 return $res->result;
107 }
108  
109 0 return;
110 }
111  
112 # function to setup cookie attrib
113 0 ▶sub isa_cookie {
Subroutine isa_cookie: not called CC=5 SCAR=34.0
Pod undocumented
114  
115 0 my $failed = 0;
116  
117 # We only want to read this once (limit io).
118 0 ▶ open COOKIE, $_[0] or $failed = 1;
Branch: 1 branch
BranchTrueFalseTotal
open COOKIE, $_[0] or $failed = 1 0 0 0
119  
120 0 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 0 0 0
121 0 die sprintf("Could not open RPC cookie file: %s\n", $_[0]);
122 }
123  
124 0 my $cookie = <COOKIE>;
125 0 close COOKIE;
126 0 ▶ if (!defined($cookie) or $cookie !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cookie and $cookie =~ /:/) 0 0 0
Condition0%0/3
defined $cookie and $cookie =~ /:/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cookie and $cookie =~ /:/
defined $cookie $cookie =~ /:/
Truth table0%0/3
A and B
A= defined $cookieB= $cookie =~ /:/
ABresult
0X0
100
111
127 0 die "Invalid RPC cookie file format\n";
128 }
129 0 $cookie =~ s/\s+//g;
130 0 $_[0] = $cookie;
131  
132 }
133  
134 2 1;
135  
136 =pod
137  
138 =head1 NAME
139  
140 Bitcoin::RPC::Client - Bitcoin Core JSON RPC Client
141  
142 =head1 SYNOPSIS
143  
144 use Bitcoin::RPC::Client;
145  
146 # Create Bitcoin::RPC::Client object
147 $btc = Bitcoin::RPC::Client->new(
148 user => "username",
149 password => "p4ssword",
150 host => "127.0.0.1",
151 );
152  
153 # Check the block height of bitcoin node
154 # https://developer.bitcoin.org/reference/rpc/getblockchaininfo.html
155 $chaininfo = $btc->getblockchaininfo;
156 $blocks = $chaininfo->{blocks};
157  
158 # Estimate a reasonable transaction fee
159 # https://developer.bitcoin.org/reference/rpc/estimatesmartfee.html
160 $fee = $btc->estimatesmartfee(6);
161 $feerate = $fee->{feerate};
162  
163 # Set the transaction fee
164 # https://developer.bitcoin.org/reference/rpc/settxfee.html
165 $settx = $btc->settxfee($feerate);
166  
167 # Check balance
168 # (JSON::Boolean objects must be passed as boolean parameters)
169 # https://developer.bitcoin.org/reference/rpc/getbalance.html
170 $balance = $btc->getbalance("*", 1, JSON::true);
171  
172 # Send to an address
173 # https://developer.bitcoin.org/reference/rpc/sendtoaddress.html
174 $transid = $btc->sendtoaddress("1DopyzQi9mX3huvGacfjpzCKFug2Dtvykp","0.01");
175  
176 # See ex/example.pl for more in depth JSON handling:
177 # https://github.com/whindsx/Bitcoin-RPC-Client/tree/master/ex
178  
179 =head1 DESCRIPTION
180  
181 This module implements in Perl the functions that are currently part of the
182 Bitcoin Core RPC client calls (bitcoin-cli). The function names and parameters
183 are identical between the L<Bitcoin Core RPC API|https://developer.bitcoin.org/reference/rpc/index.html> and this module.
184  
185 =head1 CONSTRUCTOR
186  
187 $btc = Bitcoin::RPC::Client->new( %options )
188  
189 This method creates a new C<Bitcoin::RPC::Client> and returns it.
190  
191 Key Default
192 ----------- -----------
193 host undef (Required)
194 user undef (Required)
195 password undef (Required)
196 cookie undef
197 port 8332
198 wallet undef
199 timeout 20
200 ssl 0
201 verify_hostname 1
202 debug 0
203  
204 cookie - Absolute path to your RPC cookie file (.cookie). When cookie is
205 defined user and password will be ignored and the contents of cookie will
206 be used instead.
207  
208 wallet - Work against specific wallet.dat file when Multi-wallet support is
209 enabled (Bitcoin Core v0.15+ only)
210  
211 timeout - Set the timeout in seconds for individual RPC requests. Increase
212 this for slow bitcoind instances.
213  
214 ssl - OpenSSL support has been removed from the Bitcoin Core project as of
215 v0.12.0. However Bitcoin::RPC::Client will work over SSL with earlier versions
216 or with a reverse web proxy such as nginx.
217  
218 verify_hostname - Disable SSL certificate verification. Needed when
219 bitcoind is fronted by a proxy or when using a self-signed certificate.
220  
221 debug - Turns on raw HTTP request/response output from LWP::UserAgent.
222  
223 =head1 AUTHOR
224  
225 Wesley Hinds wesley.hinds@gmail.com
226  
227 =head1 AVAILABILITY
228  
229 The latest branch is avaiable from Github.
230  
231 https://github.com/whindsx/Bitcoin-RPC-Client.git
232  
233 =head1 DONATE
234  
235 1DopyzQi9mX3huvGacfjpzCKFug2Dtvykp
236  
237 =head1 LICENSE AND COPYRIGHT
238  
239 Copyright 2016 Wesley Hinds.
240  
241 This program is free software; you can redistribute it and/or modify it
242 under the terms of the the Artistic License (2.0). You may obtain a
243 copy of the full license at:
244  
245 L<http://www.perlfoundation.org/artistic_license_2_0>
246  
247 Any use, modification, and distribution of the Standard or Modified
248 Versions is governed by this Artistic License. By using, modifying or
249 distributing the Package, you accept this license. Do not use, modify,
250 or distribute the Package, if you do not accept this license.
251  
252 If your Modified Version has been derived from a Modified Version made
253 by someone other than you, you are nevertheless required to ensure that
254 your Modified Version complies with the requirements of this license.
255  
256 This license does not grant you the right to use any trademark, service
257 mark, tradename, or logo of the Copyright Holder.
258  
259 This license includes the non-exclusive, worldwide, free-of-charge
260 patent license to make, have made, use, offer to sell, sell, import and
261 otherwise transfer the Package with respect to any patent claims
262 licensable by the Copyright Holder that are necessarily infringed by the
263 Package. If you institute patent litigation (including a cross-claim or
264 counterclaim) against any party alleging that the Package constitutes
265 direct or contributory patent infringement, then this Artistic License
266 to you shall terminate on the date that such litigation is filed.
267  
268 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
269 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
270 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
271 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
272 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
273 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
274 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
275 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276  
277  
278 =cut
↑ Summary