using System; using System.IO; using System.Threading.Tasks; namespace CryptoExchange.Net.Authentication { /// /// Api credentials, used to sign requests accessing private endpoints /// public class ApiCredentials { /// /// The api key / label to authenticate requests /// public string Key { get; set; } /// /// The api secret or private key to authenticate requests /// public string Secret { get; set; } /// /// The api passphrase. Not needed on all exchanges /// public string? Pass { get; set; } /// /// Type of the credentials /// public ApiCredentialsType CredentialType { get; set; } /// /// Create Api credentials providing an api key and secret for authentication /// /// The api key / label used for identification /// The api secret or private key used for signing /// The api pass for the key. Not always needed /// The type of credentials public ApiCredentials(string key, string secret, string? pass = null, ApiCredentialsType credentialType = ApiCredentialsType.Hmac) { if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret)) throw new ArgumentException("Key and secret can't be null/empty"); CredentialType = credentialType; Key = key; Secret = secret; Pass = pass; } /// /// Create API credentials using an API key and secret generated by the server /// public static ApiCredentials HmacCredentials(string apiKey, string apiSecret, string? pass) { return new ApiCredentials(apiKey, apiSecret, pass, ApiCredentialsType.Hmac); } /// /// Create API credentials using an API key and an RSA private key in PEM format /// public static ApiCredentials RsaPemCredentials(string apiKey, string privateKey) { return new ApiCredentials(apiKey, privateKey, credentialType: ApiCredentialsType.RsaPem); } /// /// Create API credentials using an API key and an RSA private key in XML format /// public static ApiCredentials RsaXmlCredentials(string apiKey, string privateKey) { return new ApiCredentials(apiKey, privateKey, credentialType: ApiCredentialsType.RsaXml); } /// /// Create API credentials using an API key and an Ed25519 private key /// public static ApiCredentials Ed25519Credentials(string apiKey, string privateKey) { return new ApiCredentials(apiKey, privateKey, credentialType: ApiCredentialsType.Ed25519); } /// /// Load a key from a file /// public static string ReadFromFile(string path) { using var fileStream = File.OpenRead(path); using var streamReader = new StreamReader(fileStream); return streamReader.ReadToEnd(); } /// /// Copy the credentials /// /// public virtual ApiCredentials Copy() { return new ApiCredentials(Key, Secret, Pass, CredentialType); } } }