Skip to content

Commit a80db22

Browse files
authored
Add isSnapId utility function (#2808)
This adds a `isSnapId` function which accepts any value, and returns a boolean if the input is a valid Snap ID or not. This is extracted from #2634.
1 parent e5aeb16 commit a80db22

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

packages/snaps-utils/coverage.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"branches": 99.74,
3-
"functions": 98.91,
3+
"functions": 98.92,
44
"lines": 99.45,
5-
"statements": 96.3
5+
"statements": 96.31
66
}

packages/snaps-utils/src/snaps.test.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,37 @@ import {
1414
assertIsValidSnapId,
1515
verifyRequestedSnapPermissions,
1616
stripSnapPrefix,
17+
isSnapId,
1718
} from './snaps';
19+
import { MOCK_SNAP_ID } from './test-utils';
1820
import { uri, WALLET_SNAP_PERMISSION_KEY } from './types';
1921

22+
describe('isSnapId', () => {
23+
it.each(['npm:@metamask/test-snap-bip44', 'local:http://localhost:8000'])(
24+
'returns `true` for "%s"',
25+
(value) => {
26+
expect(isSnapId(value)).toBe(true);
27+
},
28+
);
29+
30+
it.each([
31+
undefined,
32+
{},
33+
null,
34+
true,
35+
2,
36+
'foo:bar',
37+
' local:http://localhost:8000',
38+
'local:http://localhost:8000 ',
39+
'local:http://localhost:8000\n',
40+
'local:http://localhost:8000\r',
41+
'local:😎',
42+
'local:␡',
43+
])('returns `false` for "%s"', (value) => {
44+
expect(isSnapId(value)).toBe(false);
45+
});
46+
});
47+
2048
describe('assertIsValidSnapId', () => {
2149
it.each([undefined, {}, null, true, 2])(
2250
'throws for non-strings (#%#)',
@@ -239,7 +267,7 @@ describe('isSnapPermitted', () => {
239267
{
240268
type: 'snapIds',
241269
value: {
242-
foo: {},
270+
[MOCK_SNAP_ID]: {},
243271
},
244272
},
245273
],
@@ -273,9 +301,9 @@ describe('isSnapPermitted', () => {
273301
},
274302
};
275303

276-
expect(isSnapPermitted(validPermissions, 'foo')).toBe(true);
277-
expect(isSnapPermitted(invalidPermissions1, 'foo')).toBe(false);
278-
expect(isSnapPermitted(invalidPermissions2, 'foo')).toBe(false);
304+
expect(isSnapPermitted(validPermissions, MOCK_SNAP_ID)).toBe(true);
305+
expect(isSnapPermitted(invalidPermissions1, MOCK_SNAP_ID)).toBe(false);
306+
expect(isSnapPermitted(invalidPermissions2, MOCK_SNAP_ID)).toBe(false);
279307
});
280308

281309
describe('verifyRequestedSnapPermissions', () => {

packages/snaps-utils/src/snaps.ts

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { BlockReason } from '@metamask/snaps-registry';
77
import type { SnapId, Snap as TruncatedSnap } from '@metamask/snaps-sdk';
88
import type { Struct } from '@metamask/superstruct';
99
import {
10+
is,
1011
empty,
1112
enums,
1213
intersection,
@@ -311,6 +312,17 @@ export function stripSnapPrefix(snapId: string): string {
311312
return snapId.replace(getSnapPrefix(snapId), '');
312313
}
313314

315+
/**
316+
* Check if the given value is a valid snap ID. This function is a type guard,
317+
* and will narrow the type of the value to `SnapId` if it returns `true`.
318+
*
319+
* @param value - The value to check.
320+
* @returns `true` if the value is a valid snap ID, and `false` otherwise.
321+
*/
322+
export function isSnapId(value: unknown): value is SnapId {
323+
return is(value, SnapIdStruct);
324+
}
325+
314326
/**
315327
* Assert that the given value is a valid snap ID.
316328
*

0 commit comments

Comments
 (0)