Skip to content

fix: throw explicit error when KeyringController is locked - #5172

Merged
mikesposito merged 11 commits into
mainfrom
fix/assert-controller-unlocked
Feb 11, 2025
Merged

mikesposito merged 11 commits into
mainfrom
fix/assert-controller-unlocked

Conversation

@mikesposito

@mikesposito mikesposito commented Jan 17, 2025 •

Copy link
Copy Markdown
Member

Blocked by:

Explanation

No specific error is thrown when an operation that requires an unlocked vault is attempted while KeyringController.state.isUnlocked is false. This doesn't make the operations possible, but it doesn't give a clear error message either.

This PR adds an assertion on almost all KeyringController methods to check if the controller is unlocked and to throw an error when it isn't.

References

Changelog

@metamask/keyring-controller

  • CHANGED: A specific error message is thrown when any operation is attempted while the controller is locked

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@mikesposito
mikesposito requested a review from a team as a code owner January 17, 2025 12:12
Comment thread packages/keyring-controller/src/KeyringController.ts Outdated
Comment thread packages/keyring-controller/jest.config.js Outdated
@mikesposito
mikesposito marked this pull request as draft January 17, 2025 12:29
@mikesposito
mikesposito force-pushed the fix/assert-controller-unlocked branch from 5834c49 to 5a86062 Compare January 17, 2025 13:40
Comment thread packages/keyring-controller/src/KeyringController.ts Outdated
Comment thread packages/keyring-controller/src/KeyringController.ts Outdated
Base automatically changed from mikesposito/lint-fix to main February 3, 2025 10:23
@mikesposito
mikesposito force-pushed the fix/assert-controller-unlocked branch from b8a66bc to c60cc5a Compare February 3, 2025 11:01
@mikesposito
mikesposito requested a review from a team February 3, 2025 11:02
@mikesposito mikesposito added the team-wallet-framework Deprecated: Please use `team-core-platform` instead. label Feb 3, 2025
@mikesposito
mikesposito marked this pull request as ready for review February 3, 2025 11:08

@mcmire mcmire left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@mikesposito What are your thoughts on adding tests for all the cases which we need to assert that the controller is locked? I know there are a lot of methods you're changing, and also there are more changes planned, so not sure if this is unblocking some more work later or if much of this code will disappear later anyway or what.

@mcmire

mcmire commented Feb 6, 2025

Copy link
Copy Markdown
Collaborator

Regardless of the above comment, I reviewed this and it looks like the controller does not need to be in unlocked state to call these methods:

  • createNewVaultAndRestore
  • createNewVaultAndKeychain
  • verifyPassword
  • isUnlocked
  • exportSeedPhrase
  • exportAccount
  • submitEncryptionKey
  • submitPassword

Is that right?

@mikesposito

Copy link
Copy Markdown
Member Author

@mcmire the following methods should be callable also while the controller is locked:

  • createNewVaultAndRestore: the consumer can create a new vault if password is lost
  • createNewVaultAndKeychain: when the first vault will be created the wallet will be locked to begin with
  • verifyPassword: used to verify the correctness of a password
  • isUnlocked: used to check if it is unlocked, and it shouldn't throw errors
  • submitEncryptionKey: used to unlock the controller with the encryption key, so it's expected that the wallet is locked while the method is called
  • submitPassword: same as above

This one instead has a missing error in the method itself, but getKeyringForAccount is called internally which will throw the error:

  • exportAccount

And I forgot about this one:

  • exportSeedPhrase: added in f811be0

@mikesposito

Copy link
Copy Markdown
Member Author

@mikesposito What are your thoughts on adding tests for all the cases which we need to assert that the controller is locked? I know there are a lot of methods you're changing, and also there are more changes planned, so not sure if this is unblocking some more work later or if much of this code will disappear later anyway or what.

This is not being done to unblock later work, and I expect these assertions to stay in place. I think it's good to add explicit tests

@montelaidev montelaidev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lgtm

@mikesposito
mikesposito merged commit 060c729 into main Feb 11, 2025
@mikesposito
mikesposito deleted the fix/assert-controller-unlocked branch February 11, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-wallet-framework Deprecated: Please use `team-core-platform` instead.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[KeyringController] No specific error is thrown when the controller is locked

3 participants