return checksum\r
}\r
\r
+ /**\r
+ * Imports and validates an existing mnemonic phrase.\r
+ *\r
+ * The phrase must be valid according to the BIP-39 specification. Typically\r
+ * wallets use the maximum of 24 words.\r
+ *\r
+ * @param {string} phrase - String of 12, 15, 18, 21, or 24 words\r
+ * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist\r
+ */\r
+ static async fromPhrase (phrase: string): Promise<Bip39Mnemonic> {\r
+ this.#isInternal = true\r
+ const self = new this()\r
+ const isValid = await this.validate(phrase)\r
+ if (isValid) {\r
+ self.#phrase = phrase.normalize('NFKD').split(' ')\r
+ return self\r
+ } else {\r
+ throw new Error('Invalid mnemonic phrase.')\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Derives a mnemonic phrase from source of entropy or seed.\r
+ *\r
+ * The entropy must be between 32-64 characters to stay within the defined\r
+ * limit of 128-256 bits. Typically wallets use the maximum entropy allowed.\r
+ *\r
+ * @param {string} entropy - Hexadecimal string\r
+ * @returns {string} Mnemonic phrase created using the BIP-39 wordlist\r
+ */\r
+ static async fromEntropy (entropy: string | Uint8Array<ArrayBuffer>): Promise<Bip39Mnemonic> {\r
+ if (typeof entropy === 'string') entropy = hex.toBytes(entropy)\r
+ if (![16, 20, 24, 28, 32].includes(entropy.byteLength)) {\r
+ throw new RangeError('Invalid entropy byte length for BIP-39')\r
+ }\r
+ const phraseLength = 0.75 * entropy.byteLength\r
+ const checksum = await this.#checksum(entropy)\r
+\r
+ let e = 0n\r
+ for (let i = 0; i < entropy.byteLength; i++) {\r
+ e = e << 8n | BigInt(entropy[i])\r
+ }\r
+\r
+ let concatenation = (e << BigInt(entropy.byteLength) / 4n) | checksum\r
+ const words: string[] = []\r
+ for (let i = 0; i < phraseLength; i++) {\r
+ const wordBits = concatenation & 2047n\r
+ const wordIndex = Number(wordBits)\r
+ words.unshift(Bip39Words[wordIndex])\r
+ concatenation >>= 11n\r
+ }\r
+ const sentence = words.join(' ')\r
+ return this.fromPhrase(sentence)\r
+ }\r
+\r
/**\r
* Validates a mnemonic phrase.\r
*\r
Bip39Mnemonic.#isInternal = false\r
}\r
\r
- /**\r
- * Imports and validates an existing mnemonic phrase.\r
- *\r
- * The phrase must be valid according to the BIP-39 specification. Typically\r
- * wallets use the maximum of 24 words.\r
- *\r
- * @param {string} phrase - String of 12, 15, 18, 21, or 24 words\r
- * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist\r
- */\r
- static async fromPhrase (phrase: string): Promise<Bip39Mnemonic> {\r
- this.#isInternal = true\r
- const self = new this()\r
- const isValid = await this.validate(phrase)\r
- if (isValid) {\r
- self.#phrase = phrase.normalize('NFKD').split(' ')\r
- return self\r
- } else {\r
- throw new Error('Invalid mnemonic phrase.')\r
- }\r
- }\r
-\r
- /**\r
- * Derives a mnemonic phrase from source of entropy or seed.\r
- *\r
- * The entropy must be between 32-64 characters to stay within the defined\r
- * limit of 128-256 bits. Typically wallets use the maximum entropy allowed.\r
- *\r
- * @param {string} entropy - Hexadecimal string\r
- * @returns {string} Mnemonic phrase created using the BIP-39 wordlist\r
- */\r
- static async fromEntropy (entropy: string | Uint8Array<ArrayBuffer>): Promise<Bip39Mnemonic> {\r
- if (typeof entropy === 'string') entropy = hex.toBytes(entropy)\r
- if (![16, 20, 24, 28, 32].includes(entropy.byteLength)) {\r
- throw new RangeError('Invalid entropy byte length for BIP-39')\r
- }\r
- const phraseLength = 0.75 * entropy.byteLength\r
- const checksum = await this.#checksum(entropy)\r
-\r
- let e = 0n\r
- for (let i = 0; i < entropy.byteLength; i++) {\r
- e = e << 8n | BigInt(entropy[i])\r
- }\r
-\r
- let concatenation = (e << BigInt(entropy.byteLength) / 4n) | checksum\r
- const words: string[] = []\r
- for (let i = 0; i < phraseLength; i++) {\r
- const wordBits = concatenation & 2047n\r
- const wordIndex = Number(wordBits)\r
- words.unshift(Bip39Words[wordIndex])\r
- concatenation >>= 11n\r
- }\r
- const sentence = words.join(' ')\r
- return this.fromPhrase(sentence)\r
- }\r
-\r
/**\r
* Erases seed bytes and releases variable references to allow garbage\r
* collection.\r