WebSocket Authentication

HMAC Authentication

  • Endpoint: wss://ws-feed-pro.btcturk.com
  • The WebSocket feed provides real-time market data updates for orders and trades.
  • The WebSocket feed uses a bidirectional protocol, which encodes all messages as JSON objects. All messages have a type attribute that can be used to handle the message appropriately.
  • Please note that new message types can be added at any point in time. Clients are expected to ignore messages they do not support.
HMAC login mechanism for WebSocket:
From BtcTurk | PRO go to ACCOUNT>API Access
In the form select WebSocket, enter you are IP address and submit. This action will create public key and private key to achieve WebSocket login.
Code Example
C#
Python
// You can download ApiClient .net core complete library from github https://github.com/BTCTrader/broker-api-csharp-v2
private static async Task HmacTest()
{
Uri _uri = new Uri("wss://ws-feed-pro.btcturk.com");
ClientWebSocket client = new ClientWebSocket();
await client.ConnectAsync(_uri, CancellationToken.None);
string publicKey = "YOUR_PUBLIC_KEY";
string privateKey = "YOUR_PRIVATE_KEY";
long nonce = 3000;
string baseString = quot;{publicKey}{nonce}";
string signature = ComputeHash(privateKey, baseString);
long timestamp = ToUnixTime(DateTime.UtcNow);
object[] hmacMessageObject = { 114, new { type = 114, publicKey = publicKey, timestamp = timestamp, nonce = nonce, signature = signature } };
string message = JsonSerializer.Serialize(hmacMessageObject);
await client.SendAsync(buffer: new ArraySegment<byte>(array: Encoding.UTF8.GetBytes(message),
offset: 0,
count: message.Length),
messageType: WebSocketMessageType.Text,
endOfMessage: true,
cancellationToken: CancellationToken.None);
}
private static string ComputeHash(string privateKey, string baseString)
{
var key = Convert.FromBase64String(privateKey);
string hashString;
using (var hmac = new HMACSHA256(key))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(baseString));
hashString = Convert.ToBase64String(hash);
}
return hashString;
}
private static long ToUnixTime(DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalMilliseconds);
}
import asyncio
import base64
import hashlib
import hmac
import json
import time
import websockets
class InterfaceWS:
def __init__(self, exchange_name: str = None) -> None:
self.exchange_name = "BTC Turk"
self.uri = "wss://ws-feed-pro.btcturk.com/"
async def authenticate_ws(self) -> bool:
self.ws = await websockets.connect(self.uri)
publicKey = "PUBLIC_KEY_HERE";
privateKey = "PRIVATE_KEY_HERE";
nonce = 3000
print("\nnonce", nonce)
baseString = "{}{}".format(publicKey, nonce).encode("utf-8")
print("\nbaseString", baseString)
signature = hmac.new(
base64.b64decode(privateKey), baseString, hashlib.sha256
).digest()
print("\nsignature", signature)
signature = base64.b64encode(signature)
print("\nsignature", signature)
timestamp = round(time.time() * 1000)
print("\ntimestamp", timestamp)
hmacMessageObject = [
114,
{
"nonce": nonce,
"publicKey": publicKey,
"signature": signature.decode("utf-8"),
"timestamp": timestamp,
"type": 114,
},
]
print("hmacMessageObject", hmacMessageObject)
await self.ws.send(json.dumps(hmacMessageObject))
while True:
try:
response = await asyncio.wait_for(self.ws.recv(), timeout=0.5)
print("response after auth: ", response)
except Exception as e:
print(e)
async def main():
w = InterfaceWS()
await w.authenticate_ws()
if __name__ == "__main__":
asyncio.run(main())
Copy link