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

index c03efcc14807026597d919278b4308b69dcaff4d..8d72264ecc24eb82c2818f9c50223f9fcc01ff0a 100644 (file)
@@ -25,6 +25,61 @@ export class Bip39Mnemonic {
                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
@@ -86,61 +141,6 @@ export class Bip39Mnemonic {
                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