Skip to content

Commit 0406a79

Browse files
authored
perf(keyring-eth-hd): use map to store wallets (#374)
This PR replaces the list of wallets with a map. It should improve performance since search and removal operations will be O(1) instead of O(n).
1 parent d0cce81 commit 0406a79

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

packages/keyring-eth-hd/src/hd-keyring.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class HdKeyring implements Keyring {
122122

123123
hdPath: string = hdPathString;
124124

125-
#wallets: WalletData[] = [];
125+
readonly #walletMap = new Map<Hex, WalletData>();
126126

127127
readonly #cryptographicFunctions?: CryptographicFunctions;
128128

@@ -155,7 +155,7 @@ export class HdKeyring implements Keyring {
155155

156156
return {
157157
mnemonic,
158-
numberOfAccounts: this.#wallets.length,
158+
numberOfAccounts: this.#walletMap.size,
159159
hdPath: this.hdPath,
160160
};
161161
}
@@ -180,7 +180,8 @@ export class HdKeyring implements Keyring {
180180
'Eth-Hd-Keyring: Secret recovery phrase already provided',
181181
);
182182
}
183-
this.#wallets = [];
183+
184+
this.#walletMap.clear();
184185
this.mnemonic = null;
185186
this.seed = null;
186187
this.root = null;
@@ -207,8 +208,8 @@ export class HdKeyring implements Keyring {
207208
throw new Error('Eth-Hd-Keyring: No secret recovery phrase provided');
208209
}
209210

210-
const oldLen = this.#wallets.length;
211-
const newWallets: WalletData[] = [];
211+
const oldLen = this.#walletMap.size;
212+
const newAddresses: Hex[] = [];
212213
for (let i = oldLen; i < numberOfAccounts + oldLen; i++) {
213214
const hdKey = this.root.deriveChild(i);
214215
assert(hdKey.publicKey, 'Expected public key to be set');
@@ -219,11 +220,11 @@ export class HdKeyring implements Keyring {
219220
address,
220221
};
221222

222-
newWallets.push(walletData);
223-
this.#wallets.push(walletData);
223+
this.#walletMap.set(address, walletData);
224+
newAddresses.push(address);
224225
}
225-
const hexWallets = newWallets.map((walletData) => walletData.address);
226-
return Promise.resolve(hexWallets);
226+
227+
return Promise.resolve(newAddresses);
227228
}
228229

229230
/**
@@ -232,7 +233,7 @@ export class HdKeyring implements Keyring {
232233
* @returns The addresses of all accounts in the keyring.
233234
*/
234235
async getAccounts(): Promise<Hex[]> {
235-
return this.#wallets.map((walletData) => walletData.address);
236+
return Array.from(this.#walletMap.keys());
236237
}
237238

238239
/**
@@ -424,17 +425,12 @@ export class HdKeyring implements Keyring {
424425
*/
425426
removeAccount(account: Hex): void {
426427
const address = this.#normalizeAddress(account);
427-
if (
428-
!this.#wallets
429-
.map(({ address: walletAddress }) => walletAddress)
430-
.includes(address)
431-
) {
428+
429+
if (!this.#walletMap.has(address)) {
432430
throw new Error(`Address ${address} not found in this keyring`);
433431
}
434432

435-
this.#wallets = this.#wallets.filter(
436-
({ address: walletAddress }) => walletAddress !== address,
437-
);
433+
this.#walletMap.delete(address);
438434
}
439435

440436
/**
@@ -579,9 +575,7 @@ export class HdKeyring implements Keyring {
579575
{ withAppKeyOrigin }: HDKeyringAccountSelectionOptions = {},
580576
): HDKey | { privateKey: Buffer; publicKey: Buffer } {
581577
const address = this.#normalizeAddress(account);
582-
const walletData = this.#wallets.find(({ address: walletAddress }) => {
583-
return walletAddress === address;
584-
});
578+
const walletData = this.#walletMap.get(address);
585579
if (!walletData) {
586580
throw new Error('HD Keyring - Unable to find matching address.');
587581
}

0 commit comments

Comments
 (0)