| 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 | =pod | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | coinbase.pl -- Tests checking a balance using the coinbase api | ||
| 146 | |||
| 147 | =head1 SYNOPSIS | ||
| 148 | |||
| 149 | The synopsis, showing one or more typical command-line usages. | ||
| 150 | |||
| 151 | perl -Ilib bin/coinbase.pl | ||
| 152 | |||
| 153 | or | ||
| 154 | |||
| 155 | perl -Ilib bin/coinbase.pl --use-curl | ||
| 156 | |||
| 157 | |||
| 158 | both with and without curl the script outputs some debug info | ||
| 159 | |||
| 160 | =head1 DESCRIPTION | ||
| 161 | |||
| 162 | Tests checking a balance using the coinbase api | ||
| 163 | |||
| 164 | =head1 OPTIONS | ||
| 165 | |||
| 166 | Overall view of the options | ||
| 167 | |||
| 168 | coinbase.pl [-verbose] [--use-curl] [--nonce=NONCE] [--config=CONF.ini] | ||
| 169 | |||
| 170 | =over 4 | ||
| 171 | |||
| 172 | =item --config=/dif/coinbase.ini | ||
| 173 | |||
| 174 | Set path to an acmecoinbase.ini file. | ||
| 175 | The will default to ~/.acmecoinbase.ini | ||
| 176 | |||
| 177 | This file is expected to have contents like: | ||
| 178 | |||
| 179 | [default] | ||
| 180 | api_key = 123456apikeyfgYZ | ||
| 181 | api_secret = ZZZ111apisecret333DDD444EEE555ZZ | ||
| 182 | |||
| 183 | For now, the [default] part is mandatory. | ||
| 184 | |||
| 185 | =item --verbose/--noverbose | ||
| 186 | |||
| 187 | Turns on/off verbose mode. (off by default) | ||
| 188 | |||
| 189 | =item --use-curl | ||
| 190 | |||
| 191 | Use curl instead of perl LWP libraries, for test purposes. | ||
| 192 | |||
| 193 | =item --nonce=NUMBER | ||
| 194 | |||
| 195 | Hard code the nonce to a particular number. For testing. | ||
| 196 | (Techically this is supposed to 'always increase') | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =head1 COPYRIGHT | ||
| 201 | |||
| 202 | Copyright (c) 2018 Josh Rabinowitz, All Rights Reserved. | ||
| 203 | |||
| 204 | =head1 AUTHORS | ||
| 205 | |||
| 206 | Josh Rabinowitz | ||
| 207 | |||
| 208 | =cut | ||
| 209 |