UNPKG

@metamask/snaps-simulation

Version:

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

50 lines 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createInternalMethodsMiddleware = void 0; const snaps_utils_1 = require("@metamask/snaps-utils"); const accounts_1 = require("./accounts.cjs"); const chain_id_1 = require("./chain-id.cjs"); const net_version_1 = require("./net-version.cjs"); const provider_state_1 = require("./provider-state.cjs"); const switch_ethereum_chain_1 = require("./switch-ethereum-chain.cjs"); const methodHandlers = { /* eslint-disable @typescript-eslint/naming-convention */ metamask_getProviderState: provider_state_1.getProviderStateHandler, eth_requestAccounts: accounts_1.getAccountsHandler, eth_accounts: accounts_1.getAccountsHandler, eth_chainId: chain_id_1.getChainIdHandler, net_version: net_version_1.getNetworkVersionHandler, wallet_switchEthereumChain: switch_ethereum_chain_1.getSwitchEthereumChainHandler, /* eslint-enable @typescript-eslint/naming-convention */ }; /** * Create a middleware for handling JSON-RPC methods normally handled internally * by the MetaMask client. * * NOTE: This middleware provides all `hooks` to all handlers and should * therefore NOT be used outside of the simulation environment. It is intended * for testing purposes only. * * @param hooks - Any hooks used by the middleware handlers. * @returns A middleware function. */ function createInternalMethodsMiddleware(hooks) { // This should probably use createAsyncMiddleware. // eslint-disable-next-line @typescript-eslint/no-misused-promises return async function methodMiddleware(request, response, next, end) { const handler = methodHandlers[request.method]; if (handler) { try { // Implementations may or may not be async, so we must await them. return await handler(request, response, next, end, hooks); } catch (error) { (0, snaps_utils_1.logError)(error); return end(error); } } return next(); }; } exports.createInternalMethodsMiddleware = createInternalMethodsMiddleware; //# sourceMappingURL=middleware.cjs.map