Bitcoin Forum

Economy => Service Discussion => Topic started by: Dirk83 on May 18, 2013, 03:16:47 PM



Title: BTC-E Nonce Generation
Post by: Dirk83 on May 18, 2013, 03:16:47 PM
Hi,

I am testing code for the BTC-E trading platform, however, it seems like it can only process one API request per second, if I do more it gives an invalid nonce parameter error back.

I use PHP, this is the code to generate the nonce:

        $mt = explode(' ', microtime());
        $req['nonce'] = $mt[1];

If I try a more complex nonce, like:

    $req['nonce'] = $mt[1].substr($mt[0], 2, 6);

I also get an invalid nonce parameter error back.

Anyone ran into the same problem? How to solve it?

Thanks, Dirk


Title: Re: BTC-E Nonce Generation
Post by: Terk on May 18, 2013, 03:27:41 PM
I am testing code for the BTC-E trading platform, however, it seems like it can only process one API request per second, if I do more it gives an invalid nonce parameter error back.

It can only process one API request per unique nonce and your nonce is number of seconds since the Unix epoch, so you are changing your nonce only once per second. You are skipping the microseconds part of microtime() return. Please read: http://php.net/manual/en/function.microtime.php, or the quickest shortcut would be $req['nonce'] = (int) 10000*microtime(true);


Title: Re: BTC-E Nonce Generation
Post by: Dirk83 on May 18, 2013, 03:47:30 PM
I am testing code for the BTC-E trading platform, however, it seems like it can only process one API request per second, if I do more it gives an invalid nonce parameter error back.

It can only process one API request per unique nonce and your nonce is number of seconds since the Unix epoch, so you are changing your nonce only once per second. You are skipping the microseconds part of microtime() return. Please read: http://php.net/manual/en/function.microtime.php, or the quickest shortcut would be $req['nonce'] = (int) 10000*microtime(true);

I already tried generating a more complex nonce, but it didn't work.

Apparently it needs a nonce of exactly 10 numbers (this is mentioned nowhere however), which I now generate partly from the seconds and from the microseconds part:

$mt = explode(' ', microtime());
$req['nonce'] = substr($mt[1],-4).substr($mt[0], 2, 6);

Issue solved now, thanks for the reply.


Title: Re: BTC-E Nonce Generation
Post by: Terk on May 18, 2013, 04:38:37 PM
$mt = explode(' ', microtime());
$req['nonce'] = substr($mt[1],-4).substr($mt[0], 2, 6);

Just make sure that they don't require ever-increasing nonce (but they probably do), because your method will create numbers starting back from 0 after couple of hours. What you have now is a number which increases for 10000 seconds but then starts back from 0. You also don't ensure that you won't generate a nonce that has already been used in the past, as it's possible you'll get a colliding number one day. This is a very dirty hack what you have now.


Title: Re: BTC-E Nonce Generation
Post by: Dirk83 on May 18, 2013, 07:34:02 PM
Thanks, I ran into that I think, or it could be something else, it isn't working anymore.

