Author

Topic: Help with RPC to wallet (bitcoind), Perl (Read 1508 times)

legendary
Activity: 1974
Merit: 1029
May 17, 2013, 03:19:33 PM
#3
Could you please help me to edit this Perl script, to move parameters (like url/port etc) from the command line to the script

Use Getopt::Long:

Code:
$ cat getopt-rpc.pl
#!/usr/bin/perl

use warnings;
use strict;
use Term::ReadKey;
use JSON::RPC::Client;
use Getopt::Long;

GetOptions \my %opts,
    '--host=s',
    '--port=s',
    '--user=s' or
    die 'getopt';

print 'RPC password: ';
ReadMode 'noecho'; $opts{'pass'} = <>; ReadMode 'restore'; print "\n";
chomp $opts{'pass'};

my $url = sprintf 'http://%s:%s@%s:%s/', @opts{qw/user pass host port/};

my $rpc = JSON::RPC::Client->new;
$rpc->prepare ($url, [ qw/getblock getblockhash/ ]);
my $bh = $rpc->getblockhash (222222);
my $block = $rpc->getblock ($bh);
my @tx = @{ $block->{'tx'} };
printf "%d txs\n", scalar @tx;

$ ./getopt-rpc.pl --user user --host localhost --port 8332
RPC password:
749 txs
$ ./getopt-rpc.pl -u user -h localhost -p 8332   ## short options are ok too
RPC password:
749 txs

Regarding the SSL bit, I lack motivation.
sr. member
Activity: 344
Merit: 250
Anyone?
sr. member
Activity: 344
Merit: 250
Here:
https://blockchain.info/api/json_rpc_api

we can see an example of using bitcoind to contact another, remote wallet:
$ ./bitcoind -rpcconnect=rpc.blockchain.info -rpcport=443 -rpcssl -rpcuser=YourWalletIdentifier -rpcpassword=YourPassword getinfo

Now, I am trying to use Perl CGI, running on one howt, and accessing the wallet on another host (on blockchain.info):


use JSON::RPC::Client;
use Data::Dumper;
 
my $client = new JSON::RPC::Client;
 
  $client->ua->credentials(
     'localhost:8332', 'jsonrpc', 'user' => 'password'  # REPLACE WITH YOUR bitcoin.conf rpcuser/rpcpassword
      );
 
  my $uri = 'http://localhost:8332/';
  my $obj = {
      method  => 'getinfo',
      params  => [],
   };
 
  my $res = $client->call( $uri, $obj );
 
  if ($res){
      if ($res->is_error) { print "Error : ", $res->error_message; }
      else { print Dumper($res->result); }
  } else {
      print $client->status_line;
  }


Could you please help me to edit this Perl script, to move parameters (like url/port etc) from the command line to the script, so that the script could access the wallet, AND it was done using secure connection?

My second question is less urgent, but still: JSON::RPC::Client seems to be obsolete in Perl. Are there new alternatives to the code above?
Jump to: