| 1 | package Bitcoin::RPC::Client; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use JSON::RPC::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | use Bitcoin::RPC::Client::API; | ||
| 12 | |||
| 13 | our $VERSION = '0.06'; | ||
| 14 | |||
| 15 | has jsonrpc => (is => "lazy", default => sub { "JSON::RPC::Client"->new }); | ||
| 16 | has user => (is => 'ro'); | ||
| 17 | has password => (is => 'ro'); | ||
| 18 | has host => (is => 'ro'); | ||
| 19 | has port => (is => "lazy", default => 8332); | ||
| 20 | has timeout => (is => "lazy", default => 20); | ||
| 21 | has syntax => (is => "lazy", default => 0); | ||
| 22 | has debug => (is => "lazy", default => 0); | ||
| 23 | |||
| 24 | # SSL constructor options | ||
| 25 | # OpenSSL support has been removed from Bitcoin Core as of v0.12.0 | ||
| 26 | # but should work with older versions | ||
| 27 | has ssl => (is => 'ro', default => 0); | ||
| 28 | has verify_hostname => (is => 'ro', default => 1); | ||
| 29 | |||
| 30 | sub AUTOLOAD { | ||
| 31 | my $self = shift; | ||
| 32 | my $method = $Bitcoin::RPC::Client::AUTOLOAD; | ||
| 33 | |||
| 34 | $method =~ s/.*:://; | ||
| 35 | |||
| 36 | return if ($method eq 'DESTROY'); | ||
| 37 | # Thanks JSON::RPC::Client | ||
| 38 | |||
| 39 | # Are we using SSL? | ||
| 40 | my $uri = "http://"; | ||
| 41 | if ($self->ssl eq 1) { | ||
| 42 | $uri = "https://"; | ||
| 43 | } | ||
| 44 | my $url = $uri . $self->user . ":" . $self->password . "\@" . $self->host . ":" . $self->port; | ||
| 45 | |||
| 46 | my $client = $self->jsonrpc; | ||
| 47 | |||
| 48 | # Set timeout because bitcoin is slow | ||
| 49 | $client->ua->timeout($self->timeout); | ||
| 50 | |||
| 51 | # Set Agent, let them know who we be | ||
| 52 | $client->ua->agent("Bitcoin::RPC::Client/" . $VERSION); | ||
| 53 | |||
| 54 | # Turn on debugging for LWP::UserAgent | ||
| 55 | if ($self->debug) { | ||
| 56 | $client->ua->add_handler("request_send", sub { shift->dump; return }); | ||
| 57 | $client->ua->add_handler("response_done", sub { shift->dump; return }); | ||
| 58 | } else { | ||
| 59 | # We want to handle broken responses ourself | ||
| 60 | $client->ua->add_handler("response_data", | ||
| 61 | sub { | ||
| 62 | my ($response, $ua, $h, $data) = @_; | ||
| 63 | |||
| 64 | if ($response->is_error) { | ||
| 65 | my $content = JSON->new->utf8->decode($data); | ||
| 66 | |||
| 67 | print STDERR "error code: "; | ||
| 68 | print STDERR $content->{error}->{code} . "\n"; | ||
| 69 | print STDERR "error message:\n"; | ||
| 70 | print STDERR $content->{error}->{message}; | ||
| 71 | } | ||
| 72 | |||
| 73 | return; | ||
| 74 | } | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | |||
| 78 | # For self signed certs | ||
| 79 | if ($self->verify_hostname eq 0) { | ||
| 80 | $client->ua->ssl_opts( verify_hostname => 0 ); | ||
| 81 | } | ||
| 82 | |||
| 83 | #my @params = @_; | ||
| 84 | # ^ Should this be here? | ||
| 85 | |||
| 86 | # Need to fix booleans. | ||
| 87 | # JSON::true | ||
| 88 | # But cant modify @_ | ||
| 89 | |||
| 90 | my $obj = { | ||
| 91 | method => $method, | ||
| 92 | params => (ref $_[0] ? $_[0] : [@_]), | ||
| 93 | }; | ||
| 94 | |||
| 95 | my $res = $client->call( $url, $obj ); | ||
| 96 | if($res) { | ||
| 97 | if ($res->is_error) { | ||
| 98 | return $res->error_message; | ||
| 99 | } | ||
| 100 | |||
| 101 | return $res->result; | ||
| 102 | } | ||
| 103 | |||
| 104 | # Usage errors are on by default | ||
| 105 | if ($self->syntax) { | ||
| 106 | # Check that method is even availabe in the API | ||
| 107 | hasMethod($method); | ||
| 108 | |||
| 109 | # Print SYNTAX for API call | ||
| 110 | failMethod($method); | ||
| 111 | } | ||
| 112 | |||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub hasMethod { | ||
| 117 | my ($method) = @_; | ||
| 118 | |||
| 119 | # Check that method is even availabe in the API | ||
| 120 | my $hasMethod = 0; | ||
| 121 | foreach my $meth (@Bitcoin::RPC::Client::API::methods) { | ||
| 122 | if (lc($meth) eq lc($method)) { | ||
| 123 | $hasMethod = 1; | ||
| 124 | last; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | if (!$hasMethod) { | ||
| 129 | print STDERR "error : Method $method does not exist\n"; | ||
| 130 | } | ||
| 131 | |||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub failMethod { | ||
| 136 | my ($method) = @_; | ||
| 137 | |||
| 138 | foreach my $entry (@Bitcoin::RPC::Client::API::help) { | ||
| 139 | if ($entry =~ /$method / || $entry =~ /$method$/) { | ||
| 140 | print STDERR "error : usage: $entry\n"; | ||
| 141 | last; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; | ||
| 149 | |||
| 150 | =pod | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | Bitcoin::RPC::Client - Bitcoin Core API RPCs | ||
| 155 | |||
| 156 | =head1 SYNOPSIS | ||
| 157 | |||
| 158 | use Bitcoin::RPC::Client; | ||
| 159 | |||
| 160 | # Create Bitcoin::RPC::Client object | ||
| 161 | $btc = Bitcoin::RPC::Client->new( | ||
| 162 | user => "username", | ||
| 163 | password => "p4ssword", | ||
| 164 | host => "127.0.0.1", | ||
| 165 | ); | ||
| 166 | |||
| 167 | # Functions that do not return a JSON object will have a scalar result | ||
| 168 | $balance = $btc->getbalance("yourAccountName"); | ||
| 169 | print $balance; | ||
| 170 | |||
| 171 | # JSON::Boolean objects must be passed as boolean parameters | ||
| 172 | $balance = $btc->getbalance("yourAccountName", 1, JSON::true); | ||
| 173 | print $balance; | ||
| 174 | |||
| 175 | # Getting Data when JSON/hash is returned | ||
| 176 | # A person would need to know the JSON elements of | ||
| 177 | # the output https://bitcoin.org/en/developer-reference#getinfo | ||
| 178 | # | ||
| 179 | #{ | ||
| 180 | # "version": 130000, | ||
| 181 | # "protocolversion": 70014, | ||
| 182 | # "walletversion": 130000, | ||
| 183 | # "balance": 0.00000000, | ||
| 184 | # "blocks": 584240, | ||
| 185 | # "proxy": "", | ||
| 186 | # "difficulty": 1, | ||
| 187 | # "paytxfee": 0.00500000, | ||
| 188 | # "relayfee": 0.00001000, | ||
| 189 | # "errors": "" | ||
| 190 | #} | ||
| 191 | $info = $btc->getinfo; | ||
| 192 | $balance = $info->{balance}; | ||
| 193 | print $balance; | ||
| 194 | # 0.0 | ||
| 195 | |||
| 196 | # JSON Objects | ||
| 197 | # Let's say we want the timeframe value | ||
| 198 | # | ||
| 199 | #{ | ||
| 200 | # "totalbytesrecv": 7137052851, | ||
| 201 | # "totalbytessent": 211648636140, | ||
| 202 | # "uploadtarget": { | ||
| 203 | # "timeframe": 86400, | ||
| 204 | # "target": 0, | ||
| 205 | # "target_reached": false, | ||
| 206 | # "serve_historical_blocks": true, | ||
| 207 | # "bytes_left_in_cycle": 0, | ||
| 208 | # "time_left_in_cycle": 0 | ||
| 209 | # } | ||
| 210 | #} | ||
| 211 | $nettot = $btc->getnettotals; | ||
| 212 | $timeframe = $nettot->{uploadtarget}{timeframe}; | ||
| 213 | print $timeframe; | ||
| 214 | # 86400 | ||
| 215 | |||
| 216 | # JSON arrays | ||
| 217 | # Let's say we want the softfork IDs from the following: | ||
| 218 | # | ||
| 219 | #{ | ||
| 220 | # "chain": "main", | ||
| 221 | # "blocks": 464562, | ||
| 222 | # "headers": 464562, | ||
| 223 | # "pruned": false, | ||
| 224 | # "softforks": [ | ||
| 225 | # { | ||
| 226 | # "id": "bip34", | ||
| 227 | # "version": 2, | ||
| 228 | # "reject": { | ||
| 229 | # "status": true | ||
| 230 | # } | ||
| 231 | # }, | ||
| 232 | # { | ||
| 233 | # "id": "bip66", | ||
| 234 | # "version": 3, | ||
| 235 | # "reject": { | ||
| 236 | # "status": true | ||
| 237 | # } | ||
| 238 | # } | ||
| 239 | $bchain = $btc->getblockchaininfo; | ||
| 240 | @forks = @{ $bchain->{softforks} }; | ||
| 241 | foreach $f (@forks) { | ||
| 242 | print $f->{id}; | ||
| 243 | print "\n"; | ||
| 244 | } | ||
| 245 | # bip34 | ||
| 246 | # bip66 | ||
| 247 | |||
| 248 | =head1 DESCRIPTION | ||
| 249 | |||
| 250 | This module implements in PERL the functions that are currently part of the | ||
| 251 | Bitcoin Core RPC client calls (bitcoin-cli).The function names and parameters | ||
| 252 | are identical between the Bitcoin Core API and this module. This is done for | ||
| 253 | consistency so that a developer only has to reference one manual: | ||
| 254 | https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs | ||
| 255 | |||
| 256 | =head1 CONSTRUCTOR | ||
| 257 | |||
| 258 | $btc = Bitcoin::RPC::Client->new( %options ) | ||
| 259 | |||
| 260 | This method creates a new C<Bitcoin::RPC::Client> and returns it. | ||
| 261 | |||
| 262 | Key Default | ||
| 263 | ----------- ----------- | ||
| 264 | user undef (Required) | ||
| 265 | password undef (Required) | ||
| 266 | host undef (Required) | ||
| 267 | port 8332 | ||
| 268 | timeout 20 | ||
| 269 | ssl 0 | ||
| 270 | verify_hostname 1 | ||
| 271 | syntax 0 | ||
| 272 | debug 0 | ||
| 273 | |||
| 274 | verify_hostname - OpenSSL support has been removed from the Bitcoin Core | ||
| 275 | project as of v0.12.0. | ||
| 276 | |||
| 277 | syntax - setting to 1 will turn on correct method name checking as well as | ||
| 278 | usage errors. This works with all versions of Bitcoin Core, but the API class | ||
| 279 | currently only contains what is valid for v0.12.0. You may want to keep this off | ||
| 280 | if you are using a version other than v0.12.0 and you are getting errors you | ||
| 281 | think you should not be getting. | ||
| 282 | |||
| 283 | debug - Turns on raw HTTP request/response output from LWP::UserAgent. | ||
| 284 | |||
| 285 | =head1 AUTHOR | ||
| 286 | |||
| 287 | Wesley Hinds wesley.hinds@gmail.com | ||
| 288 | |||
| 289 | =head1 AVAILABILITY | ||
| 290 | |||
| 291 | The latest branch is avaiable from Github. | ||
| 292 | |||
| 293 | https://github.com/whindsx/Bitcoin-RPC-Client.git | ||
| 294 | |||
| 295 | =head1 DONATE | ||
| 296 | |||
| 297 | 1Ky49cu7FLcfVmuQEHLa1WjhRiqJU2jHxe | ||
| 298 | |||
| 299 | =head1 LICENSE AND COPYRIGHT | ||
| 300 | |||
| 301 | Copyright 2016 Wesley Hinds. | ||
| 302 | |||
| 303 | This program is free software; you can redistribute it and/or modify it | ||
| 304 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 305 | copy of the full license at: | ||
| 306 | |||
| 307 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 308 | |||
| 309 | Any use, modification, and distribution of the Standard or Modified | ||
| 310 | Versions is governed by this Artistic License. By using, modifying or | ||
| 311 | distributing the Package, you accept this license. Do not use, modify, | ||
| 312 | or distribute the Package, if you do not accept this license. | ||
| 313 | |||
| 314 | If your Modified Version has been derived from a Modified Version made | ||
| 315 | by someone other than you, you are nevertheless required to ensure that | ||
| 316 | your Modified Version complies with the requirements of this license. | ||
| 317 | |||
| 318 | This license does not grant you the right to use any trademark, service | ||
| 319 | mark, tradename, or logo of the Copyright Holder. | ||
| 320 | |||
| 321 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 322 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 323 | otherwise transfer the Package with respect to any patent claims | ||
| 324 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 325 | Package. If you institute patent litigation (including a cross-claim or | ||
| 326 | counterclaim) against any party alleging that the Package constitutes | ||
| 327 | direct or contributory patent infringement, then this Artistic License | ||
| 328 | to you shall terminate on the date that such litigation is filed. | ||
| 329 | |||
| 330 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 331 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 332 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 333 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 334 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 335 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 336 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 337 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 338 | |||
| 339 | |||
| 340 | =cut | ||