near-ca
Version:
An SDK for controlling Ethereum Accounts from a Near Account.
62 lines (61 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestRouter = requestRouter;
const viem_1 = require("viem");
const transaction_1 = require("./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
*/
async function requestRouter(request) {
const { method, chainId, params } = request;
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),
};
}
}
}