Skip to content

Instantly share code, notes, and snippets.

@agustinf
Last active February 6, 2019 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agustinf/46fd63b28dfc05c486ab3bb8dbb8dca9 to your computer and use it in GitHub Desktop.
Save agustinf/46fd63b28dfc05c486ab3bb8dbb8dca9 to your computer and use it in GitHub Desktop.
Signs a request in ruby
require "base64"
require "digest"
require "net/http"
require "uri"
require "JSON"
PRIVATE = ARGV[1]
PUBLIC = ARGV[2]
def gen_nonce
# Sleeps 200ms to avoid flooding the server with requests.
sleep(0.2)
Time.now.to_i.to_s
end
def get_headers_for(method, path = nil, payload = nil)
nonce = gen_nonce
str = "#{method} #{path} "
str += Base64.strict_encode64(payload.to_json) + ' ' if payload
str += nonce
puts "signing #{str}"
signature = Digest::HMAC.hexdigest(str, PRIVATE, Digest::SHA384)
{
'X-SBTC-APIKEY': PUBLIC,
'X-SBTC-NONCE': nonce,
'X-SBTC-SIGNATURE': signature,
'Content-Type': 'application/json'
}
end
path = ARGV[0]
payload = {
type: 'bid',
price_type: 'limit',
limit: '20000',
amount: '0.00001'
}
headers = get_headers_for("POST", path, payload)
uri = URI.parse("https://www.buda.com#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.body = payload.to_json
headers.each do |key, value|
request.add_field(key, value)
end
response = http.request(request)
puts response.code
puts response
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment