Skip to content

Instantly share code, notes, and snippets.

@leishman
Last active August 29, 2015 13:56
Show Gist options
  • Save leishman/9243373 to your computer and use it in GitHub Desktop.
Save leishman/9243373 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'hashie'
require 'Base64'
require 'addressable/uri'
def post_private(method, opts={}) # Example: post_private('Balance') #=> returns balance info from Kraken, opts not necessary
opts['nonce'] = nonce
post_data = encode_options(opts)
headers = {
'API-Key' => @api_key,
'API-Sign' => generate_signature(method, post_data, opts)
}
url = @base_uri + url_path(method)
r = self.class.post(url, { headers: headers, body: post_data }).parsed_response
r['error'].empty? ? r['result'] : r['error']
end
def nonce
Time.now.to_i.to_s.ljust(16,'0')
end
def encode_options(opts)
uri = Addressable::URI.new
uri.query_values = opts
uri.query
end
def generate_signature(method, post_data, opts={})
key = Base64.decode64(@api_secret)
message = generate_message(method, opts, post_data)
generate_hmac(key, message)
end
def generate_message(method, opts, data)
digest = OpenSSL::Digest.new('sha256', opts['nonce'] + data).digest
url_path(method) + digest
end
def generate_hmac(key, message)
Base64.encode64(OpenSSL::HMAC.digest('sha512', key, message)).split.join # to remove '/n' inserted into signature by HMAc
end
def url_path(method)
'/' + @api_version + '/private/' + method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment