]> zoso.dev Git - libnemo.git/commitdiff
Throw if Ledger account derivation fails so that null result can be removed. Derive...
authorChris Duncan <chris@zoso.dev>
Sun, 10 Nov 2024 22:12:12 +0000 (14:12 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 10 Nov 2024 22:12:12 +0000 (14:12 -0800)
src/lib/wallet.ts

index 08ada18e321008014177e877af0c0e74344e3b90..32242cb4539a6cd151bfd2124a772b26a887351b 100644 (file)
@@ -45,7 +45,7 @@ abstract class Wallet {
                return ''\r
        }\r
 \r
-       abstract ckd (index: number): Promise<Account | null>\r
+       abstract ckd (index: number): Promise<Account>\r
 \r
        constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) {\r
                if (this.constructor === Wallet) {\r
@@ -72,20 +72,16 @@ abstract class Wallet {
                        from = to\r
                        to = swap\r
                }\r
-               const accountQueue = Array(to + 1)\r
+               const accountQueue = []\r
                for (let i = from; i <= to; i++) {\r
                        if (this.#accounts[i] == null) {\r
-                               accountQueue[i] = this.ckd(i)\r
+                               accountQueue.push(new Promise(resolve => {\r
+                                       this.ckd(i).then(account => this.#accounts[i] = account).then(resolve)\r
+                               }))\r
                        }\r
                }\r
                if (accountQueue.length > 0) {\r
-                       const results = await Promise.allSettled(accountQueue)\r
-                       for (let i = results.length - 1; i >= 0; i--) {\r
-                               const result = results[i] as PromiseFulfilledResult<Account>\r
-                               if (result?.value != null) {\r
-                                       this.#accounts[i] = result.value\r
-                               }\r
-                       }\r
+                       await Promise.allSettled(accountQueue)\r
                }\r
                return this.#accounts.slice(from, to + 1)\r
        }\r
@@ -614,12 +610,12 @@ export class LedgerWallet extends Wallet {
        * @param {number} index - Index of the account\r
        * @returns {Promise<Account>}\r
        */\r
-       async ckd (index: number): Promise<Account | null> {\r
+       async ckd (index: number): Promise<Account> {\r
                const { status, publicKey } = await this.ledger.account(index)\r
                if (status === 'OK' && publicKey != null) {\r
                        return await Account.fromPublicKey(publicKey, index)\r
                }\r
-               return null\r
+               throw new Error(`Error getting Ledger account: ${status}`)\r
        }\r
 \r
        /**\r