From ee30618566e5e18ee702e517ebaf16c2a4a5c65c Mon Sep 17 00:00:00 2001 From: Jkorf Date: Fri, 20 Mar 2026 16:34:01 +0100 Subject: [PATCH] wip --- .../Authentication/AuthenticationProvider.cs | 2 +- .../{CredentialPair.cs => CredentialSet.cs} | 18 +++++++++--------- CryptoExchange.Net/Clients/BaseApiClient.cs | 7 +++++++ CryptoExchange.Net/Clients/RestApiClient.cs | 15 ++++++++++++--- CryptoExchange.Net/Clients/SocketApiClient.cs | 17 +++++++++++++---- 5 files changed, 42 insertions(+), 17 deletions(-) rename CryptoExchange.Net/Authentication/{CredentialPair.cs => CredentialSet.cs} (97%) diff --git a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs index a78e091..059aabb 100644 --- a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs +++ b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs @@ -474,7 +474,7 @@ namespace CryptoExchange.Net.Authentication /// public abstract class AuthenticationProvider : AuthenticationProvider where TApiCredentials : ApiCredentials - where TCredentialType : CredentialPair + where TCredentialType : CredentialSet { /// /// The specific credential type used for signing requests. diff --git a/CryptoExchange.Net/Authentication/CredentialPair.cs b/CryptoExchange.Net/Authentication/CredentialSet.cs similarity index 97% rename from CryptoExchange.Net/Authentication/CredentialPair.cs rename to CryptoExchange.Net/Authentication/CredentialSet.cs index e7b135a..7c042a8 100644 --- a/CryptoExchange.Net/Authentication/CredentialPair.cs +++ b/CryptoExchange.Net/Authentication/CredentialSet.cs @@ -7,9 +7,9 @@ using System.Text; namespace CryptoExchange.Net.Authentication { /// - /// Credential pair base class + /// Base class for a set of credentials /// - public abstract class CredentialPair : ApiCredentials + public abstract class CredentialSet : ApiCredentials { /// /// The (public) key/identifier for this credential pair @@ -19,12 +19,12 @@ namespace CryptoExchange.Net.Authentication /// /// ctor /// - public CredentialPair() { } + public CredentialSet() { } /// /// ctor /// - public CredentialPair(string key) + public CredentialSet(string key) { Key = key; } @@ -42,7 +42,7 @@ namespace CryptoExchange.Net.Authentication /// /// Api key credentials /// - public class ApiKeyCredential : CredentialPair + public class ApiKeyCredential : CredentialSet { /// /// ctor @@ -60,7 +60,7 @@ namespace CryptoExchange.Net.Authentication /// /// HMAC credentials /// - public class HMACCredential : CredentialPair + public class HMACCredential : CredentialSet { private byte[]? _sBytes; @@ -151,7 +151,7 @@ namespace CryptoExchange.Net.Authentication /// /// RSA credentials /// - public abstract class RSACredential : CredentialPair + public abstract class RSACredential : CredentialSet { /// /// Private key @@ -399,7 +399,7 @@ namespace CryptoExchange.Net.Authentication /// /// Credentials in Ed25519 format /// - public class Ed25519Credential : CredentialPair + public class Ed25519Credential : CredentialSet { private NSec.Cryptography.Key? _signKey; @@ -499,7 +499,7 @@ namespace CryptoExchange.Net.Authentication /// /// Credentials in ECDsa format /// - public class ECDsaCredential : CredentialPair + public class ECDsaCredential : CredentialSet { /// /// Private key diff --git a/CryptoExchange.Net/Clients/BaseApiClient.cs b/CryptoExchange.Net/Clients/BaseApiClient.cs index daea89a..717d946 100644 --- a/CryptoExchange.Net/Clients/BaseApiClient.cs +++ b/CryptoExchange.Net/Clients/BaseApiClient.cs @@ -27,6 +27,11 @@ namespace CryptoExchange.Net.Clients /// protected bool _disposing; + /// + /// Whether a proxy is configured + /// + protected bool _proxyConfigured; + /// /// Name of the client /// @@ -88,6 +93,8 @@ namespace CryptoExchange.Net.Clients ApiOptions = apiOptions; OutputOriginalData = outputOriginalData; BaseAddress = baseAddress; + + _proxyConfigured = ClientOptions.Proxy != null; } /// diff --git a/CryptoExchange.Net/Clients/RestApiClient.cs b/CryptoExchange.Net/Clients/RestApiClient.cs index 04587f1..bbcc7d8 100644 --- a/CryptoExchange.Net/Clients/RestApiClient.cs +++ b/CryptoExchange.Net/Clients/RestApiClient.cs @@ -890,6 +890,7 @@ namespace CryptoExchange.Net.Clients /// public virtual void SetOptions(UpdateOptions options) { + _proxyConfigured = options.Proxy != null; ClientOptions.Proxy = options.Proxy; ClientOptions.RequestTimeout = options.RequestTimeout ?? ClientOptions.RequestTimeout; @@ -958,15 +959,23 @@ namespace CryptoExchange.Net.Clients public override void SetApiCredentials(TApiCredentials credentials) { base.SetApiCredentials(credentials); - AuthenticationProvider = CreateAuthenticationProvider(credentials); + + AuthenticationProvider = null; + _authProviderInitialized = false; + ApiCredentials = credentials; } /// public override void SetOptions(UpdateOptions options) { base.SetOptions(options); - if (ApiCredentials != null) - AuthenticationProvider = CreateAuthenticationProvider(ApiCredentials); + + if (options.ApiCredentials != null) + { + AuthenticationProvider = null; + _authProviderInitialized = false; + ApiCredentials = options.ApiCredentials; + } } } } diff --git a/CryptoExchange.Net/Clients/SocketApiClient.cs b/CryptoExchange.Net/Clients/SocketApiClient.cs index e01814e..428a5d9 100644 --- a/CryptoExchange.Net/Clients/SocketApiClient.cs +++ b/CryptoExchange.Net/Clients/SocketApiClient.cs @@ -1102,13 +1102,14 @@ namespace CryptoExchange.Net.Clients /// public virtual void SetOptions(UpdateOptions options) { - var previousProxyIsSet = ClientOptions.Proxy != null; + var previousProxyIsSet = _proxyConfigured; ClientOptions.Proxy = options.Proxy; ClientOptions.RequestTimeout = options.RequestTimeout ?? ClientOptions.RequestTimeout; ApiCredentials = (TApiCredentials?)options.ApiCredentials?.Copy() ?? ApiCredentials; + _proxyConfigured = options.Proxy != null; if ((!previousProxyIsSet && options.Proxy == null) || _socketConnections.IsEmpty) { @@ -1180,15 +1181,23 @@ namespace CryptoExchange.Net.Clients /// public override void SetApiCredentials(TApiCredentials credentials) { - AuthenticationProvider = CreateAuthenticationProvider(credentials); + AuthenticationProvider = null; + _authProviderInitialized = false; + ApiCredentials = credentials; + base.SetApiCredentials(credentials); } /// public override void SetOptions(UpdateOptions options) { - if (ApiCredentials != null) - AuthenticationProvider = CreateAuthenticationProvider(ApiCredentials); + if (options.ApiCredentials != null) + { + AuthenticationProvider = null; + _authProviderInitialized = false; + ApiCredentials = options.ApiCredentials; + } + base.SetOptions(options); } }