Пример #1
0
 private void Sign(HttpRequestMessage request, string @params)
 {
     if (_bitmexAuthorization.Key != null && _bitmexAuthorization.Secret != null)
     {
         request.Headers.Add("api-expires", _expiresTimeProvider.Get().ToString());
         request.Headers.Add("api-key", _bitmexAuthorization.Key ?? string.Empty);
         request.Headers.Add("api-signature", _signatureProvider.CreateSignature(
                                 _bitmexAuthorization.Secret ?? string.Empty,
                                 $"{request.Method}{request.RequestUri}{_expiresTimeProvider.Get().ToString()}{@params}"));
     }
 }
Пример #2
0
        //Todo rewrite with ObjectDictionary given by the ExchangeDescriptor
        public string SignSocketUrl(Guid accountId, string socketUrl)
        {
            var keyPair = _apiKeySetStore.GetAccount(accountId);

            if (keyPair is null)
            {
                throw new InvalidOperationException($"account with given key was not found: {accountId}");
            }
            switch (keyPair.Exchange)
            {
            default:    //bybit is default by now
                var timeExpires = _expiresTimeProvider.Get(keyPair.Exchange) + 1000;
                return($"{socketUrl}?{SignUrlByBit(keyPair, timeExpires)}");
            }
        }
Пример #3
0
        private bool Authorize()
        {
            var expiresTime  = _expiresTimeProvider.Get();
            var respReceived = new ManualResetEvent(false);
            var data         = new string[0];
            var error        = string.Empty;
            AuthenticationResponse?response = null;

            void AuthenticationReceived(object sender, ResponseEventArgs <AuthenticationResponse> e)
            {
                response = e.Response;
                respReceived.Set();
            };

            var signatureString = _signatureProvider.CreateSignature(_bitmexAuthorization.Secret, $"GET/realtime{expiresTime}");
            var message         = new AuthorizationRequest(_bitmexAuthorization.Key, expiresTime, signatureString);

            this.AuthenticationReceived += AuthenticationReceived;
            _logger.LogInformation("Authorizing...");
            _bitmexApiSocketProxy.Send(message);
            var waitResult = respReceived.WaitOne(SocketMessageResponseTimeout);

            this.AuthenticationReceived -= AuthenticationReceived;

            if (!waitResult)
            {
                _logger.LogError("Timeout waiting authorization response");
                throw new BitmexSocketAuthorizationException("Authorization Failed: timeout waiting authorization response");
            }

            if (response?.Success ?? false)
            {
                _logger.LogInformation("Authorized successfully...");
                _isAuthorized = true;
                return(IsAuthorized);
            }
            else
            {
                _logger.LogError($"Not authorized {error}");
                throw new BitmexSocketAuthorizationException(error, data);
            }
        }
Пример #4
0
        private bool Authorize()
        {
            var expiresTime  = _expiresTimeProvider.Get();
            var respReceived = new ManualResetEvent(false);
            var data         = new string[0];
            var error        = string.Empty;
            OperationResultEventHandler resultReceived = args =>
            {
                if (args.OperationType == OperationType.authKeyExpires)
                {
                    _isAuthorized = args.Result;
                    error         = args.Error;
                    data          = args.Args;
                    respReceived.Set();
                }
            };

            var signatureString = _signatureProvider.CreateSignature(_bitmexAuthorization.Secret, $"GET/realtime{expiresTime}");
            var message         = new SocketAuthorizationMessage(_bitmexAuthorization.Key, expiresTime, signatureString);

            _bitmexApiSocketProxy.OperationResultReceived += resultReceived;
            Log.Info("Authorizing...");
            _bitmexApiSocketProxy.Send(message);
            var waitResult = respReceived.WaitOne(SocketMessageResponseTimeout);

            _bitmexApiSocketProxy.OperationResultReceived -= resultReceived;
            if (!waitResult)
            {
                Log.Error("Timeout waiting authorization response");
                throw new BitmexSocketAuthorizationException("Authorization Failed: timeout waiting authorization response");
            }

            if (!IsAuthorized)
            {
                Log.Error($"Not authorized {error}");
                throw new BitmexSocketAuthorizationException(error, data);
            }

            Log.Info("Authorized successfully...");
            return(IsAuthorized);
        }