@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
46 lines • 1.91 kB
JavaScript
import { logError } from "@metamask/snaps-utils";
import { getAccountsHandler } from "./accounts.mjs";
import { getChainIdHandler } from "./chain-id.mjs";
import { getNetworkVersionHandler } from "./net-version.mjs";
import { getProviderStateHandler } from "./provider-state.mjs";
import { getSwitchEthereumChainHandler } from "./switch-ethereum-chain.mjs";
const methodHandlers = {
/* eslint-disable @typescript-eslint/naming-convention */
metamask_getProviderState: getProviderStateHandler,
eth_requestAccounts: getAccountsHandler,
eth_accounts: getAccountsHandler,
eth_chainId: getChainIdHandler,
net_version: getNetworkVersionHandler,
wallet_switchEthereumChain: 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.
*/
export 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) {
logError(error);
return end(error);
}
}
return next();
};
}
//# sourceMappingURL=middleware.mjs.map