]> git.codecow.com Git - libnemo.git/commitdiff
Reorder methods.
authorChris Duncan <chris@zoso.dev>
Wed, 30 Jul 2025 14:37:25 +0000 (07:37 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 30 Jul 2025 14:37:25 +0000 (07:37 -0700)
src/lib/bip39-mnemonic.ts

index 3d05afda04fcf74b4a5aa1507f8988c7bd12a1f2..3ca66e072460f8a08ff7ac36a37020ca74b5cbea 100644 (file)
@@ -105,56 +105,6 @@ export class Bip39Mnemonic {
                this.#phrase = undefined\r
        }\r
 \r
-       /**\r
-       * Validates a mnemonic phrase.\r
-       *\r
-       * @param {string} mnemonic - Mnemonic phrase to validate\r
-       * @returns {boolean} True if the mnemonic phrase is valid\r
-       */\r
-       static async validate (mnemonic: string): Promise<boolean> {\r
-               const words = mnemonic.normalize('NFKD').split(' ')\r
-               if (words.length % 3 !== 0) {\r
-                       return false\r
-               }\r
-\r
-               let bits = 0n, bitLength = 0n\r
-               for (const word of words) {\r
-                       const wordIndex = Bip39Words.indexOf(word)\r
-                       if (wordIndex === -1) {\r
-                               return false\r
-                       }\r
-                       bits = (bits << 11n) | BigInt(Bip39Words.indexOf(word))\r
-                       bitLength += 11n\r
-               }\r
-               if (Number(bitLength) % 33 !== 0) {\r
-                       return false\r
-               }\r
-\r
-               const checksumLength = bitLength / 33n\r
-               const entropyLength = Number(bitLength - checksumLength)\r
-               const entropyBits = bits >> checksumLength\r
-               const checksumBits = bits & ((1n << checksumLength) - 1n)\r
-\r
-               if (entropyBits == null\r
-                       || entropyBits < 0n\r
-                       || entropyBits > (1n << 256n) - 1n\r
-                       || entropyLength < 128\r
-                       || entropyLength > 256\r
-                       || entropyLength % 32 !== 0\r
-               ) {\r
-                       return false\r
-               }\r
-\r
-               const entropyHex = entropyBits.toString(16).padStart(entropyLength / 4, '0')\r
-               const entropy = await Entropy.import(entropyHex)\r
-               const expectedChecksum = await this.checksum(entropy.bytes)\r
-               if (expectedChecksum !== checksumBits) {\r
-                       return false\r
-               }\r
-\r
-               return true\r
-       }\r
-\r
        async toBip39Seed (passphrase: string): Promise<Uint8Array<ArrayBuffer>>\r
        /**\r
        * Converts the mnemonic phrase to a BIP-39 seed.\r
@@ -234,5 +184,55 @@ export class Bip39Mnemonic {
                        ? bytes.toHex(this.#blake2bSeed)\r
                        : this.#blake2bSeed\r
        }\r
+\r
+       /**\r
+       * Validates a mnemonic phrase.\r
+       *\r
+       * @param {string} mnemonic - Mnemonic phrase to validate\r
+       * @returns {boolean} True if the mnemonic phrase is valid\r
+       */\r
+       static async validate (mnemonic: string): Promise<boolean> {\r
+               const words = mnemonic.normalize('NFKD').split(' ')\r
+               if (words.length % 3 !== 0) {\r
+                       return false\r
+               }\r
+\r
+               let bits = 0n, bitLength = 0n\r
+               for (const word of words) {\r
+                       const wordIndex = Bip39Words.indexOf(word)\r
+                       if (wordIndex === -1) {\r
+                               return false\r
+                       }\r
+                       bits = (bits << 11n) | BigInt(Bip39Words.indexOf(word))\r
+                       bitLength += 11n\r
+               }\r
+               if (Number(bitLength) % 33 !== 0) {\r
+                       return false\r
+               }\r
+\r
+               const checksumLength = bitLength / 33n\r
+               const entropyLength = Number(bitLength - checksumLength)\r
+               const entropyBits = bits >> checksumLength\r
+               const checksumBits = bits & ((1n << checksumLength) - 1n)\r
+\r
+               if (entropyBits == null\r
+                       || entropyBits < 0n\r
+                       || entropyBits > (1n << 256n) - 1n\r
+                       || entropyLength < 128\r
+                       || entropyLength > 256\r
+                       || entropyLength % 32 !== 0\r
+               ) {\r
+                       return false\r
+               }\r
+\r
+               const entropyHex = entropyBits.toString(16).padStart(entropyLength / 4, '0')\r
+               const entropy = await Entropy.import(entropyHex)\r
+               const expectedChecksum = await this.checksum(entropy.bytes)\r
+               if (expectedChecksum !== checksumBits) {\r
+                       return false\r
+               }\r
+\r
+               return true\r
+       }\r
 }\r
 \r