Skip to content

Commit 72edc0c

Browse files
committed
Add tests to make sure custom client cryptography is used
1 parent 7799a7c commit 72edc0c

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

packages/snaps-controllers/src/snaps/SnapController.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
} from '@metamask/snaps-utils/test-utils';
5656
import type { SemVerRange, SemVerVersion, Json } from '@metamask/utils';
5757
import {
58+
hexToBytes,
5859
assert,
5960
AssertionError,
6061
base64ToBytes,
@@ -8959,6 +8960,38 @@ describe('SnapController', () => {
89598960

89608961
snapController.destroy();
89618962
});
8963+
8964+
it('uses custom client cryptography functions', async () => {
8965+
const messenger = getSnapControllerMessenger();
8966+
8967+
const pbkdf2Sha512 = jest
8968+
.fn()
8969+
.mockResolvedValue(hexToBytes(ENCRYPTION_KEY));
8970+
8971+
const snapController = getSnapController(
8972+
getSnapControllerOptions({
8973+
messenger,
8974+
state: {
8975+
snaps: getPersistedSnapsState(),
8976+
},
8977+
clientCryptography: {
8978+
pbkdf2Sha512,
8979+
},
8980+
}),
8981+
);
8982+
8983+
const state = { foo: 'bar' };
8984+
await messenger.call(
8985+
'SnapController:updateSnapState',
8986+
MOCK_SNAP_ID,
8987+
state,
8988+
true,
8989+
);
8990+
8991+
expect(pbkdf2Sha512).toHaveBeenCalledTimes(1);
8992+
8993+
snapController.destroy();
8994+
});
89628995
});
89638996

