]> git.codecow.com Git - libnemo.git/commitdiff
Improve error handling while constructing wallets and terminate workers on failure.
authorChris Duncan <chris@zoso.dev>
Fri, 4 Jul 2025 08:46:52 +0000 (01:46 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 4 Jul 2025 08:46:52 +0000 (01:46 -0700)
src/lib/wallets/bip44-wallet.ts
src/lib/wallets/blake2b-wallet.ts

index 8d72ca915285df5f748fd46819e549a10517091e..20b6dbdef2b68c1028ac370a6bb1b56186616999 100644 (file)
@@ -75,7 +75,7 @@ export class Bip44Wallet extends Wallet {
                        const e = await Entropy.create()\r
                        return await Bip44Wallet.fromEntropy(passkey as string, e.hex, salt)\r
                } catch (err) {\r
-                       throw new Error(`Error creating new Bip44Wallet: ${err}`)\r
+                       throw new Error('Error creating new Bip44Wallet', { cause: err })\r
                }\r
        }\r
 \r
@@ -100,18 +100,24 @@ export class Bip44Wallet extends Wallet {
        */\r
        static async fromEntropy (key: Uint8Array, entropy: string, salt?: string): Promise<Bip44Wallet>\r
        static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise<Bip44Wallet> {\r
+               let wallet: Bip44Wallet\r
                try {\r
                        const id = await Entropy.create(16)\r
                        const e = await Entropy.import(entropy)\r
                        const m = await Bip39Mnemonic.fromEntropy(e.hex)\r
                        const s = await m.toBip39Seed(salt)\r
                        Bip44Wallet.#isInternal = true\r
-                       const wallet = new this(id, s, m)\r
+                       wallet = new this(id, s, m)\r
+               } catch (err) {\r
+                       throw new Error('Error importing Bip44Wallet from entropy', { cause: err })\r
+               }\r
+               try {\r
                        await wallet.lock(passkey)\r
-                       return wallet\r
                } catch (err) {\r
-                       throw new Error(`Error importing Bip44Wallet from entropy: ${err}`)\r
+                       await wallet.destroy()\r
+                       throw new Error('Error locking Bip44Wallet while importing from entropy', { cause: err })\r
                }\r
+               return wallet\r
        }\r
 \r
        /**\r
@@ -133,17 +139,23 @@ export class Bip44Wallet extends Wallet {
        */\r
        static async fromMnemonic (key: Uint8Array, mnemonic: string, salt?: string): Promise<Bip44Wallet>\r
        static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise<Bip44Wallet> {\r
+               let wallet: Bip44Wallet\r
                try {\r
                        const id = await Entropy.create(16)\r
                        const m = await Bip39Mnemonic.fromPhrase(mnemonic)\r
                        const s = await m.toBip39Seed(salt)\r
                        Bip44Wallet.#isInternal = true\r
-                       const wallet = new this(id, s, m)\r
+                       wallet = new this(id, s, m)\r
+               } catch (err) {\r
+                       throw new Error('Error importing Bip44Wallet from mnemonic', { cause: err })\r
+               }\r
+               try {\r
                        await wallet.lock(passkey)\r
-                       return wallet\r
                } catch (err) {\r
-                       throw new Error(`Error importing Bip44Wallet from mnemonic: ${err}`)\r
+                       await wallet.destroy()\r
+                       throw new Error('Error locking Bip44Wallet while importing from mnemonic', { cause: err })\r
                }\r
+               return wallet\r
        }\r
 \r
        /**\r
@@ -176,7 +188,12 @@ export class Bip44Wallet extends Wallet {
                const id = await Entropy.create(16)\r
                Bip44Wallet.#isInternal = true\r
                const wallet = new this(id, seed)\r
-               await wallet.lock(passkey)\r
+               try {\r
+                       await wallet.lock(passkey)\r
+               } catch (err) {\r
+                       await wallet.destroy()\r
+                       throw new Error('Error locking Bip44Wallet while importing from seed', { cause: err })\r
+               }\r
                return wallet\r
        }\r
 \r
index 474ce3d22fa2e05786c66b398a210ab6f4e6fcdf..880bd32245d1a8c717f16e30dc5cd69b316d0cfc 100644 (file)
@@ -56,7 +56,7 @@ export class Blake2bWallet extends Wallet {
                        const seed = await Entropy.create()\r
                        return await Blake2bWallet.fromSeed(passkey as string, seed.hex)\r
                } catch (err) {\r
-                       throw new Error(`Error creating new Blake2bWallet: ${err}`)\r
+                       throw new Error('Error creating new Blake2bWallet', { cause: err })\r
                }\r
        }\r
 \r
@@ -90,7 +90,12 @@ export class Blake2bWallet extends Wallet {
                const m = await Bip39Mnemonic.fromEntropy(seed)\r
                Blake2bWallet.#isInternal = true\r
                const wallet = new this(id, s, m)\r
-               await wallet.lock(passkey)\r
+               try {\r
+                       await wallet.lock(passkey)\r
+               } catch (err) {\r
+                       await wallet.destroy()\r
+                       throw new Error('Error locking Blake2bWallet while importing from seed', { cause: err })\r
+               }\r
                return wallet\r
        }\r
 \r
@@ -111,17 +116,23 @@ export class Blake2bWallet extends Wallet {
        */\r
        static async fromMnemonic (key: Uint8Array, mnemonic: string): Promise<Blake2bWallet>\r
        static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise<Blake2bWallet> {\r
+               let wallet: Blake2bWallet\r
                try {\r
                        const id = await Entropy.create(16)\r
                        const m = await Bip39Mnemonic.fromPhrase(mnemonic)\r
                        const s = await m.toBlake2bSeed()\r
                        Blake2bWallet.#isInternal = true\r
-                       const wallet = new this(id, s, m)\r
+                       wallet = new this(id, s, m)\r
+               } catch (err) {\r
+                       throw new Error('Error importing Blake2bWallet from mnemonic', { cause: err })\r
+               }\r
+               try {\r
                        await wallet.lock(passkey)\r
-                       return wallet\r
                } catch (err) {\r
-                       throw new Error(`Error importing Blake2bWallet from mnemonic: ${err}`)\r
+                       await wallet.destroy()\r
+                       throw new Error('Error locking Blake2bWallet while importing from mnemonic', { cause: err })\r
                }\r
+               return wallet\r
        }\r
 \r
        /**\r