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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 0 deletions packages/keyring-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Metadata for unsupported keyring is removed on unlock ([#5725](https://github.com/MetaMask/core/pull/5725))

## [21.0.6]

### Changed
Expand Down
6 changes: 3 additions & 3 deletions packages/keyring-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 93.64,
branches: 93.71,
functions: 100,
lines: 98.92,
statements: 98.93,
lines: 98.95,
statements: 98.95,
},
},

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 @@ -2288,6 +2288,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 @@ -2571,6 +2572,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 +2575 to +2579
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.

Copy link
Member

Choose a reason for hiding this comment

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

We have no mechanism today for moving a keyring from "unsupported" to "supported", to my knowledge. If and when we implement this, we can perhaps treat it like a new keyring and generate new metadata on-the-spot.

That should work OK.

Any other state relying on the metadata would get cleared when it was first moved to unsupported, that is one downside that would not avoid. But I think that's acceptable. Making a keyring unsupported must inevitably impact state that depends on that keyring.

return undefined;
}
}
Expand Down
Loading