UNPKG

@metamask/snaps-simulation

Version:

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

1 lines 3.44 kB
{"version":3,"file":"engine.mjs","sourceRoot":"","sources":["../../src/middleware/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,0CAA0C;AAE1E,OAAO,EAAE,aAAa,EAAE,kCAAkC;AAE1D,OAAO,EAAE,2BAA2B,EAAE,oCAAoC;AAG1E,OAAO,EAAE,+BAA+B,EAAE,qCAA2B;AACrE,OAAO,EAAE,oBAAoB,EAAE,mBAAe;AAC9C,OAAO,EAAE,yBAAyB,EAAE,yBAAqB;AAezD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,QAAQ,GAAG,yBAAyB,GACT;IAC3B,MAAM,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;IACnC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IAEzC,qEAAqE;IACrE,uEAAuE;IACvE,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,eAAe,CAAC,CAAC,CAAC;IAC9D,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC;IAE/D,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAClC,MAAM,CAAC,IAAI,CACT,qBAAqB,CAAC;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","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"]}