script/coinbase.pl untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 139 CC 8 SCAR 27.0
CC
8
Cov
0%
perform_request
42.0
main
12.0
Usage
2.0
CRAP 14.9
1 #!/usr/local/bin/perl -w
2 # vim: set ts=4 sw=4 expandtab showmatch
3 # PODNAME: coinbase.pl
4 # above is for POD::Weaver
5  
6 use strict;
7 use Getopt::Long;
8 use Acme::Coinbase::DefaultAuth;
9 use Acme::Coinbase::Config;
10 use File::Basename;
11 use Digest::SHA qw(hmac_sha256_hex);
12 use LWP::UserAgent;
13 use Data::Dumper;
14 use Carp;
15 use Time::HiRes;
16 use bignum;
17  
18 my $prog = basename($0);
19 my $verbose;
20 my $auth = Acme::Coinbase::DefaultAuth->new();
21 #my $nonce = time();
22 my $nonce = Time::HiRes::time() * 1E6;
23 my $config_file;# = $ENV{HOME} . "/.acmecoinbase.ini";
24 my $use_curl = 0;
25  
26 # Usage() : returns usage information
27 sub Usage {
28 "$prog [--verbose] [--use-curl] [--nonce=NONCE] [--config=CONF.ini]\n";
29 }
30  
31 # call main()
32 main();
33  
34 # main()
35 sub main {
36 GetOptions(
37 "verbose!" => \$verbose,
38 "config-file=s" => \$config_file,
39 "use-curl" => \$use_curl,
40 "nonce=n" => \$nonce,
41 ) or die Usage();
42 $SIG{__WARN__} = sub { Carp::confess $_[0] };
43 $SIG{__DIE__} = sub { Carp::confess $_[0] };
44  
45 print "$prog: NONCE: $nonce\n";
46 my $base = "https://api.coinbase.com/api";
47 #my $base = "https://api.coinbase.com";
48 my $url = "$base/v1/account/balance";
49  
50 my $default_config_file = $ENV{HOME} . "/.acmecoinbase.ini";
51 if (!$config_file && -e $default_config_file) {
52 $config_file = $default_config_file;
53 }
54 my $config = Acme::Coinbase::Config->new( );
55 if ($config_file && -e $config_file) {
56 $config->config_file($config_file);
57 $config->read_config();
58 }
59 my $api_key = $config->get_param("default", "api_key") || $auth->api_key();
60 my $api_secret = $config->get_param("default", "api_secret") || $auth->api_secret();
61 #print "$prog: using API key $api_key\n";
62  
63 perform_request( $url, $api_key, $api_secret, $verbose );
64 }
65  
66  
67 sub perform_request {
68 my ( $url, $api_key, $api_secret, $verbose ) = @_;
69 if ($use_curl) {
70 # use curl to do basic request
71 my $sig = hmac_sha256_hex($nonce . $url . "", $api_secret);
72 # somehow this is different than what we get from non-curl
73 print "$prog: in callback, str=$nonce$url, ACCESS_SIGNATURE => $sig\n";
74 my $curl = "curl";
75 if ($verbose) { $curl .= " --verbose"; }
76 my $cmd = "$curl " .
77 " -H 'Accept: */*' " .
78 " -H 'Host: coinbase.com' " .
79 " -H 'ACCESS_KEY: $api_key' " .
80 " -H 'ACCESS_NONCE: $nonce' " .
81 " -H 'ACCESS_SIGNATURE: $sig' " .
82 " -H 'Connection: close' " .
83 " -H 'Content-Type: application/json' " .
84 " $url";
85 print "$cmd\n";
86 system( $cmd );
87 print "\n";
88 } else {
89 # use LWP::UserAgent
90 my $ua = LWP::UserAgent->new();
91 $ua->default_headers->push_header( Accept => "*/*" );
92 $ua->default_headers->push_header( ACCESS_KEY => $api_key );
93 $ua->default_headers->push_header( ACCESS_NONCE => $nonce );
94 $ua->default_headers->push_header( Host => "coinbase.com" );
95 $ua->default_headers->push_header( Connection => "close" );
96 $ua->default_headers->push_header( "Content-Type" => "application/json" );
97  
98 # add ACCESS_SIGNATURE in a request_prepare handler so we can set it knowing the request content
99 # ... it doesn't matter for GETs though because the content should be blank (like we see in our code)
100 $ua->add_handler(
101 request_prepare => sub {
102 my($request, $ua, $h) = @_;
103 my $content = $request->decoded_content(); # empty string.
104 $content = "" unless defined($content);
105  
106 my $to_hmac = $nonce . $url . $content;
107 my $sig = hmac_sha256_hex( $to_hmac, $api_secret );
108 print "$prog: in callback, str=$to_hmac, ACCESS_SIGNATURE => $sig\n";
109 $request->headers->push_header( ACCESS_SIGNATURE => $sig );
110 }
111 );
112  
113 if ($verbose) {
114 # a handler to dump out the request for debugging
115 $ua->add_handler( request_send => sub {
116 print "$prog: verbose mode: BEGIN dump of request object: ***********\n";
117 shift->dump;
118 print "$prog: verbose mode: END dump of request object: *************\n";
119 return
120 });
121 }
122  
123 my $response = $ua->get( $url );
124  
125 my $noun = $response->is_success() ? "Success" : "Error";
126 #print ("$prog: $noun " . $response->status_line . ", content: " . $response->decoded_content . "\n");
127 print ("$prog: $noun " . $response->status_line . ", content: " . $response->content . "\n");
128 }
129 }
130  
131 # this is an example of a header used on the coinbase API, from their docs;
132 # GET /api/v1/account/balance HTTP/1.1
133 # Accept: */*
134 # User-Agent: Ruby
135 # ACCESS_KEY: <YOUR-API-KEY>
136 # ACCESS_SIGNATURE: <YOUR-COMPUTED-SIGNATURE>
137 # ACCESS_NONCE: <YOUR-UPDATED-NONCE>
138 # Connection: close
139 # Host: coinbase.com
140  
141 __END__