| 1 | package Finance::Bank::Kraken; | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: Kraken.pm 72 2014-06-02 11:25:16Z phil $ | ||
| 5 | # | ||
| 6 | # Kraken API connector | ||
| 7 | # author, (c): Philippe Kueck <projects at unixadm dot org> | ||
| 8 | # | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use HTTP::Request; | ||
| 14 | use LWP::UserAgent; | ||
| 15 | use MIME::Base64; | ||
| 16 | use Digest::SHA qw(hmac_sha512_base64 sha256); | ||
| 17 | |||
| 18 | require Exporter; | ||
| 19 | our @ISA = qw(Exporter); | ||
| 20 | our @EXPORT = qw(Private Public); | ||
| 21 | our $VERSION = "0.3"; | ||
| 22 | use constant Private => 1; | ||
| 23 | use constant Public => 0; | ||
| 24 | |||
| 25 | sub new {bless {'uri' => 'https://api.kraken.com', 'nonce' => time}, $_[0]} | ||
| 26 | |||
| 27 | sub key { | ||
| 28 | return $_[0]->{'key'} unless $_[1]; | ||
| 29 | $_[0]->{'key'} = $_[1] | ||
| 30 | } | ||
| 31 | |||
| 32 | sub secret { | ||
| 33 | return $_[0]->{'secret'} unless $_[1]; | ||
| 34 | $_[0]->{'secret'} = decode_base64($_[1]) | ||
| 35 | } | ||
| 36 | |||
| 37 | sub call { | ||
| 38 | my $self = shift; | ||
| 39 | my $uripath = sprintf "/0/%s/%s", $_[0]?"private":"public", $_[1]; | ||
| 40 | my $req = new HTTP::Request($_[0]?'POST':'GET'); | ||
| 41 | my $ua = new LWP::UserAgent('agent' => "perl(Finance::Bank::Kraken) api client/".$VERSION); | ||
| 42 | my $qry = defined $_[2]?(join "&", @{$_[2]}):undef; | ||
| 43 | if ($_[0]) { | ||
| 44 | $req->uri($self->{'uri'} . $uripath); | ||
| 45 | $req->content(sprintf "nonce=%d%s", $self->{'nonce'}, defined $qry?"&$qry":""); | ||
| 46 | $req->header("API-Key" => $self->{'key'}); | ||
| 47 | $req->header("API-Sign" => hmac_sha512_base64( | ||
| 48 | $uripath . sha256($self->{'nonce'} . $req->content), | ||
| 49 | $self->{'secret'}) | ||
| 50 | ); | ||
| 51 | $self->{'nonce'}++ | ||
| 52 | } else { | ||
| 53 | $req->uri(sprintf "%s%s%s", $self->{'uri'}, $uripath, defined $qry?"?$qry":"") | ||
| 54 | } | ||
| 55 | my $res = $ua->request($req); | ||
| 56 | $res->is_success?$res->content:$res->status_line | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ |