bitfinexcom / bitfinex-api-node

BITFINEX NodeJS trading API - Bitcoin, Litecoin, and Ether exchange
https://www.bitfinex.com/
MIT License
462 stars 214 forks source link

Nonce too small #406

Closed tuloski closed 6 years ago

tuloski commented 6 years ago

Using the master (not tag 2.0.0). When doing the auth via ws I'm getting the error: auth failed: nonce: small (FAILED)

The following is the used snippet

"use strict";
var EventEmitter = require('events').EventEmitter;
var util = require('util');
const BFX = require('bitfinex-api-node');

const bfx = new BFX({
    apiKey: 'removed',
    apiSecret: 'removed',

    ws: {
        autoReconnect: true,
        seqAudit: true,
        packetWDDelay: 10 * 1000
    }
})

const rest = bfx.rest(2, {
    // options
})

var ws = bfx.ws(2);

ws.on('error', (err) => {
    console.log("Error received: ");
    console.log(err)
})
ws.on('auth', (msg) => {
    console.log('ws: authenticated')
})
ws.on('open', () => {
    //this.wsMain.auth.bind(this.wsMain);
    ws.auth();
    console.log('open ws Bitfinex');
})
ws.on('close', () => {
    console.log('close ws Bitfinex');
})
ws.open();
tuloski commented 6 years ago

And the ws keeps closing and reopening as in https://github.com/bitfinexcom/bitfinex-api-node/issues/404

tux-00 commented 6 years ago

Maybe linked with https://github.com/bitfinexcom/bitfinex-api-node/pull/405

vansergen commented 6 years ago

Probably, auth() returns an error because you are already authenticated. Did you try to listen for auth message?

ws.on('open' => ws.auth.bind(ws));
ws.on('message', (data) => {
  if(data.event === 'auth') {
    this.authChannel = data.chanId;
  } else if(data[0] === this.authChannel) {
    console.log('Received a message on auth channel', data);
  };
});

I had the same problem with reopening. You can try packetWDDelay: 50 * 1000. It worked for me.

tuloski commented 6 years ago

Yeah, see my attached snippet. I'm listening to 'auth' but I'm not receiving it.

But there is not this problem on 2.0.0. It's there only on master.

vansergen commented 6 years ago

I just switched to master branch and received the same error:

{ event: 'auth',
  status: 'FAILED',
  chanId: 0,
  code: 10114,
  msg: 'nonce: small' }

It happened because of this commit So, I changed the line

  let now = new Date().getTime()

to

  let now = 1000 * new Date().getTime()

in the nonce.js file and authenticated successfully. So, you can modify nonce.js by yourself to keep using the same API key or generate a new one.

f3rno commented 6 years ago

@tuloski @vansergen is correct, you need to generate a new API key since the nonce size has been reduced (it was needlessly multiplied by 1000 before). This is the last time the nonce will be reduced in size, we apologize for the inconvenience.