]> git.codecow.com Git - libnemo.git/commitdiff
Add destroy method to mnemonic class.
authorChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 20:34:37 +0000 (13:34 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 23 Jul 2025 20:34:37 +0000 (13:34 -0700)
src/lib/bip39-mnemonic.ts

index 668df57e665272d533ccc876e0b1a295e26a73a8..81720811eb55a9d63489196cd9a3a98d9a54c53b 100644 (file)
@@ -12,8 +12,8 @@ import { Key } from '#types'
 */\r
 export class Bip39Mnemonic {\r
        static #isInternal: boolean = false\r
-       #bip44Seed: Uint8Array<ArrayBuffer> | null = null\r
-       #blake2bSeed: Uint8Array<ArrayBuffer> | null = null\r
+       #bip44Seed?: Uint8Array<ArrayBuffer>\r
+       #blake2bSeed?: Uint8Array<ArrayBuffer>\r
        #phrase: string = ''\r
        get phrase (): string { return this.#phrase.normalize('NFKD') }\r
 \r
@@ -34,9 +34,9 @@ export class Bip39Mnemonic {
        * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist\r
        */\r
        static async fromPhrase (phrase: string): Promise<Bip39Mnemonic> {\r
-               Bip39Mnemonic.#isInternal = true\r
+               this.#isInternal = true\r
                const self = new this()\r
-               const isValid = await Bip39Mnemonic.validate(phrase)\r
+               const isValid = await this.validate(phrase)\r
                if (isValid) {\r
                        self.#phrase = phrase.normalize('NFKD')\r
                        return self\r
@@ -66,7 +66,7 @@ export class Bip39Mnemonic {
                        concatenation = concatenation.substring(11)\r
                }\r
                const sentence = words.join(' ')\r
-               return Bip39Mnemonic.fromPhrase(sentence)\r
+               return this.fromPhrase(sentence)\r
        }\r
 \r
        /**\r
@@ -85,6 +85,18 @@ export class Bip39Mnemonic {
                return checksum\r
        }\r
 \r
+       /**\r
+       * Erases seed bytes and releases variable references to allow garbage\r
+       * collection.\r
+       */\r
+       destroy () {\r
+               if (this.#bip44Seed != null) bytes.erase(this.#bip44Seed)\r
+               if (this.#blake2bSeed != null) bytes.erase(this.#blake2bSeed)\r
+               this.#bip44Seed = undefined\r
+               this.#blake2bSeed = undefined\r
+               this.#phrase = ''\r
+       }\r
+\r
        /**\r
        * Validates a mnemonic phrase.\r
        *\r