Skip to content

Commit

Permalink
make '_findLastUsedIndex' method abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 authored and rileystephens28 committed Nov 13, 2024
1 parent abac1d7 commit 3920e49
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
29 changes: 1 addition & 28 deletions src/wallet/hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export abstract class AbstractHDWallet<T extends NeuteredAddressInfo = NeuteredA
abstract getPrivateKey(address: string): string;
abstract getAddressesForZone(zone: Zone): T[];
abstract getAddressesForAccount(account: number): T[];
protected abstract _findLastUsedIndex(addresses: T[] | undefined, account: number, zone: Zone): number;

/**
* Creates an instance of the HD wallet.
Expand Down Expand Up @@ -234,14 +235,6 @@ export abstract class AbstractHDWallet<T extends NeuteredAddressInfo = NeuteredA
*/
abstract signTransaction(tx: TransactionRequest): Promise<string>;

// /**
// * Abstract method to send a transaction.
// *
// * @param {TransactionRequest} tx - The transaction request.
// * @returns {Promise<TransactionResponse>} A promise that resolves to the transaction response.
// */
// abstract sendTransaction(tx: TransactionRequest): Promise<TransactionResponse>;

/**
* Connects the wallet to a provider.
*
Expand Down Expand Up @@ -349,24 +342,4 @@ export abstract class AbstractHDWallet<T extends NeuteredAddressInfo = NeuteredA
throw new Error(`Invalid coinType ${serialized.coinType} for wallet (expected ${(this as any)._coinType})`);
}
}

/**
* Retrieves the highest address index from the given address map for a specified zone, account, and change type.
*
* This method filters the address map based on the provided zone, account, and change type, then determines the
* maximum address index from the filtered addresses.
*
* @param {Map<string, NeuteredAddressInfo>} 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<string, NeuteredAddressInfo>, 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);
}
}
2 changes: 1 addition & 1 deletion src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
* @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,
);
Expand Down
9 changes: 8 additions & 1 deletion src/wallet/quai-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class QuaiHDWallet extends AbstractHDWallet<NeuteredAddressInfo> {
*/
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);
}
Expand Down Expand Up @@ -387,4 +387,11 @@ export class QuaiHDWallet extends AbstractHDWallet<NeuteredAddressInfo> {
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;
}
}

0 comments on commit 3920e49

Please sign in to comment.