diff --git a/packages/snaps-rpc-methods/src/permitted/openWebSocket.test.ts b/packages/snaps-rpc-methods/src/permitted/openWebSocket.test.ts index 8f38680e12..b95d2d89c0 100644 --- a/packages/snaps-rpc-methods/src/permitted/openWebSocket.test.ts +++ b/packages/snaps-rpc-methods/src/permitted/openWebSocket.test.ts @@ -88,7 +88,7 @@ describe('snap_openWebSocket', () => { id: 1, method: 'snap_openWebSocket', params: { - url: 'ws://metamask.io', + url: 'http://metamask.io', }, }); @@ -96,7 +96,7 @@ describe('snap_openWebSocket', () => { 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, diff --git a/packages/snaps-rpc-methods/src/permitted/openWebSocket.ts b/packages/snaps-rpc-methods/src/permitted/openWebSocket.ts index 8b5fbf3e6b..e76a4b96f9 100644 --- a/packages/snaps-rpc-methods/src/permitted/openWebSocket.ts +++ b/packages/snaps-rpc-methods/src/permitted/openWebSocket.ts @@ -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, @@ -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())), }); diff --git a/packages/snaps-sdk/src/types/methods/methods.ts b/packages/snaps-sdk/src/types/methods/methods.ts index ff49954dd2..ab2c10d018 100644 --- a/packages/snaps-sdk/src/types/methods/methods.ts +++ b/packages/snaps-sdk/src/types/methods/methods.ts @@ -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, @@ -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, @@ -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, @@ -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 { @@ -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]; diff --git a/packages/snaps-utils/coverage.json b/packages/snaps-utils/coverage.json index 00e4552dce..e800862e07 100644 --- a/packages/snaps-utils/coverage.json +++ b/packages/snaps-utils/coverage.json @@ -1,6 +1,6 @@ { "branches": 99.76, "functions": 99, - "lines": 98.68, - "statements": 97.28 + "lines": 98.69, + "statements": 97.29 } diff --git a/packages/snaps-utils/src/types.ts b/packages/snaps-utils/src/types.ts index ffea783b83..f5337ab018 100644 --- a/packages/snaps-utils/src/types.ts +++ b/packages/snaps-utils/src/types.ts @@ -1,3 +1,4 @@ +import { getErrorMessage } from '@metamask/snaps-sdk'; import { is, optional, @@ -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'; @@ -124,8 +126,11 @@ export const uri = (opts: UriOptions = {}) => 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()}"`; } });