1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2026-04-13 00:22:22 +00:00
This commit is contained in:
JKorf 2026-03-15 21:51:00 +01:00
parent 7598326bd5
commit 4a76897851
5 changed files with 83 additions and 77 deletions

View File

@ -19,7 +19,7 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// The public key/identifier for the provided credentials
/// </summary>
public string PublicKey => CredentialPairs.First().PublicKey;
public string Key => CredentialPairs.First().Key;
/// <summary>
/// HMAC credentials
@ -140,54 +140,54 @@ namespace CryptoExchange.Net.Authentication
return CredentialPairs.OfType<T>().SingleOrDefault();
}
// /// <summary>
// /// Create API credentials using an API key and secret generated by the server
// /// </summary>
// public static ApiCredentials HmacCredentials(string apiKey, string apiSecret, string? pass = null)
// {
// return new ApiCredentials(new HMACCredential(apiKey, apiSecret, pass));
// }
///// <summary>
///// Create API credentials using an API key and secret generated by the server
///// </summary>
//public static ApiCredentials HmacCredentials(string apiKey, string apiSecret, string? pass = null)
//{
// return new ApiCredentials(new HMACCredential(apiKey, apiSecret, pass));
//}
//#if NETSTANDARD2_1_OR_GREATER || NET7_0_OR_GREATER
// /// <summary>
// /// Create API credentials using an API key and an RSA private key in PEM format
// /// </summary>
// public static ApiCredentials RsaPemCredentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new RSAPemCredential(publicKey, privateKey));
// }
//#endif
//#if NETSTANDARD2_1_OR_GREATER || NET7_0_OR_GREATER
// /// <summary>
// /// Create API credentials using an API key and an RSA private key in PEM format
// /// </summary>
// public static ApiCredentials RsaPemCredentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new RSAPemCredential(publicKey, privateKey));
// }
//#endif
// /// <summary>
// /// Create API credentials using an API key and an RSA private key in XML format
// /// </summary>
// public static ApiCredentials RsaXmlCredentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new RSAXmlCredential(publicKey, privateKey));
// }
// /// <summary>
// /// Create API credentials using an API key and an RSA private key in XML format
// /// </summary>
// public static ApiCredentials RsaXmlCredentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new RSAXmlCredential(publicKey, privateKey));
// }
//#if NET8_0_OR_GREATER
// /// <summary>
// /// Create API credentials using an API key and an Ed25519 private key
// /// </summary>
// public static ApiCredentials Ed25519Credentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new ED25519Credential(publicKey, privateKey));
// }
//#endif
//#if NET8_0_OR_GREATER
// /// <summary>
// /// Create API credentials using an API key and an Ed25519 private key
// /// </summary>
// public static ApiCredentials Ed25519Credentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new ED25519Credential(publicKey, privateKey));
// }
//#endif
// /// <summary>
// /// Create API credentials using an API key and an Ecdsa private key
// /// </summary>
// public static ApiCredentials EcdsaCredentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new ECDSACredential(publicKey, privateKey));
// }
// /// <summary>
// /// Create API credentials using an API key and an Ecdsa private key
// /// </summary>
// public static ApiCredentials EcdsaCredentials(string publicKey, string privateKey)
// {
// return new ApiCredentials(new ECDSACredential(publicKey, privateKey));
// }
/// <summary>
/// Load a key from a file
/// </summary>
#warning check
#warning check
public static string ReadFromFile(string path)
{
using var fileStream = File.OpenRead(path);

View File

@ -27,7 +27,7 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// The public identifier for the provided credentials
/// </summary>
public abstract string PublicKey { get; }
public abstract string Key { get; }
/// <summary>
/// The supported credential types
@ -485,7 +485,7 @@ namespace CryptoExchange.Net.Authentication
public TCredentialType Credential { get; set; }
/// <inheritdoc />
public override string PublicKey => Credential.PublicKey;
public override string Key => Credential.Key;
/// <summary>
/// ctor

View File

@ -1,7 +1,4 @@
#if NET8_0_OR_GREATER
using NSec.Cryptography;
#endif
using System;
using System;
using System.Security.Cryptography;
using System.Text;
@ -13,9 +10,9 @@ namespace CryptoExchange.Net.Authentication
public abstract class CredentialPair
{
/// <summary>
/// API credentials identifier
/// The (public) key/identifier for this credential pair
/// </summary>
public string PublicKey { get; set; }
public string Key { get; set; }
/// <summary>
/// Type of credentials
/// </summary>
@ -24,9 +21,9 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// ctor
/// </summary>
public CredentialPair(string publicKey)
public CredentialPair(string key)
{
PublicKey = publicKey;
Key = key;
}
}
@ -105,7 +102,7 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// Passphrase
/// </summary>
public string? Passphrase { get; set; }
public string? Pass { get; set; }
/// <inheritdoc />
public override ApiCredentialsType CredentialType => ApiCredentialsType.Rsa;
@ -113,16 +110,16 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// ctor
/// </summary>
/// <param name="publicKey">Public key</param>
/// <param name="key">Public key</param>
/// <param name="privateKey">Private key</param>
/// <param name="passphrase">Passphrase</param>
public RSACredential(string publicKey, string privateKey, string? passphrase = null) : base(publicKey)
/// <param name="pass">Passphrase</param>
public RSACredential(string key, string privateKey, string? pass = null) : base(key)
{
if (string.IsNullOrEmpty(publicKey) || string.IsNullOrEmpty(privateKey))
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(privateKey))
throw new ArgumentException("Public and private key can't be null/empty");
PrivateKey = privateKey;
Passphrase = passphrase;
Pass = pass;
}
/// <summary>
@ -140,10 +137,10 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// ctor
/// </summary>
/// <param name="publicKey">Public key</param>
/// <param name="key">Public key</param>
/// <param name="privateKey">Private key</param>
/// <param name="passphrase">Passphrase</param>
public RSAPemCredential(string publicKey, string privateKey, string? passphrase = null) : base(publicKey, privateKey, passphrase)
public RSAPemCredential(string key, string privateKey, string? passphrase = null) : base(key, privateKey, passphrase)
{
}
@ -175,10 +172,10 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// ctor
/// </summary>
/// <param name="publicKey">Public key</param>
/// <param name="key">Public key</param>
/// <param name="privateKey">Private key</param>
/// <param name="passphrase">Passphrase</param>
public RSAXmlCredential(string publicKey, string privateKey, string? passphrase = null) : base(publicKey, privateKey, passphrase)
public RSAXmlCredential(string key, string privateKey, string? passphrase = null) : base(key, privateKey, passphrase)
{
}
@ -199,7 +196,7 @@ namespace CryptoExchange.Net.Authentication
/// </summary>
public class ED25519Credential : CredentialPair
{
private Key? _signKey;
private NSec.Cryptography.Key? _signKey;
/// <summary>
/// Private key
@ -208,7 +205,7 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// Passphrase
/// </summary>
public string? Passphrase { get; set; }
public string? Pass { get; set; }
/// <inheritdoc />
public override ApiCredentialsType CredentialType => ApiCredentialsType.Ed25519;
@ -216,19 +213,19 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// ctor
/// </summary>
/// <param name="publicKey">Public key</param>
/// <param name="key">Public key</param>
/// <param name="privateKey">Private key</param>
/// <param name="passphrase">Passphrase</param>
public ED25519Credential(string publicKey, string privateKey, string? passphrase = null) : base(publicKey)
/// <param name="pass">Passphrase</param>
public ED25519Credential(string key, string privateKey, string? pass = null) : base(key)
{
PrivateKey = privateKey;
Passphrase = passphrase;
Pass = pass;
}
/// <summary>
/// Get signing key
/// </summary>
public Key GetSigningKey()
public NSec.Cryptography.Key GetSigningKey()
{
if (_signKey != null)
return _signKey;
@ -239,7 +236,7 @@ namespace CryptoExchange.Net.Authentication
.Replace("-----END PRIVATE KEY-----", "")
.Trim();
var keyBytes = Convert.FromBase64String(key);
_signKey = Key.Import(SignatureAlgorithm.Ed25519, keyBytes, KeyBlobFormat.PkixPrivateKey);
_signKey = NSec.Cryptography.Key.Import(NSec.Cryptography.SignatureAlgorithm.Ed25519, keyBytes, NSec.Cryptography.KeyBlobFormat.PkixPrivateKey);
return _signKey;
}
}
@ -257,7 +254,7 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// Passphrase
/// </summary>
public string? Passphrase { get; set; }
public string? Pass { get; set; }
/// <inheritdoc />
public override ApiCredentialsType CredentialType => ApiCredentialsType.Ecdsa;
@ -265,13 +262,13 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// ctor
/// </summary>
/// <param name="publicKey">Public key</param>
/// <param name="key">Public key</param>
/// <param name="privateKey">Private key</param>
/// <param name="passphrase">Passphrase</param>
public ECDSACredential(string publicKey, string privateKey, string? passphrase = null) : base(publicKey)
/// <param name="pass">Passphrase</param>
public ECDSACredential(string key, string privateKey, string? pass = null) : base(key)
{
PrivateKey = privateKey;
Passphrase = passphrase;
Pass = pass;
}
}
}

