]> git.codecow.com Git - libnemo.git/commitdiff
Fix fetching next unopened account.
authorChris Duncan <chris@zoso.dev>
Tue, 19 Aug 2025 04:55:02 +0000 (21:55 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 19 Aug 2025 04:55:02 +0000 (21:55 -0700)
src/lib/wallet/index.ts
src/lib/wallet/unopened.ts

index 3b8c45820b780ca3ba541821fe89b0b338026b2e..fc85a6f679e3ea08d9126df37671eb7676d1a927 100644 (file)
@@ -309,7 +309,7 @@ export class Wallet {
        * @returns {Promise<Account>} The lowest-indexed unopened account belonging to the wallet\r
        */\r
        async unopened (rpc: Rpc, batchSize: number = ADDRESS_GAP, from: number = 0): Promise<Account> {\r
-               return await _unopened(this, rpc, batchSize, from + batchSize)\r
+               return await _unopened(this, rpc, batchSize, from)\r
        }\r
 \r
        /**\r
index 0b31a3f927d222807010bde13eecde0571826d4a..4b0e17d77a5799348ef7496af027886cd865363d 100644 (file)
@@ -19,19 +19,23 @@ export async function _unopened (wallet: Wallet, rpc: unknown, batchSize: unknow
        if (typeof from !== 'number' || !Number.isSafeInteger(batchSize) || batchSize < 0) {
                throw new TypeError('Invalid starting account index', { cause: from })
        }
-       const accounts = await wallet.accounts(from, from + batchSize - 1)
+       const to = from + batchSize
+       const accounts = await wallet.accounts(from, to - 1)
        const addresses = []
-       for (const a in accounts) {
-               addresses.push(accounts[a].address)
+       for (const account of accounts) {
+               addresses.push(account.address)
        }
        const data = {
                "accounts": addresses
        }
        const { errors } = await rpc.call('accounts_frontiers', data)
-       for (const key of Object.keys(errors ?? {})) {
-               const value = errors[key]
-               if (value === 'Account not found') {
-                       return Account.load(key)
+       if (errors != null) {
+               for (let i = from; i < to; i++) {
+                       const account = accounts[i]
+                       const value = errors[account.address]
+                       if (value === 'Account not found') {
+                               return account
+                       }
                }
        }
        return await _unopened(wallet, rpc, batchSize, from + batchSize)