-
Notifications
You must be signed in to change notification settings - Fork 188
/
kraken.js
183 lines (151 loc) · 5.44 KB
/
kraken.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const got = require('got');
const crypto = require('crypto');
const qs = require('qs');
// Public/Private method names
const methods = {
public : [ 'Time', 'Assets', 'AssetPairs', 'Ticker', 'Depth', 'Trades', 'Spread', 'OHLC' ],
private : [ 'Balance', 'TradeBalance', 'OpenOrders', 'ClosedOrders', 'QueryOrders', 'TradesHistory', 'QueryTrades', 'OpenPositions', 'Ledgers', 'QueryLedgers', 'TradeVolume', 'AddOrder', 'CancelOrder', 'DepositMethods', 'DepositAddresses', 'DepositStatus', 'WithdrawInfo', 'Withdraw', 'WithdrawStatus', 'WithdrawCancel', 'GetWebSocketsToken' ],
};
// Default options
const defaults = {
url : 'https://api.kraken.com',
version : 0,
timeout : 5000,
};
// Create a signature for a request
const getMessageSignature = (path, request, secret, nonce) => {
const message = qs.stringify(request);
const secret_buffer = new Buffer(secret, 'base64');
const hash = new crypto.createHash('sha256');
const hmac = new crypto.createHmac('sha512', secret_buffer);
const hash_digest = hash.update(nonce + message).digest('binary');
const hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
};
// Send an API request
const rawRequest = async (url, headers, data, timeout) => {
// Set custom User-Agent string
headers['User-Agent'] = 'Kraken Javascript API Client';
const options = { headers, timeout };
Object.assign(options, {
method : 'POST',
body : qs.stringify(data),
});
const { body } = await got(url, options);
const response = JSON.parse(body);
if(response.error && response.error.length) {
const error = response.error
.filter((e) => e.startsWith('E'))
.map((e) => e.substr(1));
if(!error.length) {
throw new Error("Kraken API returned an unknown error");
}
throw new Error(error.join(', '));
}
return response;
};
/**
* KrakenClient connects to the Kraken.com API
* @param {String} key API Key
* @param {String} secret API Secret
* @param {String|Object} [options={}] Additional options. If a string is passed, will default to just setting `options.otp`.
* @param {String} [options.otp] Two-factor password (optional) (also, doesn't work)
* @param {Number} [options.timeout] Maximum timeout (in milliseconds) for all API-calls (passed to `request`)
*/
class KrakenClient {
constructor(key, secret, options) {
// Allow passing the OTP as the third argument for backwards compatibility
if(typeof options === 'string') {
options = { otp : options };
}
this.config = Object.assign({ key, secret }, defaults, options);
}
/**
* This method makes a public or private API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
api(method, params, callback) {
// Default params to empty object
if(typeof params === 'function') {
callback = params;
params = {};
}
if(methods.public.includes(method)) {
return this.publicMethod(method, params, callback);
}
else if(methods.private.includes(method)) {
return this.privateMethod(method, params, callback);
}
else {
throw new Error(method + ' is not a valid API method.');
}
}
/**
* This method makes a public API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
publicMethod(method, params, callback) {
params = params || {};
// Default params to empty object
if(typeof params === 'function') {
callback = params;
params = {};
}
const path = '/' + this.config.version + '/public/' + method;
const url = this.config.url + path;
const response = rawRequest(url, {}, params, this.config.timeout);
if(typeof callback === 'function') {
response
.then((result) => callback(null, result))
.catch((error) => callback(error, null));
}
return response;
}
/**
* This method makes a private API request.
* @param {String} method The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Function} callback A callback function to be executed when the request is complete
* @return {Object} The request object
*/
privateMethod(method, params, callback) {
params = params || {};
// Default params to empty object
if(typeof params === 'function') {
callback = params;
params = {};
}
const path = '/' + this.config.version + '/private/' + method;
const url = this.config.url + path;
if(!params.nonce) {
params.nonce = new Date() * 1000; // spoof microsecond
}
if(this.config.otp !== undefined) {
params.otp = this.config.otp;
}
const signature = getMessageSignature(
path,
params,
this.config.secret,
params.nonce
);
const headers = {
'API-Key' : this.config.key,
'API-Sign' : signature,
};
const response = rawRequest(url, headers, params, this.config.timeout);
if(typeof callback === 'function') {
response
.then((result) => callback(null, result))
.catch((error) => callback(error, null));
}
return response;
}
}
module.exports = KrakenClient;