UNPKG

@muirglacier/jellyfish-wallet-mnemonic

Version:

A collection of TypeScript + JavaScript tools and libraries for DeFi Blockchain developers to build decentralized finance for Bitcoin

99 lines 3.85 kB
"use strict"; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.entropyAsMnemonic = exports.mnemonicAsEntropy = exports.mnemonicToSeed = exports.generateMnemonicWords = exports.validateMnemonicWord = exports.validateMnemonicSentence = void 0; const randombytes_1 = __importDefault(require("randombytes")); const bip39 = __importStar(require("bip39")); /** * @param {string} mnemonic sentence to validate * @return {boolean} validity */ function validateMnemonicSentence(mnemonic) { if (Array.isArray(mnemonic)) { return bip39.validateMnemonic(mnemonic.join(' ')); } return bip39.validateMnemonic(mnemonic); } exports.validateMnemonicSentence = validateMnemonicSentence; /** * @param {string} word to check if exist in mnemonic english word list * @return {boolean} validity */ function validateMnemonicWord(word) { return bip39.wordlists.english.includes(word); } exports.validateMnemonicWord = validateMnemonicWord; /** * Generate a random mnemonic code of length, uses crypto.randomBytes under the hood. * Defaults to 256-bits of entropy. * https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki * * | ENT | CS | ENT+CS | MS | * +-------+----+--------+------+ * | 128 | 4 | 132 | 12 | * | 160 | 5 | 165 | 15 | * | 192 | 6 | 198 | 18 | * | 224 | 7 | 231 | 21 | * | 256 | 8 | 264 | 24 | * * @param {number} length the sentence length of the mnemonic code * @param {(number) => Buffer} [rng = randomBytes] cryptographically strong random values generator required * @return {string[]} generated mnemonic word list, (COLD STORAGE) */ function generateMnemonicWords(length = 24, rng = randombytes_1.default) { const entropy = length / 3 * 32; const sentence = bip39.generateMnemonic(entropy, rng); return sentence.split(' '); } exports.generateMnemonicWords = generateMnemonicWords; /** * @param {string[]} mnemonic words, (COLD) * @return {Buffer} HD seed, (HOT) but ideally should not be kept at rest */ function mnemonicToSeed(mnemonic) { return bip39.mnemonicToSeedSync(mnemonic.join(' ')); } exports.mnemonicToSeed = mnemonicToSeed; /** * @param {string[]} mnemonic words, (COLD) * @return {Buffer} 32 byte */ function mnemonicAsEntropy(mnemonic) { const hex = bip39.mnemonicToEntropy(mnemonic.join(' ')); return Buffer.from(hex, 'hex'); } exports.mnemonicAsEntropy = mnemonicAsEntropy; /** * @param {Buffer} entropy 32 bytes buffer * @return {string[]} mnemonic words, (COLD) */ function entropyAsMnemonic(entropy) { if (entropy.length !== 32) { throw new Error('expected entropy to be 32 byte long'); } const sentence = bip39.entropyToMnemonic(entropy); return sentence.split(' '); } exports.entropyAsMnemonic = entropyAsMnemonic; //# sourceMappingURL=mnemonic.js.map