Skip to content

fix: remove metadata for unsupported keyrings #5725

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 93.67,
branches: 93.71,
functions: 100,
lines: 98.92,
lines: 98.93,
statements: 98.93,
},
},
Expand Down
86 changes: 86 additions & 0 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,92 @@ describe('KeyringController', () => {
},
);
});

it('allows removing a keyring builder without bricking the wallet when metadata was already generated', async () => {
await withController(
{
skipVaultCreation: true,
state: {
vault: 'my vault',
keyringsMetadata: [
{ id: 'hd', name: '' },
// This keyring is unsupported
{ id: 'unsupported', name: '' },
{ id: 'hd2', name: '' },
],
},
},
async ({ controller, encryptor }) => {
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([
{
type: KeyringTypes.hd,
data: '',
},
{
type: 'Unsupported',
data: '',
},
{
type: KeyringTypes.hd,
data: '',
},
]);

await controller.submitPassword(password);

expect(controller.state.keyrings).toHaveLength(2);
expect(controller.state.keyrings[0].type).toBe(KeyringTypes.hd);
expect(controller.state.keyrings[1].type).toBe(KeyringTypes.hd);
expect(controller.state.keyringsMetadata).toStrictEqual([
{ id: 'hd', name: '' },
{ id: 'hd2', name: '' },
]);
},
);
});

it('allows removing a keyring builder without bricking the wallet when metadata was not yet generated', async () => {
await withController(
{
skipVaultCreation: true,
state: {
vault: 'my vault',
keyringsMetadata: [
{ id: 'hd', name: '' },
{ id: 'hd2', name: '' },
],
},
},
async ({ controller, encryptor }) => {
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([
{
type: 'HD Key Tree',
data: '',
},
{
type: 'HD Key Tree',
data: '',
},
// This keyring was already unsupported
// (no metadata, and is at the end of the array)
{
type: MockKeyring.type,
data: 'unsupported',
},
]);

await controller.submitPassword(password);

expect(controller.state.keyrings).toHaveLength(2);
expect(controller.state.keyrings[0].type).toBe(KeyringTypes.hd);
expect(controller.state.keyrings[1].type).toBe(KeyringTypes.hd);
expect(controller.state.keyringsMetadata).toStrictEqual([
{ id: 'hd', name: '' },
{ id: 'hd2', name: '' },
]);
},
);
});
});

describe('addNewAccount', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,7 @@ export class KeyringController extends BaseController<

this.update((state) => {
state.keyrings = updatedKeyrings;
state.keyringsMetadata = this.#keyringsMetadata.slice();
if (updatedState.encryptionKey || updatedState.encryptionSalt) {
state.encryptionKey = updatedState.encryptionKey;
state.encryptionSalt = updatedState.encryptionSalt;
Expand Down Expand Up @@ -2524,6 +2525,11 @@ export class KeyringController extends BaseController<
} catch (error) {
console.error(error);
this.#unsupportedKeyrings.push(serialized);
if (this.#keyringsMetadata.length > this.#keyrings.length) {
// There was already a metadata entry for the keyring, so
// we need to remove it
this.#keyringsMetadata.splice(this.#keyrings.length, 1);
}
Comment on lines +2528 to +2532
Copy link
Member Author

Choose a reason for hiding this comment

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

The downside of this solution is that if we consider the following scenario:

  • There is some metadata for a keyring
  • The keyring fails to restore, for any reason, and metadata will be removed
  • The keyring is then supported again

The keyring will then have a different ID, compared to what it had initially. Unfortunately, to avoid this, we'd have to store the metadata along with its keyring in the vault directly.

return undefined;
}
}
Expand Down
Loading