2

I am following Coinbase pro API and API 'invalid signature' error. I have attached my code. I have also try with production account but error is still same.

  class Coinbase
    def initialize()
      @end_point = 'https://api-public.sandbox.pro.coinbase.com'
      @key = "b34ae4ffd38acfc3f11e272654fa77c4"
      @secret = "0k+YreiCq5tY3UdShw0VB0RI/kKiLv1vNGpNKpaDzDLtVPFNzlMGgoFljYRO4qsH5KCZ9M5upnq5/rxSVzENdg=="
      @passphrase = "50fyu9n04nu"
      @timestamp = Time.now.to_i
      @sign = access_signature
    end

    def access_signature
      request_path="/accounts"
      method='GET'
      body=''
      body = body.to_json if body.is_a?(Hash)
      timestamp = @timestamp
      what = "#{timestamp}#{method}#{request_path}#{body}";
      secret = Base64.decode64(@secret)
      hash  = OpenSSL::HMAC.digest('sha256', secret, what)
      Base64.strict_encode64(hash)
    end

    def hit_coinbase_api
      call :get, "/accounts"
    end

    def call(method, path, params = {}, parse_response_as_json = true, with_auth_token = true)
      uri = URI(@end_point + path)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      request = nil
      if method == :get
        request = Net::HTTP::Get.new(uri.request_uri)
      else
        raise 'Unsupported request method'
      end

      request.body = params.to_json
      request.add_field('Content-Type', 'application/json')
      request.add_field('CB-ACCESS-KEY', @key)
      request.add_field('CB-ACCESS-SIGN', @sign)
      request.add_field('CB-ACCESS-TIMESTAMP', @timestamp)
      request.add_field('CB-ACCESS-PASSPHRASE', @passphrase)

      response = http.request(request)
      json_resp = JSON.parse(response.body)
      puts json_resp
    end
  end

console> Coinbase.new.hit_coinbase_api Help will be appreciable. Thanks in advance :)

Anuj Dhanju
  • 523
  • 3
  • 13

1 Answers1

1

I have worked on your code debugging why it is not working, looks like there is header issue when we use Net::HTTP. I have used http wrapper to call API and it worked

module Coinbase
  class ApiError < RuntimeError; end
  class Api

    def initialize()
      @end_point = 'https://api-public.sandbox.pro.coinbase.com'
      @key = "b34ae4ffd38acfc3f11e272654fa77c4"
      @secret = "0k+YreiCq5tY3UdShw0VB0RI/kKiLv1vNGpNKpaDzDLtVPFNzlMGgoFljYRO4qsH5KCZ9M5upnq5/rxSVzENdg=="
      @passphrase = "50fyu9n04nu"
      @timestamp = Time.now.to_i
    end

    def access_signature method, request_path, body
      body = body.to_json if body.is_a?(Hash)
      what = "#{@timestamp}#{method}#{request_path}#{body}";
      # create a sha256 hmac with the secret
      secret = Base64.decode64(@secret)
      hash  = OpenSSL::HMAC.digest('sha256', secret, what)
      Base64.strict_encode64(hash)
    end

    def hit_coinbase_api
       call "GET",  "/accounts"
    end

    private

    def call(method, request_path, params = nil)
      @sign = access_signature(method, request_path, params)
      @headers = {
        "Content-Type" => "application/json",
        "Accept" => "application/json",
        "CB-ACCESS-KEY" => @key,
        "CB-ACCESS-SIGN" => @sign,
        "CB-ACCESS-TIMESTAMP" => @timestamp,
        "CB-ACCESS-PASSPHRASE" => @passphrase,
        "User-Agent"=> 'request'
      }
      if method=='GET'
        response = HTTParty.get("#{@end_point}#{request_path}",
                { 
                  :headers => @headers
                })
      else 
        response = HTTParty.post("#{@end_point}#{request_path}",
                { 
                  :body => params.to_json,
                  :headers => @headers
                })
      end
      JSON.parse(response.body)
    end
  end
end

Let me know if anyone found why it is not working with Net::HTTP

Vishal G
  • 1,521
  • 11
  • 30