From: Chris Duncan Date: Wed, 30 Jul 2025 14:37:25 +0000 (-0700) Subject: Reorder methods. X-Git-Tag: v0.10.5~48^2~4 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=6c43865d7dfa3d72d9ffd4ecced6eeb39d7c8825;p=libnemo.git Reorder methods. --- diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index 3d05afd..3ca66e0 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -105,56 +105,6 @@ export class Bip39Mnemonic { this.#phrase = undefined } - /** - * Validates a mnemonic phrase. - * - * @param {string} mnemonic - Mnemonic phrase to validate - * @returns {boolean} True if the mnemonic phrase is valid - */ - static async validate (mnemonic: string): Promise { - const words = mnemonic.normalize('NFKD').split(' ') - if (words.length % 3 !== 0) { - return false - } - - let bits = 0n, bitLength = 0n - for (const word of words) { - const wordIndex = Bip39Words.indexOf(word) - if (wordIndex === -1) { - return false - } - bits = (bits << 11n) | BigInt(Bip39Words.indexOf(word)) - bitLength += 11n - } - if (Number(bitLength) % 33 !== 0) { - return false - } - - const checksumLength = bitLength / 33n - const entropyLength = Number(bitLength - checksumLength) - const entropyBits = bits >> checksumLength - const checksumBits = bits & ((1n << checksumLength) - 1n) - - if (entropyBits == null - || entropyBits < 0n - || entropyBits > (1n << 256n) - 1n - || entropyLength < 128 - || entropyLength > 256 - || entropyLength % 32 !== 0 - ) { - return false - } - - const entropyHex = entropyBits.toString(16).padStart(entropyLength / 4, '0') - const entropy = await Entropy.import(entropyHex) - const expectedChecksum = await this.checksum(entropy.bytes) - if (expectedChecksum !== checksumBits) { - return false - } - - return true - } - async toBip39Seed (passphrase: string): Promise> /** * Converts the mnemonic phrase to a BIP-39 seed. @@ -234,5 +184,55 @@ export class Bip39Mnemonic { ? bytes.toHex(this.#blake2bSeed) : this.#blake2bSeed } + + /** + * Validates a mnemonic phrase. + * + * @param {string} mnemonic - Mnemonic phrase to validate + * @returns {boolean} True if the mnemonic phrase is valid + */ + static async validate (mnemonic: string): Promise { + const words = mnemonic.normalize('NFKD').split(' ') + if (words.length % 3 !== 0) { + return false + } + + let bits = 0n, bitLength = 0n + for (const word of words) { + const wordIndex = Bip39Words.indexOf(word) + if (wordIndex === -1) { + return false + } + bits = (bits << 11n) | BigInt(Bip39Words.indexOf(word)) + bitLength += 11n + } + if (Number(bitLength) % 33 !== 0) { + return false + } + + const checksumLength = bitLength / 33n + const entropyLength = Number(bitLength - checksumLength) + const entropyBits = bits >> checksumLength + const checksumBits = bits & ((1n << checksumLength) - 1n) + + if (entropyBits == null + || entropyBits < 0n + || entropyBits > (1n << 256n) - 1n + || entropyLength < 128 + || entropyLength > 256 + || entropyLength % 32 !== 0 + ) { + return false + } + + const entropyHex = entropyBits.toString(16).padStart(entropyLength / 4, '0') + const entropy = await Entropy.import(entropyHex) + const expectedChecksum = await this.checksum(entropy.bytes) + if (expectedChecksum !== checksumBits) { + return false + } + + return true + } }