UNPKG

near-ca-test

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

65 lines (64 loc) 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.requestRouter = requestRouter; const viem_1 = require("viem"); const transaction_1 = require("./transaction"); /** * Handles routing of signature requests based on the provided method, chain ID, and parameters. * * @async * @function requestRouter * @param {SignRequestData} params - An object containing the method, chain ID, and request parameters. * @returns {Promise<NearEncodedSignRequest>} * - Returns a promise that resolves to an object containing the Ethereum Virtual Machine (EVM) message, * the payload (hashed data), and recovery data needed for reconstructing the signature request. */ async function requestRouter({ method, chainId, params, }) { switch (method) { case "eth_sign": { const [_, messageHash] = params; return { evmMessage: (0, viem_1.fromHex)(messageHash, "string"), hashToSign: (0, viem_1.hashMessage)({ raw: messageHash }), }; } case "personal_sign": { const [messageHash, _] = params; return { evmMessage: (0, viem_1.fromHex)(messageHash, "string"), hashToSign: (0, viem_1.hashMessage)({ raw: messageHash }), }; } case "eth_sendTransaction": { // We only support one transaction at a time! let rlpTx; if ((0, viem_1.isHex)(params)) { rlpTx = params; } else { const tx = params[0]; const transaction = await (0, transaction_1.populateTx)({ to: tx.to, chainId, value: (0, viem_1.fromHex)(tx.value || "0x0", "bigint"), data: tx.data || "0x", ...(tx.gas ? { gas: (0, viem_1.fromHex)(tx.gas, "bigint") } : {}), }, tx.from); rlpTx = (0, viem_1.serializeTransaction)(transaction); } return { hashToSign: (0, viem_1.keccak256)(rlpTx), evmMessage: rlpTx, }; } case "eth_signTypedData": case "eth_signTypedData_v4": { const [_, dataString] = params; const typedData = JSON.parse(dataString); return { evmMessage: dataString, hashToSign: (0, viem_1.hashTypedData)(typedData), }; } } }