Skip to content

Commit

Permalink
extend sync method to sync all accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 committed Jun 20, 2024
1 parent 96eeacd commit c8771d7
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class QiHDWallet extends AbstractHDWallet {
* until the gap limit is reached for both gap and change addresses.
*
* @param {Zone} zone - The zone in which to scan for addresses.
* @param {number} [account=0] - The index of the account to scan. Defaults to 0. Default is `0`
* @param {number} [account=0] - The index of the account to scan. Defaults to 0. Default is `0` Default is `0`
*
* @returns {Promise<void>} A promise that resolves when the scan is complete.
* @throws {Error} If the zone is invalid.
Expand Down Expand Up @@ -220,15 +220,31 @@ export class QiHDWallet extends AbstractHDWallet {
*/
public async sync(zone: Zone, account?: number): Promise<void> {
this.validateZone(zone);
return this._scan(zone, account);
// if no account is specified, scan all accounts.
if (account === undefined) {
const addressInfos = Array.from(this._addresses.values());
const accounts = addressInfos.reduce<number[]>((unique, info) => {
if (!unique.includes(info.account)) {
unique.push(info.account);
}
return unique;
}, []);

for (const acc of accounts) {
await this._scan(zone, acc);
}
} else {
await this._scan(zone, account);
}
return;
}

/**
* Internal method to scan the specified zone for addresses with unspent outputs. This method handles the actual
* scanning logic, generating new addresses until the gap limit is reached for both gap and change addresses.
*
* @param {Zone} zone - The zone in which to scan for addresses.
* @param {number} [account=0] - The index of the account to scan. Defaults to 0. Default is `0`
* @param {number} [account=0] - The index of the account to scan. Defaults to 0. Default is `0` Default is `0`
*
* @returns {Promise<void>} A promise that resolves when the scan is complete.
* @throws {Error} If the provider is not set.
Expand Down

0 comments on commit c8771d7

Please sign in to comment.