@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
106 lines • 3.67 kB
JavaScript
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
import { literal, union } from "@metamask/snaps-sdk";
import { uri } from "@metamask/snaps-utils";
import { create, object, array, string, optional, StructError } from "@metamask/superstruct";
import { SnapEndowments } from "../endowments/index.mjs";
const OpenWebSocketParametersStruct = object({
url: uri({ protocol: union([literal('wss:'), literal('ws:')]) }),
protocols: optional(array(string())),
});
/**
* 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 const openWebSocketHandler = {
implementation: openWebSocketImplementation,
actionNames: ['PermissionController:hasPermission', 'WebSocketService:open'],
};
/**
* 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.
*/
async function openWebSocketImplementation(req, res, _next, end, _hooks, messenger) {
const { params, origin } = req;
if (!messenger.call('PermissionController:hasPermission', origin, SnapEndowments.NetworkAccess)) {
return end(providerErrors.unauthorized());
}
try {
const { url, protocols } = getValidatedParams(params);
res.result = await messenger.call('WebSocketService:open', origin, url, protocols);
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validates the parameters for the snap_openWebSocket method.
*
* @param params - Parameters to validate.
* @returns Validated parameters.
* @throws Throws RPC error if validation fails.
*/
function getValidatedParams(params) {
try {
return create(params, OpenWebSocketParametersStruct);
}
catch (error) {
if (error instanceof StructError) {
throw rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpcErrors.internal();
}
}
//# sourceMappingURL=openWebSocket.mjs.map