Bitcoin Forum
May 07, 2024, 12:13:03 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: BTC-E Nonce Generation  (Read 6961 times)
Dirk83 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
May 18, 2013, 03:16:47 PM
 #1

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
1715083983
Hero Member
*
Offline Offline

Posts: 1715083983

View Profile Personal Message (Offline)

Ignore
1715083983
Reply with quote  #2

1715083983
Report to moderator
1715083983
Hero Member
*
Offline Offline

Posts: 1715083983

View Profile Personal Message (Offline)

Ignore
1715083983
Reply with quote  #2

1715083983
Report to moderator
1715083983
Hero Member
*
Offline Offline

Posts: 1715083983

View Profile Personal Message (Offline)

Ignore
1715083983
Reply with quote  #2

1715083983
Report to moderator
"Bitcoin: the cutting edge of begging technology." -- Giraffe.BTC
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715083983
Hero Member
*
Offline Offline

Posts: 1715083983

View Profile Personal Message (Offline)

Ignore
1715083983
Reply with quote  #2

1715083983
Report to moderator
1715083983
Hero Member
*
Offline Offline

Posts: 1715083983

View Profile Personal Message (Offline)

Ignore
1715083983
Reply with quote  #2

1715083983
Report to moderator
1715083983
Hero Member
*
Offline Offline

Posts: 1715083983

View Profile Personal Message (Offline)

Ignore
1715083983
Reply with quote  #2

1715083983
Report to moderator
Terk
Hero Member
*****
Offline Offline

Activity: 616
Merit: 522



View Profile
May 18, 2013, 03:27:41 PM
 #2

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);

Dirk83 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
May 18, 2013, 03:47:30 PM
 #3

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.
Terk
Hero Member
*****
Offline Offline

Activity: 616
Merit: 522



View Profile
May 18, 2013, 04:38:37 PM
 #4

$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.

Dirk83 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
May 18, 2013, 07:34:02 PM
 #5

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.

      
daybyter
Legendary
*
Offline Offline

Activity: 965
Merit: 1000


View Profile
May 18, 2013, 07:51:12 PM
 #6

I think the nonce is not really tied to the time? You could just count it up at each request?

Dirk83 (OP)
Newbie
*
Offline Offline

Activity: 33
Merit: 0


View Profile
May 18, 2013, 08:08:30 PM
 #7

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 :-).
whydifficult
Sr. Member
****
Offline Offline

Activity: 287
Merit: 250



View Profile WWW
May 18, 2013, 09:34:55 PM
 #8

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 states that the nonce is capped at 4294967294.

Gekko a nodejs bitcoin trading bot!
Realtime Bitcoin Globe - visualizing all transactions and blocks
Tip jar (BTC): 1KyQdQ9ctjCrGjGRCWSBhPKcj5omy4gv5S
tuneman1980
Full Member
***
Offline Offline

Activity: 228
Merit: 100



View Profile WWW
June 17, 2013, 06:46:46 PM
 #9

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.

http://cryptocoinstats.com/

BTC: 19YQqtEdtuWhT6nk6ArBgMTiKMEjoJ5eww  LTC: Li1RLpZm8Rx7txSnQdvZvtLMsd4XDN2vMJ  FTC: 6qAU4vtyf9LPW4yV4m4Vx1jm4ZkXJHTFP7
ElectricMucus
Legendary
*
Offline Offline

Activity: 1666
Merit: 1057


Marketing manager - GO MP


View Profile WWW
June 17, 2013, 06:48:54 PM
 #10

I am using the unix time in seconds minus a constant to make if fit into 32 bit.
tuneman1980
Full Member
***
Offline Offline

Activity: 228
Merit: 100



View Profile WWW
June 17, 2013, 06:50:39 PM
 #11

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.

http://cryptocoinstats.com/

BTC: 19YQqtEdtuWhT6nk6ArBgMTiKMEjoJ5eww  LTC: Li1RLpZm8Rx7txSnQdvZvtLMsd4XDN2vMJ  FTC: 6qAU4vtyf9LPW4yV4m4Vx1jm4ZkXJHTFP7
legitnick
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile WWW
June 20, 2013, 05:07:50 AM
 #12

Im not quite sure what your making, is it a tradebot?

5 BITCOIN RAFFLE GIVEAWAY
"I dont lift" - Lord Furrycoat
daybyter
Legendary
*
Offline Offline

Activity: 965
Merit: 1000


View Profile
June 20, 2013, 01:30:20 PM
 #13

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?

Kevlar
Sr. Member
****
Offline Offline

Activity: 602
Merit: 254


🔰FERRUM NETWORK🔰


View Profile
June 23, 2013, 07:47:13 PM
 #14

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.



                            █████
                        █████████████
                     █████████████
                 ██████████████        █████
              █████████████        ████████████
          ██████████████        █████████████
       █████████████        █████████████       ██████
       ██████████        ████████████           ██████
       ███████       █████████████       ███    ██████
       ███████    █████████████       ██████    ██████
       ████████████████████       ██████████    ██████
       █████████████████       █████████████    ██████
       █████████████       █████████████        ██████
       ██████████       █████████████           ██████
       ███████      ██████████████       ███    ██████
       ██████    █████████████       ███████    ██████
       ██████    ██████████       ██████████    ██████
       ██████    ██████        █████████████    ██████
       ██████    ███       █████████████        ██████
       ██████           █████████████       ██████████
       ██████       █████████████        █████████████
                 █████████████       █████████████
              ████████████        █████████████
                  ████         ████████████
                           █████████████
                         ███████████
                            █████
Ferrum Network • Interoperability Network for Financial Applications
jwest411
Member
**
Offline Offline

Activity: 107
Merit: 10


View Profile
October 12, 2013, 07:59:32 PM
 #15

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...
daybyter
Legendary
*
Offline Offline

Activity: 965
Merit: 1000


View Profile
October 12, 2013, 08:40:37 PM
 #16

I thought key is your API key here?

jwest411
Member
**
Offline Offline

Activity: 107
Merit: 10


View Profile
October 13, 2013, 04:18:06 AM
 #17

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.
CoinFarmer
Member
**
Offline Offline

Activity: 205
Merit: 10


Upfiring Team Member


View Profile WWW
January 28, 2014, 09:26:53 PM
Last edit: September 04, 2017, 07:40:11 PM by CoinFarmer
 #18

I have a problem with nonce too, SHA512 encryption looks ok,I have checked on below calc:

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



ElectricMucus
Legendary
*
Offline Offline

Activity: 1666
Merit: 1057


Marketing manager - GO MP


View Profile WWW
January 28, 2014, 09:45:19 PM
 #19

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.
daybyter
Legendary
*
Offline Offline

Activity: 965
Merit: 1000


View Profile
January 28, 2014, 10:03:54 PM
 #20

Did you just try to print the http header you send to btc-e? Looks like the nonce is missing alltogether?

Pages: [1] 2 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!