-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
412 lines (400 loc) · 20.1 KB
/
Copy pathindex.js
File metadata and controls
412 lines (400 loc) · 20.1 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const crypto = require('crypto');
const request = require('request');
const HOST_URL = "https://www.cryptopia.co.nz/api";
let Cryptopia = function () {
let options = {
API_KEY: null,
API_SECRET: null,
HOST_URL: HOST_URL,
API_PATH: null
};
//HTTPS Private Request
async function privateRequest(opts, callback) {
request.post(opts, function (err, response, body) {
if (err) {
return callback(new Error(err));
}
body = JSON.parse(body);
if (body.Success !== true) {
return callback(new Error(body.Error));
}
return callback(undefined, body.Data);
});
}
//HTTPS Publc Request
async function publicRequest(opts, callback) {
opts.headers = {
'Content-Type': 'application/json; charset=utf-8'
};
request.get(opts, function (err, response, body) {
if (err) {
return callback(new Error(err));
}
body = JSON.parse(body);
if (body.Success !== true) {
return callback(new Error(body.Error));
}
return callback(undefined, body.Data);
});
}
//Authorization Builder
function buildAuth(params, opts) {
let nonce = crypto.randomBytes(64).toString('hex');
let md5 = crypto.createHash('md5').update(JSON.stringify(params)).digest();
let requestContentBase64String = md5.toString('base64');
let signature = opts.API_KEY + "POST" + encodeURIComponent(opts.HOST_URL + "/" + opts.API_PATH).toLowerCase() + nonce + requestContentBase64String;
let hmacsignature = crypto.createHmac('sha256', new Buffer(opts.API_SECRET, "base64")).update(signature).digest().toString('base64');
return "amx " + opts.API_KEY + ":" + hmacsignature + ":" + nonce;
}
//Client Functions
return {
//Private APIs
getBalance: async function (params, callback) {
params = params || {};
options.API_PATH = "GetBalance";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
getDepositAddress: async function (params, callback) {
params = params || {};
if (!params.Currency && !params.CurrencyId) {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC' OR you must supply a valid Currency ID, e.g. '2'!"));
} else if (params.CurrencyId && typeof params.CurrencyId !== 'number') {
return callback(new Error("You must supply a valid Currency ID, e.g. '2'!"));
} else if (params.Currency && typeof params.Currency !== 'string') {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC'!"));
}
options.API_PATH = "GetDepositAddress";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
getOpenOrders: async function (params, callback) {
params = params || {};
if (!params.Market && !params.TradePairId) {
return callback(new Error("You must supply a valid Market, e.g. 'BTC/USDT' OR you must supply a valid Trade Pair ID, e.g. '100'!"));
} else if (params.TradePairId && typeof params.TradePairId !== 'number') {
return callback(new Error("You must supply a valid Trade Pair ID, e.g. '100'!"));
} else if (params.Market && typeof params.Market !== 'string') {
return callback(new Error("You must supply a valid Market, e.g. 'DOT/BTC'!"));
} else if (params.Count && typeof params.Count !== 'number') {
return callback(new Error("You must supply a valid Count, e.g. between '1' and '100' !"));
}
options.API_PATH = "GetOpenOrders";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
getTradeHistory: async function (params, callback) {
params = params || {};
if (!params.Market && !params.TradePairId) {
return callback(new Error("You must supply a valid Market, e.g. 'BTC/USDT' OR you must supply a valid Trade Pair ID, e.g. '100'!"));
} else if (params.TradePairId && typeof params.TradePairId !== 'number') {
return callback(new Error("You must supply a valid Trade Pair ID, e.g. '100'!"));
} else if (params.Market && typeof params.Market !== 'string') {
return callback(new Error("You must supply a valid Market, e.g. 'DOT/BTC'!"));
} else if (params.Count && typeof params.Count !== 'number') {
return callback(new Error("You must supply a valid Count, e.g. between '1' and '100' !"));
}
options.API_PATH = "GetTradeHistory";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
getTransactions: async function (params, callback) {
params = params || {};
if (!params.Type) {
return callback(new Error("You must supply a valid Type, e.g. 'Deposit' or 'Withdraw'!"));
} else if (params.Type && params.Type !== 'Deposit' && params.Type !== 'Withdraw') {
return callback(new Error("You must supply a valid Type, e.g. 'Deposit' or 'Withdraw'!"));
} else if (params.Count && typeof params.Count !== 'number') {
return callback(new Error("You must supply a valid Count, e.g. between '1' and '100'!"));
}
options.API_PATH = "GetTransactions";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
submitTrade: async function (params, callback) {
params = params || {};
if (!params.Market && !params.TradePairId) {
return callback(new Error("You must supply a valid Market, e.g. 'BTC/USDT' OR you must supply a valid Trade Pair ID, e.g. '100'!"));
} else if (params.TradePairId && typeof params.TradePairId !== 'number') {
return callback(new Error("You must supply a valid Trade Pair ID, e.g. '100'!"));
} else if (params.Market && typeof params.Market !== 'string') {
return callback(new Error("You must supply a valid Market, e.g. 'DOT/BTC'!"));
} else if (!params.Type) {
return callback(new Error("You must supply a valid Type, e.g. 'Buy' or 'Sell'!"));
} else if (params.Type && params.Type !== 'Buy' && params.Type !== 'Sell') {
return callback(new Error("You must supply a valid Type, e.g. 'Buy' or 'Sell'!"));
} else if (!params.Rate || !params.Amount) {
return callback(new Error("You must supply a valid Rate and Amount, e.g. Rate: '0.00000034' or Amount: '123.00000000'!"));
} else if (typeof params.Rate !== 'number' || typeof params.Amount !== 'number') {
return callback(new Error("You must supply a valid Rate and Amount, e.g. Rate: '0.00000034' or Amount: '123.00000000'!"));
}
options.API_PATH = "SubmitTrade";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
cancelTrade: async function (params, callback) {
params = params || {};
if (!params.Type) {
return callback(new Error("You must supply a valid Type, e.g. 'All', 'Trade', or 'TradePair'!"));
} else if (params.Type && params.Type !== 'All' && params.Type !== 'Trade' && params.Type !== 'TradePair') {
return callback(new Error("You must supply a valid Type, e.g. 'All', 'Trade', or 'TradePair'!"));
} else if (params.Type === 'Trade' && typeof params.OrderId !== 'number') {
return callback(new Error("You must supply a valid OrderId, e.g. '19523'!"));
} else if (params.Type === 'TradePair' && typeof params.TradePairId !== 'number') {
return callback(new Error("You must supply a valid TradePairId, e.g. '100'!"));
}
options.API_PATH = "CancelTrade";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
submitTip: async function (params, callback) {
params = params || {};
if (!params.Currency || !params.CurrencyId) {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC' OR you must supply a valid Currency ID, e.g. '2'!"));
} else if (params.CurrencyId && typeof params.CurrencyId !== 'number') {
return callback(new Error("You must supply a valid Currency ID, e.g. '2'!"));
} else if (params.Currency && typeof params.Currency !== 'string') {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC'!"));
} else if (!params.ActiveUsers) {
return callback(new Error("You must supply a valid Active User count, e.g. between '2' and '100'!"));
} else if (params.ActiveUsers && (typeof params.ActiveUsers !== 'number' || params.ActiveUsers < 2 || params.ActiveUsers > 100)) {
return callback(new Error("You must supply a valid Active User count, e.g. between '2' and '100'!"));
} else if (!params.Amount) {
return callback(new Error("You must supply a valid Amount, e.g. Amount: '123.00000000'!"));
} else if (typeof params.Amount !== 'number') {
return callback(new Error("You must supply a valid Rate and Amount, e.g. Amount: '123.00000000'!"));
}
options.API_PATH = "SubmitTip";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
submitWithdraw: async function (params, callback) {
params = params || {};
if (!params.Currency || !params.CurrencyId) {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC' OR you must supply a valid Currency ID, e.g. '2'!"));
} else if (params.CurrencyId && typeof params.CurrencyId !== 'number') {
return callback(new Error("You must supply a valid Currency ID, e.g. '2'!"));
} else if (params.Currency && typeof params.Currency !== 'string') {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC'!"));
} else if (!params.Address) {
return callback(new Error("You must supply a valid Address that exists within your AddressBook!"));
} else if (!params.PaymentId) {
return callback(new Error("You must supply a valid Payment ID! *The unique paymentid to identify the payment. (PaymentId for CryptoNote coins.)!"));
} else if (!params.Amount || typeof params.Amount !== 'number') {
return callback(new Error("You must supply a valid Amount, e.g. Amount: '123.00000000'!"));
}
options.API_PATH = "SubmitWithdraw";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
submitTransfer: async function (params, callback) {
params = params || {};
if (!params.Currency || !params.CurrencyId) {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC' OR you must supply a valid Currency ID, e.g. '2'!"));
} else if (params.CurrencyId && typeof params.CurrencyId !== 'number') {
return callback(new Error("You must supply a valid Currency ID, e.g. '2'!"));
} else if (params.Currency && typeof params.Currency !== 'string') {
return callback(new Error("You must supply a valid Currency, e.g. 'BTC'!"));
} else if (!params.UserName) {
return callback(new Error("You must supply a valid Cryptopia UserName, e.g. 'bigdaddy438'!"));
} else if (!params.Amount || typeof params.Amount !== 'number') {
return callback(new Error("You must supply a valid Amount, e.g. Amount: '123.00000000'!"));
}
options.API_PATH = "SubmitTransfer";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Authorization': buildAuth(params, options),
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(params)
};
return await privateRequest(reqOpts, callback);
},
//Public APIs
getCurrencies: async function (callback) {
options.API_PATH = "GetCurrencies";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
return await publicRequest(reqOpts, callback);
},
getTradePairs: async function (callback) {
options.API_PATH = "GetTradePairs";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
return await publicRequest(reqOpts, callback);
},
getMarkets: async function (params, callback) {
params = params || {};
let urlParams = "";
if (params.BaseMarket && params.Hours) {
urlParams = "/" + params.BaseMarket + "/" + params.Hours;
} else if (params.Hours) {
urlParams = "/" + params.Hours;
} else if (params.BaseMarket) {
urlParams = "/" + params.BaseMarket;
}
options.API_PATH = "GetMarkets";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH + urlParams
};
return await publicRequest(reqOpts, callback);
},
getMarket: async function (params, callback) {
params = params || {};
if (!params.Market) {
callback(new Error("You must supply a valid Market or Trade Pair Id"));
}
let urlParams = "";
if (params.Market && params.Hours) {
urlParams = "/" + params.Market + "/" + params.Hours;
} else {
urlParams = "/" + params.Market;
}
options.API_PATH = "GetMarket";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH + urlParams
};
return await publicRequest(reqOpts, callback);
},
getMarketHistory: async function (params, callback) {
params = params || {};
if (!params.Market) {
callback(new Error("You must supply a valid Market or Trade Pair Id"));
}
let urlParams = "";
if (params.Market && params.Hours) {
urlParams = "/" + params.Market + "/" + params.Hours;
} else {
urlParams = "/" + params.Market;
}
options.API_PATH = "GetMarketHistory";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH + urlParams
};
return await publicRequest(reqOpts, callback);
},
getMarketOrders: async function (params, callback) {
params = params || {};
if (!params.Market) {
callback(new Error("You must supply a valid Market or Trade Pair Id, e.g. 'BTC_LTC' or '100'!"));
}
let urlParams = "";
if (params.Market && params.Count) {
urlParams = "/" + params.Market + "/" + params.Count;
} else {
urlParams = "/" + params.Market;
}
options.API_PATH = "GetMarketOrders";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH + urlParams
};
return await publicRequest(reqOpts, callback);
},
getMarketOrderGroups: async function (params, callback) {
params = params || {};
if (!params.Market || Array.isArray(params.Market) === false) {
callback(new Error("You must supply a valid Market or Trade Pair Id as an array, e.g. ['BTC_LTC', 'DOGE_USDT']!"));
}
let urlParams = "";
for (var i = 0; i < params.Market.length; i++) {
urlParams += params.Market[i];
if (i !== params.Market.length - 1) {
urlParams += "-";
}
}
if (params.Count) {
urlParams = "/" + params.Count;
}
options.API_PATH = "GetMarketOrderGroups";
let reqOpts = {
url: options.HOST_URL + "/" + options.API_PATH + urlParams
};
return await publicRequest(reqOpts, callback);
},
//Set Options for API
setOptions: function (opts) {
if (opts.API_KEY && opts.API_SECRET) {
options.API_KEY = opts.API_KEY;
options.API_SECRET = opts.API_SECRET;
} else {
throw "You must supply a valid Options object including API_KEY and API_SECRET!";
}
if (opts.HOST_URL) {
options.HOST_URL = opts.HOST_URL;
}
}
}
};
module.exports = exports = Cryptopia;