UNPKG

@metamask/snaps-rpc-methods

Version:
86 lines 3.38 kB
import { rpcErrors } from "@metamask/rpc-errors"; import { HandlerType, WALLET_SNAP_PERMISSION_KEY } from "@metamask/snaps-utils"; import { hasProperty } from "@metamask/utils"; import { getValidatedParams } from "./invokeSnapSugar.mjs"; const hookNames = { hasPermission: true, handleSnapRpcRequest: true, getSnap: true, getAllowedKeyringMethods: true, }; /** * `wallet_invokeKeyring` gets the requester's permitted and installed Snaps. */ export const 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 = 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(WALLET_SNAP_PERMISSION_KEY)) { return end(rpcErrors.invalidRequest({ message: `The snap "${snapId}" is not connected to "${origin}". Please connect before invoking the snap.`, })); } if (!getSnap(snapId)) { return end(rpcErrors.invalidRequest({ message: `The snap "${snapId}" is not installed. Please install it first, before invoking the snap.`, })); } if (!hasProperty(request, 'method') || typeof request.method !== 'string') { return end(rpcErrors.invalidRequest({ message: 'The request must have a method.', })); } const allowedMethods = getAllowedKeyringMethods(); if (!allowedMethods.includes(request.method)) { return end(rpcErrors.invalidRequest({ message: `The origin "${origin}" is not allowed to invoke the method "${request.method}".`, })); } try { res.result = (await handleSnapRpcRequest({ snapId, request, handler: HandlerType.OnKeyringRequest, })); } catch (error) { return end(error); } return end(); } //# sourceMappingURL=invokeKeyring.mjs.map