Exemplo n.º 1
0
        /// <summary>
        /// Initialize a new instance of a REST API client with the specified credentials.
        /// </summary>
        /// <param name="key">API Key</param>
        /// <param name="secret">API Secret</param>
        /// <param name="passphrase">API Passphrase</param>
        /// <param name="isSandbox">Is sandbox / not real-time.</param>
        /// <param name="url">(Optional) Alternate REST API endpoint.</param>
        /// <param name="isv1api">Is version 1 API.</param>
        /// <param name="futures">Use KuCoin Futures API endpoints.</param>
        /// <remarks>
        /// If <paramref name="key"/>, <paramref name="secret"/>, or <paramref name="passphrase"/> are null, then this class will be initialized for public API use.
        /// </remarks>
        public KucoinBaseRestApi(
            string key,
            string secret,
            string passphrase,
            bool isSandbox = false,
            string url     = null,
            bool isv1api   = false,
            bool futures   = false
            )
        {
            this.isv1api = isv1api;

            if (key != null && secret != null && passphrase != null)
            {
                cred = new MemoryEncryptedCredentialsProvider(key, secret, passphrase, null, isSandbox);

#pragma warning disable IDE0059 // Unnecessary assignment of a value
                key = secret = passphrase = null;
#pragma warning restore IDE0059 // Unnecessary assignment of a value

                GC.Collect();
            }

            SetDefaultUrl(futures, isSandbox, url);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a credentials provider to use with the <see cref="IServiceFactory"/> instance acquired from <see cref="GetServiceFactory"/> or <see cref="Initialize"/>.
        /// </summary>
        /// <param name="key">API key.</param>
        /// <param name="secret">API secret.</param>
        /// <param name="passphrase">API passphrase.</param>
        /// <param name="sandbox">The API key is a sandbox key.</param>
        /// <param name="futures">The API key is a KuCoin Futures key.</param>
        /// <param name="addToCache">Add these credentials to the global <see cref="KuCoinSystem.Credentials"/> list.</param>
        /// <returns>
        /// A new <see cref="ICredentialsProvider"/> that can be used to connect to any service that requires an API key.
        /// </returns>
        /// <remarks>
        /// This method returns a <see cref="MemoryEncryptedCredentialsProvider"/> instance.  Once your credentials are passed in,
        /// they are encrypted to be stored in memory and the original values are set to null.  The credentials remain encrypted in memory until they are needed by calling the 'Get' methods.
        /// At that point, the credentials are decrypted and returned to the caller.  The caller should dispose of the unencrypted versions as soon as they are no longer needed.
        /// Classes in this assembly only access the unecrypted versions of these values at the exact moment when a request must be sent.
        /// </remarks>
        public static ICredentialsProvider CreateCredentialsProvider(string key, string secret, string passphrase, bool sandbox = false, bool futures = false, bool addToCache = true)
        {
            var result = new MemoryEncryptedCredentialsProvider(key, secret, passphrase, sandbox: sandbox, futures: futures);

            if (addToCache)
            {
                if (Credentials == null)
                {
                    Credentials = new List <ICredentialsProvider>();
                }
                Credentials.Add(result);
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize a new instance of a REST API client with a credentials provider.
        /// </summary>
        /// <param name="credProvider">An object that implements <see cref="ICredentialsProvider"/>.</param>
        /// <param name="url">(Optional) Alternate REST API endpoint.</param>
        /// <param name="isv1api">Is version 1 API.</param>
        /// <param name="futures">Use KuCoin Futures API endpoints.</param>
        /// <remarks>
        /// If <paramref name="credProvider"/> is null, then this class will be initialized for public API use.
        /// If <paramref name="credProvider"/> is not null, then this class will be initialized for private API use,
        /// the <paramref name="futures"/> parameter is ignored, and the value of <see cref="ICredentialsProvider.GetFutures"/> is used, instead.
        /// </remarks>
        public KucoinBaseRestApi(
            ICredentialsProvider credProvider,
            string url   = null,
            bool isv1api = false,
            bool futures = false)
        {
            this.isv1api = isv1api;

            if (credProvider != null)
            {
                cred    = new MemoryEncryptedCredentialsProvider(credProvider);
                futures = cred.GetFutures();
            }

            SetDefaultUrl(futures, cred?.GetSandbox() ?? false, url);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Resolve the correct credentials according to the global <see cref="KuCoinSystem.UseCredentialsMode"/> setting.
        /// </summary>
        /// <returns></returns>
        protected ICredentialsProvider ResolveCredentials()
        {
            ICredentialsProvider cred = this.cred;

            var ucm = KuCoinSystem.UseCredentialsMode;

            if (ucm == UseCredentialsMode.Never)
            {
                return(cred);
            }

            if (ucm == UseCredentialsMode.Default || ucm == UseCredentialsMode.Always && cred != null)
            {
                return(cred);
            }
            else
            {
                cred = null;
            }

            if (KuCoinSystem.Credentials != null && KuCoinSystem.Credentials.Count > 0)
            {
                foreach (var checkCred in KuCoinSystem.Credentials)
                {
                    if (checkCred.GetFutures() == IsFutures && checkCred.GetSandbox() == IsSandbox)
                    {
                        cred = checkCred;
                        break;
                    }
                }

                if (cred == null && KuCoinSystem.UseCredentialsMode == UseCredentialsMode.Always)
                {
                    cred = KuCoinSystem.Credentials[0];
                }
            }

            return(cred ?? this.cred);
        }