UNPKG

15.7 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8var _https = _interopRequireDefault(require("https"));
9
10var _crypto = _interopRequireDefault(require("crypto"));
11
12var _querystring = _interopRequireDefault(require("querystring"));
13
14var _config = _interopRequireDefault(require("./config"));
15
16var _error = _interopRequireDefault(require("./error"));
17
18var _util = _interopRequireDefault(require("./util"));
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
21
22function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
24function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
25
26function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
27
28var API_VERSION = _config["default"].API_VERSION,
29 API_PROTOCOL = _config["default"].API_PROTOCOL,
30 API_HOST = _config["default"].API_HOST,
31 API_PATH = _config["default"].API_PATH;
32
33var Coinpayments =
34/*#__PURE__*/
35function () {
36 function Coinpayments(_ref) {
37 var _ref$key = _ref.key,
38 key = _ref$key === void 0 ? "" : _ref$key,
39 _ref$secret = _ref.secret,
40 secret = _ref$secret === void 0 ? "" : _ref$secret;
41
42 _classCallCheck(this, Coinpayments);
43
44 if (!key) throw new _error["default"]("Missing public key");
45 if (!secret) throw new _error["default"]("Missing private key");
46 this.credentials = {
47 key: key,
48 secret: secret
49 };
50 this.getBasicInfo = this.getBasicInfo.bind(this);
51 this.rates = this.rates.bind(this);
52 this.balances = this.balances.bind(this);
53 this.getDepositAddress = this.getDepositAddress.bind(this);
54 this.createTransaction = this.createTransaction.bind(this);
55 this.getCallbackAddress = this.getCallbackAddress.bind(this);
56 this.getTx = this.getTx.bind(this);
57 this.getTxList = this.getTxList.bind(this);
58 this.getTxMulti = this.getTxMulti.bind(this);
59 this.createTransfer = this.createTransfer.bind(this);
60 this.convertCoins = this.convertCoins.bind(this);
61 this.convertLimits = this.convertLimits.bind(this);
62 this.getWithdrawalHistory = this.getWithdrawalHistory.bind(this);
63 this.getWithdrawalInfo = this.getWithdrawalInfo.bind(this);
64 this.getConversionInfo = this.getConversionInfo.bind(this);
65 this.getProfile = this.getProfile.bind(this);
66 this.tagList = this.tagList.bind(this);
67 this.updateTagProfile = this.updateTagProfile.bind(this);
68 this.claimTag = this.claimTag.bind(this);
69 }
70
71 _createClass(Coinpayments, [{
72 key: "_getPrivateHeaders",
73 value: function _getPrivateHeaders(parameters) {
74 var _this$credentials = this.credentials,
75 secret = _this$credentials.secret,
76 key = _this$credentials.key;
77 parameters.key = key;
78
79 var paramString = _querystring["default"].stringify(parameters);
80
81 var signature = _crypto["default"].createHmac("sha512", secret).update(paramString).digest("hex");
82
83 return {
84 "Content-Type": "application/x-www-form-urlencoded",
85 HMAC: signature
86 };
87 }
88 }, {
89 key: "request",
90 value: function request(parameters, callback) {
91 var assertResult = _util["default"].assertPayload(parameters);
92
93 if (assertResult.isError) {
94 if (callback) return callback(assertResult.error);
95 return Promise.reject(assertResult.error);
96 }
97
98 parameters.version = API_VERSION;
99 var options = {
100 protocol: API_PROTOCOL,
101 method: "post",
102 host: API_HOST,
103 path: API_PATH,
104 headers: this._getPrivateHeaders(parameters)
105 };
106
107 var query = _querystring["default"].stringify(parameters);
108
109 if (callback) {
110 return this.callbackRequest(options, query, callback);
111 } else {
112 return this.promiseRequest(options, query);
113 }
114 }
115 }, {
116 key: "callbackRequest",
117 value: function callbackRequest(options, query, callback) {
118 var req = _https["default"].request(options, function (res) {
119 var data = "";
120 res.setEncoding("utf8");
121 res.on("data", function (chunk) {
122 data += chunk;
123 });
124 res.on("end", function () {
125 try {
126 data = JSON.parse(data);
127 } catch (e) {
128 return callback(new _error["default"]("Invalid response", data));
129 }
130
131 if (data.error !== "ok") {
132 return callback(new _error["default"](data.error, data));
133 }
134
135 return callback(null, data.result);
136 });
137 });
138
139 req.on("error", callback);
140 req.write(query);
141 req.end();
142 }
143 }, {
144 key: "promiseRequest",
145 value: function promiseRequest(options, query) {
146 return new Promise(function (resolve, reject) {
147 var req = _https["default"].request(options, function (res) {
148 var data = "";
149 res.setEncoding("utf8");
150 res.on("data", function (chunk) {
151 data += chunk;
152 });
153 res.on("end", function () {
154 try {
155 data = JSON.parse(data);
156 } catch (e) {
157 return reject(new _error["default"]("Invalid response", data));
158 }
159
160 if (data.error !== "ok") {
161 return reject(new _error["default"](data.error, data));
162 }
163
164 return resolve(data.result);
165 });
166 });
167
168 req.on("error", reject);
169 req.write(query);
170 req.end();
171 });
172 }
173 /**
174 *
175 * @param {Object} options
176 * @param {Function} [callback]
177 * @returns {(Function|Promise)}
178 */
179
180 }, {
181 key: "createTransaction",
182 value: function createTransaction(options, callback) {
183 options = Object.assign({}, options, {
184 cmd: "create_transaction"
185 });
186 return this.request(options, callback);
187 }
188 /**
189 *
190 * @param {Object} [options]
191 * @param {Function} [callback]
192 * @returns {(Function|Promise)}
193 */
194
195 }, {
196 key: "rates",
197 value: function rates() {
198 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
199 var callback = arguments.length > 1 ? arguments[1] : undefined;
200
201 if (typeof options == "function") {
202 callback = options;
203 options = {};
204 }
205
206 options = Object.assign({}, options, {
207 cmd: "rates"
208 });
209 return this.request(options, callback);
210 }
211 /**
212 *
213 * @param {Object} [options]
214 * @param {Function} [callback]
215 * @returns {(Function|Promise)}
216 */
217
218 }, {
219 key: "balances",
220 value: function balances() {
221 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
222 var callback = arguments.length > 1 ? arguments[1] : undefined;
223
224 if (typeof options == "function") {
225 callback = options;
226 options = {};
227 }
228
229 options = Object.assign({}, options, {
230 cmd: "balances"
231 });
232 return this.request(options, callback);
233 }
234 /**
235 *
236 * @param {Object} options
237 * @param {Function} [callback]
238 * @returns {(Function|Promise)}
239 */
240
241 }, {
242 key: "createWithdrawal",
243 value: function createWithdrawal(options, callback) {
244 options = Object.assign({
245 auto_confirm: 1
246 }, options, {
247 cmd: "create_withdrawal"
248 });
249 return this.request(options, callback);
250 }
251 /**
252 *
253 * @param {Array} withdrawalArray
254 * @param {Function} [callback]
255 * @returns {(Function|Promise)}
256 */
257
258 }, {
259 key: "createMassWithdrawal",
260 value: function createMassWithdrawal(withdrawalArray, callback) {
261 if (!(withdrawalArray instanceof Array)) {
262 return callback(new _error["default"]("Invalid withdrawal array".withdrawalArray));
263 }
264
265 withdrawalArray = withdrawalArray.filter(function (w) {
266 return w.currency && w.amount && w.address;
267 });
268 if (!withdrawalArray.length) return callback(new _error["default"]("Invalid withdrawal array".withdrawalArray));
269 var options = {
270 cmd: "create_mass_withdrawal"
271 };
272 withdrawalArray.reduce(function (options, w, index) {
273 options["wd[wd".concat(index + 1, "][amount]")] = w.amount;
274 options["wd[wd".concat(index + 1, "][address]")] = w.address;
275 options["wd[wd".concat(index + 1, "][currency]")] = w.currency;
276
277 if (w.dest_tag) {
278 options["wd[wd".concat(index + 1, "][dest_tag]")] = w.dest_tag;
279 }
280
281 return options;
282 }, options);
283 return this.request(options, callback);
284 }
285 /**
286 *
287 * @param {Object} options
288 * @param {Function} [callback]
289 * @returns {(Function|Promise)}
290 */
291
292 }, {
293 key: "getTx",
294 value: function getTx(options, callback) {
295 options = Object.assign({}, options, {
296 cmd: "get_tx_info"
297 });
298 return this.request(options, callback);
299 }
300 /**
301 *
302 * @param {Object} options
303 * @param {Function} [callback]
304 * @returns {(Function|Promise)}
305 */
306
307 }, {
308 key: "getWithdrawalInfo",
309 value: function getWithdrawalInfo(options, callback) {
310 options = Object.assign({}, options, {
311 cmd: "get_withdrawal_info"
312 });
313 return this.request(options, callback);
314 }
315 /**
316 *
317 * @param {Array} tx_id_array
318 * @param {Function} [callback]
319 * @returns {(Function|Promise)}
320 */
321
322 }, {
323 key: "getTxMulti",
324 value: function getTxMulti() {
325 var tx_id_array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
326 var callback = arguments.length > 1 ? arguments[1] : undefined;
327 var options = {};
328
329 if (!(tx_id_array instanceof Array) || !tx_id_array.length) {
330 return callback(new _error["default"]("Invalid argument", tx_id_array));
331 }
332
333 Object.assign(options, {
334 txid: tx_id_array.join("|")
335 }, {
336 cmd: "get_tx_info_multi"
337 });
338 return this.request(options, callback);
339 }
340 /**
341 *
342 * @param {Object} [options]
343 * @param {Function} [callback]
344 * @returns {(Function|Promise)}
345 */
346
347 }, {
348 key: "getTxList",
349 value: function getTxList() {
350 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
351 var callback = arguments.length > 1 ? arguments[1] : undefined;
352
353 if (typeof options == "function") {
354 callback = options;
355 options = {};
356 }
357
358 options = Object.assign({}, options, {
359 cmd: "get_tx_ids"
360 });
361 return this.request(options, callback);
362 }
363 /**
364 *
365 * @param {Function} [callback]
366 * @returns {(Function|Promise)}
367 */
368
369 }, {
370 key: "getBasicInfo",
371 value: function getBasicInfo(callback) {
372 var options = {
373 cmd: "get_basic_info"
374 };
375 return this.request(options, callback);
376 }
377 /**
378 *
379 * @param {Object} options
380 * @param {Function} [callback]
381 * @returns {(Function|Promise)}
382 */
383
384 }, {
385 key: "getDepositAddress",
386 value: function getDepositAddress(options, callback) {
387 options = Object.assign({}, options, {
388 cmd: "get_deposit_address"
389 });
390 return this.request(options, callback);
391 }
392 /**
393 *
394 * @param {Object} options
395 * @param {Function} [callback]
396 * @returns {(Function|Promise)}
397 */
398
399 }, {
400 key: "getCallbackAddress",
401 value: function getCallbackAddress(options, callback) {
402 options = Object.assign({}, options, {
403 cmd: "get_callback_address"
404 });
405 return this.request(options, callback);
406 }
407 /**
408 *
409 * @param {Object} options
410 * @param {Function} [callback]
411 * @returns {(Function|Promise)}
412 */
413
414 }, {
415 key: "createTransfer",
416 value: function createTransfer(options, callback) {
417 options = Object.assign({
418 auto_confirm: 1
419 }, options, {
420 cmd: "create_transfer"
421 });
422 return this.request(options, callback);
423 }
424 /**
425 *
426 * @param {Object} options
427 * @param {Function} [callback]
428 * @returns {(Function|Promise)}
429 */
430
431 }, {
432 key: "convertCoins",
433 value: function convertCoins(options, callback) {
434 options = Object.assign({}, options, {
435 cmd: "convert"
436 });
437 return this.request(options, callback);
438 }
439 /**
440 *
441 * @param {Object} options
442 * @param {Function} [callback]
443 * @returns {(Function|Promise)}
444 */
445
446 }, {
447 key: "convertLimits",
448 value: function convertLimits(options, callback) {
449 options = Object.assign({}, options, {
450 cmd: "convert_limits"
451 });
452 return this.request(options, callback);
453 }
454 /**
455 *
456 * @param {Object} [options]
457 * @param {Function} [callback]
458 * @returns {(Function|Promise)}
459 */
460
461 }, {
462 key: "getWithdrawalHistory",
463 value: function getWithdrawalHistory() {
464 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
465 var callback = arguments.length > 1 ? arguments[1] : undefined;
466
467 if (typeof options == "function") {
468 callback = options;
469 options = {};
470 }
471
472 options = Object.assign({}, options, {
473 cmd: "get_withdrawal_history"
474 });
475 return this.request(options, callback);
476 }
477 /**
478 *
479 * @param {Object} options
480 * @param {Function} [callback]
481 * @returns {(Function|Promise)}
482 */
483
484 }, {
485 key: "getConversionInfo",
486 value: function getConversionInfo(options, callback) {
487 options = Object.assign({}, options, {
488 cmd: "get_conversion_info"
489 });
490 return this.request(options, callback);
491 }
492 /**
493 *
494 * @param {Object} options
495 * @param {Function} [callback]
496 * @returns {(Function|Promise)}
497 */
498
499 }, {
500 key: "getProfile",
501 value: function getProfile(options, callback) {
502 options = Object.assign({}, options, {
503 cmd: "get_pbn_info"
504 });
505 return this.request(options, callback);
506 }
507 /**
508 *
509 * @param {Function} [callback]
510 * @returns {(Function|Promise)}
511 */
512
513 }, {
514 key: "tagList",
515 value: function tagList(callback) {
516 var options = {
517 cmd: "get_pbn_list"
518 };
519 return this.request(options, callback);
520 }
521 /**
522 *
523 * @param {Object} options
524 * @param {Function} [callback]
525 * @returns {(Function|Promise)}
526 */
527
528 }, {
529 key: "updateTagProfile",
530 value: function updateTagProfile(options, callback) {
531 options = Object.assign({}, options, {
532 cmd: "update_pbn_tag"
533 });
534 return this.request(options, callback);
535 }
536 /**
537 *
538 * @param {Object} options
539 * @param {Function} [callback]
540 * @returns {(Function|Promise)}
541 */
542
543 }, {
544 key: "claimTag",
545 value: function claimTag(options, callback) {
546 options = Object.assign({}, options, {
547 cmd: "claim_pbn_tag"
548 });
549 return this.request(options, callback);
550 }
551 }]);
552
553 return Coinpayments;
554}();
555
556var _default = Coinpayments;
557exports["default"] = _default;
\No newline at end of file