UNPKG

@bpanel/bpanel-utils

Version:
168 lines (141 loc) 3.95 kB
'use strict'; var _getIterator2 = require('babel-runtime/core-js/get-iterator'); var _getIterator3 = _interopRequireDefault(_getIterator2); var _entries = require('babel-runtime/core-js/object/entries'); var _entries2 = _interopRequireDefault(_entries); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _require = require('bcoin'), networks = _require.networks; var assert = require('bsert'); var url = require('url'); // supported chains var CHAINS = ['bitcoin', 'bitcoincash']; // supported block explorers // can add more here var BLOCK_EXPLORERS = { bitcoin: { main: { 'btc.com': 'https://btc.com', // /{txhash} blocktrail: 'https://www.blocktrail.com/BTC' }, testnet: { blocktrail: 'https://www.blocktrail.com/tBTC' // /tx/{txhash} } }, bitcoincash: { main: { 'btc.com': 'https://bch.btc.com', blocktrail: 'https://www.blocktrail.com/BCC' }, testnet: { blocktrail: 'https://www.blocktrail.com/tBCC' } } }; // the hyperlink suffixes for // different types of queries var EXPLORER_SUFFIXES = { blocktrail: { transaction: '/tx/' }, 'btc.com': { transaction: '/' } }; /** * block explorer client * * currently supports: * networks: main,testnet * network: bitcoin,bitcoincash * * rendering of tx hyperlinks * */ class BlockExplorerClient { /** * Create a block explorer client * @constructor * @param options * */ constructor(options) { this._explorers = BLOCK_EXPLORERS; this._chains = CHAINS; this._suffixes = EXPLORER_SUFFIXES; this._networks = networks.types; this._chain = null; this._network = null; if (options) this.fromOptions(options); } static fromOptions(options) { return new this().fromOptions(options); } fromOptions(options) { assert((typeof options === 'undefined' ? 'undefined' : (0, _typeof3.default)(options)) === 'object', 'options must be an object'); assert(options.chain, 'must pass options.chain'); assert(options.network, 'must past options.network'); assert(this._chains.includes(options.chain), options.chain + ' must be a valid chain: ' + this._chains); this._chain = options.chain; assert(this._networks.includes(options.network), options.network + ' must be a valid network: ' + this._networks); this._network = options.network; return this; } /** * get compatible explorers */ getExplorers() { return this._explorers[this._chain][this._network]; } /** * get suffixes for explorer hyperlinks */ getSuffixes() { return this._suffixes; } /** * string interpolate the hyperlink * @param name * @param url * @param type * @returns {String} */ toLink(name, url, type) { var suffixes = this.getSuffixes(); return '' + url + suffixes[name][type]; } /** * render transaction specific * hyperlinks for each supported * block explorer * @param txhash * @returns {[]url.URL} */ getTransactionLinks(txhash) { var explorers = this.getExplorers(); var links = []; for (var _iterator = (0, _entries2.default)(explorers), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) { var _ref2; if (_isArray) { if (_i >= _iterator.length) break; _ref2 = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref2 = _i.value; } var _ref = _ref2; var key = _ref[0]; var val = _ref[1]; var link = this.toLink(key, val, 'transaction'); var u = url.parse('' + link + txhash); links.push(u); } return links; } } module.exports = { BlockExplorerClient: BlockExplorerClient };