Search code examples
c#apiauthenticationexecution

How do I, given the following methods, send the request to the Bitstamp API using RestSharp?


I am trying to figure out as to how I can successfully retrieve my Bitcoin balance from Bitstamp using their API. I have spent the entire day on Stack Overflow and on youtube to try and figure this out, as there are a lot of small bits that could be molded together. I think that I'm pretty close to succeeding, but there is one small part that I can't seem to figure out.

How do I exactly execute this request? I should probably add the API authentication somewhere and then sign it using the HMACSHA256 method. But in what order? How do I go about it? This is the API documentation -> https://www.bitstamp.net/api/

And here is my entire code so far:

using RestSharp;
using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp5
{
    class Program
    {
        private readonly String _clientId = "xxx";
        private readonly String _apiKey = "xxx";
        private readonly String _apiSecret = "xxx";

        static void Main(string[] args)
        {
            Console.ReadLine();
        }

        public void AddApiAuthentication(RestRequest restRequest)
        {
            var nonce = DateTime.Now.Ticks;
            var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);

            restRequest.AddParameter("key", _apiKey);
            restRequest.AddParameter("signature", signature);
            restRequest.AddParameter("nonce", nonce);

        }

        private string GetSignature(long nonce, string key, string secret, string clientId)
        {
            string msg = string.Format("{0}{1}{2}", nonce,
                clientId,
                key);

            return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
        }
        public static byte[] SignHMACSHA256(String key, byte[] data)
        {
            HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
            return hashMaker.ComputeHash(data);
        }

        public static byte[] StringToByteArray(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        public static string ByteArrayToString(byte[] hash)
        {
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        }
    }
}

It's really just the executing part that I'm missing. I should've covered everything else according to the API documentation. Some thoughts would be great.

Thanks in advance, Nexigen.

EDIT::

I worked a little more on it and I now know how to execute it

using RestSharp;
using System;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp5
{
    class Program
    {
        private readonly String _clientId = "xxx";
        private readonly String _apiKey = "xxx";
        private readonly String _apiSecret = "xxx";

        static void Main(string[] args)
        {
            Program program = new Program();

            var _request = new RestRequest();
            _request.Resource = "api/v2/balance";

            program.AddApiAuthentication(_request);

            Console.ReadLine();
        }

        public void AddApiAuthentication(RestRequest restRequest)
        {
            var nonce = DateTime.Now.Ticks;
            var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);

            restRequest.AddParameter("X-Auth", _apiKey);
            restRequest.AddParameter("X-Auth-Signature", signature);
            restRequest.AddParameter("X-Auth-Nonce", nonce);

            var client = new RestClient();
            client.BaseUrl = new Uri("http://www.bitstamp.net/");

            IRestResponse response = client.Execute(restRequest);
            Console.WriteLine(response.Content);
        }

        private string GetSignature(long nonce, string key, string secret, string clientId)
        {
            string msg = string.Format("{0}{1}{2}", nonce,
                clientId,
                key);

            return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
        }
        public static byte[] SignHMACSHA256(String key, byte[] data)
        {
            HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
            return hashMaker.ComputeHash(data);
        }

        public static byte[] StringToByteArray(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        public static string ByteArrayToString(byte[] hash)
        {
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        }
    }
}

However, when I now try to make the request to http://www.bitstamp.net/api/balance, It's telling me that it's missing my api key, signature and nonce parameters. And when I use http://www.bitstamp.net/api/v2/balance it's telling me that it's a POST endpoint only. What am I missing now?


Solution

  • There are a couple of problems here:

    • you need to create a RestClient and a RestRequest and pass the request to the client after populating its headers
    • the header names you are using don't match those quoted in the bitstamp api documentation and the examples: enter image description here