-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
104 lines (91 loc) · 2 KB
/
utils.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var { HmacSHA384, enc } = require('crypto-js')
function getConfig () {
return {
apiKey: process.env[ 'BFX_API_KEY' ] || '-',
apiSecret: process.env[ 'BFX_API_SECRET' ] || '-',
baseURL: process.env[ 'BFX_API_URL' ] || 'api.bitfinex.com'
}
}
function getApiKeys() {
const conf = getConfig()
return {
apiKey: conf.apiKey,
apiSecret: conf.apiSecret
}
}
function getRestURL (args = {}) {
const { version = 2 } = args
const { baseURL } = getConfig()
return `https://${baseURL}/v${version}`
}
function getWssURL (version = 2) {
const { baseURL } = getConfig()
switch(+version) {
case 2: {
return `wss://${baseURL}/ws/2`
}
case 1:
case 1.1: {
return `wss://${baseURL}/ws`
}
default: {
console.error('unknown version', version)
return null
}
}
}
function toQueryString (json = {}) {
const keys = Object.keys(json)
if (!keys.length) {
return ''
}
const params = keys.map(
(key) => `${encodeURIComponent(key)}=${encodeURIComponent(json[key])}`
)
return `?${params.join('&')}`
}
// wss
const generateAuth = (args = {}) => {
const {
apiKey = '',
apiSecret = '',
version = '2.0',
calc = 0
} = args
const nonce = Date.now() * 1000
let payload = {
apiKey,
event: 'auth',
authPayload: 'AUTH' + nonce,
calc
}
if (version === '2.0') {
payload.authNonce = +nonce
} else {
console.warn('Only wss v2 is now supported, sorry!')
return {}
}
payload.authSig = HmacSHA384(payload.authPayload, apiSecret).toString(enc.Hex)
return payload
}
function printResponse (error, response, body) {
const output = body || response
if (error) {
console.log('response (error)', error)
}
try {
const j = JSON.parse(output)
console.log('response (json):', JSON.stringify(j, 0, 2))
} catch (err) {
console.log('response (raw):', output)
}
}
module.exports = {
getConfig,
getApiKeys,
getRestURL,
getWssURL,
generateAuth,
toQueryString,
printResponse,
}