Skip to content

Commit

Permalink
Add caching for passphrase sign
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Aug 1, 2024
1 parent f08aeaf commit 192882d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Kucoin.Net/KucoinAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Kucoin.Net.Objects.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand All @@ -17,6 +18,8 @@ namespace Kucoin.Net
{
internal class KucoinAuthenticationProvider : AuthenticationProvider<KucoinApiCredentials>
{
private readonly static ConcurrentDictionary<string, string> _phraseCache = new();

public KucoinAuthenticationProvider(KucoinApiCredentials credentials): base(credentials)
{
if (credentials.CredentialType != ApiCredentialsType.Hmac)
Expand Down Expand Up @@ -50,7 +53,14 @@ public override void AuthenticateRequest(
uri = uri.SetParameters(uriParameters, arraySerialization);
headers.Add("KC-API-KEY", _credentials.Key!.GetString());
headers.Add("KC-API-TIMESTAMP", GetMillisecondTimestamp(apiClient).ToString());
headers.Add("KC-API-PASSPHRASE", SignHMACSHA256(_credentials.PassPhrase.GetString(), SignOutputType.Base64));
var phrase = _credentials.PassPhrase.GetString();
if (!_phraseCache.TryGetValue(phrase, out var phraseSign))
{
phraseSign = SignHMACSHA256(phrase, SignOutputType.Base64);
_phraseCache.TryAdd(phrase, phraseSign);
}

headers.Add("KC-API-PASSPHRASE", phraseSign);
headers.Add("KC-API-KEY-VERSION", "3");

string jsonContent = string.Empty;
Expand Down

0 comments on commit 192882d

Please sign in to comment.