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: throw explicit error when KeyringController is locked #5172

Merged
merged 11 commits into from
Feb 11, 2025

Conversation

mikesposito
Copy link
Member

@mikesposito mikesposito commented Jan 17, 2025

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
@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
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 marked this pull request as ready for review February 3, 2025 11:08
Copy link
Contributor

@mcmire mcmire left a comment

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
Copy link
Contributor

mcmire commented Feb 6, 2025

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
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
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

Copy link
Contributor

@montelaidev montelaidev left a comment

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
127 checks passed
@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
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