示例#1
0
        protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload)
        {
            if (CanMakeAuthenticatedRequest(payload))
            {
                if (payload.TryGetValue("body", out var body))
                {
                    payload.Remove("body");
                }

                var nonce = payload["nonce"].ToString();
                payload.Remove("nonce");

                var json = JsonConvert.SerializeObject(body ?? payload);
                if (json == "{}")
                {
                    json = "";
                }

                var passphrase = Passphrase?.ToUnsecureString();
                if (string.IsNullOrEmpty(passphrase))
                {
                    passphrase = PrivateApiKey?.ToUnsecureString();
                }

                var hexSha384 = CryptoUtility.SHA384Sign(
                    $"{request.RequestUri.AbsolutePath.Replace("/spot", string.Empty)}{nonce}{json}",
                    passphrase);
                request.AddHeader("btse-sign", hexSha384);
                request.AddHeader("btse-nonce", nonce);
                request.AddHeader("btse-api", PublicApiKey.ToUnsecureString());
                await request.WriteToRequestAsync(json);
            }

            await base.ProcessRequestAsync(request, payload);
        }
示例#2
0
        protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload)
        {
            if (CanMakeAuthenticatedRequest(payload))
            {
                request.AddHeader("KC-API-KEY", PublicApiKey.ToUnsecureString());
                request.AddHeader("KC-API-TIMESTAMP", payload["nonce"].ToStringInvariant());
                request.AddHeader("KC-API-PASSPHRASE", CryptoUtility.SHA256Sign(Passphrase.ToUnsecureString(), PrivateApiKey.ToUnsecureString(), true));
                var endpoint = request.RequestUri.PathAndQuery;
                //For Gets, Deletes, no need to add the parameters in JSON format
                var message = "";
                var sig     = "";
                if (request.Method == "GET" || request.Method == "DELETE")
                {
                    //Request will be a querystring
                    message = string.Format("{0}{1}{2}", payload["nonce"], request.Method, endpoint);
                    sig     = CryptoUtility.SHA256Sign(message, PrivateApiKey.ToUnsecureString(), true);
                }
                else if (request.Method == "POST")
                {
                    message = string.Format("{0}{1}{2}{3}", payload["nonce"], request.Method, endpoint, CryptoUtility.GetJsonForPayload(payload, true));
                    sig     = CryptoUtility.SHA256Sign(message, PrivateApiKey.ToUnsecureString(), true);
                }
                request.AddHeader("KC-API-KEY-VERSION", 2.ToStringInvariant());
                request.AddHeader("KC-API-SIGN", sig);
            }

            if (request.Method == "POST")
            {
                string msg     = CryptoUtility.GetJsonForPayload(payload, true);
                byte[] content = msg.ToBytesUTF8();
                await request.WriteAllAsync(content, 0, content.Length);
            }
        }
示例#3
0
        protected internal override Uri ProcessRequestUrl(UriBuilder url, Dictionary <string, object> payload, string method)
        {
            if (CanMakeAuthenticatedRequest(payload))
            {
                if (!payload.ContainsKey("method"))
                {
                    return(url.Uri);
                }
                method = payload["method"].ToStringInvariant();
                payload.Remove("method");

                var dict = new Dictionary <string, object>
                {
                    ["Timestamp"]        = DateTime.UtcNow.ToString("s"),
                    ["AccessKeyId"]      = PublicApiKey.ToUnsecureString(),
                    ["SignatureMethod"]  = "HmacSHA256",
                    ["SignatureVersion"] = "2"
                };

                string msg = null;
                if (method == "GET")
                {
                    dict = dict.Concat(payload).ToDictionary(x => x.Key, x => x.Value);
                }

                msg = CryptoUtility.GetFormForPayload(dict, false);

                // must sort case sensitive
                msg = string.Join("&", new SortedSet <string>(msg.Split('&'), StringComparer.Ordinal));

                StringBuilder sb = new StringBuilder();
                sb.Append(method).Append("\n")
                .Append(url.Host).Append("\n")
                .Append(url.Path).Append("\n")
                .Append(msg);

                var sign    = CryptoUtility.SHA256SignBase64(sb.ToString(), PrivateApiKey.ToBytesUTF8());
                var signUrl = Uri.EscapeDataString(sign);
                msg += $"&Signature={signUrl}";

                // https://github.com/huobiapi/API_Docs_en/wiki/Signing_API_Requests
                // API Authentication Change
                var privateSign    = GetPrivateSignatureStr(Passphrase.ToUnsecureString(), sign);
                var privateSignUrl = Uri.EscapeDataString(privateSign);
                msg += $"&PrivateSignature={privateSignUrl}";

                url.Query = msg;
            }
            return(url.Uri);
        }