Skip to content

Commit 443340f

Browse files
fix: Add missing WS types and allow local URLs (#3459)
Fixes a couple of issues after the merge of the WebSockets PR. Adds the proper types for the RPC methods and allows local WebSocket URLs in `snap_openWebSocket`. Also fixes the formatting for Uri validation errors. Closes #3457
1 parent 1f4fc7a commit 443340f

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ describe('snap_openWebSocket', () => {
8888
id: 1,
8989
method: 'snap_openWebSocket',
9090
params: {
91-
url: 'ws://metamask.io',
91+
url: 'http://metamask.io',
9292
},
9393
});
9494

9595
expect(response).toStrictEqual({
9696
error: {
9797
code: -32602,
9898
message:
99-
'Invalid params: At path: url -- Expected URL, got "ws://metamask.io"..',
99+
'Invalid params: At path: url -- At path: protocol -- Expected the value to satisfy a union of `"wss:" | "ws:"`, but received: "http:".',
100100
stack: expect.any(String),
101101
},
102102
id: 1,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PermittedHandlerExport } from '@metamask/permission-controller';
33
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
44
import {
55
literal,
6+
union,
67
type JsonRpcRequest,
78
type OpenWebSocketParams,
89
type OpenWebSocketResult,
@@ -32,7 +33,7 @@ export type OpenWebSocketMethodHooks = {
3233
};
3334

3435
const OpenWebSocketParametersStruct = object({
35-
url: uri({ protocol: literal('wss:') }),
36+
url: uri({ protocol: union([literal('wss:'), literal('ws:')]) }),
3637
protocols: optional(array(string())),
3738
});
3839

packages/snaps-sdk/src/types/methods/methods.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import type {
33
CancelBackgroundEventResult,
44
} from './cancel-background-event';
55
import type { ClearStateParams, ClearStateResult } from './clear-state';
6+
import type {
7+
CloseWebSocketParams,
8+
CloseWebSocketResult,
9+
} from './close-web-socket';
610
import type {
711
CreateInterfaceParams,
812
CreateInterfaceResult,
@@ -49,6 +53,10 @@ import type {
4953
} from './get-preferences';
5054
import type { GetSnapsParams, GetSnapsResult } from './get-snaps';
5155
import type { GetStateParams, GetStateResult } from './get-state';
56+
import type {
57+
GetWebSocketsParams,
58+
GetWebSocketsResult,
59+
} from './get-web-sockets';
5260
import type {
5361
InvokeKeyringParams,
5462
InvokeKeyringResult,
@@ -64,6 +72,10 @@ import type {
6472
} from './manage-accounts';
6573
import type { ManageStateParams, ManageStateResult } from './manage-state';
6674
import type { NotifyParams, NotifyResult } from './notify';
75+
import type {
76+
OpenWebSocketParams,
77+
OpenWebSocketResult,
78+
} from './open-web-socket';
6779
import type { RequestSnapsParams, RequestSnapsResult } from './request-snaps';
6880
import type {
6981
ResolveInterfaceParams,
@@ -73,6 +85,10 @@ import type {
7385
ScheduleBackgroundEventParams,
7486
ScheduleBackgroundEventResult,
7587
} from './schedule-background-event';
88+
import type {
89+
SendWebSocketMessageParams,
90+
SendWebSocketMessageResult,
91+
} from './send-web-socket-message';
7692
import type { SetStateParams, SetStateResult } from './set-state';
7793
import type { TrackEventParams, TrackEventResult } from './track-event';
7894
import type {
@@ -125,6 +141,13 @@ export type SnapMethods = {
125141
snap_resolveInterface: [ResolveInterfaceParams, ResolveInterfaceResult];
126142
snap_setState: [SetStateParams, SetStateResult];
127143
snap_trackEvent: [TrackEventParams, TrackEventResult];
144+
snap_openWebSocket: [OpenWebSocketParams, OpenWebSocketResult];
145+
snap_closeWebSocket: [CloseWebSocketParams, CloseWebSocketResult];
146+
snap_getWebSockets: [GetWebSocketsParams, GetWebSocketsResult];
147+
snap_sendWebSocketMessage: [
148+
SendWebSocketMessageParams,
149+
SendWebSocketMessageResult,
150+
];
128151
wallet_getSnaps: [GetSnapsParams, GetSnapsResult];
129152
wallet_invokeKeyring: [InvokeKeyringParams, InvokeKeyringResult];
130153
wallet_invokeSnap: [InvokeSnapParams, InvokeSnapResult];

packages/snaps-utils/coverage.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"branches": 99.76,
33
"functions": 99,
4-
"lines": 98.68,
5-
"statements": 97.28
4+
"lines": 98.69,
5+
"statements": 97.29
66
}

packages/snaps-utils/src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@metamask/snaps-sdk';
12
import {
23
is,
34
optional,
@@ -6,6 +7,7 @@ import {
67
string,
78
type,
89
assert as assertSuperstruct,
10+
StructError,
911
} from '@metamask/superstruct';
1012
import type { Infer, Struct } from '@metamask/superstruct';
1113
import type { Json } from '@metamask/utils';
@@ -124,8 +126,11 @@ export const uri = (opts: UriOptions<any> = {}) =>
124126
const UrlStruct = type(opts);
125127
assertSuperstruct(url, UrlStruct);
126128
return true;
127-
} catch {
128-
return `Expected URL, got "${value.toString()}".`;
129+
} catch (error) {
130+
if (error instanceof StructError) {
131+
return getErrorMessage(error);
132+
}
133+
return `Expected URL, got "${value.toString()}"`;
129134
}
130135
});
131136

0 commit comments

Comments
 (0)