I now use this, that should work for at least a while (if you have a better way I'd gladly hear so, but I think it is limited since you have only 10 digits and you want some accuracy on the milli or microsecond level, the best I can do probably is to add 110000000 to it):

$req['nonce'] = substr($mt[1],-7).substr($mt[0], 2, 3);

I get this error message back:

invalid nonce parameter; 0 8905540614

I don know why it gives a zero first, but the 8905540614 is the nonce I send. It's kinda weird, because it is 10 digits and it's ever-increasing (I reset the keys), so something else might still be causing it.

      


Title: Re: BTC-E Nonce Generation
Post by: daybyter on May 18, 2013, 07:51:12 PM
I think the nonce is not really tied to the time? You could just count it up at each request?


Title: Re: BTC-E Nonce Generation
Post by: Dirk83 on May 18, 2013, 08:08:30 PM
I think the nonce is not really tied to the time? You could just count it up at each request?

Possible, though I need to keep a database of the nonce value then, which I currently don't have :-).


Title: Re: BTC-E Nonce Generation
Post by: whydifficult on May 18, 2013, 09:34:55 PM
but the 8905540614 is the nonce I send. It's kinda weird, because it is 10 digits and it's ever-increasing (I reset the keys), so something else might still be causing it.

This page (https://npmjs.org/package/btc-e) states that the nonce is capped at 4294967294.


Title: Re: BTC-E Nonce Generation
Post by: tuneman1980 on June 17, 2013, 06:46:46 PM
No matter what I do I get:

'{"success":0,"error":"invalid nonce parameter; 0 "}'

I've tried all sorts of solutions.  Currently, I'm just sending a "1" in the hopes of getting anything other than a "0" to show up in the response.


Title: Re: BTC-E Nonce Generation
Post by: ElectricMucus on June 17, 2013, 06:48:54 PM
I am using the unix time in seconds minus a constant to make if fit into 32 bit.


Title: Re: BTC-E Nonce Generation
Post by: tuneman1980 on June 17, 2013, 06:50:39 PM
Currently I'm posting this basic URL:

    URL := DSFormat(
      '%s?%s',
      ['https://btc-e.com/tapi',
      Join([
        WebArg('method', 'getInfo'),
        WebArg('nonce', 1)], '&', True)]);

Along with the API-Key header of course.


Title: Re: BTC-E Nonce Generation
Post by: legitnick on June 20, 2013, 05:07:50 AM
Im not quite sure what your making, is it a tradebot?


Title: Re: BTC-E Nonce Generation
Post by: daybyter on June 20, 2013, 01:30:20 PM
Did you try my sources yet? I think, they still work, so you'd just add some debug output to see the data, that it sends to the server?


Title: Re: BTC-E Nonce Generation
Post by: Kevlar on June 23, 2013, 07:47:13 PM
I usually use the current time in milliseconds /250. This will allow 4 requests per second, and will hit max_int sometime in the year 2030.



Title: Re: BTC-E Nonce Generation
Post by: jwest411 on October 12, 2013, 07:59:32 PM
I did a similar thing to kevlar and legitnick, took the time in tenths of a second and then shave off the leading digits by subtracting some, so if the system time in deciseconds is 1281646886112, I take that and subtract 1280000000000 to get 1646886112, which is less than the maximum, ten digits, and increments every 1/10 seconds.

But my nonce is still not working. I am getting {"success":0,"error":"invalid nonce parameter; on key:3816054893, you sent:1160549562"}. The key is a nonce I sent on a completely different API key, even though I discarded the key. In my utter frustration, I tried opening another BTCE account under another email address I have. But somehow, when I tried using the new key/secret on the new account, it still gave me the same error with the same "key". 1) how is this possible and 2) Does this mean I'm screwed?

On a related note, what exactly is the value of the "key" here? Sometimes it appears to be a higher value I sent in the past. Sometimes it is zero. And sometimes, if I accidentally send the same nonce parameter twice, it is a duplicate of the "you sent" value. So I guess the key is basically an error code?

This had better not happen once I have more $ invested...


Title: Re: BTC-E Nonce Generation
Post by: daybyter on October 12, 2013, 08:40:37 PM
I thought key is your API key here?


Title: Re: BTC-E Nonce Generation
Post by: jwest411 on October 13, 2013, 04:18:06 AM
update: it turns out that the method I gave before was fine, but I accidentally forgot to change the nonce in one of my methods so it was screwing everything up. I just discarded my old key and got a new one, and now it is working.

@DayBiter In my last post, I was referring to two different types of keys... My API key, and then the "key" i was referring to was the key in the "invalid nonce parameter" error response. That key is generally one of the past nonces you have sent or 0. But it would be nice to have a little more explanation about what the different possible responses are/mean.


