From: Chris Duncan Date: Wed, 23 Jul 2025 20:34:37 +0000 (-0700) Subject: Add destroy method to mnemonic class. X-Git-Tag: v0.10.5~55^2~5 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=7a72aafd2376a6c1558cce99e19c26b6fd4f42fb;p=libnemo.git Add destroy method to mnemonic class. --- diff --git a/src/lib/bip39-mnemonic.ts b/src/lib/bip39-mnemonic.ts index 668df57..8172081 100644 --- a/src/lib/bip39-mnemonic.ts +++ b/src/lib/bip39-mnemonic.ts @@ -12,8 +12,8 @@ import { Key } from '#types' */ export class Bip39Mnemonic { static #isInternal: boolean = false - #bip44Seed: Uint8Array | null = null - #blake2bSeed: Uint8Array | null = null + #bip44Seed?: Uint8Array + #blake2bSeed?: Uint8Array #phrase: string = '' get phrase (): string { return this.#phrase.normalize('NFKD') } @@ -34,9 +34,9 @@ export class Bip39Mnemonic { * @returns {string} Mnemonic phrase validated using the BIP-39 wordlist */ static async fromPhrase (phrase: string): Promise { - Bip39Mnemonic.#isInternal = true + this.#isInternal = true const self = new this() - const isValid = await Bip39Mnemonic.validate(phrase) + const isValid = await this.validate(phrase) if (isValid) { self.#phrase = phrase.normalize('NFKD') return self @@ -66,7 +66,7 @@ export class Bip39Mnemonic { concatenation = concatenation.substring(11) } const sentence = words.join(' ') - return Bip39Mnemonic.fromPhrase(sentence) + return this.fromPhrase(sentence) } /** @@ -85,6 +85,18 @@ export class Bip39Mnemonic { return checksum } + /** + * Erases seed bytes and releases variable references to allow garbage + * collection. + */ + destroy () { + if (this.#bip44Seed != null) bytes.erase(this.#bip44Seed) + if (this.#blake2bSeed != null) bytes.erase(this.#blake2bSeed) + this.#bip44Seed = undefined + this.#blake2bSeed = undefined + this.#phrase = '' + } + /** * Validates a mnemonic phrase. *