protected override async Task ProcessRequestAsync(HttpWebRequest request, Dictionary <string, object> payload)
 {
     if (payload == null || PrivateApiKey == null || PublicApiKey == null || !payload.ContainsKey("nonce"))
     {
         await CryptoUtility.WritePayloadFormToRequestAsync(request, payload);
     }
     else
     {
         string nonce = payload["nonce"].ToStringInvariant();
         payload.Remove("nonce");
         string form = CryptoUtility.GetFormForPayload(payload);
         // nonce must be first on Kraken
         form = "nonce=" + nonce + (string.IsNullOrWhiteSpace(form) ? string.Empty : "&" + form);
         using (SHA256 sha256 = SHA256Managed.Create())
         {
             string hashString  = nonce + form;
             byte[] sha256Bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(hashString));
             byte[] pathBytes   = Encoding.UTF8.GetBytes(request.RequestUri.AbsolutePath);
             byte[] sigBytes    = new byte[sha256Bytes.Length + pathBytes.Length];
             pathBytes.CopyTo(sigBytes, 0);
             sha256Bytes.CopyTo(sigBytes, pathBytes.Length);
             byte[] privateKey = System.Convert.FromBase64String(CryptoUtility.ToUnsecureString(PrivateApiKey));
             using (System.Security.Cryptography.HMACSHA512 hmac = new System.Security.Cryptography.HMACSHA512(privateKey))
             {
                 string sign = System.Convert.ToBase64String(hmac.ComputeHash(sigBytes));
                 request.Headers.Add("API-Sign", sign);
             }
         }
         request.Headers.Add("API-Key", CryptoUtility.ToUnsecureString(PublicApiKey));
         await CryptoUtility.WriteToRequestAsync(request, form);
     }
 }
        protected override Task ProcessRequestAsync(HttpWebRequest request, Dictionary <string, object> payload)
        {
            if (CanMakeAuthenticatedRequest(payload))
            {
                payload.Add("request", request.RequestUri.AbsolutePath);
                string json      = JsonConvert.SerializeObject(payload);
                string json64    = System.Convert.ToBase64String(CryptoUtility.UTF8EncodingNoPrefix.GetBytes(json));
                string hexSha384 = CryptoUtility.SHA384Sign(json64, CryptoUtility.ToUnsecureString(PrivateApiKey));
                request.Headers["X-GEMINI-PAYLOAD"]   = json64;
                request.Headers["X-GEMINI-SIGNATURE"] = hexSha384;
                request.Headers["X-GEMINI-APIKEY"]    = CryptoUtility.ToUnsecureString(PublicApiKey);
                request.Method = "POST";

                // gemini doesn't put the payload in the post body it puts it in as a http header, so no need to write to request stream
            }
            return(base.ProcessRequestAsync(request, payload));
        }
 protected override async Task ProcessRequestAsync(HttpWebRequest request, Dictionary <string, object> payload)
 {
     if (CanMakeAuthenticatedRequest(payload))
     {
         // gdax is funny and wants a seconds double for the nonce, weird... we convert it to double and back to string invariantly to ensure decimal dot is used and not comma
         string timestamp = payload["nonce"].ToStringInvariant();
         payload.Remove("nonce");
         string form   = CryptoUtility.GetJsonForPayload(payload);
         byte[] secret = CryptoUtility.ToBytesBase64Decode(PrivateApiKey);
         string toHash = timestamp + request.Method.ToUpper() + request.RequestUri.PathAndQuery + form;
         string signatureBase64String = CryptoUtility.SHA256SignBase64(toHash, secret);
         secret = null;
         toHash = null;
         request.Headers["CB-ACCESS-KEY"]        = PublicApiKey.ToUnsecureString();
         request.Headers["CB-ACCESS-SIGN"]       = signatureBase64String;
         request.Headers["CB-ACCESS-TIMESTAMP"]  = timestamp;
         request.Headers["CB-ACCESS-PASSPHRASE"] = CryptoUtility.ToUnsecureString(Passphrase);
         await CryptoUtility.WriteToRequestAsync(request, form);
     }
 }
Exemplo n.º 4
0
        protected override async Task ProcessRequestAsync(HttpWebRequest request, Dictionary <string, object> payload)
        {
            if (CanMakeAuthenticatedRequest(payload))
            {
                payload.Remove("nonce");
                string body      = CryptoUtility.GetJsonForPayload(payload);
                string timestamp = ((int)DateTime.UtcNow.UnixTimestampFromDateTimeSeconds()).ToStringInvariant();
                string msg       = timestamp + request.Method + request.RequestUri.PathAndQuery + (request.Method.Equals("POST") ? body : string.Empty);
                string sign      = CryptoUtility.SHA256SignBase64(msg, CryptoUtility.ToBytesBase64Decode(PrivateApiKey));

                request.Headers["AC-ACCESS-KEY"]        = CryptoUtility.ToUnsecureString(PublicApiKey);
                request.Headers["AC-ACCESS-SIGN"]       = sign;
                request.Headers["AC-ACCESS-TIMESTAMP"]  = timestamp;
                request.Headers["AC-ACCESS-PASSPHRASE"] = CryptoUtility.ToUnsecureString(Passphrase);

                if (request.Method == "POST")
                {
                    await CryptoUtility.WriteToRequestAsync(request, body);
                }
            }
        }