@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
66 lines • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWebSocketsHandler = void 0;
const rpc_errors_1 = require("@metamask/rpc-errors");
const endowments_1 = require("../endowments/index.cjs");
/**
* Get the connected WebSockets for the Snap.
*
* @example
* ```json name="Manifest"
* {
* "initialPermissions": {
* "endowment:network-access": {}
* }
* }
* ```
* ```ts name="Usage"
* const webSockets = await snap.request({ method: 'snap_getWebSockets' });
* console.log(webSockets);
* // Example output:
* // [
* // {
* // id: 'websocket-1',
* // url: 'wss://example.com/socket',
* // protocols: ['protocol1', 'protocol2'],
* // },
* // {
* // id: 'websocket-2',
* // url: 'ws://example.org/endpoint',
* // protocols: [],
* // },
* // ]
* ```
*/
exports.getWebSocketsHandler = {
implementation: getWebSocketsImplementation,
actionNames: [
'PermissionController:hasPermission',
'WebSocketService:getAll',
],
};
/**
* The `snap_getWebSockets` 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.
*/
function getWebSocketsImplementation(req, res, _next, end, _hooks, messenger) {
const { origin } = req;
if (!messenger.call('PermissionController:hasPermission', origin, endowments_1.SnapEndowments.NetworkAccess)) {
return end(rpc_errors_1.providerErrors.unauthorized());
}
try {
res.result = messenger.call('WebSocketService:getAll', origin);
}
catch (error) {
return end(error);
}
return end();
}
//# sourceMappingURL=getWebSockets.cjs.map