crypto.com exchange API has close to no documentation available, at least for C#. I am trying to get as little as the account summary, and failing miserably. Can we try to have the right functions here for everybody to use?
Here is the doc link
Basically only 2 functions as per below, classes removed for simplification:
using Newtonsoft.Json;
using RestSharp;
using System.Security.Cryptography;
using System.Text;
namespace Crypto.Account
{
internal class AccountSummary
{
private readonly string apiKey = "xxxxxx";
private readonly string apiSecret = "xxxxx";
private readonly string apiUrl = "https://api.crypto.com/v2/private/get-account-summary";
public AccountResponse GetAccountSummary()
{
var client = new RestClient();
var request = new RestRequest(apiUrl, Method.Post);
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
string sign = GetSignature(timestamp);
var requestBody = new
{
id = 1,
method = "private/get-account-summary",
@params = new { },
nonce = timestamp,
api_key = apiKey,
sig = sign
};
request.AddJsonBody(requestBody);
var response = client.Execute(request);
if (response.IsSuccessful)
{
//return JsonConvert.DeserializeObject<AccountResponse>(response.Content);
return "hooray";
}
else
{
throw new Exception($"Error: {response.StatusCode}, {response.Content}");
}
}
private string GetSignature(long timestamp)
{
string sigPayload = $"{apiKey}{timestamp}private/get-account-summary";
using (var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(apiSecret)))
{
var hash = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(sigPayload));
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
}
I am getting either this:
10002 401 UNAUTHORIZED Not authenticated, or key/signature incorrect
or this
10003 401 IP_ILLEGAL IP address not whitelisted
What needs to be corrected? Any suggestions?