@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
80 lines • 3.49 kB
text/typescript
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
import type { Messenger } from "@metamask/messenger";
import type { PermissionControllerHasPermissionAction } from "@metamask/permission-controller";
import { type OpenWebSocketParams, type OpenWebSocketResult } from "@metamask/snaps-sdk";
import { type InferMatching } from "@metamask/snaps-utils";
import type { PendingJsonRpcResponse } from "@metamask/utils";
import type { JsonRpcRequestWithOrigin, WebSocketServiceOpenAction } from "../types.mjs";
export type OpenWebSocketMethodActions = PermissionControllerHasPermissionAction | WebSocketServiceOpenAction;
declare const OpenWebSocketParametersStruct: import("@metamask/superstruct").Struct<{
url: string;
protocols?: string[] | undefined;
}, {
url: import("@metamask/superstruct").Struct<string, null>;
protocols: import("@metamask/superstruct").Struct<string[] | undefined, import("@metamask/superstruct").Struct<string, null>>;
}>;
export type OpenWebSocketParameters = InferMatching<typeof OpenWebSocketParametersStruct, OpenWebSocketParams>;
/**
* Open a WebSocket connection to the specified URL with optional protocols.
*
* Note: This method is only available to snaps that have the
* [`endowment:network-access`](https://docs.metamask.io/snaps/features/network-access/)
* permission.
*
* @example
* ```json name="Manifest"
* {
* "initialPermissions": {
* "endowment:network-access": {}
* }
* }
* ```
* ```ts name="Usage"
* // Open a connection to a WebSocket server, e.g., in the JSON-RPC handler of
* // the Snap:
* snap.request({
* method: 'snap_openWebSocket',
* params: {
* url: 'wss://example.com/socket',
* protocols: ['protocol1', 'protocol2'], // Optional
* },
* });
*
* // Listen for events from the WebSocket connection in the `onWebSocketEvent`
* // handler of the Snap:
* export const onWebSocketEvent: OnWebSocketEventHandler = async ({ event }) => {
* switch (event.type) {
* case 'open':
* console.log(`WebSocket connection opened with origin ${event.origin}`);
* break;
* case 'message':
* console.log(`WebSocket message received from origin ${event.origin}:`, event.data);
* break;
* case 'close':
* console.log(`WebSocket connection closed with origin ${event.origin}`);
* break;
* case 'error':
* console.error(`WebSocket error from origin ${event.origin}:`, event.error);
* break;
* }
* };
* ```
*/
export declare const openWebSocketHandler: {
implementation: typeof openWebSocketImplementation;
actionNames: ("WebSocketService:open" | "PermissionController:hasPermission")[];
};
/**
* The `snap_openWebSocket` method implementation.
*
* @param req - The JSON-RPC request object.
* @param res - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this function.
* @param end - The `json-rpc-engine` "end" callback.
* @param _hooks - The RPC method hooks. Not used by this function.
* @param messenger - The messenger used to call controller actions.
* @returns Nothing.
*/
declare function openWebSocketImplementation(req: JsonRpcRequestWithOrigin<OpenWebSocketParameters>, res: PendingJsonRpcResponse<OpenWebSocketResult>, _next: unknown, end: JsonRpcEngineEndCallback, _hooks: never, messenger: Messenger<string, OpenWebSocketMethodActions>): Promise<void>;
export {};
//# sourceMappingURL=openWebSocket.d.mts.map