From 6e493f5ee98f86aaca5cbf101f9071c6447dc862 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 10 Nov 2024 14:12:12 -0800 Subject: [PATCH] Throw if Ledger account derivation fails so that null result can be removed. Derive accounts in parallel with Promise.all. --- src/lib/wallet.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 08ada18..32242cb 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -45,7 +45,7 @@ abstract class Wallet { return '' } - abstract ckd (index: number): Promise + abstract ckd (index: number): Promise constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) { if (this.constructor === Wallet) { @@ -72,20 +72,16 @@ abstract class Wallet { from = to to = swap } - const accountQueue = Array(to + 1) + const accountQueue = [] for (let i = from; i <= to; i++) { if (this.#accounts[i] == null) { - accountQueue[i] = this.ckd(i) + accountQueue.push(new Promise(resolve => { + this.ckd(i).then(account => this.#accounts[i] = account).then(resolve) + })) } } if (accountQueue.length > 0) { - const results = await Promise.allSettled(accountQueue) - for (let i = results.length - 1; i >= 0; i--) { - const result = results[i] as PromiseFulfilledResult - if (result?.value != null) { - this.#accounts[i] = result.value - } - } + await Promise.allSettled(accountQueue) } return this.#accounts.slice(from, to + 1) } @@ -614,12 +610,12 @@ export class LedgerWallet extends Wallet { * @param {number} index - Index of the account * @returns {Promise} */ - async ckd (index: number): Promise { + async ckd (index: number): Promise { const { status, publicKey } = await this.ledger.account(index) if (status === 'OK' && publicKey != null) { return await Account.fromPublicKey(publicKey, index) } - return null + throw new Error(`Error getting Ledger account: ${status}`) } /** -- 2.34.1