UNPKG

@finn_gal/patchright-wallet-mock-ts

Version:

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

120 lines 4.86 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.createWallet = createWallet; 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") { const [address, messageJson] = params; const message = JSON.parse(messageJson); return await client.account.signTypedData({ domain: message.domain, types: message.types, primaryType: message.primaryType, message: message.message }); } if (method === "eth_sendTransaction") { const from = (params?.[0]).from; if (from.toLowerCase() !== account.address.toLowerCase()) { throw new Error("Invalid from address"); } return await client.sendTransaction({ to: (params?.[0]).to, data: (params?.[0]).data, value: BigInt((params?.[0]).value ?? 0), // 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, }); }, }; } 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