2
votes

Trying to authenticate to crypto.com but cant seem to get it to work... Been trying few days now and getting really frustrated, any help? Their api docs @ https://exchange-docs.crypto.com/?python#digital-signature

Heres how to do it + sample code, Im stuck..


The authentication is based on the pairing of the API Key, along with the HMAC-SHA256 hash of the request parameters using the API Secret as the cryptographic key.

The algorithm for generating the HMAC-SHA256 signature is as follows:

If "params" exist in the request, sort the request parameter keys in ascending order.

Combine all the ordered parameter keys as key + value (no spaces, no delimiters). Let's call this the parameter string

Next, do the following: method + id + api_key + parameter string + nonce

Use HMAC-SHA256 to hash the above using the API Secret as the cryptographic key

Encode the output as a hex string -- this is your Digital Signature

import hmac
import hashlib
import json
import requests
import time

API_KEY = "API_KEY"
SECRET_KEY = "SECRET_KEY"

req = {
  "id": 11,
  "method": "private/get-order-detail",
  "api_key": API_KEY,
  "params": {
    "order_id": "337843775021233500",
  },
  "nonce": int(time.time() * 1000)
};

# First ensure the params are alphabetically sorted by key
paramString = ""

if "params" in req:
  for key in req['params']:
    paramString += key
    paramString += str(req['params'][key])

sigPayload = req['method'] + str(req['id']) + req['api_key'] + paramString + str(req['nonce'])

request['sig'] = hmac.new(
  bytes(str(SECRET_KEY), 'utf-8'),
  msg=bytes(sigPayload, 'utf-8'),
  digestmod=hashlib.sha256
).hexdigest()
1

1 Answers

0
votes

I ran into the same issue today. Overall I found the API doc from crypto.com very poor.

Change the last bit of your code so that you add a field to the "req" dictionnary:

req['sig'] = hmac.new(
  bytes(str(SECRET_KEY), 'utf-8'),
  msg=bytes(sigPayload, 'utf-8'),
  digestmod=hashlib.sha256
).hexdigest()

To get your order detail (as is written in your example), you can make the API call with a POST request like so:

api_url = f"https://api.crypto.com/v2/{req["method"]}"
response = requests.post(api_url, json=req)
order_detail = response.json()