Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit ad96120

Browse files
authored
Use encryptor isVaultUpdated (#310)
* chore: update browser-passworder * refactor: remove `updateVault` from `GenericEncryptor`
1 parent 7692a2d commit ad96120

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"dependencies": {
4646
"@ethereumjs/tx": "^4.2.0",
47-
"@metamask/browser-passworder": "^4.2.0",
47+
"@metamask/browser-passworder": "^4.3.0",
4848
"@metamask/eth-hd-keyring": "^7.0.1",
4949
"@metamask/eth-sig-util": "^7.0.0",
5050
"@metamask/eth-simple-keyring": "^6.0.1",

src/KeyringController.test.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -863,13 +863,11 @@ describe('KeyringController', () => {
863863
});
864864
deleteEncryptionKeyAndSalt(keyringController);
865865
const initialVault = keyringController.store.getState().vault;
866-
const updatedVaultMock =
867-
'{"vault": "updated_vault_detail", "salt": "salt"}';
868866
const mockEncryptionResult = {
869867
data: '0x1234',
870868
iv: 'an iv',
871869
};
872-
sinon.stub(mockEncryptor, 'updateVault').resolves(updatedVaultMock);
870+
sinon.stub(mockEncryptor, 'isVaultUpdated').returns(false);
873871
sinon
874872
.stub(mockEncryptor, 'encryptWithKey')
875873
.resolves(mockEncryptionResult);
@@ -898,21 +896,12 @@ describe('KeyringController', () => {
898896
},
899897
});
900898
const initialVault = keyringController.store.getState().vault;
901-
const updatedVaultMock =
902-
'{"vault": "updated_vault_detail", "salt": "salt"}';
903-
const mockEncryptionResult = {
904-
data: '0x1234',
905-
iv: 'an iv',
906-
};
907-
sinon.stub(mockEncryptor, 'updateVault').resolves(updatedVaultMock);
908-
sinon
909-
.stub(mockEncryptor, 'encryptWithKey')
910-
.resolves(mockEncryptionResult);
899+
sinon.stub(mockEncryptor, 'isVaultUpdated').returns(false);
911900

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

915-
expect(initialVault).not.toBe(updatedVault);
904+
expect(initialVault).toBe(updatedVault);
916905
});
917906
});
918907

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

935924
await keyringController.unlockKeyrings(PASSWORD);

src/KeyringController.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,8 @@ class KeyringController extends EventEmitter {
931931
if (
932932
this.password &&
933933
(!this.#cacheEncryptionKey || !encryptionKey) &&
934-
this.#encryptor.updateVault &&
935-
(await this.#encryptor.updateVault(encryptedVault, this.password)) !==
936-
encryptedVault
934+
this.#encryptor.isVaultUpdated &&
935+
!this.#encryptor.isVaultUpdated(encryptedVault)
937936
) {
938937
// Re-encrypt the vault with safer method if one is available
939938
await this.persistAllKeyrings();

src/test/encryptor.mock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export class MockEncryptor implements ExportableKeyEncryptor {
8181
return _vault;
8282
}
8383

84+
isVaultUpdated(_vault: string) {
85+
return true;
86+
}
87+
8488
generateSalt() {
8589
return MOCK_ENCRYPTION_SALT;
8690
}

src/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
DetailedDecryptResult,
33
DetailedEncryptionResult,
44
EncryptionResult,
5+
KeyDerivationOptions,
56
} from '@metamask/browser-passworder';
67
import type { Json, Keyring } from '@metamask/utils';
78

@@ -63,14 +64,17 @@ export type GenericEncryptor = {
6364
*/
6465
decrypt: (password: string, encryptedString: string) => Promise<unknown>;
6566
/**
66-
* Optional vault migration helper. Updates the provided vault, re-encrypting
67-
* data with a safer algorithm if one is available.
67+
* Optional vault migration helper. Checks if the provided vault is up to date
68+
* with the desired encryption algorithm.
6869
*
69-
* @param vault - The encrypted string to update.
70-
* @param password - The password to decrypt the vault with.
70+
* @param vault - The encrypted string to check.
71+
* @param targetDerivationParams - The desired target derivation params.
7172
* @returns The updated encrypted string.
7273
*/
73-
updateVault?: (vault: string, password: string) => Promise<string>;
74+
isVaultUpdated?: (
75+
vault: string,
76+
targetDerivationParams?: KeyDerivationOptions,
77+
) => boolean;
7478
};
7579

7680
/**

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ __metadata:
11271127
languageName: node
11281128
linkType: hard
11291129

1130-
"@metamask/browser-passworder@npm:^4.2.0":
1130+
"@metamask/browser-passworder@npm:^4.3.0":
11311131
version: 4.3.0
11321132
resolution: "@metamask/browser-passworder@npm:4.3.0"
11331133
dependencies:
@@ -1208,7 +1208,7 @@ __metadata:
12081208
"@lavamoat/allow-scripts": ^2.3.1
12091209
"@lavamoat/preinstall-always-fail": ^1.0.0
12101210
"@metamask/auto-changelog": ^3.0.0
1211-
"@metamask/browser-passworder": ^4.2.0
1211+
"@metamask/browser-passworder": ^4.3.0
12121212
"@metamask/eslint-config": ^12.2.0
12131213
"@metamask/eslint-config-jest": ^12.1.0
12141214
"@metamask/eslint-config-nodejs": ^12.1.0

0 commit comments

Comments
 (0)