Exemplo n.º 1
0
 private void Sign(IBitmexAuthorization bitmexAuthorization, HttpRequestMessage request, string @params)
 {
     request.Headers.Add("api-key", bitmexAuthorization.Key ?? string.Empty);
     request.Headers.Add("api-expires", _expiresTimeProvider.Get().ToString());
     request.Headers.Add("api-signature", _signatureProvider.CreateSignature(bitmexAuthorization.Secret ?? string.Empty,
                                                                             $"{request.Method}{request.RequestUri}{_expiresTimeProvider.Get().ToString()}{@params}"));
 }
Exemplo n.º 2
0
        internal void SignMessage(ISignatureProvider signatureProvider)
        {
            if (signatureProvider.ShouldSignMessage(Domain, Identity))
            {
                AssertDomainIsSet();

                Signature = signatureProvider.CreateSignature(Domain, GetSignatureFields());
            }
        }
Exemplo n.º 3
0
        internal void VerifySignature(ISignatureProvider signatureProvider)
        {
            if (signatureProvider.ShouldSignMessage(Domain, Identity))
            {
                var mac = signatureProvider.CreateSignature(Domain, GetSignatureFields());

                if (!Unsafe.ArraysEqual(Signature, mac))
                {
                    throw new WrongMessageSignatureException();
                }
            }
        }
Exemplo n.º 4
0
        private void Auth(HttpClient httpClient, string url, HttpMethods httpMethod, string @params = "")
        {
            var key             = _bitmexAuthorization.Key ?? string.Empty;
            var secret          = _bitmexAuthorization.Secret ?? string.Empty;
            var nonce           = _nonceProvider.GetNonce().ToString();
            var signatureString = _signatureProvider.CreateSignature(secret, $"{httpMethod}{url}{nonce}{@params}");

            httpClient.DefaultRequestHeaders.Add("api-nonce", nonce);
            httpClient.DefaultRequestHeaders.Add("api-key", key);
            httpClient.DefaultRequestHeaders.Add("api-signature", signatureString);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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);
        }
 public void Execute(CreateSignatureCommand command)
 {
     command.Result = signatureProvider.CreateSignature(command.PrivateKey, command.ContentToSign);
 }
Exemplo n.º 8
0
        private string SignUrlByBit(ApiKeySet keyPair, long timeExpires)
        {
            var signString = _signatureProvider.CreateSignature(keyPair.ApiSecret, "GET/realtime" + timeExpires);

            return($"api_key={keyPair.ApiKey}&expires={timeExpires}&signature={signString}");
        }