UNPKG

xud

Version:
93 lines 4.22 kB
"use strict"; 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.generate = exports.deriveChild = exports.decipher = exports.encipher = exports.keystore = void 0; const assert_1 = __importDefault(require("assert")); const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); const util_1 = require("util"); const seedutilPath = path_1.default.join(__dirname, '..', '..', 'seedutil', 'seedutil'); /** A promisified wrapped for the NodeJS `child_process.exec` method. */ const exec = util_1.promisify(child_process_1.exec); /** * Executes the seedutil tool to generate an ethereum keystore from the given * mnemonic and password at the specified path. * @param mnemonic the 24 seed recovery mnemonic * @param password the password to protect the keystore * @param pathVal the path in which to create the keystore directory */ function keystore(mnemonic, password, pathVal) { return __awaiter(this, void 0, void 0, function* () { const { stdout, stderr } = yield exec(`${seedutilPath} keystore -pass=${password} -path=${pathVal} ${mnemonic.join(' ')}`); if (stderr) { throw new Error(stderr); } if (!stdout.includes('Keystore created')) { throw new Error(stdout); } }); } exports.keystore = keystore; /** * Executes the seedutil tool to encipher a deciphered seed hex string into a mnemonic * @param decipheredSeedHex the deciphered seed in hex format */ function encipher(decipheredSeedHex) { return __awaiter(this, void 0, void 0, function* () { const { stdout, stderr } = yield exec(`${seedutilPath} encipher ${decipheredSeedHex}`); if (stderr) { throw new Error(stderr); } const mnemonic = stdout.trim().split(' '); assert_1.default.equal(mnemonic.length, 24, 'seedutil did not encipher mnemonic of exactly 24 words'); return mnemonic; }); } exports.encipher = encipher; function decipher(mnemonic) { return __awaiter(this, void 0, void 0, function* () { const { stdout, stderr } = yield exec(`${seedutilPath} decipher ${mnemonic.join(' ')}`); if (stderr) { throw new Error(stderr); } const decipheredSeed = stdout.trim(); return Buffer.from(decipheredSeed, 'hex'); }); } exports.decipher = decipher; function deriveChild(mnemonic, clientType) { return __awaiter(this, void 0, void 0, function* () { const { stdout, stderr } = yield exec(`${seedutilPath} derivechild -client ${clientType} ${mnemonic.join(' ')}`); if (stderr) { throw new Error(stderr); } const childMnenomic = stdout.trim().split(' '); assert_1.default.equal(childMnenomic.length, 24, 'seedutil did not derive child mnemonic of exactly 24 words'); return childMnenomic; }); } exports.deriveChild = deriveChild; function generate() { return __awaiter(this, void 0, void 0, function* () { const { stdout, stderr } = yield exec(`${seedutilPath} generate`); if (stderr) { throw new Error(stderr); } const mnemonic = stdout.trim().split(' '); assert_1.default.equal(mnemonic.length, 24, 'seedutil did not generate mnemonic of exactly 24 words'); return mnemonic; }); } exports.generate = generate; //# sourceMappingURL=seedutil.js.map