@@ -122,7 +122,7 @@ export class HdKeyring implements Keyring {
122
122
123
123
hdPath : string = hdPathString ;
124
124
125
- #wallets: WalletData [ ] = [ ] ;
125
+ readonly #walletMap = new Map < Hex , WalletData > ( ) ;
126
126
127
127
readonly #cryptographicFunctions?: CryptographicFunctions ;
128
128
@@ -155,7 +155,7 @@ export class HdKeyring implements Keyring {
155
155
156
156
return {
157
157
mnemonic,
158
- numberOfAccounts : this . #wallets . length ,
158
+ numberOfAccounts : this . #walletMap . size ,
159
159
hdPath : this . hdPath ,
160
160
} ;
161
161
}
@@ -180,7 +180,8 @@ export class HdKeyring implements Keyring {
180
180
'Eth-Hd-Keyring: Secret recovery phrase already provided' ,
181
181
) ;
182
182
}
183
- this . #wallets = [ ] ;
183
+
184
+ this . #walletMap. clear ( ) ;
184
185
this . mnemonic = null ;
185
186
this . seed = null ;
186
187
this . root = null ;
@@ -207,8 +208,8 @@ export class HdKeyring implements Keyring {
207
208
throw new Error ( 'Eth-Hd-Keyring: No secret recovery phrase provided' ) ;
208
209
}
209
210
210
- const oldLen = this . #wallets . length ;
211
- const newWallets : WalletData [ ] = [ ] ;
211
+ const oldLen = this . #walletMap . size ;
212
+ const newAddresses : Hex [ ] = [ ] ;
212
213
for ( let i = oldLen ; i < numberOfAccounts + oldLen ; i ++ ) {
213
214
const hdKey = this . root . deriveChild ( i ) ;
214
215
assert ( hdKey . publicKey , 'Expected public key to be set' ) ;
@@ -219,11 +220,11 @@ export class HdKeyring implements Keyring {
219
220
address,
220
221
} ;
221
222
222
- newWallets . push ( walletData ) ;
223
- this . #wallets . push ( walletData ) ;
223
+ this . #walletMap . set ( address , walletData ) ;
224
+ newAddresses . push ( address ) ;
224
225
}
225
- const hexWallets = newWallets . map ( ( walletData ) => walletData . address ) ;
226
- return Promise . resolve ( hexWallets ) ;
226
+
227
+ return Promise . resolve ( newAddresses ) ;
227
228
}
228
229
229
230
/**
@@ -232,7 +233,7 @@ export class HdKeyring implements Keyring {
232
233
* @returns The addresses of all accounts in the keyring.
233
234
*/
234
235
async getAccounts ( ) : Promise < Hex [ ] > {
235
- return this . #wallets . map ( ( walletData ) => walletData . address ) ;
236
+ return Array . from ( this . #walletMap . keys ( ) ) ;
236
237
}
237
238
238
239
/**
@@ -424,17 +425,12 @@ export class HdKeyring implements Keyring {
424
425
*/
425
426
removeAccount ( account : Hex ) : void {
426
427
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 ) ) {
432
430
throw new Error ( `Address ${ address } not found in this keyring` ) ;
433
431
}
434
432
435
- this . #wallets = this . #wallets. filter (
436
- ( { address : walletAddress } ) => walletAddress !== address ,
437
- ) ;
433
+ this . #walletMap. delete ( address ) ;
438
434
}
439
435
440
436
/**
@@ -579,9 +575,7 @@ export class HdKeyring implements Keyring {
579
575
{ withAppKeyOrigin } : HDKeyringAccountSelectionOptions = { } ,
580
576
) : HDKey | { privateKey : Buffer ; publicKey : Buffer } {
581
577
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 ) ;
585
579
if ( ! walletData ) {
586
580
throw new Error ( 'HD Keyring - Unable to find matching address.' ) ;
587
581
}
0 commit comments