UNPKG

@johanneskares/wallet-mock

Version:

Mock Web3 Browser wallets, like Metamask, in Playwright tests.

103 lines 4.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createWallet = void 0; const viem_1 = require("viem"); const chains = __importStar(require("viem/chains")); function createWallet(account, transports, defaultChain) { let chain = defaultChain ?? getChain(); return { request: async ({ method, params, }) => { const client = (0, viem_1.createWalletClient)({ account, chain, transport: transports?.[chain.id] ?? (0, viem_1.http)(), }); if (method === "eth_accounts" || method === "eth_requestAccounts") { return await client.getAddresses(); } if (method === "wallet_requestPermissions" || method === "wallet_revokePermissions") { return [{ parentCapability: "eth_accounts" }]; } if (method === "wallet_getPermissions") return []; if (method === "wallet_switchEthereumChain") { chain = getChain((params?.[0]).chainId); return null; } if (method === "personal_sign") { return await client.account.signMessage({ message: { raw: params?.[0], }, }); } if (method === "eth_signTypedData") { throw new Error("eth_signTypedData is not yet supported"); } if (method === "eth_signTypedData_v3") { throw new Error("eth_signTypedData_v4 is not yet supported"); } if (method === "eth_signTypedData_v4") { throw new Error("eth_signTypedData_v4 is not yet supported"); } if (method === "eth_sendTransaction") { const from = (params?.[0]).from; if (from !== account.address) throw new Error("Invalid from address"); return await client.sendTransaction({ to: (params?.[0]).to, data: (params?.[0]).data, value: (params?.[0]).value, // Let viem handle the gas calcutation // gas: (params?.[0] as any).gas ?? (params?.[0] as any).gasLimit, // gasPrice: (params?.[0] as any).gasPrice, // maxFeePerGas: (params?.[0] as any).maxFeePerGas, // maxPriorityFeePerGas: (params?.[0] as any).maxPriorityFeePerGas, }); } return await client.request({ method: method, params: params, }); }, }; } exports.createWallet = createWallet; function getChain(chainIdHex) { if (!chainIdHex) return chains.mainnet; const chainId = (0, viem_1.fromHex)(chainIdHex, "number"); for (const chain of Object.values(chains)) { if ("id" in chain) { if (chain.id === chainId) { return chain; } } } return chains.mainnet; } //# sourceMappingURL=createWallet.js.map