UNPKG

bitcore-wallet-service

Version:
256 lines 10.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BanxaService = void 0; const crypto = __importStar(require("crypto")); const _ = __importStar(require("lodash")); const request = __importStar(require("request")); const config_1 = __importDefault(require("../config")); const clienterror_1 = require("../lib/errors/clienterror"); const server_1 = require("../lib/server"); class BanxaService { constructor() { this.request = request; } banxaGetKeys(req) { if (!config_1.default.banxa) throw new Error('Banxa missing credentials'); let env; env = req.body.env === 'production' ? 'production' : 'sandbox'; if (req.body.context === 'web') { env += 'Web'; } delete req.body.env; delete req.body.context; const keys = { API: config_1.default.banxa[env].api, API_KEY: config_1.default.banxa[env].apiKey, SECRET_KEY: config_1.default.banxa[env].secretKey }; return keys; } getBanxaSignature(method, endpoint, apiKey, secret, body) { let signature, auth; const nonce = Date.now().toString(); switch (method) { case 'get': signature = 'GET' + '\n' + `/api${endpoint}` + '\n' + nonce; break; case 'post': const stringifiedBody = body ? JSON.stringify(_.cloneDeep(body)) : ''; signature = 'POST' + '\n' + `/api${endpoint}` + '\n' + nonce + '\n' + stringifiedBody; break; default: signature = undefined; break; } const localSignature = crypto.createHmac('sha256', secret).update(signature).digest('hex'); auth = `${apiKey}:${localSignature}:${nonce}`; return auth; } banxaGetCoins(req) { return new Promise((resolve, reject) => { const keys = this.banxaGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const SECRET_KEY = keys.SECRET_KEY; if (!(0, server_1.checkRequired)(req.body, ['orderType'])) { return reject(new clienterror_1.ClientError("Banxa's request missing arguments")); } if (!['buy', 'sell'].includes(req.body.orderType)) { return reject(new clienterror_1.ClientError("Banxa's 'orderType' property must be 'sell' or 'buy'")); } const UriPath = `/coins/${req.body.orderType}`; const URL = API + UriPath; const auth = this.getBanxaSignature('get', UriPath, API_KEY, SECRET_KEY); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }; this.request.get(URL, { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } banxaGetPaymentMethods(req) { return new Promise((resolve, reject) => { const keys = this.banxaGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const SECRET_KEY = keys.SECRET_KEY; let qs = []; if (req.body.source) qs.push('source=' + req.body.source); if (req.body.target) qs.push('target=' + req.body.target); const UriPath = `/payment-methods${qs.length > 0 ? '?' + qs.join('&') : ''}`; const URL = API + UriPath; const auth = this.getBanxaSignature('get', UriPath, API_KEY, SECRET_KEY); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }; this.request.get(URL, { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } banxaGetQuote(req) { return new Promise((resolve, reject) => { const keys = this.banxaGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const SECRET_KEY = keys.SECRET_KEY; if (!(0, server_1.checkRequired)(req.body, ['source', 'target'])) { return reject(new clienterror_1.ClientError("Banxa's request missing arguments")); } let qs = []; qs.push('source=' + req.body.source); qs.push('target=' + req.body.target); if (req.body.source_amount) qs.push('source_amount=' + req.body.source_amount); if (req.body.target_amount) qs.push('target_amount=' + req.body.target_amount); if (req.body.payment_method_id) qs.push('payment_method_id=' + req.body.payment_method_id); if (req.body.account_reference) qs.push('account_reference=' + req.body.account_reference); if (req.body.blockchain) qs.push('blockchain=' + req.body.blockchain); const UriPath = `/prices${qs.length > 0 ? '?' + qs.join('&') : ''}`; const URL = API + UriPath; const auth = this.getBanxaSignature('get', UriPath, API_KEY, SECRET_KEY); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }; this.request.get(URL, { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } banxaCreateOrder(req) { return new Promise((resolve, reject) => { const keys = this.banxaGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const SECRET_KEY = keys.SECRET_KEY; if (!(0, server_1.checkRequired)(req.body, ['account_reference', 'source', 'target', 'wallet_address', 'return_url_on_success'])) { return reject(new clienterror_1.ClientError("Banxa's request missing arguments")); } delete req.body.payment_method_id; const UriPath = '/orders'; const URL = API + UriPath; const auth = this.getBanxaSignature('post', UriPath, API_KEY, SECRET_KEY, req.body); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }; this.request.post(URL, { headers, body: req.body, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } banxaGetOrder(req) { return new Promise((resolve, reject) => { const keys = this.banxaGetKeys(req); const API = keys.API; const API_KEY = keys.API_KEY; const SECRET_KEY = keys.SECRET_KEY; if (!(0, server_1.checkRequired)(req.body, ['order_id'])) { return reject(new clienterror_1.ClientError("Banxa's request missing arguments")); } let qs = []; if (req.body.fx_currency) qs.push('fx_currency=' + req.body.fx_currency); const UriPath = `/orders/${req.body.order_id}${qs.length > 0 ? '?' + qs.join('&') : ''}`; const URL = API + UriPath; const auth = this.getBanxaSignature('get', UriPath, API_KEY, SECRET_KEY); const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }; this.request.get(URL, { headers, json: true }, (err, data) => { if (err) { return reject(err.body ? err.body : err); } else { return resolve(data.body ? data.body : data); } }); }); } } exports.BanxaService = BanxaService; //# sourceMappingURL=banxa.js.map