|
| 1 | +using CryptoExchange.Net.Authentication; |
| 2 | +using System; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace Bybit.Net |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Bybit credentials |
| 9 | + /// </summary> |
| 10 | + public class BybitCredentials : ApiCredentials |
| 11 | + { |
| 12 | + internal CredentialSet Credential { get; set; } |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// HMAC credentials |
| 16 | + /// </summary> |
| 17 | + public HMACCredential? HMAC |
| 18 | + { |
| 19 | + get => Credential as HMACCredential; |
| 20 | + set { if (value != null) Credential = value; } |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// RSA credentials in XML format |
| 25 | + /// </summary> |
| 26 | + public RSAXmlCredential? RSAXml |
| 27 | + { |
| 28 | + get => Credential as RSAXmlCredential; |
| 29 | + set { if (value != null) Credential = value; } |
| 30 | + } |
| 31 | + |
| 32 | +#if NETSTANDARD2_1_OR_GREATER || NET7_0_OR_GREATER |
| 33 | + /// <summary> |
| 34 | + /// RSA credentials in PEM/Base64 format |
| 35 | + /// </summary> |
| 36 | + public RSAPemCredential? RSAPem |
| 37 | + { |
| 38 | + get => Credential as RSAPemCredential; |
| 39 | + set { if (value != null) Credential = value; } |
| 40 | + } |
| 41 | +#endif |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Create new credentials |
| 45 | + /// </summary> |
| 46 | +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. |
| 47 | + public BybitCredentials() { } |
| 48 | +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Create new credentials providing HMAC credentials |
| 52 | + /// </summary> |
| 53 | + /// <param name="key">API key</param> |
| 54 | + /// <param name="secret">API secret</param> |
| 55 | + public BybitCredentials(string key, string secret) |
| 56 | + { |
| 57 | + Credential = new HMACCredential(key, secret); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Create new credentials providing HMAC credentials |
| 62 | + /// </summary> |
| 63 | + /// <param name="credential">HMAC Credentials</param> |
| 64 | + public BybitCredentials(HMACCredential credential) |
| 65 | + { |
| 66 | + Credential = credential; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Create new credentials providing RSA credentials in XML format |
| 71 | + /// </summary> |
| 72 | + /// <param name="credential">RSA credentials in XML format</param> |
| 73 | + public BybitCredentials(RSAXmlCredential credential) |
| 74 | + { |
| 75 | + Credential = credential; |
| 76 | + } |
| 77 | + |
| 78 | +#if NETSTANDARD2_1_OR_GREATER || NET7_0_OR_GREATER |
| 79 | + /// <summary> |
| 80 | + /// Create new credentials providing RSA credentials in PEM/Base64 format |
| 81 | + /// </summary> |
| 82 | + /// <param name="credential">RSA credentials in PEM/Base64 format</param> |
| 83 | + public BybitCredentials(RSAPemCredential credential) |
| 84 | + { |
| 85 | + Credential = credential; |
| 86 | + } |
| 87 | +#endif |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Specify the HMAC credentials |
| 91 | + /// </summary> |
| 92 | + /// <param name="key">API key</param> |
| 93 | + /// <param name="secret">API secret</param> |
| 94 | + public BybitCredentials WithHMAC(string key, string secret) |
| 95 | + { |
| 96 | + if (Credential != null) throw new InvalidOperationException("Credentials already set"); |
| 97 | + |
| 98 | + Credential = new HMACCredential(key, secret); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Specify the RSA credentials in XML format |
| 104 | + /// </summary> |
| 105 | + /// <param name="key">API key</param> |
| 106 | + /// <param name="privateKey">Private key</param> |
| 107 | + public BybitCredentials WithRSAXml(string key, string privateKey) |
| 108 | + { |
| 109 | + if (Credential != null) throw new InvalidOperationException("Credentials already set"); |
| 110 | + |
| 111 | + Credential = new RSAXmlCredential(key, privateKey); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | +#if NETSTANDARD2_1_OR_GREATER || NET7_0_OR_GREATER |
| 116 | + /// <summary> |
| 117 | + /// Specify the RSA credentials in PEM/Base64 format |
| 118 | + /// </summary> |
| 119 | + /// <param name="key">API key</param> |
| 120 | + /// <param name="privateKey">Private key</param> |
| 121 | + public BybitCredentials WithRSAPem(string key, string privateKey) |
| 122 | + { |
| 123 | + if (Credential != null) throw new InvalidOperationException("Credentials already set"); |
| 124 | + |
| 125 | + Credential = new RSAPemCredential(key, privateKey); |
| 126 | + return this; |
| 127 | + } |
| 128 | +#endif |
| 129 | + |
| 130 | + /// <inheritdoc /> |
| 131 | + public override ApiCredentials Copy() => new BybitCredentials { Credential = Credential }; |
| 132 | + |
| 133 | + /// <inheritdoc /> |
| 134 | + public override void Validate() |
| 135 | + { |
| 136 | + if (Credential == null) |
| 137 | + throw new ArgumentException($"No credentials provided on {GetType().Name}"); |
| 138 | + |
| 139 | + Credential.Validate(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments