xud
Version:
Exchange Union Daemon
146 lines • 6.96 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callback = exports.loadXudInitClient = exports.loadXudClient = void 0;
const fs_1 = __importDefault(require("fs"));
const grpc_1 = __importStar(require("grpc"));
const path_1 = __importDefault(require("path"));
const Config_1 = __importDefault(require("../Config"));
const xudrpc_grpc_pb_1 = require("../proto/xudrpc_grpc_pb");
/**
* Attempts to load the xud configuration file to dynamically determine the
* port and interface that xud is listening for rpc calls on as well as the
* tls cert path, if arguments specifying these parameters were not provided.
* @param argv the command line arguments
*/
const loadXudConfig = (argv) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const config = new Config_1.default();
try {
yield config.load({
xudir: argv.xudir,
rpc: {
port: argv.rpcport,
host: argv.rpchost,
},
});
}
catch (err) {
// if we can't load the config file, we should alert the user but continue
// on to attempt the call with the commands args or default config values
console.error(err);
}
// for any args that were not set, we update them to the values we
// determined by loading the config
argv.xudir = (_a = argv.xudir) !== null && _a !== void 0 ? _a : config.xudir;
argv.rpcport = (_b = argv.rpcport) !== null && _b !== void 0 ? _b : config.rpc.port;
if (argv.rpchost === undefined) {
argv.rpchost = (config.rpc.host === '0.0.0.0' || config.rpc.host === '::') ?
'localhost' : // if xud is listening on any address, try reaching it with localhost
config.rpc.host;
}
});
const getTlsCert = (certPath) => {
try {
return fs_1.default.readFileSync(certPath);
}
catch (err) {
if (err.code === 'ENOENT') {
throw `tls cert could not be found at ${certPath}, it may take several seconds to be created on xud's first run`;
}
throw err;
}
};
/**
* A generic function to instantiate an XU client.
* @param argv the command line arguments
*/
exports.loadXudClient = (argv) => __awaiter(void 0, void 0, void 0, function* () {
yield loadXudConfig(argv);
const certPath = argv.tlscertpath || path_1.default.join(argv.xudir, 'tls.cert');
const cert = getTlsCert(certPath);
const credentials = grpc_1.default.credentials.createSsl(cert);
return new xudrpc_grpc_pb_1.XudClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
});
exports.loadXudInitClient = (argv) => __awaiter(void 0, void 0, void 0, function* () {
yield loadXudConfig(argv);
const certPath = argv.tlscertpath || path_1.default.join(argv.xudir, 'tls.cert');
const cert = getTlsCert(certPath);
const credentials = grpc_1.default.credentials.createSsl(cert);
return new xudrpc_grpc_pb_1.XudInitClient(`${argv.rpchost}:${argv.rpcport}`, credentials);
});
exports.callback = (argv, formatOutput, displayJson) => {
return (error, response) => {
if (error) {
process.exitCode = 1;
if (error.code === grpc_1.status.UNAVAILABLE && error.message.includes('xud is starting')) {
console.error('xud is starting... try again in a few seconds');
}
else if (error.details === 'failed to connect to all addresses') {
console.error(`could not connect to xud at ${argv.rpchost}:${argv.rpcport}, is xud running?`);
}
else if (error.code === grpc_1.status.UNIMPLEMENTED && error.message.includes('xud is locked')) {
console.error("xud is locked, run 'xucli unlock', 'xucli create', or 'xucli restore' then try again");
}
else if (error.code === grpc_1.status.UNIMPLEMENTED && error.message.includes('xud node cannot be created because it already exists')) {
console.error("an xud node already exists, try unlocking it with 'xucli unlock'");
}
else if (error.code === grpc_1.status.UNIMPLEMENTED && error.message.includes('xud node cannot be unlocked because it does not exist')) {
console.error("no xud node exists to unlock, try creating one with 'xucli create' or 'xucli restore'");
}
else if (error.code === grpc_1.status.UNIMPLEMENTED && error.message.includes('xud init service is disabled')) {
console.error("xud is running and unlocked, try checking its status with 'xucli getinfo'");
}
else {
console.error(`${error.name}: ${error.message}`);
}
}
else {
const responseObj = response.toObject();
if (argv.json || !formatOutput) {
if (Object.keys(responseObj).length === 0) {
console.log('success');
}
else {
displayJson
? displayJson(responseObj, argv)
: console.log(JSON.stringify(responseObj, undefined, 2));
}
}
else {
formatOutput(responseObj, argv);
}
}
};
};
//# sourceMappingURL=command.js.map