diff --git a/jest.config.js b/jest.config.js index 95acdb90..6b29cc7c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -41,10 +41,10 @@ module.exports = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 68.06, - functions: 88.57, - lines: 81.29, - statements: 81.21, + branches: 68.33, + functions: 88.23, + lines: 81.46, + statements: 81.38, }, }, diff --git a/src/ledger-keyring.ts b/src/ledger-keyring.ts index 08bd6103..0ae4dd32 100644 --- a/src/ledger-keyring.ts +++ b/src/ledger-keyring.ts @@ -138,11 +138,10 @@ export class LedgerKeyring extends EventEmitter { this.implementFullBIP44 = opts.implementFullBIP44 ?? false; + const keys = new Set(Object.keys(this.accountDetails)); // Remove accounts that don't have corresponding account details this.accounts = this.accounts.filter((account) => - Object.keys(this.accountDetails).includes( - ethUtil.toChecksumAddress(account), - ), + keys.has(ethUtil.toChecksumAddress(account)), ); return Promise.resolve(); @@ -157,26 +156,23 @@ export class LedgerKeyring extends EventEmitter { }; } } - + const keys = new Set(Object.keys(this.accountDetails)); // try to migrate non-LedgerLive accounts too if (!this.#isLedgerLiveHdPath()) { - this.accounts - .filter( - (account) => - !Object.keys(this.accountDetails).includes( - ethUtil.toChecksumAddress(account), - ), - ) - .forEach((account) => { - try { - this.accountDetails[ethUtil.toChecksumAddress(account)] = { + this.accounts.forEach((account) => { + try { + const key = ethUtil.toChecksumAddress(account); + + if (!keys.has(key)) { + this.accountDetails[key] = { bip44: false, hdPath: this.#pathFromAddress(account), }; - } catch (error) { - console.log(`failed to migrate account ${account}`); } - }); + } catch (error) { + console.log(`failed to migrate account ${account}`); + } + }); } } @@ -275,15 +271,15 @@ export class LedgerKeyring extends EventEmitter { } removeAccount(address: string) { - if ( - !this.accounts.map((a) => a.toLowerCase()).includes(address.toLowerCase()) - ) { + const filteredAccounts = this.accounts.filter( + (a) => a.toLowerCase() !== address.toLowerCase(), + ); + + if (filteredAccounts.length === this.accounts.length) { throw new Error(`Address ${address} not found in this keyring`); } - this.accounts = this.accounts.filter( - (a) => a.toLowerCase() !== address.toLowerCase(), - ); + this.accounts = filteredAccounts; delete this.accountDetails[ethUtil.toChecksumAddress(address)]; }