-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREST-v2.js
78 lines (67 loc) · 1.7 KB
/
REST-v2.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
const crypto = require('crypto')
const request = require('request')
const utils = require ('./utils.js')
const {
getRestURL,
getApiKeys,
toQueryString,
printResponse
} = utils
// Public Endpoints (GET)
function publicMessage ({ url, params = {} }) {
const completeURL = `${getRestURL()}/${url}`
const queryString = toQueryString(params)
// DEBUG
console.log('url:', completeURL)
console.log('params:', JSON.stringify(params, 0, 2))
console.log('query string:', queryString)
const options = {
url: `${completeURL}${queryString}`
}
return request.get(
options,
printResponse
)
}
function authMessage (args = {}) {
const {
url,
body = {},
params = {}
} = args
const {
apiKey = '',
apiSecret = ''
} = getApiKeys()
const baseUrl = getRestURL({ version: 2 })
const nonce = JSON.stringify(Date.now().toString() * 1000)
const rawBody = JSON.stringify(body)
const queryString = toQueryString(params)
const signatureUrl = `/api/v2/${url}/${nonce}${rawBody}`
const hmac = crypto.createHmac('sha384', apiSecret).update(signatureUrl)
const signature = hmac.digest('hex')
const completeURL = `${baseUrl}/${url}/${queryString}`
// DEBUG
console.log('completeURL:',completeURL)
console.log('signature url:', signatureUrl) // completeURL
console.log('body:', JSON.stringify(body, 0, 2))
console.log('nonce:', nonce)
console.log('signature:', signature)
const options = {
url: completeURL,
headers: {
'bfx-nonce': nonce,
'bfx-apikey': apiKey,
'bfx-signature': signature
},
json: body
}
return request.post(
options,
printResponse
)
}
module.exports = {
get: publicMessage,
post: authMessage
}