· 5 years ago · Jun 21, 2020, 05:18 PM
1using System;
2using System.Net.Http;
3using System.Net.Http.Headers;
4using System.Security.Cryptography;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace AspNetCore.Api.CoinSpot
9{
10 public class CoinSpotClient
11 {
12 public static async Task<string> GetMyDeposits()
13 {
14 long nonce = DateTime.UtcNow.Ticks; //Get ticks for nonce
15 string APIKey = "-";
16 string Secret = "-";
17
18 HttpClient client = new HttpClient();
19 string url = "https://www.coinspot.com.au/api/ro/my/deposits";
20
21 var request = new HttpRequestMessage()
22 {
23 RequestUri = new Uri(url),
24 Method = HttpMethod.Post
25 };
26 request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
27
28 var hash = new StringBuilder();
29
30 //Create data string in JSON format
31 string signatureBaseString = "{\"nonce\":\"" + nonce + "\"}";
32
33 //Set POST data
34 request.Content = new StringContent(signatureBaseString, Encoding.UTF8, "application/json");
35 //Calculate HMAC512
36 hash.Append(SHA512_ComputeHash(request.Content.ReadAsStringAsync().Result, Secret));
37
38 //Set request headers
39 request.Headers.Add("key", APIKey);
40 request.Headers.Add("sign", hash.ToString());
41
42 //Send the request
43 HttpResponseMessage response = await client.SendAsync(request);
44
45 //Response as String
46 var result = await response.Content.ReadAsStringAsync();
47 return result;
48 }
49
50 public static string SHA512_ComputeHash(string text, string secretKey)
51 {
52 var hash = new StringBuilder();
53 byte[] secretkeyBytes = Encoding.UTF8.GetBytes(secretKey);
54 byte[] inputBytes = Encoding.UTF8.GetBytes(text);
55 using (var hmac = new HMACSHA512(secretkeyBytes))
56 {
57 byte[] hashValue = hmac.ComputeHash(inputBytes); //Update HMAC with postdata
58 foreach (var theByte in hashValue)
59 {
60 hash.Append(theByte.ToString("x2")); //Output to HEX
61 }
62 }
63
64 return hash.ToString();
65 }
66 }
67}