script/coinbase.pl untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 8 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 99 CC 5 SCAR 23.7
CC
5
Cov
0%
perform_request
30.0
Usage
2.0
main
2.0
CRAP 10.8
1 #!/usr/local/bin/perl -w
2 # vim: set ts=4 sw=4 expandtab showmatch
3  
4 use strict;
5 use Getopt::Long;
6 use Acme::Coinbase::DefaultAuth;
7 use Acme::Coinbase::Config;
8 use File::Basename;
9 use Digest::SHA qw(hmac_sha256_hex);
10 use LWP::UserAgent;
11 use Data::Dumper;
12 use Carp;
13  
14 my $prog = basename($0);
15 my $verbose;
16 my $auth = Acme::Coinbase::DefaultAuth->new();
17 my $nonce = time();
18 my $config_file = $ENV{HOME} . "/.acmecoinbase.ini";
19  
20 # Usage() : returns usage information
21 sub Usage {
22 "$prog [--verbose] [--config=CONF.ini]\n";
23 }
24  
25 # call main()
26 main();
27  
28 # main()
29 sub main {
30 GetOptions(
31 "verbose!" => \$verbose,
32 "config-file=s" => \$config_file,
33 ) or die Usage();
34 #$SIG{__WARN__} = sub { Carp::confess $_[0] };
35 #$SIG{__DIE__} = sub { Carp::confess $_[0] };
36  
37 #print "$prog: NONCE: $nonce\n";
38 my $base = "https://api.coinbase.com/api";
39 my $url = "$base/v1/account/balance";
40  
41 my $config = Acme::Coinbase::Config->new( config_file => $config_file );
42 $config->read_config();
43 my $api_key = $config->get_param("default", "api_key") || $auth->api_key();
44 my $api_secret = $config->get_param("default", "api_secret") || $auth->api_secret();
45 print "$prog: using API key $api_key\n";
46  
47 perform_request( $url, $api_key, $api_secret, $verbose );
48 }
49  
50  
51 sub perform_request {
52 my ( $url, $api_key, $api_secret, $verbose ) = @_;
53 if (0) {
54 # USE CURL TO DO BASIC REQUEST
55 my $sig = hmac_sha256_hex($nonce . $url, $api_secret); # this is wrong
56 my $curl = "curl";
57 if ($verbose) { $curl .= " --verbose"; }
58 my $cmd = "$curl " .
59 " -H 'Accept: */*' " .
60 " -H 'Host: coinbase.com' " .
61 " -H 'ACCESS_KEY: $api_key' " .
62 " -H 'ACCESS_NONCE: $nonce' " .
63 " -H 'ACCESS_SIGNATURE: $sig' " .
64 " $url";
65 print "$cmd\n";
66 system( $cmd );
67 print "\n";
68 } else {
69 # use LWP::UserAgent
70 my $ua = LWP::UserAgent->new();
71 $ua->default_headers->push_header( Accept => "*/*" );
72 $ua->default_headers->push_header( ACCESS_KEY => $api_key );
73 $ua->default_headers->push_header( ACCESS_NONCE => $nonce );
74 $ua->default_headers->push_header( Host => "coinbase.com" );
75  
76 # add ACCESS_SIGNATURE in a request_prepare handler so we can set it knowing the request content
77 # ... it doesn't matter for GETs though because the content should be blank (like we see in our code)
78 $ua->add_handler(
79 request_prepare => sub {
80 my($request, $ua, $h) = @_;
81 my $content = $request->decoded_content(); # empty string.
82 $content = "" unless defined($content);
83 my $sig = hmac_sha256_hex( $nonce . $url . $content, $api_secret ); # this conforms to spec but is wrong. won't validate
84 #print "$prog: in callback, setting header ACCESS_SIGNATURE => $sig\n";
85 $request->headers->push_header( ACCESS_SIGNATURE => $sig );
86 }
87 );
88  
89 # a handler to dump out the request for debugging
90 #$ua->add_handler( request_send => sub { shift->dump; return });
91  
92 my $response = $ua->get( $url );
93  
94 if ($response->is_success) {
95 print( "$prog: Success: " . $response->decoded_content ); # or whatever
96 } else {
97 die ("$prog: Error: " . $response->status_line . ", content: " . $response->decoded_content);
98 }
99 }
100 }
101  
102 __END__