Bitstamp API signature in Ruby

0 votes

I'm trying to access balance of Bitstamp account with API.

secret = "secret"
key = "key"
nonce = (1000*Time.now.to_f).to_i.to_s
client_id = "123123"

message = nonce + client_id + key
signature = HMAC::SHA256.hexdigest(secret, message).upcase

puts open("https://www.bitstamp.net/api/balance/?nonce=#{nonce}&key=#{key}&signature=#{signature}").read

I see that it generates all the attributes that are required

https://www.bitstamp.net/api/balance/?nonce=1392137355403&key=key&signature=955A3FFC6FEBE69385B9503307873DBCD21E9B7B8EDE67817FFF70961189CE50

But still i get this error. 

{"error": "Missing key, signature and nonce parameters"}
Aug 28, 2018 in Blockchain by digger
• 26,740 points
1,073 views

1 answer to this question.

0 votes

Here is a code that works:

require 'open-uri'
require 'json'
require 'base64'
require 'openssl'
require 'hmac-sha2'
require 'net/http'
require 'net/https'
require 'uri'

def bitstamp_private_request(method, attrs = {})
  secret = "xxx"
  key = "xxx"
  client_id = "xxx"
  nonce = nonce_generator

  message = nonce + client_id + key
  signature = HMAC::SHA256.hexdigest(secret, message).upcase

  url = URI.parse("https://www.bitstamp.net/api/#{method}/")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  data = {
    nonce: nonce,
    key: key,
    signature: signature
  }
  data.merge!(attrs)
  data = data.map { |k,v| "#{k}=#{v}"}.join('&')

  headers = {
    'Content-Type' => 'application/x-www-form-urlencoded'
  }

  resp = http.post(url.path, data, headers)
  console_log "https://www.bitstamp.net/api/#{method}/"
  resp.body
end

def nonce_generator
  (Time.now.to_f*1000).to_i.to_s
end
answered Aug 28, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer

Curl Script Usage in Ethereum Blockcypher's API

You should store and keep the private ...READ MORE

answered May 28, 2018 in Blockchain by Christine
• 15,790 points
1,431 views
0 votes
1 answer

Send Payment error in Blockchain API

The following code should help: $address = null; try ...READ MORE

answered Jul 6, 2018 in Blockchain by Perry
• 17,100 points
1,752 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,427 views
0 votes
1 answer

Creating bitcoin address in ruby

When you calculate the SHA256 checksum, make ...READ MORE

answered Aug 28, 2018 in Blockchain by slayer
• 29,370 points
758 views
0 votes
1 answer

How to generate coin address using bitcoin-ruby?

The only difference between the addresses is ...READ MORE

answered Aug 29, 2018 in Blockchain by slayer
• 29,370 points
802 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

How to generate Bitcoin address in Ruby?

In your generate_address_from_public_key_hash method, the checksum should be over ...READ MORE

answered Aug 22, 2018 in Blockchain by slayer
• 29,370 points
2,683 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP