@bpanel/bpanel-utils
Version:
Utilities for bpanel
117 lines (94 loc) • 2.85 kB
JavaScript
/*!
* clients.js - client extensions of bclient to connect to bpanel app server
* Copyright (c) 2018, Bcoin Devs (MIT License).
* https://github.com/bcoin-org/bpanel-utils
*/
;
exports.__esModule = true;
exports.getClient = exports.bmultisigClient = exports.bwalletClient = exports.bpanelClient = exports.hostname = exports.walletPath = exports.nodePath = exports.ssl = exports.bpanelPort = undefined;
var _bsert = require('bsert');
var _bsert2 = _interopRequireDefault(_bsert);
var _bclient = require('bclient');
var _bmultisig = require('bmultisig');
var _client = require('./client');
var _client2 = _interopRequireDefault(_client);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _options = options(),
bpanelPort = _options.bpanelPort,
ssl = _options.ssl,
nodePath = _options.nodePath,
walletPath = _options.walletPath,
hostname = _options.hostname;
exports.bpanelPort = bpanelPort;
exports.ssl = ssl;
exports.nodePath = nodePath;
exports.walletPath = walletPath;
exports.hostname = hostname;
var nodeClient = null;
var walletClient = null;
var multisigClient = null;
var bpClient = null;
function getClient(id) {
var chain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'bitcoin';
if (!bpClient) {
bpClient = new _client2.default({
id: id,
chain: chain,
port: bpanelPort,
host: hostname,
ssl: ssl
});
}
return bpClient;
}
function bpanelClient() {
if (!nodeClient) nodeClient = new _bclient.NodeClient({
port: bpanelPort,
host: hostname,
path: nodePath,
ssl: ssl
});
return nodeClient;
}
function bwalletClient() {
if (!walletClient) walletClient = new _bclient.WalletClient({
port: bpanelPort,
path: walletPath,
host: hostname,
ssl: ssl
});
return walletClient;
}
function bmultisigClient() {
if (!multisigClient) multisigClient = new _bmultisig.Client({
port: bpanelPort,
path: walletPath,
host: hostname,
ssl: ssl
});
return multisigClient;
}
function options() {
// bpanel endpoints
var nodePath = '/bcoin';
var walletPath = '/bwallet';
// determine the port and ssl usage
var protocol = window.location.protocol;
var hostname = window.location.hostname;
var port = window.location.port;
var ssl = false;
// use https and http ports when the window doesn't render them
if (port === '') protocol === 'https:' ? port = '443' : port = '80';
if (protocol === 'https:') ssl = true;
return {
bpanelPort: parseInt(port, 10),
ssl: ssl,
hostname: hostname,
nodePath: nodePath,
walletPath: walletPath
};
}
exports.bpanelClient = bpanelClient;
exports.bwalletClient = bwalletClient;
exports.bmultisigClient = bmultisigClient;
exports.getClient = getClient;