UNPKG

@bozhkovatanas/wallet-mock

Version:

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

172 lines 7.55 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")); // @ts-expect-error Polyfill method // eslint-disable-next-line no-extend-native BigInt.prototype.toJSON = function () { return this.toString(); }; function createWallet(account, transports) { let chainId; let localAccount = account; return { request: async ({ method, params, }) => { try { let chain = getChain(chainId); const client = (0, viem_1.createWalletClient)({ account, chain: chain, transport: transports.get(chain.id) ?? (0, viem_1.http)(), }).extend(viem_1.publicActions); 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_switchEthereumChain") { chainId = (params?.[0]).chainId; return null; } if (method === "personal_sign") { if (!client.account.signMessage) throw new Error("Method `personal_sign` not supported by account"); return await client.account.signMessage({ message: { raw: params?.[0], }, }); } if (method === "eth_chainId") { const chainIdResult = chainId ?? (0, viem_1.toHex)(1); console.log("Returning: eth_chainId", chainIdResult); return chainIdResult; } if (method === "eth_sendRawTransaction") { console.log("eth_sendRawTransaction", params); return await client.sendRawTransaction({ serializedTransaction: params?.[0], }); } if (method === "eth_signTypedData_v4" || method === "eth_signTypedData" || method === "eth_signTypedData_v3") { if (!client.account.signTypedData) throw new Error("Method `eth_signTypedData` not supported by account"); const from = params?.[0]; if (from !== localAccount.address) throw new Error("Invalid from address"); const { domain, types, primaryType, message } = JSON.parse(params?.[1]); const parsedAmount = message.amount && BigInt(message.amount); if (parsedAmount) { message.amount = parsedAmount; } const domainChainId = getChain(chainId).id; console.log("Domain Chain ID", domainChainId); domain.chainId = domainChainId; const signedTypeDataParams = { domain: { ...domain, chainId: domainChainId, }, types, primaryType, message, }; console.log(signedTypeDataParams); const signature = await localAccount.signTypedData(signedTypeDataParams); // TODO: Add verifyTypedData and recoverTypedDataAddress - at the moment they break because params are not iterable. // const valid = await verifyTypedData({ // address: localAccount.address, // domain, // types, // primaryType: 'Mail', // message, // signature, // }); // console.log(`Signature valid: ${valid}`); // const recoveredAddress = await recoverTypedDataAddress({ // domain: domain, // types: types, // primaryType: 'Mail', // message: message, // signature, // }); // console.log(`Recovered Address: ${recoveredAddress}`); return signature; } if (method === "eth_sendTransaction") { const from = (params?.[0]).from; if (from !== account.address) throw new Error("Invalid from address"); const { to, data } = params?.[0]; let { value, maxFeePerGas, maxPriorityFeePerGas, gas, gasPrice } = params?.[0]; value = value && BigInt(value); gas = gas && BigInt(gas); gasPrice = gasPrice && BigInt(gasPrice); maxFeePerGas = maxFeePerGas && BigInt(maxFeePerGas); maxPriorityFeePerGas = maxPriorityFeePerGas && BigInt(maxPriorityFeePerGas); return await client.sendTransaction({ to, value, data, gas, gasPrice, maxFeePerGas, maxPriorityFeePerGas, }); } return await client.request({ method: method, params: params, }); } catch (error) { console.error("Error within Mock Wallet:", error); return null; } }, }; } 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