protected override async Task ProcessRequestAsync(HttpWebRequest request, Dictionary <string, object> payload) { var timestamp = TimeHelper.GetTotalMilliseconds().ToString(); payload?.Remove("method"); payload?.Remove("nonce"); string msg = CryptoUtility.GetFormForPayload(payload, false); msg = string.Join("&", new SortedSet <string>(msg.Split('&'), StringComparer.Ordinal)); //var msg = payload?.Count > 0 ? string.Join("&", new SortedSet<string>(payload.Select(a => $"{a.Key}={a.Value}").ToList())) : ""; string signStr = (request.Method == "GET") ? $"{request.Method}{request.Address}{timestamp}" : $"{request.Method}{request.Address.AbsoluteUri}{timestamp}{msg}"; Debug.WriteLine(signStr); var sig = CryptoUtility.SHA1SignBase64(signStr, PrivateApiKey.ToBytes()); request.Headers = new WebHeaderCollection() { { "FC-ACCESS-KEY", PublicApiKey.ToUnsecureString() }, { "FC-ACCESS-SIGNATURE", sig }, { "FC-ACCESS-TIMESTAMP", timestamp } }; if (request.Method == "POST") { request.ContentType = "application/json"; msg = CryptoUtility.GetJsonForPayload(payload); await CryptoUtility.WriteToRequestAsync(request, msg); } else { request.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; } }
protected override async Task ProcessRequestAsync(HttpWebRequest request, Dictionary <string, object> payload) { if (CanMakeAuthenticatedRequest(payload)) { string payloadForm = CryptoUtility.GetFormForPayload(payload, false); request.Headers["API-Key"] = PublicApiKey.ToUnsecureString(); request.Headers["Sign"] = CryptoUtility.SHA256Sign(payloadForm, PrivateApiKey.ToBytes()).ToUpperInvariant(); await request.WriteToRequestAsync(payloadForm); } }
protected 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.ToBytes()); 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); }