From: Chris Duncan Date: Tue, 19 Aug 2025 04:55:02 +0000 (-0700) Subject: Fix fetching next unopened account. X-Git-Tag: v0.10.5~41^2~54 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=3ccaedc9a638411495f9afcfc30169cd92b84716;p=libnemo.git Fix fetching next unopened account. --- diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index 3b8c458..fc85a6f 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -309,7 +309,7 @@ export class Wallet { * @returns {Promise} The lowest-indexed unopened account belonging to the wallet */ async unopened (rpc: Rpc, batchSize: number = ADDRESS_GAP, from: number = 0): Promise { - return await _unopened(this, rpc, batchSize, from + batchSize) + return await _unopened(this, rpc, batchSize, from) } /** diff --git a/src/lib/wallet/unopened.ts b/src/lib/wallet/unopened.ts index 0b31a3f..4b0e17d 100644 --- a/src/lib/wallet/unopened.ts +++ b/src/lib/wallet/unopened.ts @@ -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)