Title: Re: BTC-E Nonce Generation
Post by: CoinFarmer on January 28, 2014, 09:26:53 PM
I have a problem with nonce too, SHA512 encryption looks ok,I have checked on below calc:

http://hash.online-convert.com/sha512-generator (http://hash.online-convert.com/sha512-generator)

I received this response from BTCE:

Code:
: Response : "{"success":0,"error":"invalid nonce parameter; on key:0, you sent:"}" : String

Could you please take a look into my VBA code?

Code:
Dim postdata As String
Dim Method As String
Dim API As String
Dim Secret As String
Dim URL As String
Dim ObjHttp As Object
Dim nonce As String
Dim Sign As String
Dim Response As String

Private Sub Trade()
        
        API = "API_KEY"
        Secret = "SECRET_KEY"
        
        
        URL = "https://btc-e.com/tapi"
        Method = "getInfo"
        nonce = DateDiff("s", "1/1/1970", Date)
        postdata = "method=" & Method & Chr(38) & "nonce=" & nonce
        Sign = Base64_HMACSHA512(postdata, Secret)
        Set ObjHttp = New MSXML2.XMLHTTP60
        
        With ObjHttp
            .Open "POST", URL, False
            .setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)"
            .setRequestHeader "Key", API
            .setRequestHeader "Sign", Sign
            .send (postdata)
            Response = .responseText
        End With
        
    End Sub
    
Private Function EncodeBase64(ByRef arrData() As Byte) As String

    Dim objXML As MSXML2.DOMDocument60
    Dim objNode As MSXML2.IXMLDOMElement
    
    Set objXML = New MSXML2.DOMDocument60
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.Text
 
    
    Set objNode = Nothing
    Set objXML = Nothing
 
End Function

Function Base64_HMACSHA512(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)

    Dim asc As Object, enc As Object
    Dim TextToHash() As Byte
    Dim SharedSecretKey() As Byte
    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.HMACSHA512")

    TextToHash = asc.Getbytes_4(sTextToHash)
    SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
    enc.Key = SharedSecretKey

    Dim bytes() As Byte
    bytes = enc.ComputeHash_2((TextToHash))
    Base64_HMACSHA512 = EncodeBase64(bytes)
    Set asc = Nothing
    Set enc = Nothing

End Function





Title: Re: BTC-E Nonce Generation
Post by: ElectricMucus on January 28, 2014, 09:45:19 PM
yeah google for: btc-e faceplant
They still haven't fixed it... your code is probably fine you just have to do this really stupid thing.


Title: Re: BTC-E Nonce Generation
Post by: daybyter on January 28, 2014, 10:03:54 PM
Did you just try to print the http header you send to btc-e? Looks like the nonce is missing alltogether?


Title: Re: BTC-E Nonce Generation
Post by: CoinFarmer on January 29, 2014, 11:08:08 PM
I fixed nonce issue - it was format issue, but the Sign Key issue exists.


Title: Re: BTC-E Nonce Generation
Post by: JackH on November 10, 2014, 04:45:36 PM
We are having problem with this exact issue. Does anyone have a more comprehensive tutorial on how to communicate with the BTC-E API without getting the following response every time:

{success=0, error=invalid nonce parameter; on key:0, you sent:'', you should send:1}


Title: Re: BTC-E Nonce Generation
Post by: ElectricMucus on November 11, 2014, 06:24:01 PM
We are having problem with this exact issue. Does anyone have a more comprehensive tutorial on how to communicate with the BTC-E API without getting the following response every time:

{success=0, error=invalid nonce parameter; on key:0, you sent:'', you should send:1}

You've seen that?
https://bitcointalk.org/index.php?topic=122006


Title: Re: BTC-E Nonce Generation
Post by: JackH on November 12, 2014, 11:56:44 AM
Yes I did, but we solved it. The problem was: The libraries was not sending the data in binary format, but in a text format. Our dev redid everything manually without using the library and send it as binary. That worked.