Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"dependencies": {
"@ethereumjs/tx": "^4.2.0",
"@metamask/browser-passworder": "^4.2.0",
"@metamask/browser-passworder": "^4.3.0",
"@metamask/eth-hd-keyring": "^7.0.1",
"@metamask/eth-sig-util": "^7.0.0",
"@metamask/eth-simple-keyring": "^6.0.1",
Expand Down
19 changes: 4 additions & 15 deletions src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,11 @@ describe('KeyringController', () => {
});
deleteEncryptionKeyAndSalt(keyringController);
const initialVault = keyringController.store.getState().vault;
const updatedVaultMock =
'{"vault": "updated_vault_detail", "salt": "salt"}';
const mockEncryptionResult = {
data: '0x1234',
iv: 'an iv',
};
sinon.stub(mockEncryptor, 'updateVault').resolves(updatedVaultMock);
sinon.stub(mockEncryptor, 'isVaultUpdated').returns(false);
sinon
.stub(mockEncryptor, 'encryptWithKey')
.resolves(mockEncryptionResult);
Expand Down Expand Up @@ -898,21 +896,12 @@ describe('KeyringController', () => {
},
});
const initialVault = keyringController.store.getState().vault;
const updatedVaultMock =
'{"vault": "updated_vault_detail", "salt": "salt"}';
const mockEncryptionResult = {
data: '0x1234',
iv: 'an iv',
};
sinon.stub(mockEncryptor, 'updateVault').resolves(updatedVaultMock);
sinon
.stub(mockEncryptor, 'encryptWithKey')
.resolves(mockEncryptionResult);
sinon.stub(mockEncryptor, 'isVaultUpdated').returns(false);

await keyringController.unlockKeyrings(PASSWORD);
const updatedVault = keyringController.store.getState().vault;

expect(initialVault).not.toBe(updatedVault);
expect(initialVault).toBe(updatedVault);
});
});

Expand All @@ -929,7 +918,7 @@ describe('KeyringController', () => {
const initialVault = keyringController.store.getState().vault;
const updatedVaultMock =
'{"vault": "updated_vault_detail", "salt": "salt"}';
sinon.stub(mockEncryptor, 'updateVault').resolves(updatedVaultMock);
sinon.stub(mockEncryptor, 'isVaultUpdated').returns(false);
sinon.stub(mockEncryptor, 'encrypt').resolves(updatedVaultMock);

await keyringController.unlockKeyrings(PASSWORD);
Expand Down
5 changes: 2 additions & 3 deletions src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,8 @@ class KeyringController extends EventEmitter {
if (
this.password &&
(!this.#cacheEncryptionKey || !encryptionKey) &&
this.#encryptor.updateVault &&
(await this.#encryptor.updateVault(encryptedVault, this.password)) !==
encryptedVault
this.#encryptor.isVaultUpdated &&
!this.#encryptor.isVaultUpdated(encryptedVault)
) {
// Re-encrypt the vault with safer method if one is available
await this.persistAllKeyrings();
Expand Down
4 changes: 4 additions & 0 deletions src/test/encryptor.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export class MockEncryptor implements ExportableKeyEncryptor {
return _vault;
}

isVaultUpdated(_vault: string) {
return true;
}

generateSalt() {
return MOCK_ENCRYPTION_SALT;
}
Expand Down
14 changes: 9 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
DetailedDecryptResult,
DetailedEncryptionResult,
EncryptionResult,
KeyDerivationOptions,
} from '@metamask/browser-passworder';
import type { Json, Keyring } from '@metamask/utils';

Expand Down Expand Up @@ -63,14 +64,17 @@ export type GenericEncryptor = {
*/
decrypt: (password: string, encryptedString: string) => Promise<unknown>;
/**
* Optional vault migration helper. Updates the provided vault, re-encrypting
* data with a safer algorithm if one is available.
* Optional vault migration helper. Checks if the provided vault is up to date
* with the desired encryption algorithm.
*
* @param vault - The encrypted string to update.
* @param password - The password to decrypt the vault with.
* @param vault - The encrypted string to check.
* @param targetDerivationParams - The desired target derivation params.
* @returns The updated encrypted string.
*/
updateVault?: (vault: string, password: string) => Promise<string>;
isVaultUpdated?: (
vault: string,
targetDerivationParams?: KeyDerivationOptions,
) => boolean;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ __metadata:
languageName: node
linkType: hard

"@metamask/browser-passworder@npm:^4.2.0":
"@metamask/browser-passworder@npm:^4.3.0":
version: 4.3.0
resolution: "@metamask/browser-passworder@npm:4.3.0"
dependencies:
Expand Down Expand Up @@ -1208,7 +1208,7 @@ __metadata:
"@lavamoat/allow-scripts": ^2.3.1
"@lavamoat/preinstall-always-fail": ^1.0.0
"@metamask/auto-changelog": ^3.0.0
"@metamask/browser-passworder": ^4.2.0
"@metamask/browser-passworder": ^4.3.0
"@metamask/eslint-config": ^12.2.0
"@metamask/eslint-config-jest": ^12.1.0
"@metamask/eslint-config-nodejs": ^12.1.0
Expand Down