1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-22 05:42:53 +00:00
This commit is contained in:
Jan Korf
2019-10-11 14:48:30 +02:00
parent 715fe378d5
commit 8ec902951d
33 changed files with 725 additions and 655 deletions
@@ -14,17 +14,17 @@ namespace CryptoExchange.Net.Authentication
/// <summary>
/// The api key to authenticate requests
/// </summary>
public SecureString Key { get; }
public SecureString? Key { get; }
/// <summary>
/// The api secret to authenticate requests
/// </summary>
public SecureString Secret { get; }
public SecureString? Secret { get; }
/// <summary>
/// The private key to authenticate requests
/// </summary>
public PrivateKey PrivateKey { get; }
public PrivateKey? PrivateKey { get; }
/// <summary>
/// Create Api credentials providing a private key for authentication
@@ -56,8 +56,20 @@ namespace CryptoExchange.Net.Authentication
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret))
throw new ArgumentException("Key and secret can't be null/empty");
Key = CreateSecureString(key);
Secret = CreateSecureString(secret);
Key = key.ToSecureString();
Secret = secret.ToSecureString();
}
/// <summary>
/// Copy the credentials
/// </summary>
/// <returns></returns>
public ApiCredentials Copy()
{
if (PrivateKey == null)
return new ApiCredentials(Key!.GetString(), Secret!.GetString());
else
return new ApiCredentials(PrivateKey!.Copy());
}
/// <summary>
@@ -66,24 +78,23 @@ namespace CryptoExchange.Net.Authentication
/// <param name="inputStream">The stream containing the json data</param>
/// <param name="identifierKey">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'.</param>
/// <param name="identifierSecret">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'.</param>
public ApiCredentials(Stream inputStream, string identifierKey = null, string identifierSecret = null)
public ApiCredentials(Stream inputStream, string? identifierKey = null, string? identifierSecret = null)
{
using (var reader = new StreamReader(inputStream, Encoding.ASCII, false, 512, true))
{
var stringData = reader.ReadToEnd();
var jsonData = stringData.ToJToken();
if(jsonData == null)
throw new ArgumentException("Input stream not valid json data");
using var reader = new StreamReader(inputStream, Encoding.ASCII, false, 512, true);
var stringData = reader.ReadToEnd();
var jsonData = stringData.ToJToken();
if(jsonData == null)
throw new ArgumentException("Input stream not valid json data");
var key = TryGetValue(jsonData, identifierKey ?? "apiKey");
var secret = TryGetValue(jsonData, identifierSecret ?? "apiSecret");
var key = TryGetValue(jsonData, identifierKey ?? "apiKey");
var secret = TryGetValue(jsonData, identifierSecret ?? "apiSecret");
if (key == null || secret == null)
throw new ArgumentException("apiKey or apiSecret value not found in Json credential file");
if (key == null || secret == null)
throw new ArgumentException("apiKey or apiSecret value not found in Json credential file");
Key = CreateSecureString(key);
Secret = CreateSecureString(secret);
}
Key = key.ToSecureString();
Secret = secret.ToSecureString();
inputStream.Seek(0, SeekOrigin.Begin);
}
@@ -94,26 +105,12 @@ namespace CryptoExchange.Net.Authentication
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
protected string TryGetValue(JToken data, string key)
protected string? TryGetValue(JToken data, string key)
{
if (data[key] == null)
return null;
return (string) data[key];
}
/// <summary>
/// Create a secure string from a string
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
protected SecureString CreateSecureString(string source)
{
var secureString = new SecureString();
foreach (var c in source)
secureString.AppendChar(c);
secureString.MakeReadOnly();
return secureString;
}
}
/// <summary>
/// Dispose