xud
Version:
Exchange Union Daemon
151 lines • 6.36 kB
JavaScript
;
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.handler = exports.builder = exports.describe = exports.command = void 0;
const fs_1 = require("fs");
const utils_1 = require("../../utils/utils");
const path_1 = require("path");
const readline_1 = __importDefault(require("readline"));
const xudrpc_pb_1 = require("../../proto/xudrpc_pb");
const command_1 = require("../command");
const utils_2 = require("../utils");
exports.command = 'restore [backup_directory]';
exports.describe = 'restore an xud instance from seed';
exports.builder = {
backup_directory: {
description: `The directory to which backups were written,
uses the default directory if unspecified`,
type: 'string',
},
};
const exitWithError = (message) => {
console.error(message);
process.exit(1);
};
const formatOutput = (response) => {
let walletRestoredMessage = 'The following wallets were restored: ';
if (response.restoredLndsList.length) {
walletRestoredMessage += response.restoredLndsList.join(', ') + (response.restoredConnext ? ', ETH' : '');
}
else if (response.restoredConnext) {
walletRestoredMessage += 'ETH';
}
console.log(walletRestoredMessage);
};
exports.handler = (argv) => __awaiter(void 0, void 0, void 0, function* () {
const request = new xudrpc_pb_1.RestoreNodeRequest();
const readDbBackupPromises = [];
let backupDir;
if (argv.backup_directory) {
try {
const x = yield fs_1.promises.stat(argv.backup_directory);
if (!x.isDirectory()) {
exitWithError(`${argv.backup_directory} is not a directory`);
return;
}
backupDir = argv.backup_directory;
}
catch (err) {
exitWithError(`could not read from ${argv.backup_directory}`);
return;
}
}
else {
backupDir = utils_1.getDefaultBackupDir();
}
try {
yield fs_1.promises.access(backupDir);
// we must load backup files from disk before sending the restore request
const backupDirectory = yield fs_1.promises.readdir(backupDir);
backupDirectory.forEach((filename) => {
readDbBackupPromises.push(new Promise((resolve) => __awaiter(void 0, void 0, void 0, function* () {
const fileContent = yield fs_1.promises.readFile(path_1.join(backupDir, filename));
if (filename.startsWith('lnd-')) {
request.getLndBackupsMap().set(filename.substr(4), fileContent);
resolve();
return;
}
switch (filename) {
case 'xud':
request.setXudDatabase(fileContent);
break;
}
resolve();
})));
});
}
catch (err) {
if (err.code !== 'ENOENT') {
exitWithError(err.message);
return;
}
}
const rl = readline_1.default.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
});
console.log(`
You are restoring an xud node key and underlying wallets. All will be secured by
a single password provided below.
`);
rl.question('Enter your 24 word mnemonic separated by spaces: ', (mnemonicStr) => {
rl.close();
const rlQuiet = readline_1.default.createInterface({
input: process.stdin,
terminal: true,
});
const mnemonic = mnemonicStr.split(' ');
if (mnemonic.length !== 24) {
exitWithError('Mnemonic must be exactly 24 words');
return;
}
process.stdout.write('Enter a password: ');
rlQuiet.question('', (password1) => {
process.stdout.write('\nRe-enter password: ');
rlQuiet.question('', (password2) => __awaiter(void 0, void 0, void 0, function* () {
process.stdout.write('\n\n');
rlQuiet.close();
if (password1 === password2) {
if (password1.length < 8) {
exitWithError('Password must be at least 8 characters');
return;
}
request.setPassword(password1);
request.setSeedMnemonicList(mnemonic);
// we must wait for any db backup files to have been read and set on the request
yield Promise.all(readDbBackupPromises);
const certPath = argv.tlscertpath ? argv.tlscertpath : utils_2.getDefaultCertPath();
try {
yield utils_2.waitForCert(certPath);
}
catch (err) {
console.error(err);
process.exitCode = 1;
return;
}
const client = yield command_1.loadXudInitClient(argv);
// wait up to 3 seconds for rpc server to listen before call in case xud was just started
client.waitForReady(Date.now() + 3000, () => {
client.restoreNode(request, command_1.callback(argv, formatOutput));
});
}
else {
exitWithError('Passwords do not match, please try again');
}
}));
});
});
});
//# sourceMappingURL=restore.js.map