Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ describe('snap_openWebSocket', () => {
id: 1,
method: 'snap_openWebSocket',
params: {
url: 'ws://metamask.io',
url: 'http://metamask.io',
},
});

expect(response).toStrictEqual({
error: {
code: -32602,
message:
'Invalid params: At path: url -- Expected URL, got "ws://metamask.io"..',
'Invalid params: At path: url -- At path: protocol -- Expected the value to satisfy a union of `"wss:" | "ws:"`, but received: "http:".',
stack: expect.any(String),
},
id: 1,
Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-rpc-methods/src/permitted/openWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PermittedHandlerExport } from '@metamask/permission-controller';
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
import {
literal,
union,
type JsonRpcRequest,
type OpenWebSocketParams,
type OpenWebSocketResult,
Expand Down Expand Up @@ -32,7 +33,7 @@ export type OpenWebSocketMethodHooks = {
};

const OpenWebSocketParametersStruct = object({
url: uri({ protocol: literal('wss:') }),
url: uri({ protocol: union([literal('wss:'), literal('ws:')]) }),
protocols: optional(array(string())),
});

Expand Down
23 changes: 23 additions & 0 deletions packages/snaps-sdk/src/types/methods/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type {
CancelBackgroundEventResult,
} from './cancel-background-event';
import type { ClearStateParams, ClearStateResult } from './clear-state';
import type {
CloseWebSocketParams,
CloseWebSocketResult,
} from './close-web-socket';
import type {
CreateInterfaceParams,
CreateInterfaceResult,
Expand Down Expand Up @@ -49,6 +53,10 @@ import type {
} from './get-preferences';
import type { GetSnapsParams, GetSnapsResult } from './get-snaps';
import type { GetStateParams, GetStateResult } from './get-state';
import type {
GetWebSocketsParams,
GetWebSocketsResult,
} from './get-web-sockets';
import type {
InvokeKeyringParams,
InvokeKeyringResult,
Expand All @@ -64,6 +72,10 @@ import type {
} from './manage-accounts';
import type { ManageStateParams, ManageStateResult } from './manage-state';
import type { NotifyParams, NotifyResult } from './notify';
import type {
OpenWebSocketParams,
OpenWebSocketResult,
} from './open-web-socket';
import type { RequestSnapsParams, RequestSnapsResult } from './request-snaps';
import type {
ResolveInterfaceParams,
Expand All @@ -73,6 +85,10 @@ import type {
ScheduleBackgroundEventParams,
ScheduleBackgroundEventResult,
} from './schedule-background-event';
import type {
SendWebSocketMessageParams,
SendWebSocketMessageResult,
} from './send-web-socket-message';
import type { SetStateParams, SetStateResult } from './set-state';
import type { TrackEventParams, TrackEventResult } from './track-event';
import type {
Expand Down Expand Up @@ -125,6 +141,13 @@ export type SnapMethods = {
snap_resolveInterface: [ResolveInterfaceParams, ResolveInterfaceResult];
snap_setState: [SetStateParams, SetStateResult];
snap_trackEvent: [TrackEventParams, TrackEventResult];
snap_openWebSocket: [OpenWebSocketParams, OpenWebSocketResult];
snap_closeWebSocket: [CloseWebSocketParams, CloseWebSocketResult];
snap_getWebSockets: [GetWebSocketsParams, GetWebSocketsResult];
snap_sendWebSocketMessage: [
SendWebSocketMessageParams,
SendWebSocketMessageResult,
];
wallet_getSnaps: [GetSnapsParams, GetSnapsResult];
wallet_invokeKeyring: [InvokeKeyringParams, InvokeKeyringResult];
wallet_invokeSnap: [InvokeSnapParams, InvokeSnapResult];
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 99.76,
"functions": 99,
"lines": 98.68,
"statements": 97.28
"lines": 98.69,
"statements": 97.29
}
9 changes: 7 additions & 2 deletions packages/snaps-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getErrorMessage } from '@metamask/snaps-sdk';
import {
is,
optional,
Expand All @@ -6,6 +7,7 @@ import {
string,
type,
assert as assertSuperstruct,
StructError,
} from '@metamask/superstruct';
import type { Infer, Struct } from '@metamask/superstruct';
import type { Json } from '@metamask/utils';
Expand Down Expand Up @@ -124,8 +126,11 @@ export const uri = (opts: UriOptions<any> = {}) =>
const UrlStruct = type(opts);
assertSuperstruct(url, UrlStruct);
return true;
} catch {
return `Expected URL, got "${value.toString()}".`;
} catch (error) {
if (error instanceof StructError) {
return getErrorMessage(error);
}
return `Expected URL, got "${value.toString()}"`;
}
});

Expand Down
Loading