near-safe
Version:
An SDK for controlling Ethereum Smart Accounts via ERC4337 from a Near Account.
84 lines (83 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeTxData = decodeTxData;
exports.determineBroadcastTarget = determineBroadcastTarget;
const near_ca_1 = require("near-ca");
const viem_1 = require("viem");
const types_1 = require("../types");
const util_1 = require("./util");
/**
* Decodes transaction data for a given EVM transaction and extracts relevant details.
*
* @param {EvmTransactionData} data - The raw transaction data to be decoded.
* @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
*/
function decodeTxData({ evmMessage, chainId, }) {
const data = evmMessage;
if ((0, near_ca_1.isRlpHex)(evmMessage)) {
return (0, util_1.decodeRlpHex)(chainId, evmMessage);
}
if ((0, near_ca_1.isTransactionSerializable)(data)) {
return (0, util_1.decodeTransactionSerializable)(chainId, data);
}
const parsedTypedData = (0, types_1.parseEip712TypedData)(data);
if (parsedTypedData) {
return (0, util_1.decodeTypedData)(chainId, parsedTypedData);
}
const userOp = (0, types_1.parseUserOperation)(data);
if (userOp) {
return (0, util_1.decodeUserOperation)(chainId, userOp);
}
// At this point we are certain that the data is a string.
// Typescript would disagree here because of the EIP712TypedData possibility that remains.
// However this is captured (indirectly) by parseEip712TypedData above.
// We check now if its a string and return a reasonable default (for the case of a raw message).
if (typeof data === "string") {
return {
chainId,
costEstimate: "0",
transactions: [],
message: data,
};
}
// Otherwise we have no idea what the data is and we throw.
console.warn("Unrecognized txData format,", chainId, data);
throw new Error(`decodeTxData: Invalid or unsupported message format ${data}`);
}
/**
* Determines where and how an EVM message should be broadcast
* @param evmMessage - The message to be analyzed
* @returns Information about how to broadcast the message, or null if invalid
*/
function determineBroadcastTarget(evmMessage) {
// Case 1: User Operation
if (typeof evmMessage === "string") {
try {
const parsed = (0, types_1.parseUserOperation)(evmMessage);
if (parsed) {
return {
type: "bundler",
userOp: parsed,
};
}
}
catch (error) {
console.warn("Failed to parse potential UserOperation:", error);
}
}
// Case 2: RLP Encoded EVM transaction
if ((0, near_ca_1.isRlpHex)(evmMessage)) {
return {
type: "evm",
transaction: (0, viem_1.parseTransaction)(evmMessage),
};
}
// Case 3: Serializable Transaction
if ((0, near_ca_1.isTransactionSerializable)(evmMessage)) {
return {
type: "evm",
transaction: evmMessage,
};
}
return null;
}