-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitfinex.js
76 lines (58 loc) · 2.18 KB
/
bitfinex.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
// From https://bitfinex.readme.io/v1/docs/rest-auth
const superagent = require('superagent-use')(require('superagent'));
const crypto = require('crypto');
const assert = require('assert');
superagent.use(require('superagent-verbose-errors'));
const baseUrl = 'https://api.bitfinex.com';
const createBitfinexClient = (...args) => {
const nextKeyPair = typeof args[0] === 'function' ? args[0] : () => args.slice(0, 2);
return async (url, fields = {}) => {
const nonce = Date.now().toString();
const completeURL = baseUrl + url;
const requestBody = {
request: url,
nonce,
...fields,
};
const requestBodyStringified = JSON.stringify(requestBody);
const requestBodyEncoded = new Buffer(requestBodyStringified).toString('base64');
const [apiKey, apiSecret] = nextKeyPair();
assert(apiKey);
assert(apiSecret);
const signature = crypto
.createHmac('sha384', apiSecret)
.update(requestBodyEncoded)
.digest('hex');
const requestPromise = superagent
.post(completeURL)
.set('X-BFX-APIKEY', apiKey)
.set('X-BFX-PAYLOAD', requestBodyEncoded)
.set('X-BFX-SIGNATURE', signature)
.send(requestBodyStringified);
const response = await requestPromise;
const { body } = response;
assert(body, `body missing from response: ${response.text}`);
// Certain responses (like deposit, withdraw) have a result field, which is
// set to either "success" or "error"
if (body.result !== undefined) {
if (body.result === 'error') {
let message;
// address [string] The deposit address (or error message if result = “error”)
// https://docs.bitfinex.com/v1/reference#rest-auth-deposit
if (url === '/v1/deposit/new') {
message = body.address;
} else {
message = body.message;
}
if (!message) {
message = JSON.stringify(body);
}
throw new Error(`Request failed (result equals error): ${message}`);
} else if (body.result !== 'success') {
throw new Error(`Unexpected result field value, "${body.result}"`);
}
}
return body;
};
};
module.exports = createBitfinexClient;