-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoinfalcon.js
54 lines (40 loc) · 1.36 KB
/
coinfalcon.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
// From https://docs.coinfalcon.com/#authentication
// const superagent = require('superagent-use')(require('superagent'));
const superagent = require('superagent');
const crypto = require('crypto');
const assert = require('assert');
// superagent.use(require('superagent-verbose-errors'));
const baseUrl = 'https://coinfalcon.com';
const createCoinFalconClient = (apiKey, apiSecret) => {
const getHeaders = (method, pathWithQuery, body) => {
assert(method, 'method');
assert(pathWithQuery, 'pathWithQuery');
const timestamp = Math.floor(new Date() / 1e3);
const payload = [timestamp, method.toUpperCase(), pathWithQuery];
if (body) {
payload.push(JSON.stringify(body));
}
const signature = crypto
.createHmac('sha256', apiSecret)
.update(payload.join('|'))
.digest('hex');
return {
'CF-API-KEY': apiKey,
'CF-API-TIMESTAMP': timestamp,
'CF-API-SIGNATURE': signature,
};
};
const request = async (method, path, body) => {
let chain = superagent(method, baseUrl + path).set(getHeaders(method, path, body));
if (body) {
chain = chain.send(body);
}
const { body: { error, data } } = await chain;
if (error) {
throw new Error(`Coinfalcon: ${error}`);
}
return data;
};
return { getHeaders, baseUrl, request };
};
module.exports = createCoinFalconClient;