UNPKG

@metamask/snaps-simulation

Version:

A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment

85 lines 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEndowments = exports.getPermissionSpecifications = exports.asyncResolve = exports.resolve = void 0; const snaps_rpc_methods_1 = require("@metamask/snaps-rpc-methods"); const snaps_utils_1 = require("@metamask/snaps-utils"); const constants_1 = require("./constants.cjs"); const hooks_1 = require("./hooks/index.cjs"); /** * Get a function which resolves with the specified result. * * @param result - The result to return. * @returns The function implementation. */ function resolve(result) { return () => result; } exports.resolve = resolve; /** * Get a function which resolves with the specified result. * * @param result - The result to return. If not specified, the function will * resolve with `undefined`. * @returns The function implementation. */ function asyncResolve(result) { return async () => result; } exports.asyncResolve = asyncResolve; /** * Get the permission specifications for the Snap. * * @param options - The options. * @param options.controllerMessenger - The controller messenger. * @param options.hooks - The hooks. * @param options.runSaga - The function to run a saga outside the usual Redux * flow. * @param options.options - The simulation options. * @returns The permission specifications for the Snap. */ function getPermissionSpecifications({ controllerMessenger, hooks, runSaga, options, }) { return { ...(0, snaps_rpc_methods_1.buildSnapEndowmentSpecifications)(constants_1.EXCLUDED_SNAP_ENDOWMENTS), ...(0, snaps_rpc_methods_1.buildSnapRestrictedMethodSpecifications)(constants_1.EXCLUDED_SNAP_PERMISSIONS, { // Shared hooks. ...hooks, // Snaps-specific hooks. clearSnapState: (0, hooks_1.getClearSnapStateMethodImplementation)(runSaga), getPreferences: (0, hooks_1.getGetPreferencesMethodImplementation)(options), getSnapState: (0, hooks_1.getGetSnapStateMethodImplementation)(runSaga), getUnlockPromise: asyncResolve(true), // TODO: Allow the user to specify the result of this function. isOnPhishingList: resolve(false), maybeUpdatePhishingList: asyncResolve(), requestUserApproval: (0, hooks_1.getRequestUserApprovalImplementation)(runSaga), showInAppNotification: (0, hooks_1.getShowInAppNotificationImplementation)(runSaga), showNativeNotification: (0, hooks_1.getShowNativeNotificationImplementation)(runSaga), updateSnapState: (0, hooks_1.getUpdateSnapStateMethodImplementation)(runSaga), createInterface: (0, hooks_1.getCreateInterfaceImplementation)(controllerMessenger), getInterface: (0, hooks_1.getGetInterfaceImplementation)(controllerMessenger), }), }; } exports.getPermissionSpecifications = getPermissionSpecifications; /** * Get the endowments for the Snap. * * @param permissionController - The permission controller. * @param snapId - The ID of the Snap. * @returns The endowments for the Snap. */ async function getEndowments(permissionController, snapId) { const allEndowments = await Object.keys(snaps_rpc_methods_1.endowmentPermissionBuilders).reduce(async (promise, permissionName) => { const accumulator = await promise; if (permissionController.hasPermission(snapId, permissionName)) { const endowments = await permissionController.getEndowments(snapId, permissionName); if (endowments) { return accumulator.concat(endowments); } } return accumulator; }, Promise.resolve([])); return [...new Set([...snaps_utils_1.DEFAULT_ENDOWMENTS, ...allEndowments])]; } exports.getEndowments = getEndowments; //# sourceMappingURL=specifications.cjs.map