From 5ddab12972e4af99330bec843bb941a8de1ea5e4 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 30 Jul 2025 08:27:56 -0700 Subject: [PATCH] Reorder static methods. --- src/lib/bip39-mnemonic.ts | 110 +++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index c03efcc..8d72264 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -25,6 +25,61 @@ export class Bip39Mnemonic { return checksum } + /** + * Imports and validates an existing mnemonic phrase. + * + * The phrase must be valid according to the BIP-39 specification. Typically + * wallets use the maximum of 24 words. + * + * @param {string} phrase - String of 12, 15, 18, 21, or 24 words + * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist + */ + static async fromPhrase (phrase: string): Promise { + this.#isInternal = true + const self = new this() + const isValid = await this.validate(phrase) + if (isValid) { + self.#phrase = phrase.normalize('NFKD').split(' ') + return self + } else { + throw new Error('Invalid mnemonic phrase.') + } + } + + /** + * Derives a mnemonic phrase from source of entropy or seed. + * + * The entropy must be between 32-64 characters to stay within the defined + * limit of 128-256 bits. Typically wallets use the maximum entropy allowed. + * + * @param {string} entropy - Hexadecimal string + * @returns {string} Mnemonic phrase created using the BIP-39 wordlist + */ + static async fromEntropy (entropy: string | Uint8Array): Promise { + if (typeof entropy === 'string') entropy = hex.toBytes(entropy) + if (![16, 20, 24, 28, 32].includes(entropy.byteLength)) { + throw new RangeError('Invalid entropy byte length for BIP-39') + } + const phraseLength = 0.75 * entropy.byteLength + const checksum = await this.#checksum(entropy) + + let e = 0n + for (let i = 0; i < entropy.byteLength; i++) { + e = e << 8n | BigInt(entropy[i]) + } + + let concatenation = (e << BigInt(entropy.byteLength) / 4n) | checksum + const words: string[] = [] + for (let i = 0; i < phraseLength; i++) { + const wordBits = concatenation & 2047n + const wordIndex = Number(wordBits) + words.unshift(Bip39Words[wordIndex]) + concatenation >>= 11n + } + const sentence = words.join(' ') + return this.fromPhrase(sentence) + } + /** * Validates a mnemonic phrase. * @@ -86,61 +141,6 @@ export class Bip39Mnemonic { Bip39Mnemonic.#isInternal = false } - /** - * Imports and validates an existing mnemonic phrase. - * - * The phrase must be valid according to the BIP-39 specification. Typically - * wallets use the maximum of 24 words. - * - * @param {string} phrase - String of 12, 15, 18, 21, or 24 words - * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist - */ - static async fromPhrase (phrase: string): Promise { - this.#isInternal = true - const self = new this() - const isValid = await this.validate(phrase) - if (isValid) { - self.#phrase = phrase.normalize('NFKD').split(' ') - return self - } else { - throw new Error('Invalid mnemonic phrase.') - } - } - - /** - * Derives a mnemonic phrase from source of entropy or seed. - * - * The entropy must be between 32-64 characters to stay within the defined - * limit of 128-256 bits. Typically wallets use the maximum entropy allowed. - * - * @param {string} entropy - Hexadecimal string - * @returns {string} Mnemonic phrase created using the BIP-39 wordlist - */ - static async fromEntropy (entropy: string | Uint8Array): Promise { - if (typeof entropy === 'string') entropy = hex.toBytes(entropy) - if (![16, 20, 24, 28, 32].includes(entropy.byteLength)) { - throw new RangeError('Invalid entropy byte length for BIP-39') - } - const phraseLength = 0.75 * entropy.byteLength - const checksum = await this.#checksum(entropy) - - let e = 0n - for (let i = 0; i < entropy.byteLength; i++) { - e = e << 8n | BigInt(entropy[i]) - } - - let concatenation = (e << BigInt(entropy.byteLength) / 4n) | checksum - const words: string[] = [] - for (let i = 0; i < phraseLength; i++) { - const wordBits = concatenation & 2047n - const wordIndex = Number(wordBits) - words.unshift(Bip39Words[wordIndex]) - concatenation >>= 11n - } - const sentence = words.join(' ') - return this.fromPhrase(sentence) - } - /** * Erases seed bytes and releases variable references to allow garbage * collection. -- 2.47.3