Author

Topic: Yobit tapi (Read 480 times)

newbie
Activity: 8
Merit: 1
August 07, 2017, 04:10:54 PM
#5
Thanks all for help. The problem was with WebRequest code. There is fixed version of method
Code:
public void GetInfo()
        {
            string parameters = $"method=getInfo&nonce=" + (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

            string address = $"{tapi}/";

            var keyByte = Encoding.UTF8.GetBytes(secret);

            string sign1 = string.Empty;
            byte[] inputBytes = Encoding.UTF8.GetBytes(parameters);
            using (var hmac = new HMACSHA512(keyByte))
            {
                byte[] hashValue = hmac.ComputeHash(inputBytes);

                StringBuilder hex1 = new StringBuilder(hashValue.Length * 2);
                foreach (byte b in hashValue)
                {
                    hex1.AppendFormat("{0:x2}", b);
                }
                sign1 = hex1.ToString();
            }

            WebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(address);
            if (webRequest != null)
            {
                webRequest.Method = "POST";
                webRequest.Timeout = 20000;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Headers.Add("Key", key);
                webRequest.Headers.Add("Sign", sign1);

                webRequest.ContentLength = parameters.Length;
                using (var dataStream = webRequest.GetRequestStream())
                {
                    dataStream.Write(inputBytes, 0, parameters.Length);
                }

                using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                    {
                        var jsonResponse = sr.ReadToEnd();
                        Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                    }
                }
            }

        }
legendary
Activity: 1638
Merit: 1163
Where is my ring of blades...
August 06, 2017, 11:33:37 AM
#4
your url should be: https://yobit.net/tapi without the parameters and just post those params to that base url.

your nonce is always 1. use a dynamically increasing one instead. something like
Code:
int n = DataConverter.DateTimeToUnixTimestamp(DateTime.Now);

I am not familiar with 'WebRequest' class but it seems like you are setting the content type ok:
ContentType = "application/x-www-form-urlencoded";
but you never give it any content! and never make that content in the correct type!

this is your content
Code:
"method=getInfo&nonce=" + n

and you should make it a correct type like this:
Code:
StringContent myContent = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded");

right now Yobit is receiving an empty content, with hash of params that doesn't match that empty content with an addition of a wrong nonce.
newbie
Activity: 8
Merit: 1
August 06, 2017, 07:59:24 AM
#3
Thank you for your answer

I post real API key and secret

I compare your code with mine and I can't find differences. I know about nonce increasing, but I have not any successes request so i think, that it should work. I tried to rewrite calculation of nonce but i still have this error.

I have another API key and secret pair for my test, but they don't work too
newbie
Activity: 35
Merit: 0
August 06, 2017, 07:33:36 AM
#2
You did not post your real private-API key here?

I think you can not hardcode the nonce for testing. It must increase with every request.

I can give you my (working) code in Javascript for comparing.


    function (command, parameters, apikey, secret) { // ex.: command = "returnCompleteBalances",
        return new Promise(function (resolve, reject) {
            const PUBLIC_API_URL  = 'https://yobit.net/api/3';
            const PRIVATE_API_URL = 'https://yobit.net/tapi';

            parameters || (parameters = {});

            let paramURIString;
            let requestoptions;
            if (secret) {
                // create nonce for yobit -> needs to be limited to 2^31
                const keySeed = parseInt(apikey.substring(0,5), 16);
                const dateSeed = parseInt(Date.now() / 1000);
                const yobitNonce = dateSeed + keySeed;

                // add nonce
                parameters.method = command;
                parameters.nonce = yobitNonce;

                // Convert paramters to URI-String: `arg1=foo&arg2=bar`
                paramURIString = Object.keys(parameters).map(function (param) {
                    return encodeURIComponent(param) + '=' + encodeURIComponent(parameters[param]);
                }).join('&');

                // create sign with secret
                const signatureHMAC = crypto.createHmac('sha512', secret).update(paramURIString).digest('hex');

                // create POST header for secret call
                const header = {
                    'Content-Type': 'application/x-www-form-urlencoded', //'application/json'
                    'User-Agent': 'xdx v1.0',
                    'key': apikey,
                    'sign': signatureHMAC
                };

                requestoptions = {
                    method: 'POST',
                    url: PRIVATE_API_URL,
                    form: parameters,
                    headers: header,
                    json: true,
                    strictSSL: false,
                    withCredentials: false    // because of cors client-side call
                };
            } else {
                paramURIString = Object.keys(parameters).map(function (param) {
                    return encodeURIComponent(param) + '=' + encodeURIComponent(parameters[param]);
                }).join('&');

                paramURIString = PUBLIC_API_URL + '/' + command + "?" + paramURIString;

                requestoptions = {
                    method: 'GET',              // query method
                    url: paramURIString,        // query target
                    qs: parameters,             // query parameters
                    json: true,
                    strictSSL: false,
                    withCredentials: false    // because of cors client-side call
                };
            }

            const time1 = new Date(); // for call duration
            request(requestoptions, (err, response, body) => {
                if (err) { reject(err); return }

                const timeAtThisServer = Date.parse(new Date());
                const timeFromAPICallServer = Date.parse(response.headers.date) || timeAtThisServer;

                const time2 = new Date() - time1;
                let result = {
                    body: body, //body.return,

                    statusCode: response.statusCode || 0,
                    apiTime: timeFromAPICallServer,
                    serverTime: timeAtThisServer,
                    timeOffsetMS: timeFromAPICallServer-timeAtThisServer,
                    callDuration: time2,

                    command: command,
                    parameters: parameters,
                };
                resolve(result);
                });
        });
newbie
Activity: 8
Merit: 1
August 06, 2017, 05:37:56 AM
#1
I am trying to write simple application for myself and when i try to call getInfo method i always get a error into the response. Key, sign, method or nonce is incorrect. I found a number of examples but i still can't find mistake in my code. Could anyone help me with it? My code:

    const string key = "072BCC223A1ADBE86854A4B4A9468EAB";
    const string secret = "4c962093fb943d418afb8fae14841c6b";
    const string tapi = "https://yobit.net/tapi";

    public void GetInfo() {
        int nonce = 1;

        string parameters = $"method=getInfo&nonce=1";

        string address = $"{tapi}?{parameters}";

        var keyByte = Encoding.UTF8.GetBytes(secret);

        string sign1 = string.Empty;
        byte[] inputBytes = Encoding.UTF8.GetBytes(parameters);
        using (var hmac = new HMACSHA512(keyByte))
        {
            byte[] hashValue = hmac.ComputeHash(inputBytes);

            StringBuilder hex1 = new StringBuilder(hashValue.Length * 2);
            foreach (byte b in hashValue)
            {
                hex1.AppendFormat("{0:x2}", b);
            }
            sign1 =  hex1.ToString();
        }

        WebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(address);
        if (webRequest != null)
        {
            webRequest.Method = "POST";
            webRequest.Timeout = 20000;
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Headers.Add("Key", key);
            webRequest.Headers.Add("Sign", sign1);

            using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                {
                    var jsonResponse = sr.ReadToEnd();
                    Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                }
            }
        }


        Log(nonce.ToString());
    }
KeyAPI - InfoOnly, I specially hardcode nonce as 1 because i think that it will be easier to find a problem.
Jump to: