Skip to content

Instantly share code, notes, and snippets.

@codemilan
Created July 11, 2018 09:49
Show Gist options
  • Select an option

  • Save codemilan/352789372b7accd33a92c7124b69329d to your computer and use it in GitHub Desktop.

Select an option

Save codemilan/352789372b7accd33a92c7124b69329d to your computer and use it in GitHub Desktop.
Signature is a HMAC-SHA256 encoded message
# NOTES::Important (!) To construct a signature string, parameters should be ordered alphabetically by
# initials of parameter names. Otherwise the e-signature verification process will fail.
# Post request.
require "net/http"
require "uri"
require 'openssl'
require "base64"
require "rubygems"
require "json"
uri = URI::parse("https://api.livecoin.net/exchange/buylimit")
api_key = "gJx7Wa7qXkPtmTAaK3ADCtr6m5rCYYMy"
secret_key = "8eLps29wsXszNyEhOl9w8dxsOsM2lTzg"
data = {
'currencyPair'=> 'BTC/USD',
'price'=>'100',
'quantity'=>'0.01'
}
sorted_data = data.sort_by { |key, value| key }
sha256 = OpenSSL::Digest::SHA256.new
signature = OpenSSL::HMAC.hexdigest(sha256, secret_key, URI.encode_www_form(sorted_data)).upcase
request = Net::HTTP::Post.new(uri)
request.set_form_data(sorted_data)
request.add_field("Api-key", api_key)
request.add_field("Sign", signature)
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl =>uri.scheme == 'https') {|http|
http.request(request)
}
response_data = JSON.parse(response.body())
print response_data
# Get Request.
require "net/http"
require "uri"
require 'openssl'
require "base64"
require "rubygems"
require "json"
uri = URI::parse("https://api.livecoin.net/exchange/client_orders")
api_key = "gJx7Wa7qXkPtmTAaK3ADCtr6m5rCYYMy"
secret_key = "8eLps29wsXszNyEhOl9w8dxsOsM2lTzg"
uri.query = URI.encode_www_form({
'currencyPair'=> 'BTC/USD'
})
sha256 = OpenSSL::Digest::SHA256.new
signature = OpenSSL::HMAC.hexdigest(sha256, secret_key, uri.query).upcase
request = Net::HTTP::Get.new(uri)
request.add_field("Api-key", api_key)
request.add_field("Sign", signature)
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl =>uri.scheme == 'https') {|http|
http.request(request)
}
response_data = JSON.parse(response.body())
print response_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment