Time synchronization with server

- wrapper for /v2/public/time rest endpoint
- synchronize time before trying to authenticate a request in order
  to avoid invalid authentication because prevented replay attacks.

Resolves #1
This commit is contained in:
Stefan Aebischer
2019-09-16 14:17:48 +02:00
parent ed28d14171
commit b6d2803a49
4 changed files with 47 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ const {EventEmitter} = require('events');
const WebSocket = require('ws');
const defaultLogger = require('./logger.js');
const RestClient = require('./rest-client.js');
const {signMessage} = require('./utility.js');
const wsUrls = {
@@ -33,6 +34,8 @@ module.exports = class WebsocketClient extends EventEmitter {
...options
}
this.client = new RestClient(null, null, this.options.livenet);
this._connect();
}
@@ -54,10 +57,11 @@ module.exports = class WebsocketClient extends EventEmitter {
this.ws.close();
}
_connect() {
async _connect() {
if(this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING;
const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + this._authenticate();
const authParams = await this._authenticate();
const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams;
this.ws = new WebSocket(url);
@@ -67,12 +71,15 @@ module.exports = class WebsocketClient extends EventEmitter {
this.ws.on('close', this._wsCloseHandler.bind(this));
}
_authenticate() {
async _authenticate() {
if(this.options.key && this.options.secret) {
this.logger.debug('Starting authenticated websocket client.', {category: 'bybit-ws'});
const timeOffset = await this.client.getTimeOffset();
const params = {
api_key: this.options.key,
expires: (Date.now() + 10000)
expires: (Date.now() + timeOffset + 5000)
};
params.signature = signMessage('GET/realtime' + params.expires, this.options.secret);