Skip to content

Commit

Permalink
feat: enhance code performance (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyyconsensys authored Feb 27, 2024
1 parent b60652b commit 7749409
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},

Expand Down
42 changes: 19 additions & 23 deletions src/ledger-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ export class LedgerKeyring extends EventEmitter {

this.implementFullBIP44 = opts.implementFullBIP44 ?? false;

const keys = new Set<string>(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();
Expand All @@ -157,26 +156,23 @@ export class LedgerKeyring extends EventEmitter {
};
}
}

const keys = new Set<string>(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}`);
}
});
}
}

Expand Down Expand Up @@ -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)];
}

Expand Down

0 comments on commit 7749409

Please sign in to comment.