-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Changes from 4 commits
30c9a60
7398719
bc9ea62
7deb9e2
401452a
58cf5cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import { TransactionFactory } from '@ethereumjs/tx'; | |
import { MetaMaskKeyring as QRKeyring } from '@keystonehq/metamask-airgapped-keyring'; | ||
import { CryptoHDKey, ETHSignature } from '@keystonehq/bc-ur-registry-eth'; | ||
import * as uuid from 'uuid'; | ||
import { NetworkType } from '@metamask/controller-utils'; | ||
import { isValidHexAddress, NetworkType } from '@metamask/controller-utils'; | ||
import { keyringBuilderFactory } from '@metamask/eth-keyring-controller'; | ||
import MockEncryptor from '../tests/mocks/mockEncryptor'; | ||
import { | ||
|
@@ -217,20 +217,46 @@ describe('KeyringController', () => { | |
}); | ||
|
||
describe('createNewVaultAndKeychain', () => { | ||
it('should create new vault, mnemonic and keychain', async () => { | ||
const initialSeedWord = await keyringController.exportSeedPhrase( | ||
password, | ||
); | ||
expect(initialSeedWord).toBeDefined(); | ||
const currentState = await keyringController.createNewVaultAndKeychain( | ||
password, | ||
); | ||
expect(initialState).not.toBe(currentState); | ||
const currentSeedWord = await keyringController.exportSeedPhrase( | ||
password, | ||
); | ||
expect(currentSeedWord).toBeDefined(); | ||
expect(initialSeedWord).not.toBe(currentSeedWord); | ||
describe('when there is no existing vault', () => { | ||
it('should create new vault, mnemonic and keychain', async () => { | ||
const cleanKeyringController = new KeyringController( | ||
preferences, | ||
baseConfig, | ||
); | ||
const initialSeedWord = await keyringController.exportSeedPhrase( | ||
password, | ||
); | ||
const currentState = | ||
await cleanKeyringController.createNewVaultAndKeychain(password); | ||
const currentSeedWord = await cleanKeyringController.exportSeedPhrase( | ||
password, | ||
); | ||
expect(initialSeedWord).toBeDefined(); | ||
expect(initialState).not.toBe(currentState); | ||
expect(currentSeedWord).toBeDefined(); | ||
expect(initialSeedWord).not.toBe(currentSeedWord); | ||
expect(isValidHexAddress(currentState.keyrings[0].accounts[0])).toBe( | ||
true, | ||
); | ||
}); | ||
}); | ||
|
||
describe('when there is an existing vault', () => { | ||
it('should return existing vault', async () => { | ||
const initialSeedWord = await keyringController.exportSeedPhrase( | ||
password, | ||
); | ||
const currentState = await keyringController.createNewVaultAndKeychain( | ||
password, | ||
); | ||
const currentSeedWord = await keyringController.exportSeedPhrase( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we are testing the results of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
password, | ||
); | ||
expect(initialSeedWord).toBeDefined(); | ||
expect(initialState).toBe(currentState); | ||
expect(currentSeedWord).toBeDefined(); | ||
expect(initialSeedWord).toBe(currentSeedWord); | ||
}); | ||
}); | ||
}); | ||
|
||
|
There was a problem hiding this comment.
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?)There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).