-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const crypto = require('crypto');
const WebSocket = require('ws');
const baseUrl = 'wss://socket.btcmarkets.net/v2';
const channels = ['trade', 'heartbeat'];
const marketIds = ['BTC-AUD', 'ETH-AUD', 'LTC-AUD'];
// if using private channels then set api key and secret for authentication
const key = undefined;
const secret = 'add your API key secret here';
const ws = new WebSocket(baseUrl);
var request = {
marketIds:marketIds,
channels: channels,
messageType: 'subscribe'
}
if (key) {
const now = Date.now();
const strToSign = "/users/self/subscribe" + "\n" + now;
const signature = signMessage(secret, strToSign);
request.timestamp = now;
request.key = key;
request.signature = signature;
}
ws.on('open', function open() {
ws.send(JSON.stringify(request));
});
ws.on('message', function incoming(data) {
console.log(data);
});
ws.on('close', function close() {
console.log('socket closed');
});
ws.on('error', function error(err) {
console.error('error with websocket ', err);
});
function signMessage(secret, message) {
var key = Buffer.from(secret, 'base64');
var hmac = crypto.createHmac('sha512', key);
var signature = hmac.update(message).digest('base64');
return signature;
}