]> git.codecow.com Git - libnemo.git/commitdiff
Remove redundant success variable. Always zero out password bytes regardless of lock...
authorChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 21:11:20 +0000 (14:11 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 21:11:20 +0000 (14:11 -0700)
src/lib/account.ts
src/lib/wallets/wallet.ts

index 01bd615bfd9d45d8c760b6d5a69dec98c5eae0db..f74c95059bbf882a7312c173ad9703e05d00283f 100644 (file)
@@ -151,6 +151,8 @@ export class Account {
                } catch (err) {\r
                        console.error(`Failed to lock account ${this.address}`, err)\r
                        return false\r
+               } finally {\r
+                       password.fill(0)\r
                }\r
                this.#prv = null\r
                return true\r
@@ -172,6 +174,8 @@ export class Account {
                } catch (err) {\r
                        console.error(`Failed to unlock account ${this.address}`, err)\r
                        return false\r
+               } finally {\r
+                       password.fill(0)\r
                }\r
                return true\r
        }\r
index 474a8bab11957ff6dffa2c812f291abb4cc71c34..c69bd9f9835cf08a822cf18dcbd0d639b219a81d 100644 (file)
@@ -232,7 +232,6 @@ export abstract class Wallet {
                if (password == null || password.constructor.name !== 'Uint8Array') {\r
                        throw new Error('Failed to unlock wallet')\r
                }\r
-               let success = true\r
                try {\r
                        const data: { id: string, mnemonic: string | null, seed: string | null } = {\r
                                id: this.id,\r
@@ -245,23 +244,25 @@ export abstract class Wallet {
                        if (typeof this.#seed === 'string') {\r
                                data.seed = this.#seed\r
                        }\r
-                       success &&= await this.#poolSafe.assign({\r
+                       const res = await this.#poolSafe.assign({\r
                                method: 'put',\r
                                name: this.id,\r
                                password,\r
                                data\r
                        })\r
+                       const success = res[0].result\r
+                       if (!success) {\r
+                               throw null\r
+                       }\r
                        const promises = []\r
                        for (const account of this.#accounts) {\r
                                promises.push(account.lock(password))\r
                        }\r
                        await Promise.all(promises)\r
-                       password.fill(0)\r
-                       if (!success) {\r
-                               throw null\r
-                       }\r
                } catch (err) {\r
                        throw new Error('Failed to lock wallet')\r
+               } finally {\r
+                       password.fill(0)\r
                }\r
                this.#locked = true\r
                this.#mnemonic = null\r
@@ -283,31 +284,31 @@ export abstract class Wallet {
                        throw new Error('Failed to unlock wallet')\r
                }\r
                try {\r
-                       const data = await this.#poolSafe.assign({\r
+                       const res = await this.#poolSafe.assign({\r
                                method: 'get',\r
                                name: this.id,\r
                                password\r
                        })\r
-                       console.log(data[0].result)\r
-                       const { id, mnemonic, seed } = data[0].result\r
+                       const { id, mnemonic, seed } = res[0].result\r
                        if (id !== this.id) {\r
                                throw null\r
                        }\r
-                       const promises = []\r
-                       for (const account of this.#accounts) {\r
-                               promises.push(account.unlock(password))\r
-                       }\r
-                       await Promise.all(promises)\r
-                       password.fill(0)\r
                        if (mnemonic != null) {\r
                                this.#mnemonic = await Bip39Mnemonic.fromPhrase(mnemonic)\r
                        }\r
                        if (seed != null) {\r
                                this.#seed = seed\r
                        }\r
+                       const promises = []\r
+                       for (const account of this.#accounts) {\r
+                               promises.push(account.unlock(password))\r
+                       }\r
+                       await Promise.all(promises)\r
                        this.#locked = false\r
                } catch (err) {\r
                        throw new Error('Failed to unlock wallet')\r
+               } finally {\r
+                       password.fill(0)\r
                }\r
                return true\r
        }\r