@pgchain/blockchain-libs
Version:
PGWallet Blockchain Libs
54 lines • 2.42 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 (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.revealEntropy = exports.mnemonicToRevealableSeed = void 0;
const crypto = __importStar(require("crypto"));
const bip39 = __importStar(require("bip39"));
const exceptions_1 = require("../basic/exceptions");
const precondtion_1 = require("../basic/precondtion");
function mnemonicToRevealableSeed(mnemonic, passphrase) {
try {
const entropyHexStr = bip39.mnemonicToEntropy(mnemonic, bip39.wordlists.english);
const entropyLength = entropyHexStr.length / 2;
const seed = bip39.mnemonicToSeedSync(mnemonic, passphrase);
return {
entropyWithLangPrefixed: Buffer.concat([
Buffer.from([1]),
Buffer.from([entropyLength]),
Buffer.from(entropyHexStr, 'hex'),
crypto.randomBytes(32 - entropyLength), // Always pad entropy to 32 bytes.
]),
seed: seed,
};
}
catch {
throw new exceptions_1.InvalidMnemonic();
}
}
exports.mnemonicToRevealableSeed = mnemonicToRevealableSeed;
function revealEntropy(entropyWithLangPrefixed) {
const langCode = entropyWithLangPrefixed[0];
const entropyLength = entropyWithLangPrefixed[1];
(0, precondtion_1.check)(langCode == 1 && [16, 20, 24, 28, 32].includes(entropyLength), 'invalid entropy');
return bip39.entropyToMnemonic(entropyWithLangPrefixed.slice(2, 2 + entropyLength), bip39.wordlists.english);
}
exports.revealEntropy = revealEntropy;
//# sourceMappingURL=bip39.js.map