-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkraken.js
217 lines (193 loc) · 6.17 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Kraken API by Robert Myers (https://github.com/nothingisdead)
*
* updated to modify timeout
*/
var request = require('request');
var crypto = require('crypto');
var querystring = require('querystring');
/**
* KrakenClient connects to the Kraken.com API
* @param {String} key API Key
* @param {String} secret API Secret
* @param {String} [otp] Two-factor password (optional) (also, doesn't work)
*/
function KrakenClient(key, secret, otp, timeout) {
var self = this;
var config = {
url: 'https://api.kraken.com',
version: '0',
key: key,
secret: secret,
otp: otp,
timeoutMS: timeout || 5000
};
/**
* 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
*/
function api(method, params, callback) {
var 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'
]
};
if (methods.public.indexOf(method) !== -1) {
return publicMethod(method, params, callback);
} else if (methods.private.indexOf(method) !== -1) {
return 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
*/
function publicMethod(method, params, callback) {
params = params || {};
var path = '/' + config.version + '/public/' + method;
var url = config.url + path;
return rawRequest(url, {}, params, callback);
}
/**
* 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
*/
function privateMethod(method, params, callback) {
params = params || {};
var path = '/' + config.version + '/private/' + method;
var url = config.url + path;
params.nonce = new Date() * 1000; // spoof microsecond
if (config.otp !== undefined) {
params.otp = config.otp;
}
var signature = getMessageSignature(path, params, params.nonce);
var headers = {
'API-Key': config.key,
'API-Sign': signature
};
return rawRequest(url, headers, params, callback);
}
/**
* This method returns a signature for a request as a Base64-encoded string
* @param {String} path The relative URL path for the request
* @param {Object} request The POST body
* @param {Integer} nonce A unique, incrementing integer
* @return {String} The request signature
*/
function getMessageSignature(path, request, nonce) {
var message = querystring.stringify(request);
var secret = new Buffer(config.secret, 'base64');
var hash = new crypto.createHash('sha256');
var hmac = new crypto.createHmac('sha512', secret);
var hash_digest = hash.update(nonce + message).digest('binary');
var hmac_digest = hmac
.update(path + hash_digest, 'binary')
.digest('base64');
return hmac_digest;
}
/**
* This method sends the actual HTTP request
* @param {String} url The URL to make the request
* @param {Object} headers Request headers
* @param {Object} params POST body
* @param {Function} callback A callback function to call when the request is complete
* @return {Object} The request object
*/
function rawRequest(url, headers, params, callback) {
// Set custom User-Agent string
headers['User-Agent'] = 'Kraken Javascript API Client';
var options = {
url: url,
method: 'POST',
headers: headers,
form: params,
timeout: config.timeoutMS
};
var req = request.post(options, function(error, response, body) {
if (typeof callback === 'function') {
var data;
if (error) {
return callback.call(
self,
new Error('Error in server response: ' + JSON.stringify(error)),
null
);
}
try {
data = JSON.parse(body);
} catch (e) {
return callback.call(
self,
new Error('Could not understand response from server: ' + body),
null
);
}
//If any errors occured, Kraken will give back an array with error strings under
//the key "error". We should then propagate back the error message as a proper error.
if (data.error && data.error.length) {
var krakenError = null;
data.error.forEach(function(element) {
if (element.charAt(0) === 'E') {
krakenError = element.substr(1);
return false;
}
});
if (krakenError) {
return callback.call(
self,
new Error('Kraken API returned error: ' + krakenError),
null
);
}
} else {
return callback.call(self, null, data);
}
}
});
return req;
}
self.api = api;
self.publicMethod = publicMethod;
self.privateMethod = privateMethod;
}
module.exports = KrakenClient;