Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance code performance #211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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