protected async Task InvokeAuthWebSocket(WebSocketAuthenticationRequest req) { if (AuthSubscriptionResult.ClientWebSocket.State == WebSocketState.Open) { try { string strReq = JsonConvert.SerializeObject(req, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); byte[] msgArray = Encoding.ASCII.GetBytes(strReq); ArraySegment <byte> bytesToSend = new ArraySegment <byte>(msgArray); await AuthSubscriptionResult.ClientWebSocket.SendAsync(bytesToSend, WebSocketMessageType.Text, true, CancellationToken.None); } catch (Exception) { throw; } } else { throw new Exception(string.Format("Not connected to BitMex on subscription channel:{0}", AuthSubscriptionResult.ClientWebSocket.State.ToString())); } }
protected async Task <AuthenticationResult> DoAuthenticate() { if (AuthSubscriptionResult == null) { AuthSubscriptionResult = await ConnectSubscriptions(); } lock (tSubscrLock) { if (!AuthSubscriptionResult.Authenticated) { //1- A UNIX timestamp after which the request is no longer valid. This is to prevent replay attacks. //In the example, we create an authentication valid for 12 hours Int32 expires = (Int32)(DateTime.UtcNow.AddHours(12).Subtract(new DateTime(1970, 1, 1))).TotalSeconds; //2-api-key: Your public API key. This the id param returned when you create an API Key via the AP string api_key = UserCredentials.BitMexID; string api_secret = UserCredentials.BitMexSecret; //3-api-signature: A signature of the request you are making. //It is calculated as hex(HMAC_SHA256(apiSecret, verb + path + expires + data)). string hexSignature = SignHMACSHA256(api_secret, _VERB, _ENDPOINT, expires); WebSocketAuthenticationRequest autRequest = new WebSocketAuthenticationRequest() { op = "authKeyExpires", args = new object[] { api_key, expires, hexSignature } }; InvokeAuthWebSocket(autRequest).Wait(); } } return(AuthSubscriptionResult); }
public string Build(DateTime utcDateTime) { string strDateTime = utcDateTime.ToString("s"); var request = new GetRequest() .AddParam(_aKKey, _aKValue) .AddParam(_sMKey, _sMVaue) .AddParam(_sVKey, _sVValue) .AddParam(_tKey, strDateTime); string signature = _signer.Sign("GET", _host, _path, request.BuildParams()); var auth = new WebSocketAuthenticationRequest { AccessKeyId = _aKValue, Timestamp = strDateTime, Signature = signature }; return(auth.ToJson()); }