@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
1 lines • 3.34 kB
Source Map (JSON)
{"version":3,"file":"engine.cjs","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":";;;AAAA,+EAA0E;AAE1E,+DAA0D;AAE1D,mEAA0E;AAG1E,mEAAqE;AACrE,qCAA8C;AAC9C,gDAAyD;AAezD;;;;;;;;;;;;;GAaG;AACH,SAAgB,mBAAmB,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,QAAQ,GAAG,qCAAyB,GACT;IAC3B,MAAM,MAAM,GAAG,IAAI,+BAAa,EAAE,CAAC;IACnC,MAAM,CAAC,IAAI,CAAC,IAAA,2BAAoB,EAAC,KAAK,CAAC,CAAC,CAAC;IAEzC,qEAAqE;IACrE,uEAAuE;IACvE,MAAM,CAAC,IAAI,CAAC,IAAA,kDAA+B,EAAC,eAAe,CAAC,CAAC,CAAC;IAC9D,MAAM,CAAC,IAAI,CAAC,IAAA,+CAA2B,EAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC;IAE/D,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CACT,IAAA,+CAAqB,EAAC;QACpB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,MAAM,EAAE,QAAQ;KACjB,CAAC,CACH,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAzBD,kDAyBC","sourcesContent":["import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport type { RestrictedMethodParameters } from '@metamask/permission-controller';\nimport { createSnapsMethodMiddleware } from '@metamask/snaps-rpc-methods';\nimport type { Json } from '@metamask/utils';\n\nimport { createInternalMethodsMiddleware } from './internal-methods';\nimport { createMockMiddleware } from './mock';\nimport { DEFAULT_JSON_RPC_ENDPOINT } from '../constants';\nimport type {\n PermittedMiddlewareHooks,\n RestrictedMiddlewareHooks,\n} from '../simulation';\nimport type { Store } from '../store';\n\nexport type CreateJsonRpcEngineOptions = {\n store: Store;\n restrictedHooks: RestrictedMiddlewareHooks;\n permittedHooks: PermittedMiddlewareHooks;\n permissionMiddleware: JsonRpcMiddleware<RestrictedMethodParameters, Json>;\n endpoint?: string;\n};\n\n/**\n * Create a JSON-RPC engine for use in a simulated environment. This engine\n * should be used to handle all JSON-RPC requests. It is set up to handle\n * requests that would normally be handled internally by the MetaMask client, as\n * well as Snap-specific requests.\n *\n * @param options - The options to use when creating the engine.\n * @param options.store - The Redux store to use.\n * @param options.restrictedHooks - Any hooks used by the middleware handlers.\n * @param options.permittedHooks - Any hooks used by the middleware handlers.\n * @param options.permissionMiddleware - The permission middleware to use.\n * @param options.endpoint - The JSON-RPC endpoint to use for Ethereum requests.\n * @returns A JSON-RPC engine.\n */\nexport function createJsonRpcEngine({\n store,\n restrictedHooks,\n permittedHooks,\n permissionMiddleware,\n endpoint = DEFAULT_JSON_RPC_ENDPOINT,\n}: CreateJsonRpcEngineOptions) {\n const engine = new JsonRpcEngine();\n engine.push(createMockMiddleware(store));\n\n // The hooks here do not match the hooks used by the clients, so this\n // middleware should not be used outside of the simulation environment.\n engine.push(createInternalMethodsMiddleware(restrictedHooks));\n engine.push(createSnapsMethodMiddleware(true, permittedHooks));\n\n engine.push(permissionMiddleware);\n engine.push(\n createFetchMiddleware({\n btoa: globalThis.btoa,\n fetch: globalThis.fetch,\n rpcUrl: endpoint,\n }),\n );\n\n return engine;\n}\n"]}