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

fix!(keyring-eth-ledger-bridge): change addAccounts to return new accounts only #63

Merged
merged 2 commits into from
Oct 1, 2024
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
4 changes: 2 additions & 2 deletions packages/keyring-eth-ledger-bridge/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = merge(baseConfig, {
global: {
branches: 90.64,
functions: 95.95,
lines: 93.02,
statements: 93.09,
lines: 94.76,
statements: 94.81,
},
},
});
32 changes: 21 additions & 11 deletions packages/keyring-eth-ledger-bridge/src/ledger-keyring.test.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests were passing regardless of the issue so even if we are not adding any new test case, they have been changed to also cover this behavior

Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,26 @@ describe('LedgerKeyring', function () {
describe('with a numeric argument', function () {
it('returns that number of accounts', async function () {
keyring.setAccountToUnlock(0);
const accounts = await keyring.addAccounts(5);
expect(accounts).toHaveLength(5);
const firstBatch = await keyring.addAccounts(3);
keyring.setAccountToUnlock(3);
const secondBatch = await keyring.addAccounts(2);

expect(firstBatch).toHaveLength(3);
expect(secondBatch).toHaveLength(2);
});

it('returns the expected accounts', async function () {
keyring.setAccountToUnlock(0);
const accounts = await keyring.addAccounts(3);
expect(accounts[0]).toBe(fakeAccounts[0]);
expect(accounts[1]).toBe(fakeAccounts[1]);
expect(accounts[2]).toBe(fakeAccounts[2]);
const firstBatch = await keyring.addAccounts(3);
keyring.setAccountToUnlock(3);
const secondBatch = await keyring.addAccounts(2);

expect(firstBatch).toStrictEqual([
fakeAccounts[0],
fakeAccounts[1],
fakeAccounts[2],
]);
expect(secondBatch).toStrictEqual([fakeAccounts[3], fakeAccounts[4]]);
});
});

Expand Down Expand Up @@ -425,13 +435,13 @@ describe('LedgerKeyring', function () {
describe('when called multiple times', function () {
it('does not remove existing accounts', async function () {
keyring.setAccountToUnlock(0);
await keyring.addAccounts(1);
const firstBatch = await keyring.addAccounts(1);
keyring.setAccountToUnlock(1);
const accounts = await keyring.addAccounts(1);
const secondBatch = await keyring.addAccounts(1);

expect(accounts).toHaveLength(2);
expect(accounts[0]).toBe(fakeAccounts[0]);
expect(accounts[1]).toBe(fakeAccounts[1]);
expect(await keyring.getAccounts()).toHaveLength(2);
expect(firstBatch).toStrictEqual([fakeAccounts[0]]);
expect(secondBatch).toStrictEqual([fakeAccounts[1]]);
});
});
});
Expand Down
4 changes: 3 additions & 1 deletion packages/keyring-eth-ledger-bridge/src/ledger-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export class LedgerKeyring extends EventEmitter {
.then(async (_) => {
const from = this.unlockedAccount;
const to = from + amount;
const newAccounts: string[] = [];
for (let i = from; i < to; i++) {
const path = this.#getPathForIndex(i);
let address;
Expand All @@ -250,10 +251,11 @@ export class LedgerKeyring extends EventEmitter {

if (!this.accounts.includes(address)) {
this.accounts = [...this.accounts, address];
newAccounts.push(address);
}
this.page = 0;
}
resolve(this.accounts.slice());
resolve(newAccounts);
})
.catch(reject);
});
Expand Down