@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
1 lines • 26.8 kB
Source Map (JSON)
{"version":3,"file":"simulation.mjs","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,kCAAkC;AACtD,OAAO,EAAE,kBAAkB,EAAE,6CAA6C;AAE1E,OAAO,EAAE,0BAA0B,EAAE,sCAAsC;AAE3E,OAAO,EACL,kBAAkB,EAClB,SAAS,EACT,0BAA0B,EAC1B,cAAc,EACf,yCAAyC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,oCAAoC;AAcpE,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAGjD,OAAO,EAAE,QAAQ,EAAE,wBAAwB;AAE3C,OAAO,EAAE,MAAM,EAAE,2BAA2B;AAG5C,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,0BAAsB;AAC7D,OAAO,EAAE,WAAW,EAAE,oBAAgB;AAEtC,OAAO,EAAE,UAAU,EAAE,sBAAkB;AACvC,OAAO,EAAE,eAAe,EAAE,wBAAoB;AAC9C,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,4BAAkB;AACxD,OAAO,EACL,8CAA8C,EAC9C,4CAA4C,EAC5C,+CAA+C,EAC/C,kCAAkC,EAClC,4BAA4B,EAC5B,wBAAwB,EACxB,2BAA2B,EAC3B,2BAA2B,EAC3B,yBAAyB,EACzB,2BAA2B,EAC5B,kCAAwB;AACzB,OAAO,EAAE,gCAAgC,EAAE,8CAA0C;AACrF,OAAO,EAAE,mBAAmB,EAAE,+BAAqB;AAEnD,OAAO,EAAE,UAAU,EAAE,sBAAkB;AAEvC,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,0BAAgB;AAC3D,OAAO,EAAE,wBAAwB,EAAE,4BAAwB;AA0P3D;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAK/B,MAAc,EACd,EACE,gBAAgB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,uBAAuB,CACxB,CAAC,MAAM,EAAE,CAAC;QACX,OAAO,CACL,gBAAgB,EAAE,IAAI,KAAK,qBAAqB,CAAC,OAAO;YACxD,gBAAgB,EAAE,EAAE,KAAK,IAAI,EAAE,EAAE,CAClC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,mBAAmB,CAAC,qBAAqB,CACvC,kCAAkC,EAClC,KAAK,EAAE,GAAW,EAAE,KAAc,EAAE,EAAE;QACpC,MAAM,OAAO,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC,SAAS,EAAE,CAAC;QAElD,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n} from '@metamask/base-controller';\nimport { Messenger } from '@metamask/base-controller';\nimport { createEngineStream } from '@metamask/json-rpc-middleware-stream';\nimport type { CryptographicFunctions } from '@metamask/key-tree';\nimport { PhishingDetectorResultType } from '@metamask/phishing-controller';\nimport type { AbstractExecutionService } from '@metamask/snaps-controllers';\nimport {\n detectSnapLocation,\n fetchSnap,\n NodeThreadExecutionService,\n setupMultiplex,\n} from '@metamask/snaps-controllers/node';\nimport { DIALOG_APPROVAL_TYPES } from '@metamask/snaps-rpc-methods';\nimport type {\n TrackEventParams,\n AuxiliaryFileEncoding,\n Component,\n InterfaceState,\n InterfaceContext,\n SnapId,\n EntropySource,\n TraceRequest,\n EndTraceRequest,\n TraceContext,\n} from '@metamask/snaps-sdk';\nimport type { FetchedSnapFiles, Snap } from '@metamask/snaps-utils';\nimport { logError } from '@metamask/snaps-utils';\nimport type { CaipAssetType, Json } from '@metamask/utils';\nimport type { Duplex } from 'readable-stream';\nimport { pipeline } from 'readable-stream';\nimport type { SagaIterator } from 'redux-saga';\nimport { select } from 'redux-saga/effects';\n\nimport type { RootControllerMessenger } from './controllers';\nimport { getControllers, registerSnap } from './controllers';\nimport { getSnapFile } from './files';\nimport type { SnapHelpers } from './helpers';\nimport { getHelpers } from './helpers';\nimport { resolveWithSaga } from './interface';\nimport { asyncResolve, getEndowments } from './methods';\nimport {\n getPermittedClearSnapStateMethodImplementation,\n getPermittedGetSnapStateMethodImplementation,\n getPermittedUpdateSnapStateMethodImplementation,\n getGetEntropySourcesImplementation,\n getGetMnemonicImplementation,\n getGetSnapImplementation,\n getTrackEventImplementation,\n getTrackErrorImplementation,\n getEndTraceImplementation,\n getStartTraceImplementation,\n} from './methods/hooks';\nimport { getGetMnemonicSeedImplementation } from './methods/hooks/get-mnemonic-seed';\nimport { createJsonRpcEngine } from './middleware';\nimport type { SimulationOptions, SimulationUserOptions } from './options';\nimport { getOptions } from './options';\nimport type { Interface, RunSagaFunction, Store } from './store';\nimport { createStore, getCurrentInterface } from './store';\nimport { addSnapMetadataToAccount } from './utils/account';\n\n/**\n * Options for the execution service, without the options that are shared\n * between all execution services.\n *\n * @template Service - The type of the execution service, i.e., the class that\n * creates the execution service.\n */\nexport type ExecutionServiceOptions<\n Service extends new (...args: any[]) => any,\n> = Omit<\n ConstructorParameters<Service>[0],\n keyof ConstructorParameters<typeof AbstractExecutionService<unknown>>[0]\n>;\n\n/**\n * The options for running a Snap in a simulated environment.\n *\n * @property executionService - The execution service to use.\n * @property executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @property options - The simulation options.\n * @template Service - The type of the execution service.\n */\nexport type InstallSnapOptions<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService<unknown>>,\n> =\n ExecutionServiceOptions<Service> extends Record<string, never>\n ? {\n executionService: Service;\n executionServiceOptions?: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n }\n : {\n executionService: Service;\n executionServiceOptions: ExecutionServiceOptions<Service>;\n options?: SimulationUserOptions;\n };\n\nexport type InstalledSnap = {\n snapId: SnapId;\n store: Store;\n executionService: InstanceType<typeof AbstractExecutionService>;\n controllerMessenger: Messenger<ActionConstraint, EventConstraint>;\n runSaga: RunSagaFunction;\n};\n\nexport type RestrictedMiddlewareHooks = {\n /**\n * A hook that returns the user's secret recovery phrase.\n *\n * @param source - The entropy source to get the mnemonic from.\n * @returns The user's secret recovery phrase.\n */\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * A hook that returns the seed derived from the user's secret recovery phrase.\n *\n * @param source - The entropy source to get the seed from.\n * @returns The seed.\n */\n getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object to fall back to the default cryptographic functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions;\n};\n\nexport type PermittedMiddlewareHooks = {\n /**\n * A hook that gets whether the requesting origin has a given permission.\n *\n * @param permissionName - The name of the permission to check.\n * @returns Whether the origin has the permission.\n */\n hasPermission: (permissionName: string) => boolean;\n\n /**\n * A hook that returns the entropy sources available to the Snap.\n *\n * @returns The entropy sources available to the Snap.\n */\n getEntropySources: () => EntropySource[];\n\n /**\n * A hook that returns a promise that resolves once the extension is unlocked.\n *\n * @param shouldShowUnlockRequest - Whether to show the unlock request.\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * A hook that returns whether the client is locked or not.\n *\n * @returns A boolean flag signaling whether the client is locked.\n */\n getIsLocked: () => boolean;\n\n /**\n * A hook that returns whether the client is active or not.\n *\n * @returns A boolean flag signaling whether the client is opened.\n */\n getIsActive: () => boolean;\n\n /**\n * A hook that returns the Snap's auxiliary file for the given path. This hook\n * is bound to the Snap ID.\n *\n * @param path - The path of the auxiliary file to get.\n * @param encoding - The encoding to use when returning the file.\n * @returns The Snap's auxiliary file for the given path.\n */\n getSnapFile: (\n path: string,\n encoding: AuxiliaryFileEncoding,\n ) => Promise<string | null>;\n\n /**\n * A hook that gets the state of the Snap. This hook is bound to the Snap ID.\n *\n * @param encrypted - Whether to get the encrypted or unencrypted state.\n * @returns The current state of the Snap.\n */\n getSnapState: (encrypted: boolean) => Promise<Record<string, Json>>;\n\n /**\n * A hook that updates the state of the Snap. This hook is bound to the Snap\n * ID.\n *\n * @param newState - The new state.\n * @param encrypted - Whether to update the encrypted or unencrypted state.\n */\n updateSnapState: (\n newState: Record<string, Json>,\n encrypted: boolean,\n ) => Promise<void>;\n\n /**\n * A hook that clears the state of the Snap. This hook is bound to the Snap\n * ID.\n *\n * @param encrypted - Whether to clear the encrypted or unencrypted state.\n */\n clearSnapState: (encrypted: boolean) => Promise<void>;\n\n /**\n * A hook that creates an interface for the Snap. This hook is bound to the\n * Snap ID.\n *\n * @param content - The content of the interface.\n * @param context - The context of the interface.\n * @returns The ID of the created interface.\n */\n createInterface: (\n content: Component,\n context?: InterfaceContext,\n ) => Promise<string>;\n\n /**\n * A hook that updates an interface for the Snap. This hook is bound to the\n * Snap ID.\n *\n * @param id - The ID of the interface to update.\n * @param content - The content of the interface.\n */\n updateInterface: (id: string, content: Component) => Promise<void>;\n\n /**\n * A hook that gets the state of an interface for the Snap. This hook is bound\n * to the Snap ID.\n *\n * @param id - The ID of the interface to get.\n * @returns The state of the interface.\n */\n getInterfaceState: (id: string) => InterfaceState;\n\n /**\n * A hook that gets the context of an interface for the Snap. This hook is\n * bound to the Snap ID.\n *\n * @param id - The ID of the interface to get.\n * @returns The context of the interface.\n */\n getInterfaceContext: (id: string) => InterfaceContext | null;\n\n /**\n * A hook that resolves an interface for the Snap. This hook is bound to the\n * Snap ID.\n *\n * @param id - The ID of the interface to resolve.\n * @param value - The value to resolve the interface with.\n */\n resolveInterface: (id: string, value: Json) => Promise<void>;\n\n /**\n * A hook that gets the Snap's metadata.\n *\n * @param snapId - The ID of the Snap to get.\n * @returns The Snap's metadata.\n */\n getSnap(snapId: string): Snap;\n\n /**\n * A hook that tracks an error.\n *\n * @param error - The error object containing error details and properties.\n */\n trackError(error: Error): void;\n\n /**\n * A hook that tracks an event.\n *\n * @param event - The event object containing event details and properties.\n */\n trackEvent(event: TrackEventParams['event']): void;\n\n /**\n * A hook that starts a performance trace.\n *\n * @param request - The trace request object containing trace details.\n */\n startTrace(request: TraceRequest): TraceContext;\n\n /**\n * A hook that ends a performance trace.\n *\n * @param request - The trace request object containing trace details.\n * @returns The trace data.\n */\n endTrace(request: EndTraceRequest): void;\n};\n\n/**\n * Install a Snap in a simulated environment. This will fetch the Snap files,\n * create a Redux store, set up the controllers and JSON-RPC stack, register the\n * Snap, and run the Snap code in the execution service.\n *\n * @param snapId - The ID of the Snap to install.\n * @param options - The options to use when installing the Snap.\n * @param options.executionService - The execution service to use.\n * @param options.executionServiceOptions - The options to use when creating the\n * execution service, if any. This should only include options specific to the\n * provided execution service.\n * @param options.options - The simulation options.\n * @returns The installed Snap object.\n * @template Service - The type of the execution service.\n */\nexport async function installSnap<\n Service extends new (\n ...args: any[]\n ) => InstanceType<typeof AbstractExecutionService>,\n>(\n snapId: SnapId,\n {\n executionService,\n executionServiceOptions,\n options: rawOptions = {},\n }: Partial<InstallSnapOptions<Service>> = {},\n): Promise<InstalledSnap & SnapHelpers> {\n const options = getOptions(rawOptions);\n\n // Fetch Snap files.\n const location = detectSnapLocation(snapId, {\n allowLocal: true,\n });\n\n const snapFiles = await fetchSnap(snapId, location);\n\n // Create Redux store.\n const { store, runSaga } = createStore(options);\n\n const controllerMessenger = new Messenger<any, any>();\n\n registerActions(controllerMessenger, runSaga, options, snapId);\n\n // Set up controllers and JSON-RPC stack.\n const restrictedHooks = getRestrictedHooks(options);\n const permittedHooks = getPermittedHooks(\n snapId,\n snapFiles,\n controllerMessenger,\n runSaga,\n );\n\n const { subjectMetadataController, permissionController } = getControllers({\n controllerMessenger,\n hooks: restrictedHooks,\n runSaga,\n options,\n });\n\n const engine = createJsonRpcEngine({\n store,\n restrictedHooks,\n permittedHooks,\n permissionMiddleware: permissionController.createPermissionMiddleware({\n origin: snapId,\n }),\n });\n\n // Create execution service.\n const ExecutionService = executionService ?? NodeThreadExecutionService;\n const service = new ExecutionService({\n ...executionServiceOptions,\n messenger: controllerMessenger.getRestricted({\n name: 'ExecutionService',\n allowedActions: [],\n allowedEvents: [],\n }),\n setupSnapProvider: (_snapId: string, rpcStream: Duplex) => {\n const mux = setupMultiplex(rpcStream, 'snapStream');\n const stream = mux.createStream('metamask-provider');\n const providerStream = createEngineStream({ engine });\n\n // Error function is difficult to test, so we ignore it.\n /* istanbul ignore next 2 */\n pipeline(stream, providerStream, stream, (error) => {\n if (error && !error.message?.match('Premature close')) {\n logError(`Provider stream failure.`, error);\n }\n });\n },\n });\n\n // Register the Snap. This sets up the Snap's permissions and subject\n // metadata.\n await registerSnap(snapId, snapFiles.manifest.result, {\n permissionController,\n subjectMetadataController,\n });\n\n // Run the Snap code in the execution service.\n await service.executeSnap({\n snapId,\n sourceCode: snapFiles.sourceCode.toString('utf8'),\n endowments: await getEndowments(permissionController, snapId),\n });\n\n const helpers = getHelpers({\n snapId,\n store,\n controllerMessenger,\n runSaga,\n executionService: service,\n options,\n });\n\n return {\n snapId,\n store,\n executionService: service,\n controllerMessenger,\n runSaga,\n ...helpers,\n };\n}\n\n/**\n * Get the hooks for the simulation.\n *\n * @param options - The simulation options.\n * @returns The hooks for the simulation.\n */\nexport function getRestrictedHooks(\n options: SimulationOptions,\n): RestrictedMiddlewareHooks {\n return {\n getMnemonic: getGetMnemonicImplementation(options.secretRecoveryPhrase),\n getMnemonicSeed: getGetMnemonicSeedImplementation(\n options.secretRecoveryPhrase,\n ),\n getIsLocked: () => false,\n getClientCryptography: () => ({}),\n };\n}\n\n/**\n * Get the permitted hooks for the simulation.\n *\n * @param snapId - The ID of the Snap.\n * @param snapFiles - The fetched Snap files.\n * @param controllerMessenger - The controller messenger.\n * @param runSaga - The run saga function.\n * @returns The permitted hooks for the simulation.\n */\nexport function getPermittedHooks(\n snapId: SnapId,\n snapFiles: FetchedSnapFiles,\n controllerMessenger: RootControllerMessenger,\n runSaga: RunSagaFunction,\n): PermittedMiddlewareHooks {\n return {\n hasPermission: () => true,\n getUnlockPromise: asyncResolve(),\n getIsLocked: () => false,\n getIsActive: () => true,\n\n getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>\n await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),\n\n createInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:createInterface',\n snapId,\n ...args,\n ),\n updateInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:updateInterface',\n snapId,\n ...args,\n ),\n getInterfaceState: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).state,\n getInterfaceContext: (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:getInterface',\n snapId,\n ...args,\n ).context,\n resolveInterface: async (...args) =>\n controllerMessenger.call(\n 'SnapInterfaceController:resolveInterface',\n snapId,\n ...args,\n ),\n\n getEntropySources: getGetEntropySourcesImplementation(),\n getSnapState: getPermittedGetSnapStateMethodImplementation(runSaga),\n updateSnapState: getPermittedUpdateSnapStateMethodImplementation(runSaga),\n clearSnapState: getPermittedClearSnapStateMethodImplementation(runSaga),\n\n getSnap: getGetSnapImplementation(true),\n trackError: getTrackErrorImplementation(runSaga),\n trackEvent: getTrackEventImplementation(runSaga),\n startTrace: getStartTraceImplementation(runSaga),\n endTrace: getEndTraceImplementation(runSaga),\n };\n}\n\n/**\n * Register mocked action handlers.\n *\n * @param controllerMessenger - The controller messenger.\n * @param runSaga - The run saga function.\n * @param options - The simulation options.\n * @param snapId - The ID of the Snap.\n */\nexport function registerActions(\n controllerMessenger: RootControllerMessenger,\n runSaga: RunSagaFunction,\n options: SimulationOptions,\n snapId: SnapId,\n) {\n controllerMessenger.registerActionHandler(\n 'PhishingController:testOrigin',\n () => ({ result: false, type: PhishingDetectorResultType.All }),\n );\n\n controllerMessenger.registerActionHandler(\n 'AccountsController:getAccountByAddress',\n // @ts-expect-error - This is a partial account with only the necessary\n // data used by the interface controller.\n (address) => {\n const matchingAccount = options.accounts.find(\n (account) => address === account.address,\n );\n\n if (!matchingAccount) {\n return undefined;\n }\n\n return addSnapMetadataToAccount(matchingAccount, snapId);\n },\n );\n\n controllerMessenger.registerActionHandler(\n 'AccountsController:getSelectedMultichainAccount',\n // @ts-expect-error - This is a partial account with only the necessary\n // data used by the interface controller.\n () => {\n const selectedAccount = options.accounts.find(\n (account) => account.selected,\n );\n\n if (!selectedAccount) {\n return undefined;\n }\n\n return addSnapMetadataToAccount(selectedAccount, snapId);\n },\n );\n\n controllerMessenger.registerActionHandler(\n 'AccountsController:listMultichainAccounts',\n\n () =>\n // @ts-expect-error - These are partial accounts with only the necessary\n // data used by the interface controller.\n options.accounts.map((account) =>\n addSnapMetadataToAccount(account, snapId),\n ),\n );\n\n controllerMessenger.registerActionHandler(\n 'MultichainAssetsController:getState',\n () => ({\n // @ts-expect-error - These are partial assets with only the\n // necessary data used by the interface controller.\n assetsMetadata: options.assets,\n accountsAssets: options.accounts.reduce<Record<string, CaipAssetType[]>>(\n (acc, account) => {\n acc[account.id] = account.assets ?? [];\n return acc;\n },\n {},\n ),\n }),\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:hasRequest',\n (opts) => {\n /**\n * Get the current interface from the store.\n *\n * @yields Selects the current interface from the store.\n * @returns The current interface.\n */\n function* getCurrentInterfaceSaga(): SagaIterator {\n const currentInterface: Interface = yield select(getCurrentInterface);\n return currentInterface;\n }\n\n const currentInterface: Interface | undefined = runSaga(\n getCurrentInterfaceSaga,\n ).result();\n return (\n currentInterface?.type === DIALOG_APPROVAL_TYPES.default &&\n currentInterface?.id === opts?.id\n );\n },\n );\n\n controllerMessenger.registerActionHandler(\n 'ApprovalController:acceptRequest',\n async (_id: string, value: unknown) => {\n await runSaga(resolveWithSaga, value).toPromise();\n\n return { value };\n },\n );\n}\n"]}