near-ca
Version:
An SDK for controlling Ethereum Accounts from a Near Account.
59 lines (58 loc) • 2.06 kB
JavaScript
import { fromHex, hashMessage, hashTypedData, isHex, keccak256, serializeTransaction, } from "viem";
import { populateTx } from "./transaction";
/**
* Routes signature requests to appropriate handlers based on method type
*
* @param request - The signature request data
* @returns Object containing the EVM message, payload hash, and recovery data
*/
export async function requestRouter(request) {
const { method, chainId, params } = request;
switch (method) {
case "eth_sign": {
const [_, messageHash] = params;
return {
evmMessage: fromHex(messageHash, "string"),
hashToSign: hashMessage({ raw: messageHash }),
};
}
case "personal_sign": {
const [messageHash, _] = params;
return {
evmMessage: fromHex(messageHash, "string"),
hashToSign: hashMessage({ raw: messageHash }),
};
}
case "eth_sendTransaction": {
// We only support one transaction at a time!
let rlpTx;
if (isHex(params)) {
rlpTx = params;
}
else {
const tx = params[0];
const transaction = await populateTx({
to: tx.to,
chainId,
value: fromHex(tx.value || "0x0", "bigint"),
data: tx.data || "0x",
...(tx.gas ? { gas: fromHex(tx.gas, "bigint") } : {}),
}, tx.from);
rlpTx = serializeTransaction(transaction);
}
return {
hashToSign: keccak256(rlpTx),
evmMessage: rlpTx,
};
}
case "eth_signTypedData":
case "eth_signTypedData_v4": {
const [_, dataString] = params;
const typedData = JSON.parse(dataString);
return {
evmMessage: dataString,
hashToSign: hashTypedData(typedData),
};
}
}
}