From 3920e498f439dfe67b115a27cf3c3efb47ac0927 Mon Sep 17 00:00:00 2001 From: Alejo Acosta Date: Wed, 6 Nov 2024 15:05:15 -0300 Subject: [PATCH] make '_findLastUsedIndex' method abstract --- src/wallet/hdwallet.ts | 29 +---------------------------- src/wallet/qi-hdwallet.ts | 2 +- src/wallet/quai-hdwallet.ts | 9 ++++++++- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/wallet/hdwallet.ts b/src/wallet/hdwallet.ts index 82ad8414..367213d9 100644 --- a/src/wallet/hdwallet.ts +++ b/src/wallet/hdwallet.ts @@ -147,6 +147,7 @@ export abstract class AbstractHDWallet; - // /** - // * Abstract method to send a transaction. - // * - // * @param {TransactionRequest} tx - The transaction request. - // * @returns {Promise} A promise that resolves to the transaction response. - // */ - // abstract sendTransaction(tx: TransactionRequest): Promise; - /** * Connects the wallet to a provider. * @@ -349,24 +342,4 @@ export abstract class AbstractHDWallet} addressMap - The map containing address information, where the key is - * an address string and the value is a NeuteredAddressInfo object. - * @param {Zone} zone - The specific zone to filter the addresses by. - * @param {number} account - The account number to filter the addresses by. - * @returns {number} - The highest address index for the specified criteria, or -1 if no addresses match. - * @protected - */ - protected getLastAddressIndex(addressMap: Map, zone: Zone, account: number): number { - const addresses = Array.from(addressMap.values()).filter( - (addressInfo) => addressInfo.account === account && addressInfo.zone === zone, - ); - return addresses.reduce((maxIndex, addressInfo) => Math.max(maxIndex, addressInfo.index), -1); - } } diff --git a/src/wallet/qi-hdwallet.ts b/src/wallet/qi-hdwallet.ts index 1b76a199..f3ce4c1a 100644 --- a/src/wallet/qi-hdwallet.ts +++ b/src/wallet/qi-hdwallet.ts @@ -232,7 +232,7 @@ export class QiHDWallet extends AbstractHDWallet { * @param {QiAddressInfo[]} addresses - The array of QiAddressInfo objects. * @returns {number} The last used index. */ - private _findLastUsedIndex(addresses: QiAddressInfo[] | undefined, account: number, zone: Zone): number { + protected _findLastUsedIndex(addresses: QiAddressInfo[] | undefined, account: number, zone: Zone): number { const filteredAddresses = addresses?.filter( (addressInfo) => addressInfo.account === account && addressInfo.zone === zone, ); diff --git a/src/wallet/quai-hdwallet.ts b/src/wallet/quai-hdwallet.ts index 46123614..2e742bc7 100644 --- a/src/wallet/quai-hdwallet.ts +++ b/src/wallet/quai-hdwallet.ts @@ -281,7 +281,7 @@ export class QuaiHDWallet extends AbstractHDWallet { */ protected _getNextAddress(accountIndex: number, zone: Zone): NeuteredAddressInfo { this.validateZone(zone); - const lastIndex = this.getLastAddressIndex(this._addresses, zone, accountIndex); + const lastIndex = this._findLastUsedIndex(Array.from(this._addresses.values()), accountIndex, zone); const addressNode = this.deriveNextAddressNode(accountIndex, lastIndex + 1, zone, false); return this.createAndStoreAddressInfo(addressNode, accountIndex, zone); } @@ -387,4 +387,11 @@ export class QuaiHDWallet extends AbstractHDWallet { const addresses = this._addresses.values(); return Array.from(addresses).filter((addressInfo) => addressInfo.account === account); } + + protected _findLastUsedIndex(addresses: NeuteredAddressInfo[] | undefined, account: number, zone: Zone): number { + const filteredAddresses = addresses?.filter( + (addressInfo) => addressInfo.account === account && addressInfo.zone === zone, + ); + return filteredAddresses?.reduce((maxIndex, addressInfo) => Math.max(maxIndex, addressInfo.index), -1) || -1; + } }