daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2018 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private async Task ConnectToGdaxFix()
  2.     {
  3.         _socketTcpClient = new TcpClient();
  4.         _socketTcpClient.NoDelay = true;
  5.         _bufferEnd = 0;
  6.         await _socketTcpClient.ConnectAsync(_gdaxFixGateway, _gdaxFixPort);
  7.         _sslStream = new SslStream(_socketTcpClient.GetStream());
  8.         await _sslStream.AuthenticateAsClientAsync(_gdaxFixGateway);
  9.         byte[] buffer = new byte[2048];
  10.         var message = await FixSendLoginMessage();
  11.         var bytes = await _sslStream.ReadAsync(buffer, 0, buffer.Length);
  12.     }
  13.  
  14.     private async Task<string> FixSendLoginMessage()
  15.     {
  16.         var sendTime = DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss.fff");
  17.         string msgType = "A";
  18.         var messageSeqNumber = (++messageSeqNum).ToString();
  19.         var senderCompId = "apiKey";
  20.         var targetCompId = "Coinbase";
  21.         var password = "passphrase";
  22.         var accessSign = CreateAccessSign(
  23.                 sendTime,
  24.                 msgType,
  25.                 messageSeqNumber,
  26.                 senderCompId,
  27.                 targetCompId,
  28.                 password
  29.             );
  30.         var logonMessage = $"98=0{'x01'}108=30{'x01'}554={password}{'x01'}96={accessSign}{'x01'}8013=Y";
  31.         var headerAfter9Tag = $"35={msgType}{'x01'}34={messageSeqNumber}{'x01'}52={sendTime}{'x01'}49={senderCompId}{'x01'}56={targetCompId}";
  32.         var length = logonMessage.Length + headerAfter9Tag.Length + 1;
  33.         var messageHeader = $"8=FIX.4.2{'x01'}9={length}{'x01'}{headerAfter9Tag}";
  34.         var messageWithHeader = $"{messageHeader}{'x01'}{logonMessage}";
  35.         int sum = 0;
  36.         int len = messageWithHeader.Length;
  37.         for (int i = 0; i < len; i++)
  38.         {
  39.             sum += Convert.ToChar(messageWithHeader.Substring(i, 1));
  40.         }
  41.         sum = sum % 256;
  42.         var messageWithChecksum = $"{messageWithHeader}{'x01'}10={sum.ToString("000")}{'x01'}";
  43.         byte[] buffer = Encoding.ASCII.GetBytes(messageWithChecksum);
  44.         await _sslStream.WriteAsync(buffer, 0, buffer.Length);
  45.         return messageWithChecksum;
  46.     }
  47.  
  48.     private string CreateAccessSign(
  49.             string sendingTime,
  50.             string msgType,
  51.             string msgSeqNumber,
  52.             string senderCompId,
  53.             string targetCompId,
  54.             string password
  55.         )
  56.     {
  57.         var stringToHash = string.Join('x01', sendingTime, msgType, msgSeqNumber, senderCompId, targetCompId, password);
  58.         var privateKeyAsBytes = Encoding.UTF8.GetBytes("privateKey");
  59.         using (var hmac = new HMACSHA256(privateKeyAsBytes))
  60.         {
  61.             var signature = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToHash));
  62.             return Convert.ToBase64String(signature);
  63.         }
  64.     }
RAW Paste Data
Top