-
-
Notifications
You must be signed in to change notification settings - Fork 220
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: keyrings restore failure #5535
Merged
mikesposito
merged 13 commits into
main
from
mikesposito/keyring-controller/restore-keyring
Mar 25, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
400c4b8
fix: add keyring to controller as last step when restoring
mikesposito cea598f
fix: clear `#unsupportedKeyrings` when clearing keyrings
mikesposito c0d5a15
fix: slice metadata in constructor
mikesposito 14479a4
fix: metadata length check
mikesposito 5c7e496
update lint rule threshold
mikesposito 4955fc2
test: `submitPassword` with existing keyringsMetadata
mikesposito 5b0b22f
fix: add metadata in the state after generating it
mikesposito ad1b253
test: add case for too many metadata objects
mikesposito b430926
fix: throw error when unlocking the wallet with too many metadata
mikesposito 35af73b
Update packages/keyring-controller/src/KeyringController.ts
mikesposito b435e27
update changelog
mikesposito 9c251df
fix changelog
mikesposito 8a8331b
Merge branch 'main' into mikesposito/keyring-controller/restore-keyring
mikesposito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,17 +193,19 @@ describe('KeyringController', () => { | |
}); | ||
|
||
it('should throw an error if there is no primary keyring', async () => { | ||
await withController(async ({ controller, encryptor }) => { | ||
await controller.setLocked(); | ||
jest | ||
.spyOn(encryptor, 'decrypt') | ||
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]); | ||
await controller.submitPassword('123'); | ||
await withController( | ||
{ skipVaultCreation: true, state: { vault: 'my vault' } }, | ||
async ({ controller, encryptor }) => { | ||
jest | ||
.spyOn(encryptor, 'decrypt') | ||
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]); | ||
await controller.submitPassword('123'); | ||
|
||
await expect(controller.addNewAccount()).rejects.toThrow( | ||
'No HD keyring found', | ||
); | ||
}); | ||
await expect(controller.addNewAccount()).rejects.toThrow( | ||
'No HD keyring found', | ||
); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
|
@@ -260,46 +262,6 @@ describe('KeyringController', () => { | |
}); | ||
}); | ||
|
||
describe('when the keyringMetadata length is different from the number of keyrings', () => { | ||
it('should throw an error if the keyring metadata length mismatch', async () => { | ||
const vaultWithOneKeyring = await withController( | ||
async ({ controller }) => controller.state.vault, | ||
); | ||
|
||
await withController( | ||
{ | ||
skipVaultCreation: true, | ||
state: { | ||
vault: vaultWithOneKeyring, // pass non-empty vault | ||
keyringsMetadata: [ | ||
{ id: '1', name: '' }, | ||
{ id: '2', name: '' }, | ||
], | ||
}, | ||
}, | ||
async ({ controller, encryptor }) => { | ||
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([ | ||
{ | ||
type: 'HD Key Tree', | ||
data: { | ||
keyrings: [ | ||
{ | ||
type: 'HD Key Tree', | ||
accounts: ['0x123'], | ||
}, | ||
], | ||
}, | ||
}, | ||
]); | ||
await controller.submitPassword(password); | ||
await expect(controller.addNewAccount()).rejects.toThrow( | ||
KeyringControllerError.KeyringMetadataLengthMismatch, | ||
); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
describe('addNewAccountForKeyring', () => { | ||
describe('when accountCount is not provided', () => { | ||
it('should add new account', async () => { | ||
|
@@ -1088,21 +1050,23 @@ describe('KeyringController', () => { | |
}); | ||
|
||
it('should throw an error if there is no keyring', async () => { | ||
await withController(async ({ controller, encryptor }) => { | ||
await controller.setLocked(); | ||
jest | ||
.spyOn(encryptor, 'decrypt') | ||
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]); | ||
await controller.submitPassword('123'); | ||
await withController( | ||
{ skipVaultCreation: true, state: { vault: 'my vault' } }, | ||
async ({ controller, encryptor }) => { | ||
jest | ||
.spyOn(encryptor, 'decrypt') | ||
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]); | ||
await controller.submitPassword('123'); | ||
|
||
await expect( | ||
controller.getKeyringForAccount( | ||
'0x0000000000000000000000000000000000000000', | ||
), | ||
).rejects.toThrow( | ||
'KeyringController - No keyring found. Error info: There are no keyrings', | ||
); | ||
}); | ||
await expect( | ||
controller.getKeyringForAccount( | ||
'0x0000000000000000000000000000000000000000', | ||
), | ||
).rejects.toThrow( | ||
'KeyringController - No keyring found. Error info: There are no keyrings', | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
it('should throw an error if the controller is locked', async () => { | ||
|
@@ -2586,9 +2550,12 @@ describe('KeyringController', () => { | |
|
||
it('should unlock also with unsupported keyrings', async () => { | ||
await withController( | ||
{ cacheEncryptionKey }, | ||
{ | ||
cacheEncryptionKey, | ||
skipVaultCreation: true, | ||
state: { vault: 'my vault' }, | ||
}, | ||
async ({ controller, encryptor }) => { | ||
await controller.setLocked(); | ||
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([ | ||
{ | ||
type: 'UnsupportedKeyring', | ||
|
@@ -2605,9 +2572,12 @@ describe('KeyringController', () => { | |
|
||
it('should throw error if vault unlocked has an unexpected shape', async () => { | ||
await withController( | ||
{ cacheEncryptionKey }, | ||
{ | ||
cacheEncryptionKey, | ||
skipVaultCreation: true, | ||
state: { vault: 'my vault' }, | ||
}, | ||
async ({ controller, encryptor }) => { | ||
await controller.setLocked(); | ||
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([ | ||
{ | ||
foo: 'bar', | ||
|
@@ -2632,6 +2602,60 @@ describe('KeyringController', () => { | |
); | ||
}); | ||
|
||
it('should unlock succesfully when the controller is instantiated with an existing `keyringsMetadata`', async () => { | ||
await withController( | ||
{ | ||
cacheEncryptionKey, | ||
state: { keyringsMetadata: [], vault: 'my vault' }, | ||
skipVaultCreation: true, | ||
}, | ||
async ({ controller, encryptor }) => { | ||
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([ | ||
{ | ||
type: KeyringTypes.hd, | ||
data: { | ||
accounts: ['0x123'], | ||
}, | ||
}, | ||
]); | ||
|
||
await controller.submitPassword(password); | ||
|
||
expect(controller.state.keyringsMetadata).toHaveLength(1); | ||
}, | ||
); | ||
}); | ||
|
||
it('should throw an error when the controller is instantiated with an existing `keyringsMetadata` with too many objects', async () => { | ||
await withController( | ||
{ | ||
cacheEncryptionKey, | ||
state: { | ||
keyringsMetadata: [ | ||
{ id: '123', name: '' }, | ||
{ id: '456', name: '' }, | ||
], | ||
vault: 'my vault', | ||
}, | ||
skipVaultCreation: true, | ||
}, | ||
async ({ controller, encryptor }) => { | ||
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([ | ||
{ | ||
type: KeyringTypes.hd, | ||
data: { | ||
accounts: ['0x123'], | ||
}, | ||
}, | ||
]); | ||
|
||
await expect(controller.submitPassword(password)).rejects.toThrow( | ||
KeyringControllerError.KeyringMetadataLengthMismatch, | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
!cacheEncryptionKey && | ||
it('should throw error if password is of wrong type', async () => { | ||
await withController( | ||
|
@@ -2676,9 +2700,17 @@ describe('KeyringController', () => { | |
|
||
it('should unlock also with unsupported keyrings', async () => { | ||
await withController( | ||
{ cacheEncryptionKey: true }, | ||
{ | ||
cacheEncryptionKey: true, | ||
skipVaultCreation: true, | ||
state: { | ||
vault: JSON.stringify({ data: '0x123', salt: 'my salt' }), | ||
// @ts-expect-error we want to force the controller to have an | ||
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. Hmm. Strange that the constructor is typed to only allow a subset of state, seems like a mistake in the type definition. This shouldn't be an error. Ah well, we can fix later. |
||
// encryption salt equal to the one in the vault | ||
encryptionSalt: 'my salt', | ||
}, | ||
}, | ||
async ({ controller, initialState, encryptor }) => { | ||
await controller.setLocked(); | ||
jest.spyOn(encryptor, 'decrypt').mockResolvedValueOnce([ | ||
{ | ||
type: 'UnsupportedKeyring', | ||
|
@@ -2805,17 +2837,19 @@ describe('KeyringController', () => { | |
}); | ||
|
||
it('should throw an error if there is no primary keyring', async () => { | ||
await withController(async ({ controller, encryptor }) => { | ||
await controller.setLocked(); | ||
jest | ||
.spyOn(encryptor, 'decrypt') | ||
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]); | ||
await controller.submitPassword('123'); | ||
await withController( | ||
{ skipVaultCreation: true, state: { vault: 'my vault' } }, | ||
async ({ controller, encryptor }) => { | ||
jest | ||
.spyOn(encryptor, 'decrypt') | ||
.mockResolvedValueOnce([{ type: 'Unsupported', data: '' }]); | ||
await controller.submitPassword('123'); | ||
|
||
await expect(controller.verifySeedPhrase()).rejects.toThrow( | ||
KeyringControllerError.KeyringNotFound, | ||
); | ||
}); | ||
await expect(controller.verifySeedPhrase()).rejects.toThrow( | ||
KeyringControllerError.KeyringNotFound, | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
it('should throw error when the controller is locked', async () => { | ||
|
@@ -4208,18 +4242,20 @@ describe('KeyringController', () => { | |
it('should rollback the controller keyrings if the keyring creation fails', async () => { | ||
const mockAddress = '0x4584d2B4905087A100420AFfCe1b2d73fC69B8E4'; | ||
stubKeyringClassWithAccount(MockKeyring, mockAddress); | ||
|
||
// Mocking the serialize method to throw an error will | ||
// halt the controller everytime it tries to persist the keyring, | ||
// making it impossible to update the vault | ||
jest | ||
.spyOn(MockKeyring.prototype, 'serialize') | ||
.mockImplementation(async () => { | ||
throw new Error('You will never be able to persist me!'); | ||
}); | ||
await withController( | ||
{ keyringBuilders: [keyringBuilderFactory(MockKeyring)] }, | ||
async ({ controller, initialState }) => { | ||
// We're mocking BaseController .update() to throw an error, as it's the last operation | ||
// that is called before the function is rolled back. | ||
jest.spyOn(controller, 'update' as never).mockImplementation(() => { | ||
throw new Error('You will never be able to change me!'); | ||
}); | ||
await expect( | ||
controller.addNewKeyring(MockKeyring.type), | ||
).rejects.toThrow('You will never be able to change me!'); | ||
).rejects.toThrow('You will never be able to persist me!'); | ||
|
||
expect(controller.state).toStrictEqual(initialState); | ||
await expect( | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This has been removed from
addNewAccount
test cases, because it should fail way earlier (i.e. when the controller is unlocked, as opposed to when adding an account)