Skip to content

Commit 5dab9ca

Browse files
authored
fix: confusing error message when using wrong password (#5627)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> When calling `submitPassword` with the wrong password, a confusing error is shown instead of `Incorrect password`. This is due to the recently added metadata length check in `#restoreSerializedKeyrings`, which doesn't match the correct behavior when `isUnlocked = true`. We can move the metadata length check to `#unlockKeyrings`, so that it will be skipped everytime the controller is locked ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> * Related to MetaMask/metamask-extension#31436 ## Changelog <!-- THIS SECTION IS NO LONGER NEEDED. The process for updating changelogs has changed. Please consult the "Updating changelogs" section of the Contributing doc for more. --> Changelog updated in the PR ## 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 communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 36cce8f commit 5dab9ca

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

packages/keyring-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `ExportableKeyEncryptor` is now a generic type with a type parameter `EncryptionKey` ([#5395](https://github.com/MetaMask/core/pull/5395))
1313
- The type parameter defaults to `unknown`
1414

15+
### Fixed
16+
17+
- Fixed wrong error message thrown when using the wrong password ([#5627](https://github.com/MetaMask/core/pull/5627))
18+
1519
## [21.0.2]
1620

1721
### Changed

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,21 @@ describe('KeyringController', () => {
27342734
expect(controller.state.encryptionSalt).toBeDefined();
27352735
});
27362736
});
2737+
2738+
it('should throw error when using the wrong password', async () => {
2739+
await withController(
2740+
{
2741+
skipVaultCreation: true,
2742+
cacheEncryptionKey,
2743+
state: { vault: 'my vault' },
2744+
},
2745+
async ({ controller }) => {
2746+
await expect(
2747+
controller.submitPassword('wrong password'),
2748+
).rejects.toThrow('Incorrect password.');
2749+
},
2750+
);
2751+
});
27372752
}),
27382753
);
27392754
});

packages/keyring-controller/src/KeyringController.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,10 +2159,6 @@ export class KeyringController extends BaseController<
21592159
for (const serializedKeyring of serializedKeyrings) {
21602160
await this.#restoreKeyring(serializedKeyring);
21612161
}
2162-
2163-
if (this.#keyrings.length !== this.#keyringsMetadata.length) {
2164-
throw new Error(KeyringControllerError.KeyringMetadataLengthMismatch);
2165-
}
21662162
}
21672163

21682164
/**
@@ -2240,6 +2236,13 @@ export class KeyringController extends BaseController<
22402236
}
22412237

22422238
await this.#restoreSerializedKeyrings(vault);
2239+
2240+
// The keyrings array and the keyringsMetadata array should
2241+
// always have the same length while the controller is unlocked.
2242+
if (this.#keyrings.length !== this.#keyringsMetadata.length) {
2243+
throw new Error(KeyringControllerError.KeyringMetadataLengthMismatch);
2244+
}
2245+
22432246
const updatedKeyrings = await this.#getUpdatedKeyrings();
22442247

22452248
this.update((state) => {

0 commit comments

Comments
 (0)