View File

@ -334,7 +334,7 @@ namespace CryptoExchange.Net.Clients
RateLimitItemType.Request,
definition,
host,
GetAuthenticationProvider()?.PublicKey,
GetAuthenticationProvider()?.Key,
requestWeight,
ClientOptions.RateLimitingBehaviour,
rateLimitKeySuffix,
@ -360,7 +360,7 @@ namespace CryptoExchange.Net.Clients
RateLimitItemType.Request,
definition,
host,
GetAuthenticationProvider()?.PublicKey,
GetAuthenticationProvider()?.Key,
singleRequestWeight,
ClientOptions.RateLimitingBehaviour,
rateLimitKeySuffix,

View File

@ -21,7 +21,7 @@ namespace CryptoExchange.Net
public static ILogger? StaticLogger
{
get => _staticLogger;
internal set
internal set
{
if (_staticLogger != null)
return;
@ -161,5 +161,14 @@ namespace CryptoExchange.Net
return httpHandler;
#endif
}
/// <summary>
/// Validate the provided credentials, throw an exception if invalid
/// </summary>
public static void ValidateCredentials(ApiCredentials? credentials)
{
if (credentials != null && credentials.CredentialPairs.Length == 0)
throw new ArgumentException("ApiCredentials configuration invalid");
}
}
}