@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
78 lines • 3.74 kB
JavaScript
import { endowmentPermissionBuilders, buildSnapEndowmentSpecifications, buildSnapRestrictedMethodSpecifications } from "@metamask/snaps-rpc-methods";
import { DEFAULT_ENDOWMENTS } from "@metamask/snaps-utils";
import { EXCLUDED_SNAP_ENDOWMENTS, EXCLUDED_SNAP_PERMISSIONS } from "./constants.mjs";
import { getGetPreferencesMethodImplementation, getClearSnapStateMethodImplementation, getGetSnapStateMethodImplementation, getUpdateSnapStateMethodImplementation, getShowInAppNotificationImplementation, getShowNativeNotificationImplementation, getCreateInterfaceImplementation, getGetInterfaceImplementation, getRequestUserApprovalImplementation } from "./hooks/index.mjs";
/**
* Get a function which resolves with the specified result.
*
* @param result - The result to return.
* @returns The function implementation.
*/
export function resolve(result) {
return () => result;
}
/**
* 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.
*/
export function asyncResolve(result) {
return async () => result;
}
/**
* 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.
*/
export function getPermissionSpecifications({ controllerMessenger, hooks, runSaga, options, }) {
return {
...buildSnapEndowmentSpecifications(EXCLUDED_SNAP_ENDOWMENTS),
...buildSnapRestrictedMethodSpecifications(EXCLUDED_SNAP_PERMISSIONS, {
// Shared hooks.
...hooks,
// Snaps-specific hooks.
clearSnapState: getClearSnapStateMethodImplementation(runSaga),
getPreferences: getGetPreferencesMethodImplementation(options),
getSnapState: getGetSnapStateMethodImplementation(runSaga),
getUnlockPromise: asyncResolve(true),
// TODO: Allow the user to specify the result of this function.
isOnPhishingList: resolve(false),
maybeUpdatePhishingList: asyncResolve(),
requestUserApproval: getRequestUserApprovalImplementation(runSaga),
showInAppNotification: getShowInAppNotificationImplementation(runSaga),
showNativeNotification: getShowNativeNotificationImplementation(runSaga),
updateSnapState: getUpdateSnapStateMethodImplementation(runSaga),
createInterface: getCreateInterfaceImplementation(controllerMessenger),
getInterface: getGetInterfaceImplementation(controllerMessenger),
}),
};
}
/**
* Get the endowments for the Snap.
*
* @param permissionController - The permission controller.
* @param snapId - The ID of the Snap.
* @returns The endowments for the Snap.
*/
export async function getEndowments(permissionController, snapId) {
const allEndowments = await Object.keys(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([...DEFAULT_ENDOWMENTS, ...allEndowments])];
}
//# sourceMappingURL=specifications.mjs.map