· 6 years ago · Feb 20, 2020, 09:28 PM
1using System;
2using System.Diagnostics;
3using System.Text;
4using IO.Swagger.Api;
5using IO.Swagger.Client;
6using IO.Swagger.Model;
7using System.Security.Cryptography;
8
9
10namespace ByBit
11{
12 public class Program
13 {
14 [STAThread]
15 static void Main(string[] args)
16 {
17 var timestamp = (long)((DateTime.UtcNow) - new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalMilliseconds;
18
19 // Configure API key authorization: apiKey
20 Configuration.Default.ApiKey.Add("api_key", "KEY");
21 Configuration.Default.ApiKey.Add("secret", "KEY");
22 Configuration.Default.ApiKey.Add("timestamp", timestamp.ToString());
23 Configuration.Default.ApiKey.Add("sign", SignRequest());
24
25
26 var apiOrders = new OrderApi();
27
28 try
29 {
30 //Object result = apiOrders.OrderNewV2("long", "BTCUSD", "Limit", 200, "GoodTillCancel");
31 apiOrders.OrderCancelAll("BTCUSD");
32 //Object result=apiOrders.OrderCancelAllAsyncWithHttpInfo("BTCUSD");
33 }
34 catch (Exception e)
35 { Debug.Print("Exception when calling OrderApi: " + e.Message); }
36 }
37
38 private static string SignRequest()
39 {
40 return RequestEncryptor.CreateSignature(
41 Configuration.Default.ApiKey["secret"], new string[]
42 {
43 $"api_key={Configuration.Default.ApiKey["api_key"]}",
44 $"timestamp={Configuration.Default.ApiKey["timestamp"]}"
45 });
46
47 }
48 }
49
50 /// <summary>
51 ///
52 /// </summary>
53 public static class RequestEncryptor
54 {
55
56 /// <summary>
57 ///
58 /// </summary>
59 /// <param name="secret"></param>
60 /// <param name="message"></param>
61 /// <returns></returns>
62 public static string CreateSignature(string secret, string[] parameters)
63 {
64 var parameterString = string.Join("&", parameters);
65 var signatureBytes = Hmacsha256(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(parameterString));
66
67 return ByteArrayToString(signatureBytes);
68 }
69
70 private static byte[] Hmacsha256(byte[] keyByte, byte[] messageBytes)
71 {
72 using (var hash = new HMACSHA256(keyByte))
73 {
74 return hash.ComputeHash(messageBytes);
75 }
76 }
77
78 public static string ByteArrayToString(byte[] ba)
79 {
80 var hex = new StringBuilder(ba.Length * 2);
81
82 foreach (var b in ba)
83 {
84 hex.AppendFormat("{0:x2}", b);
85 }
86 return hex.ToString();
87 }
88 }
89}