89648997
describe('SnapController:clearSnapState', () => {

packages/snaps-rpc-methods/src/restricted/getBip32Entropy.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,42 @@ describe('getBip32EntropyImplementation', () => {
192192
}
193193
`);
194194
});
195+
196+
it('uses custom client cryptography functions', async () => {
197+
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
198+
const getMnemonic = jest
199+
.fn()
200+
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE_BYTES);
201+
202+
const pbkdf2Sha512 = jest.fn().mockResolvedValue(new Uint8Array(64));
203+
const getClientCryptography = jest.fn().mockReturnValue({
204+
pbkdf2Sha512,
205+
});
206+
207+
expect(
208+
await getBip32EntropyImplementation({
209+
getUnlockPromise,
210+
getMnemonic,
211+
getClientCryptography,
212+
// @ts-expect-error Missing other required properties.
213+
})({
214+
params: { path: ['m', "44'", "1'"], curve: 'secp256k1' },
215+
}),
216+
).toMatchInlineSnapshot(`
217+
{
218+
"chainCode": "0x8472428420c7fd8ef7280545bb6d2bde1d7c6b490556ccd59895f242716388d1",
219+
"curve": "secp256k1",
220+
"depth": 2,
221+
"index": 2147483649,
222+
"masterFingerprint": 3276136937,
223+
"network": "mainnet",
224+
"parentFingerprint": 1981505209,
225+
"privateKey": "0x71d945aba22cd337ff26a107073ae2606dee5dbf7ecfe5c25870b8eaf62b9f1b",
226+
"publicKey": "0x0491c4b234ca9b394f40d90f09092e04fd3bca2aa68c57e1311b25acfd972c5a6fc7ffd19e7812127473aa2bd827917b6ec7b57bec73cf022fc1f1fa0593f48770",
227+
}
228+
`);
229+
230+
expect(pbkdf2Sha512).toHaveBeenCalledTimes(1);
231+
});
195232
});
196233
});

packages/snaps-rpc-methods/src/restricted/getBip32PublicKey.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,36 @@ describe('getBip32PublicKeyImplementation', () => {
154154
`"0x022de17487a660993177ce2a85bb73b6cd9ad436184d57bdf5a93f5db430bea914"`,
155155
);
156156
});
157+
158+
it('uses custom client cryptography functions', async () => {
159+
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
160+
const getMnemonic = jest
161+
.fn()
162+
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE_BYTES);
163+
164+
const pbkdf2Sha512 = jest.fn().mockResolvedValue(new Uint8Array(64));
165+
const getClientCryptography = jest.fn().mockReturnValue({
166+
pbkdf2Sha512,
167+
});
168+
169+
expect(
170+
await getBip32PublicKeyImplementation({
171+
getUnlockPromise,
172+
getMnemonic,
173+
getClientCryptography,
174+
// @ts-expect-error Missing other required properties.
175+
})({
176+
params: {
177+
path: ['m', "44'", "1'", '1', '2', '3'],
178+
curve: 'secp256k1',
179+
compressed: true,
180+
},
181+
}),
182+
).toMatchInlineSnapshot(
183+
`"0x03102d63c39b6dda3f9aa06b247c50653cd9d01a91efce00ccc8735e9714058a01"`,
184+
);
185+
186+
expect(pbkdf2Sha512).toHaveBeenCalledTimes(1);
187+
});
157188
});
158189
});

packages/snaps-rpc-methods/src/restricted/getBip44Entropy.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,43 @@ describe('getBip44EntropyImplementation', () => {
8989
}
9090
`);
9191
});
92+
93+
it('uses custom client cryptography functions', async () => {
94+
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
95+
const getMnemonic = jest
96+
.fn()
97+
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE_BYTES);
98+
99+
const pbkdf2Sha512 = jest.fn().mockResolvedValue(new Uint8Array(64));
100+
const getClientCryptography = jest.fn().mockReturnValue({
101+
pbkdf2Sha512,
102+
});
103+
104+
expect(
105+
await getBip44EntropyImplementation({
106+
getUnlockPromise,
107+
getMnemonic,
108+
getClientCryptography,
109+
// @ts-expect-error Missing other required properties.
110+
})({
111+
params: { coinType: 1 },
112+
}),
113+
).toMatchInlineSnapshot(`
114+
{
115+
"chainCode": "0x8472428420c7fd8ef7280545bb6d2bde1d7c6b490556ccd59895f242716388d1",
116+
"coin_type": 1,
117+
"depth": 2,
118+
"index": 2147483649,
119+
"masterFingerprint": 3276136937,
120+
"network": "mainnet",
121+
"parentFingerprint": 1981505209,
122+
"path": "m / bip32:44' / bip32:1'",
123+
"privateKey": "0x71d945aba22cd337ff26a107073ae2606dee5dbf7ecfe5c25870b8eaf62b9f1b",
124+
"publicKey": "0x0491c4b234ca9b394f40d90f09092e04fd3bca2aa68c57e1311b25acfd972c5a6fc7ffd19e7812127473aa2bd827917b6ec7b57bec73cf022fc1f1fa0593f48770",
125+
}
126+
`);
127+
128+
expect(pbkdf2Sha512).toHaveBeenCalledTimes(1);
129+
});
92130
});
93131
});

packages/snaps-rpc-methods/src/restricted/getEntropy.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,43 @@ describe('getEntropyImplementation', () => {
7272
'0x6d8e92de419401c7da3cedd5f60ce5635b26059c2a4a8003877fec83653a4921',
7373
);
7474
});
75+
76+
it('uses custom client cryptography functions', async () => {
77+
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
78+
const getMnemonic = jest
79+
.fn()
80+
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE_BYTES);
81+
82+
const pbkdf2Sha512 = jest.fn().mockResolvedValue(new Uint8Array(64));
83+
const getClientCryptography = jest.fn().mockReturnValue({
84+
pbkdf2Sha512,
85+
});
86+
87+
const methodHooks = {
88+
getMnemonic,
89+
getUnlockPromise,
90+
getClientCryptography,
91+
};
92+
93+
const implementation = getEntropyBuilder.specificationBuilder({
94+
methodHooks,
95+
}).methodImplementation;
96+
97+
const result = await implementation({
98+
method: 'snap_getEntropy',
99+
params: {
100+
version: 1,
101+
salt: 'foo',
102+
},
103+
context: {
104+
origin: MOCK_SNAP_ID,
105+
},
106+
});
107+
108+
expect(result).toBe(
109+
'0x9bea47f2180fd874147f2f455a5ccc779826cfeff005605190cf0c568b3de7b5',
110+
);
111+
112+
expect(pbkdf2Sha512).toHaveBeenCalledTimes(1);
113+
});
75114
});

0 commit comments

Comments
 (0)