Skip to content

Commit 3ad1e63

Browse files
committed
Fix some minor issues
1 parent aea7f62 commit 3ad1e63

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "REliam7FRJGwKCI4NaC2G3mBSD5iYR7DCEhrXLcBDqA=",
10+
"shasum": "82KbG3cf0wtxooJpWzHeM1g4FhO8O7zSYCAAGNPshfM=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-rpc-methods/jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
1010
],
1111
coverageThreshold: {
1212
global: {
13-
branches: 92.98,
13+
branches: 93.28,
1414
functions: 97.46,
15-
lines: 97.94,
16-
statements: 97.53,
15+
lines: 98.03,
16+
statements: 97.61,
1717
},
1818
},
1919
});

packages/snaps-rpc-methods/src/permitted/getState.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,6 @@ describe('get', () => {
279279
expect(get(object, 'a.b.c')).toBe('value');
280280
});
281281

282-
it('returns the object if the key is empty', () => {
283-
expect(get(object, '')).toBe(object);
284-
});
285-
286282
it('returns `null` if the object is `null`', () => {
287283
expect(get(null, '')).toBeNull();
288284
});

packages/snaps-rpc-methods/src/permitted/getState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function get(
153153
value: Record<string, Json> | null,
154154
key?: string | undefined,
155155
): Json {
156-
if (key === undefined || key === '') {
156+
if (key === undefined) {
157157
return value;
158158
}
159159

packages/snaps-rpc-methods/src/permitted/setState.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,61 @@ describe('snap_setState', () => {
285285
});
286286
});
287287

288+
it('throws if the encrypted parameter is invalid', async () => {
289+
const { implementation } = setStateHandler;
290+
291+
const getSnapState = jest.fn().mockReturnValue({
292+
foo: 'bar',
293+
});
294+
295+
const updateSnapState = jest.fn().mockReturnValue(null);
296+
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
297+
const hasPermission = jest.fn().mockReturnValue(true);
298+
299+
const hooks = {
300+
getSnapState,
301+
updateSnapState,
302+
getUnlockPromise,
303+
hasPermission,
304+
};
305+
306+
const engine = new JsonRpcEngine();
307+
308+
engine.push((request, response, next, end) => {
309+
const result = implementation(
310+
request as JsonRpcRequest<SetStateParameters>,
311+
response as PendingJsonRpcResponse<SetStateResult>,
312+
next,
313+
end,
314+
hooks,
315+
);
316+
317+
result?.catch(end);
318+
});
319+
320+
const response = await engine.handle({
321+
jsonrpc: '2.0',
322+
id: 1,
323+
method: 'snap_setState',
324+
params: {
325+
key: 'foo',
326+
value: 'bar',
327+
encrypted: 'baz',
328+
},
329+
});
330+
331+
expect(response).toStrictEqual({
332+
jsonrpc: '2.0',
333+
id: 1,
334+
error: {
335+
code: errorCodes.rpc.invalidParams,
336+
message:
337+
'Invalid params: At path: encrypted -- Expected a value of type `boolean`, but received: `"baz"`.',
338+
stack: expect.any(String),
339+
},
340+
});
341+
});
342+
288343
it('throws if `key` is not provided and `value` is not an object', async () => {
289344
const { implementation } = setStateHandler;
290345

packages/snaps-rpc-methods/src/permitted/setState.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ async function setStateImplementation(
137137
await getUnlockPromise(true);
138138
}
139139

140-
const newState = await getNewState(key, value, getSnapState);
140+
const newState = await getNewState(key, value, encrypted, getSnapState);
141141
await updateSnapState(newState, encrypted);
142142
response.result = null;
143143
} catch (error) {
@@ -180,20 +180,22 @@ function getValidatedParams(params?: unknown) {
180180
*
181181
* @param key - The key to set.
182182
* @param value - The value to set the key to.
183+
* @param encrypted - Whether the state is encrypted.
183184
* @param getSnapState - The `getSnapState` hook.
184185
* @returns The new state of the Snap.
185186
*/
186187
async function getNewState(
187188
key: string | undefined,
188189
value: Json,
190+
encrypted: boolean,
189191
getSnapState: SetStateHooks['getSnapState'],
190192
) {
191193
if (key === undefined) {
192194
assert(isObject(value));
193195
return value;
194196
}
195197

196-
const state = await getSnapState(false);
198+
const state = await getSnapState(encrypted);
197199
return set(state, key, value);
198200
}
199201

0 commit comments

Comments
 (0)