From bbce4f79fce250afbb4bcf17226eeca7e4fea649 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 4 Jul 2025 01:46:52 -0700 Subject: [PATCH] Improve error handling while constructing wallets and terminate workers on failure. --- src/lib/wallets/bip44-wallet.ts | 33 +++++++++++++++++++++++-------- src/lib/wallets/blake2b-wallet.ts | 21 +++++++++++++++----- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/lib/wallets/bip44-wallet.ts b/src/lib/wallets/bip44-wallet.ts index 8d72ca9..20b6dbd 100644 --- a/src/lib/wallets/bip44-wallet.ts +++ b/src/lib/wallets/bip44-wallet.ts @@ -75,7 +75,7 @@ export class Bip44Wallet extends Wallet { const e = await Entropy.create() return await Bip44Wallet.fromEntropy(passkey as string, e.hex, salt) } catch (err) { - throw new Error(`Error creating new Bip44Wallet: ${err}`) + throw new Error('Error creating new Bip44Wallet', { cause: err }) } } @@ -100,18 +100,24 @@ export class Bip44Wallet extends Wallet { */ static async fromEntropy (key: Uint8Array, entropy: string, salt?: string): Promise static async fromEntropy (passkey: string | Uint8Array, entropy: string, salt: string = ''): Promise { + let wallet: Bip44Wallet try { const id = await Entropy.create(16) const e = await Entropy.import(entropy) const m = await Bip39Mnemonic.fromEntropy(e.hex) const s = await m.toBip39Seed(salt) Bip44Wallet.#isInternal = true - const wallet = new this(id, s, m) + wallet = new this(id, s, m) + } catch (err) { + throw new Error('Error importing Bip44Wallet from entropy', { cause: err }) + } + try { await wallet.lock(passkey) - return wallet } catch (err) { - throw new Error(`Error importing Bip44Wallet from entropy: ${err}`) + await wallet.destroy() + throw new Error('Error locking Bip44Wallet while importing from entropy', { cause: err }) } + return wallet } /** @@ -133,17 +139,23 @@ export class Bip44Wallet extends Wallet { */ static async fromMnemonic (key: Uint8Array, mnemonic: string, salt?: string): Promise static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string, salt: string = ''): Promise { + let wallet: Bip44Wallet try { const id = await Entropy.create(16) const m = await Bip39Mnemonic.fromPhrase(mnemonic) const s = await m.toBip39Seed(salt) Bip44Wallet.#isInternal = true - const wallet = new this(id, s, m) + wallet = new this(id, s, m) + } catch (err) { + throw new Error('Error importing Bip44Wallet from mnemonic', { cause: err }) + } + try { await wallet.lock(passkey) - return wallet } catch (err) { - throw new Error(`Error importing Bip44Wallet from mnemonic: ${err}`) + await wallet.destroy() + throw new Error('Error locking Bip44Wallet while importing from mnemonic', { cause: err }) } + return wallet } /** @@ -176,7 +188,12 @@ export class Bip44Wallet extends Wallet { const id = await Entropy.create(16) Bip44Wallet.#isInternal = true const wallet = new this(id, seed) - await wallet.lock(passkey) + try { + await wallet.lock(passkey) + } catch (err) { + await wallet.destroy() + throw new Error('Error locking Bip44Wallet while importing from seed', { cause: err }) + } return wallet } diff --git a/src/lib/wallets/blake2b-wallet.ts b/src/lib/wallets/blake2b-wallet.ts index 474ce3d..880bd32 100644 --- a/src/lib/wallets/blake2b-wallet.ts +++ b/src/lib/wallets/blake2b-wallet.ts @@ -56,7 +56,7 @@ export class Blake2bWallet extends Wallet { const seed = await Entropy.create() return await Blake2bWallet.fromSeed(passkey as string, seed.hex) } catch (err) { - throw new Error(`Error creating new Blake2bWallet: ${err}`) + throw new Error('Error creating new Blake2bWallet', { cause: err }) } } @@ -90,7 +90,12 @@ export class Blake2bWallet extends Wallet { const m = await Bip39Mnemonic.fromEntropy(seed) Blake2bWallet.#isInternal = true const wallet = new this(id, s, m) - await wallet.lock(passkey) + try { + await wallet.lock(passkey) + } catch (err) { + await wallet.destroy() + throw new Error('Error locking Blake2bWallet while importing from seed', { cause: err }) + } return wallet } @@ -111,17 +116,23 @@ export class Blake2bWallet extends Wallet { */ static async fromMnemonic (key: Uint8Array, mnemonic: string): Promise static async fromMnemonic (passkey: string | Uint8Array, mnemonic: string): Promise { + let wallet: Blake2bWallet try { const id = await Entropy.create(16) const m = await Bip39Mnemonic.fromPhrase(mnemonic) const s = await m.toBlake2bSeed() Blake2bWallet.#isInternal = true - const wallet = new this(id, s, m) + wallet = new this(id, s, m) + } catch (err) { + throw new Error('Error importing Blake2bWallet from mnemonic', { cause: err }) + } + try { await wallet.lock(passkey) - return wallet } catch (err) { - throw new Error(`Error importing Blake2bWallet from mnemonic: ${err}`) + await wallet.destroy() + throw new Error('Error locking Blake2bWallet while importing from mnemonic', { cause: err }) } + return wallet } /** -- 2.47.3