@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
1 lines • 6.56 kB
Source Map (JSON)
{"version":3,"file":"specifications.mjs","sourceRoot":"","sources":["../../src/methods/specifications.ts"],"names":[],"mappings":"AACA,OAAO,EACL,2BAA2B,EAC3B,gCAAgC,EAChC,uCAAuC,EACxC,oCAAoC;AAErC,OAAO,EAAE,kBAAkB,EAAE,8BAA8B;AAE3D,OAAO,EACL,wBAAwB,EACxB,yBAAyB,EAC1B,wBAAoB;AACrB,OAAO,EACL,qCAAqC,EACrC,qCAAqC,EACrC,mCAAmC,EACnC,sCAAsC,EACtC,sCAAsC,EACtC,uCAAuC,EACvC,gCAAgC,EAChC,6BAA6B,EAC7B,oCAAoC,EACrC,0BAAgB;AAqBjB;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,MAAe;IACrC,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC;AACtB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAO,MAAa;IAC9C,OAAO,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,2BAA2B,CAAC,EAC1C,mBAAmB,EACnB,KAAK,EACL,OAAO,EACP,OAAO,GAC4B;IACnC,OAAO;QACL,GAAG,gCAAgC,CAAC,wBAAwB,CAAC;QAC7D,GAAG,uCAAuC,CAAC,yBAAyB,EAAE;YACpE,gBAAgB;YAChB,GAAG,KAAK;YAER,wBAAwB;YACxB,cAAc,EAAE,qCAAqC,CAAC,OAAO,CAAC;YAC9D,cAAc,EAAE,qCAAqC,CAAC,OAAO,CAAC;YAC9D,YAAY,EAAE,mCAAmC,CAAC,OAAO,CAAC;YAC1D,gBAAgB,EAAE,YAAY,CAAC,IAAI,CAAC;YAEpC,+DAA+D;YAC/D,gBAAgB,EAAE,OAAO,CAAC,KAAK,CAAC;YAEhC,uBAAuB,EAAE,YAAY,EAAE;YACvC,mBAAmB,EAAE,oCAAoC,CAAC,OAAO,CAAC;YAClE,qBAAqB,EAAE,sCAAsC,CAAC,OAAO,CAAC;YACtE,sBAAsB,EAAE,uCAAuC,CAAC,OAAO,CAAC;YACxE,eAAe,EAAE,sCAAsC,CAAC,OAAO,CAAC;YAChE,eAAe,EAAE,gCAAgC,CAAC,mBAAmB,CAAC;YACtE,YAAY,EAAE,6BAA6B,CAAC,mBAAmB,CAAC;SACjE,CAAC;KACH,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,oBAAiD,EACjD,MAAc;IAEd,MAAM,aAAa,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC,MAAM,CAEzE,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,EAAE;QAClC,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC;QAClC,IAAI,oBAAoB,CAAC,aAAa,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE,CAAC;YAC/D,MAAM,UAAU,GAAG,MAAM,oBAAoB,CAAC,aAAa,CACzD,MAAM,EACN,cAAc,CACf,CAAC;YAEF,IAAI,UAAU,EAAE,CAAC;gBACf,OAAO,WAAW,CAAC,MAAM,CAAC,UAAsB,CAAC,CAAC;YACpD,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;IAExB,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,kBAAkB,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import type { GenericPermissionController } from '@metamask/permission-controller';\nimport {\n endowmentPermissionBuilders,\n buildSnapEndowmentSpecifications,\n buildSnapRestrictedMethodSpecifications,\n} from '@metamask/snaps-rpc-methods';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { DEFAULT_ENDOWMENTS } from '@metamask/snaps-utils';\n\nimport {\n EXCLUDED_SNAP_ENDOWMENTS,\n EXCLUDED_SNAP_PERMISSIONS,\n} from './constants';\nimport {\n getGetPreferencesMethodImplementation,\n getClearSnapStateMethodImplementation,\n getGetSnapStateMethodImplementation,\n getUpdateSnapStateMethodImplementation,\n getShowInAppNotificationImplementation,\n getShowNativeNotificationImplementation,\n getCreateInterfaceImplementation,\n getGetInterfaceImplementation,\n getRequestUserApprovalImplementation,\n} from './hooks';\nimport type { RootControllerMessenger } from '../controllers';\nimport type { SimulationOptions } from '../options';\nimport type { RunSagaFunction } from '../store';\n\nexport type PermissionSpecificationsHooks = {\n /**\n * A hook that returns the user's secret recovery phrase.\n *\n * @returns The user's secret recovery phrase.\n */\n getMnemonic: () => Promise<Uint8Array>;\n};\n\nexport type GetPermissionSpecificationsOptions = {\n controllerMessenger: RootControllerMessenger;\n hooks: PermissionSpecificationsHooks;\n runSaga: RunSagaFunction;\n options: SimulationOptions;\n};\n\n/**\n * Get a function which resolves with the specified result.\n *\n * @param result - The result to return.\n * @returns The function implementation.\n */\nexport function resolve(result: unknown) {\n return () => result;\n}\n\n/**\n * Get a function which resolves with the specified result.\n *\n * @param result - The result to return. If not specified, the function will\n * resolve with `undefined`.\n * @returns The function implementation.\n */\nexport function asyncResolve<Type>(result?: Type) {\n return async () => result;\n}\n\n/**\n * Get the permission specifications for the Snap.\n *\n * @param options - The options.\n * @param options.controllerMessenger - The controller messenger.\n * @param options.hooks - The hooks.\n * @param options.runSaga - The function to run a saga outside the usual Redux\n * flow.\n * @param options.options - The simulation options.\n * @returns The permission specifications for the Snap.\n */\nexport function getPermissionSpecifications({\n controllerMessenger,\n hooks,\n runSaga,\n options,\n}: GetPermissionSpecificationsOptions) {\n return {\n ...buildSnapEndowmentSpecifications(EXCLUDED_SNAP_ENDOWMENTS),\n ...buildSnapRestrictedMethodSpecifications(EXCLUDED_SNAP_PERMISSIONS, {\n // Shared hooks.\n ...hooks,\n\n // Snaps-specific hooks.\n clearSnapState: getClearSnapStateMethodImplementation(runSaga),\n getPreferences: getGetPreferencesMethodImplementation(options),\n getSnapState: getGetSnapStateMethodImplementation(runSaga),\n getUnlockPromise: asyncResolve(true),\n\n // TODO: Allow the user to specify the result of this function.\n isOnPhishingList: resolve(false),\n\n maybeUpdatePhishingList: asyncResolve(),\n requestUserApproval: getRequestUserApprovalImplementation(runSaga),\n showInAppNotification: getShowInAppNotificationImplementation(runSaga),\n showNativeNotification: getShowNativeNotificationImplementation(runSaga),\n updateSnapState: getUpdateSnapStateMethodImplementation(runSaga),\n createInterface: getCreateInterfaceImplementation(controllerMessenger),\n getInterface: getGetInterfaceImplementation(controllerMessenger),\n }),\n };\n}\n\n/**\n * Get the endowments for the Snap.\n *\n * @param permissionController - The permission controller.\n * @param snapId - The ID of the Snap.\n * @returns The endowments for the Snap.\n */\nexport async function getEndowments(\n permissionController: GenericPermissionController,\n snapId: SnapId,\n) {\n const allEndowments = await Object.keys(endowmentPermissionBuilders).reduce<\n Promise<string[]>\n >(async (promise, permissionName) => {\n const accumulator = await promise;\n if (permissionController.hasPermission(snapId, permissionName)) {\n const endowments = await permissionController.getEndowments(\n snapId,\n permissionName,\n );\n\n if (endowments) {\n return accumulator.concat(endowments as string[]);\n }\n }\n\n return accumulator;\n }, Promise.resolve([]));\n\n return [...new Set([...DEFAULT_ENDOWMENTS, ...allEndowments])];\n}\n"]}