UNPKG

@metamask/snaps-rpc-methods

Version:
89 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.invokeKeyringHandler = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const snaps_utils_1 = require("@metamask/snaps-utils"); const utils_1 = require("@metamask/utils"); const invokeSnapSugar_1 = require("./invokeSnapSugar.cjs"); const hookNames = { hasPermission: true, handleSnapRpcRequest: true, getSnap: true, getAllowedKeyringMethods: true, }; /** * `wallet_invokeKeyring` gets the requester's permitted and installed Snaps. */ exports.invokeKeyringHandler = { methodNames: ['wallet_invokeKeyring'], implementation: invokeKeyringImplementation, hookNames, }; /** * The `wallet_invokeKeyring` method implementation. * Invokes onKeyringRequest if the snap requested is installed and connected to the dapp. * * @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. * @param hooks.handleSnapRpcRequest - Invokes a snap with a given RPC request. * @param hooks.hasPermission - Checks whether a given origin has a given permission. * @param hooks.getSnap - Gets information about a given snap. * @param hooks.getAllowedKeyringMethods - Get the list of allowed Keyring * methods for a given origin. * @returns Nothing. */ async function invokeKeyringImplementation(req, // `InvokeKeyringResult` is an alias for `Json` (which is the default type // argument for `PendingJsonRpcResponse`), but that may not be the case in the // future. We use `InvokeKeyringResult` here to make it clear that this is the // expected type of the result. // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments res, _next, end, { handleSnapRpcRequest, hasPermission, getSnap, getAllowedKeyringMethods, }) { let params; try { params = (0, invokeSnapSugar_1.getValidatedParams)(req.params); } catch (error) { return end(error); } // We expect the MM middleware stack to always add the origin to requests const { origin } = req; const { snapId, request } = params; if (!origin || !hasPermission(snaps_utils_1.WALLET_SNAP_PERMISSION_KEY)) { return end(rpc_errors_1.rpcErrors.invalidRequest({ message: `The snap "${snapId}" is not connected to "${origin}". Please connect before invoking the snap.`, })); } if (!getSnap(snapId)) { return end(rpc_errors_1.rpcErrors.invalidRequest({ message: `The snap "${snapId}" is not installed. Please install it first, before invoking the snap.`, })); } if (!(0, utils_1.hasProperty)(request, 'method') || typeof request.method !== 'string') { return end(rpc_errors_1.rpcErrors.invalidRequest({ message: 'The request must have a method.', })); } const allowedMethods = getAllowedKeyringMethods(); if (!allowedMethods.includes(request.method)) { return end(rpc_errors_1.rpcErrors.invalidRequest({ message: `The origin "${origin}" is not allowed to invoke the method "${request.method}".`, })); } try { res.result = (await handleSnapRpcRequest({ snapId, request, handler: snaps_utils_1.HandlerType.OnKeyringRequest, })); } catch (error) { return end(error); } return end(); } //# sourceMappingURL=invokeKeyring.cjs.map