Skip to content

Commit 8be5dfc

Browse files
grypezclaude
andcommitted
test(wallet): add encryptorFactory unit tests
Export encryptorFactory so its returned functions can be tested directly without constructing a full Wallet. Add keyring-controller.test.ts with a describe block per factory: - encrypt: round-trips and embeds the PBKDF2 iteration count - encryptWithDetail: round-trips via the vault field and embeds the count - isVaultUpdated: true for matching iterations, false for mismatched Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 061637b commit 8be5dfc

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { decrypt, encryptWithDetail, isVaultUpdated } from '@metamask/browser-passworder';
2+
3+
import { encryptorFactory } from './keyring-controller';
4+
5+
const PASSWORD = 'test-password';
6+
const DATA = { foo: 'bar' };
7+
// Use a low iteration count so tests run quickly; we are testing wiring, not
8+
// cryptographic strength.
9+
const ITERATIONS = 1_000;
10+
11+
describe('encryptorFactory', () => {
12+
describe('encrypt', () => {
13+
it('produces ciphertext that decrypts back to the original data', async () => {
14+
const { encrypt } = encryptorFactory(ITERATIONS);
15+
const ciphertext = await encrypt(PASSWORD, DATA);
16+
expect(await decrypt(PASSWORD, ciphertext)).toStrictEqual(DATA);
17+
});
18+
19+
it('embeds the specified PBKDF2 iteration count', async () => {
20+
const { encrypt } = encryptorFactory(ITERATIONS);
21+
const ciphertext = await encrypt(PASSWORD, DATA);
22+
expect(
23+
isVaultUpdated(ciphertext, {
24+
algorithm: 'PBKDF2',
25+
params: { iterations: ITERATIONS },
26+
}),
27+
).toBe(true);
28+
});
29+
});
30+
31+
describe('encryptWithDetail', () => {
32+
it('returns a vault that decrypts back to the original data', async () => {
33+
const { encryptWithDetail: encryptWithDetailFn } =
34+
encryptorFactory(ITERATIONS);
35+
const { vault } = await encryptWithDetailFn(PASSWORD, DATA);
36+
expect(await decrypt(PASSWORD, vault)).toStrictEqual(DATA);
37+
});
38+
39+
it('embeds the specified PBKDF2 iteration count in the vault', async () => {
40+
const { encryptWithDetail: encryptWithDetailFn } =
41+
encryptorFactory(ITERATIONS);
42+
const { vault } = await encryptWithDetailFn(PASSWORD, DATA);
43+
expect(
44+
isVaultUpdated(vault, {
45+
algorithm: 'PBKDF2',
46+
params: { iterations: ITERATIONS },
47+
}),
48+
).toBe(true);
49+
});
50+
});
51+
52+
describe('isVaultUpdated', () => {
53+
it('returns true for a vault encrypted with the matching iteration count', async () => {
54+
const { vault } = await encryptWithDetail(PASSWORD, DATA, undefined, {
55+
algorithm: 'PBKDF2',
56+
params: { iterations: ITERATIONS },
57+
});
58+
expect(encryptorFactory(ITERATIONS).isVaultUpdated(vault)).toBe(true);
59+
});
60+
61+
it('returns false for a vault encrypted with a different iteration count', async () => {
62+
const { vault } = await encryptWithDetail(PASSWORD, DATA, undefined, {
63+
algorithm: 'PBKDF2',
64+
params: { iterations: ITERATIONS - 1 },
65+
});
66+
expect(encryptorFactory(ITERATIONS).isVaultUpdated(vault)).toBe(false);
67+
});
68+
});
69+
});

packages/wallet/src/initialization/instances/keyring-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const isVaultUpdatedFactory =
124124
* @param iterations - The number of iterations to use for the PBKDF2 algorithm.
125125
* @returns An encryptor set with the given number of iterations.
126126
*/
127-
const encryptorFactory = (iterations: number): Encryptor => ({
127+
export const encryptorFactory = (iterations: number): Encryptor => ({
128128
encrypt: encryptFactory(iterations),
129129
encryptWithKey,
130130
encryptWithDetail: encryptWithDetailFactory(iterations),

0 commit comments

Comments
 (0)