askmike / bitstamp

Bitstamp REST API wrapper
81 stars 54 forks source link

nonce question #18

Closed element6 closed 9 years ago

element6 commented 9 years ago

Hi,

Perhaps this is something I don't know about javascript. If you initialize nonce in the constructor as Date.now(), then generateNonce function returns nonce++. Wouldn't that always be unique as long as you use the same object?

element6 commented 9 years ago

I've read the comment, I think I've got my answer.

askmike commented 9 years ago

To further clarify: Calling Date.now() will give back a Date object representing the current time (the object will always be unique). Saying nonce++ on that object will force it to it's numerical representation (and add 1 to it), which in the case of a Date object is the timestamp in ms. When you call the function multiple times you'll get different date objects that boil down to the same timestamp in ms.

The padding makes sure that this still results in a different nonce.