@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
1 lines • 3.37 kB
Source Map (JSON)
{"version":3,"file":"middleware.mjs","sourceRoot":"","sources":["../../../src/middleware/internal-methods/middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AAGjD,OAAO,EAAE,kBAAkB,EAAE,uBAAmB;AAChD,OAAO,EAAE,iBAAiB,EAAE,uBAAmB;AAC/C,OAAO,EAAE,wBAAwB,EAAE,0BAAsB;AACzD,OAAO,EAAE,uBAAuB,EAAE,6BAAyB;AAC3D,OAAO,EAAE,6BAA6B,EAAE,oCAAgC;AAWxE,MAAM,cAAc,GAAG;IACrB,yDAAyD;IACzD,yBAAyB,EAAE,uBAAuB;IAClD,mBAAmB,EAAE,kBAAkB;IACvC,YAAY,EAAE,kBAAkB;IAChC,WAAW,EAAE,iBAAiB;IAC9B,WAAW,EAAE,wBAAwB;IACrC,0BAA0B,EAAE,6BAA6B;IACzD,wDAAwD;CACzD,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,+BAA+B,CAC7C,KAAqC;IAErC,kDAAkD;IAClD,kEAAkE;IAClE,OAAO,KAAK,UAAU,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;QACjE,MAAM,OAAO,GACX,cAAc,CAAC,OAAO,CAAC,MAAqC,CAAC,CAAC;QAChE,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC;gBACH,kEAAkE;gBAClE,OAAO,MAAM,OAAO,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;YAC5D,CAAC;YAAC,OAAO,KAAU,EAAE,CAAC;gBACpB,QAAQ,CAAC,KAAK,CAAC,CAAC;gBAChB,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { logError } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport { getAccountsHandler } from './accounts';\nimport { getChainIdHandler } from './chain-id';\nimport { getNetworkVersionHandler } from './net-version';\nimport { getProviderStateHandler } from './provider-state';\nimport { getSwitchEthereumChainHandler } from './switch-ethereum-chain';\n\nexport type InternalMethodsMiddlewareHooks = {\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\nconst methodHandlers = {\n /* eslint-disable @typescript-eslint/naming-convention */\n metamask_getProviderState: getProviderStateHandler,\n eth_requestAccounts: getAccountsHandler,\n eth_accounts: getAccountsHandler,\n eth_chainId: getChainIdHandler,\n net_version: getNetworkVersionHandler,\n wallet_switchEthereumChain: getSwitchEthereumChainHandler,\n /* eslint-enable @typescript-eslint/naming-convention */\n};\n\n/**\n * Create a middleware for handling JSON-RPC methods normally handled internally\n * by the MetaMask client.\n *\n * NOTE: This middleware provides all `hooks` to all handlers and should\n * therefore NOT be used outside of the simulation environment. It is intended\n * for testing purposes only.\n *\n * @param hooks - Any hooks used by the middleware handlers.\n * @returns A middleware function.\n */\nexport function createInternalMethodsMiddleware(\n hooks: InternalMethodsMiddlewareHooks,\n): JsonRpcMiddleware<JsonRpcParams, Json> {\n // This should probably use createAsyncMiddleware.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n return async function methodMiddleware(request, response, next, end) {\n const handler =\n methodHandlers[request.method as keyof typeof methodHandlers];\n if (handler) {\n try {\n // Implementations may or may not be async, so we must await them.\n return await handler(request, response, next, end, hooks);\n } catch (error: any) {\n logError(error);\n return end(error);\n }\n }\n\n return next();\n };\n}\n"]}