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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user