using System; using System.IO; using CryptoExchange.Net.Converters.SystemTextJson; using CryptoExchange.Net.Converters.MessageParsing; 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; } /// /// 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 type of credentials public ApiCredentials(string key, string secret, 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; } /// /// Copy the credentials /// /// public virtual ApiCredentials Copy() { return new ApiCredentials(Key, Secret, CredentialType); } /// /// Create Api credentials providing a stream containing json data. The json data should include two values: apiKey and apiSecret /// /// The stream containing the json data /// A key to identify the credentials for the API. For example, when set to `binanceKey` the json data should contain a value for the property `binanceKey`. Defaults to 'apiKey'. /// A key to identify the credentials for the API. For example, when set to `binanceSecret` the json data should contain a value for the property `binanceSecret`. Defaults to 'apiSecret'. public static ApiCredentials FromStream(Stream inputStream, string? identifierKey = null, string? identifierSecret = null) { var accessor = new SystemTextJsonStreamMessageAccessor(); if (!accessor.Read(inputStream, false).Result) throw new ArgumentException("Input stream not valid json data"); var key = accessor.GetValue(MessagePath.Get().Property(identifierKey ?? "apiKey")); var secret = accessor.GetValue(MessagePath.Get().Property(identifierSecret ?? "apiSecret")); if (key == null || secret == null) throw new ArgumentException("apiKey or apiSecret value not found in Json credential file"); inputStream.Seek(0, SeekOrigin.Begin); return new ApiCredentials(key, secret); } } }