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

Skip vault creation if one already exists #1324

Merged
merged 6 commits into from
May 5, 2023

Conversation

mikesposito
Copy link
Member

@mikesposito mikesposito commented May 4, 2023

Description

This PR changes the KeyringController.createNewVaultAndRestore method to not create a new vault if there is already an existing one. In this case, it will simply return the existing vault.

Changes

  • CHANGED: createNewVaultAndRestore from KeyringController, to not overwrite existing vault.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation for new or updated code as appropriate (note: this will usually be JSDoc)
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mikesposito mikesposito requested a review from a team as a code owner May 4, 2023 07:54
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

const currentState = await keyringController.createNewVaultAndKeychain(
password,
);
const currentSeedWord = await keyringController.exportSeedPhrase(
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like we are testing the results of exportSeedPhrase in these tests, but the describe block has to do with createNewVaultAndKeychain. I'm wondering if there is another way to test the effects of createNewVaultAndKeychain without depending on other methods. Or, perhaps we should test the results of exportSeedPhrase in the tests for that method? Out of scope for this PR, but something to think about.

Copy link
Member

Choose a reason for hiding this comment

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

The effect of creating a new vault would be along the lines of:

  • Generate a new SRP / HD keyring
  • Encrypt the keyring with the given password
  • Add the resulting encrypted string to the persisted store

Calling exportSeedPhrase does seem like a reasonably direct way to test the first two parts. It might be worth verifying that the vault was updated in the persisted store as well though.

Copy link
Member

Choose a reason for hiding this comment

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

We could consider manually "decrypting" the vault here, as an alternative to using exportSeedPhrase. That would also provide the SRP, and would verify the password; all three effects verified in one assertion.

That seems reasonable 🤔 It would require a bit more more setup though; we'd want to pass in a mock encryption module, so that the test isn't coupled with browser-passworder internal implementation details. That would let us use a "fake" encryption/decryption as well, for a faster test with easier-to-read data.

const initialSeedWord = await keyringController.exportSeedPhrase(
password,
);
const currentState =
Copy link
Contributor

Choose a reason for hiding this comment

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

We are testing the return value of the method, which is good, but should we also test the state of the controller directly as well? Same goes for the next test. (Unless exportSeedPhrase returns a part of the state already?)

Copy link
Member

Choose a reason for hiding this comment

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

The return value is the controller state. At least it's the in-memory state, which is the more informative piece (the persisted state is just the encrypted vault).

Is there something else in particular you had in mind that we should be testing?

Copy link
Contributor

Choose a reason for hiding this comment

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

If that's the case then I suppose it's not required. My thought was that if we ask the controller directly it makes this test more future-proof. But I suppose this would do.

Copy link
Member

Choose a reason for hiding this comment

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

Making this access the state in a more conventional way sounds like a good idea to me too. But this is a bit of a weird case. The "real" controller state right now is just the persisted state; the in-memory state would need to be accessed via a custom property (not exactly future proof).

@mikesposito mikesposito merged commit 09aacdb into main May 5, 2023
@mikesposito mikesposito deleted the feat/1250-skip-create-vault-if-exists branch May 5, 2023 08:09
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
* feat:  don't create new vault if one already exists

* fix: use internal fullUpdate
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
* feat:  don't create new vault if one already exists

* fix: use internal fullUpdate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeyringController: createNewVaultAndKeychain shouldn't create a vault if one already exists
3 participants