class Hexsafe::Api
Public Class Methods
new(options = {})
click to toggle source
# File lib/hexsafe/api.rb, line 5 def initialize options = {} @mode = options[:env].upcase rescue nil @key = options[:key] rescue nil @secret = options[:secret] rescue nil end
Public Instance Methods
cancel_deposit(request_id)
click to toggle source
# File lib/hexsafe/api.rb, line 35 def cancel_deposit request_id response = rest_api("DELETE", "deposit/#{request_id}", {}) end
cancel_withdraw(request_id)
click to toggle source
# File lib/hexsafe/api.rb, line 47 def cancel_withdraw request_id response = rest_api("DELETE", "withdraw/#{request_id}", {}) end
create_deposit(body)
click to toggle source
# File lib/hexsafe/api.rb, line 31 def create_deposit body response = rest_api("post", "deposit", body) end
create_withdraw(body)
click to toggle source
# File lib/hexsafe/api.rb, line 43 def create_withdraw body response = rest_api("post", "withdraw", body) end
delete_subscription(uuid)
click to toggle source
# File lib/hexsafe/api.rb, line 63 def delete_subscription uuid response = rest_api("DELETE", "webhook/#{uuid}", {}) end
get_account(account_id)
click to toggle source
# File lib/hexsafe/api.rb, line 15 def get_account account_id response = rest_api("GET", "account/#{account_id}", {}) end
get_accounts()
click to toggle source
# File lib/hexsafe/api.rb, line 11 def get_accounts response = rest_api("GET", "account", {}) end
get_balance(account_id)
click to toggle source
# File lib/hexsafe/api.rb, line 19 def get_balance account_id response = rest_api("GET", "balance/#{account_id}", {}) end
get_deposit(request_id)
click to toggle source
# File lib/hexsafe/api.rb, line 39 def get_deposit request_id response = rest_api("GET", "deposit/#{request_id}", {}) end
get_subscriptions(account_id)
click to toggle source
# File lib/hexsafe/api.rb, line 59 def get_subscriptions account_id response = rest_api("GET", "webhook/#{account_id}", {}) end
get_txn(ticker, tx_hash)
click to toggle source
# File lib/hexsafe/api.rb, line 23 def get_txn ticker, tx_hash response = rest_api("GET", "transaction/asset_ticker/#{ticker.upcase}/hash/#{tx_hash}", {}) end
get_txn_ac(account_id, start_time, end_time)
click to toggle source
# File lib/hexsafe/api.rb, line 27 def get_txn_ac account_id, start_time, end_time response = rest_api("GET", "transaction/account_id/#{account_id}/start_time/#{start_time}/end_time/#{end_time}", {}) end
get_withdraw(request_id)
click to toggle source
# File lib/hexsafe/api.rb, line 51 def get_withdraw request_id response = rest_api("GET", "withdraw/#{request_id}", {}) end
subscribe_webhook()
click to toggle source
# File lib/hexsafe/api.rb, line 55 def subscribe_webhook response = rest_api("post", "webhook", body) end
Protected Instance Methods
rest_api(verb, path, body = nil)
click to toggle source
# File lib/hexsafe/api.rb, line 69 def rest_api(verb, path, body = nil) verb = verb.upcase body_digest = nil timestamp = new_timestamp.to_s + "0000" request_line = verb + ' ' + base_url+path + ' HTTP/1.1' if body if non_get_verb? verb body_digest = Base64.strict_encode64(digest(JSON.generate(body))) end end signature = encode_hmac_512(timestamp, host, request_line, body_digest) url = host+base_url+path if ['POST', 'PUT', 'PATCH', 'DELETE'].include?(verb) authorization = "hmac username=\"" + key + "\", algorithm=\"hmac-sha512\", headers=\"nonce host digest request-line\", signature=\"" + signature + "\"" else authorization = "hmac username=\"" + key + "\", algorithm=\"hmac-sha512\", headers=\"nonce host request-line\", signature=\"" + signature + "\""; end if is_get? verb response = Faraday.get(url) do |req| req.headers['nonce'] = timestamp.to_s req.headers['authorization'] = authorization req.headers['Date'] = http_format_date req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' end elsif non_get_verb? verb response = Faraday.post(url) do |req| req.body = body.compact.to_json req.headers['nonce'] = timestamp.to_s req.headers['digest'] = "SHA-256="+body_digest req.headers['authorization'] = authorization req.headers['Date'] = http_format_date req.headers['Content-Type'] = 'application/json' end end JSON.parse(response.body) end
Private Instance Methods
base_url()
click to toggle source
# File lib/hexsafe/api.rb, line 110 def base_url return "/hexsafe/api/v4/" end
digest(data)
click to toggle source
# File lib/hexsafe/api.rb, line 129 def digest(data) sha256 = OpenSSL::Digest::SHA256.new() sha256.digest(data.encode('utf-8')) end
encode_hmac_512(timestamp, host, request_line, body_digest = nil)
click to toggle source
# File lib/hexsafe/api.rb, line 138 def encode_hmac_512(timestamp, host, request_line, body_digest = nil) data = '' data << 'nonce: ' + timestamp.to_s + "\n" data << 'host: ' + URI.parse(host).host + "\n" if body_digest data << 'digest: SHA-256=' + body_digest + "\n" end data << request_line Base64.strict_encode64(hmac_512(secret,data)) end
hmac_512(secret, data)
click to toggle source
# File lib/hexsafe/api.rb, line 134 def hmac_512(secret, data) OpenSSL::HMAC.digest('sha512',secret, data) end
host()
click to toggle source
# File lib/hexsafe/api.rb, line 114 def host return "Invalid Environment" unless %w(TEST MAIN).include?(@mode) @mode == "TEST" ? "https://api-test.hexsafe.io" : "https://api.hexsafe.io" end
http_format_date()
click to toggle source
# File lib/hexsafe/api.rb, line 149 def http_format_date Time.now.httpdate end
is_get?(verb)
click to toggle source
# File lib/hexsafe/api.rb, line 157 def is_get? verb return ['GET'].include?(verb) end
key()
click to toggle source
# File lib/hexsafe/api.rb, line 119 def key return "Empty key" if @key.nil? @key ||= @key end
new_timestamp()
click to toggle source
# File lib/hexsafe/api.rb, line 153 def new_timestamp (Time.now.to_f*1000).to_i.to_s end
non_get_verb?(verb)
click to toggle source
# File lib/hexsafe/api.rb, line 161 def non_get_verb? verb return ['POST', 'PUT', 'PATCH', 'DELETE'].include?(verb) end
secret()
click to toggle source
# File lib/hexsafe/api.rb, line 124 def secret return "Empty secret" if @secret.nil? @secret ||= @secret end