UNPKG

near-ca-test

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

91 lines (90 loc) 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isEIP712TypedData = exports.isTypedDataDomain = void 0; exports.isSignMethod = isSignMethod; exports.isTransactionSerializable = isTransactionSerializable; exports.isRlpHex = isRlpHex; const viem_1 = require("viem"); function isSignMethod(method) { return (typeof method === "string" && [ "eth_sign", "personal_sign", "eth_sendTransaction", "eth_signTypedData", "eth_signTypedData_v4", ].includes(method)); } const isTypedDataDomain = (domain) => { if (typeof domain !== "object" || domain === null) return false; const candidate = domain; // Check that all properties, if present, are of the correct type return Object.entries(candidate).every(([key, value]) => { switch (key) { case "chainId": return (typeof value === "undefined" || typeof value === "number" || (0, viem_1.isHex)(value) || (typeof value === "string" && typeof parseInt(value) === "number")); case "name": case "version": return typeof value === "undefined" || typeof value === "string"; case "verifyingContract": return (typeof value === "undefined" || (typeof value === "string" && (0, viem_1.isAddress)(value))); case "salt": return typeof value === "undefined" || typeof value === "string"; default: return false; // Reject unknown properties } }); }; exports.isTypedDataDomain = isTypedDataDomain; const isTypedMessageTypes = (types) => { if (typeof types !== "object" || types === null) return false; return Object.entries(types).every(([_, value]) => { return (Array.isArray(value) && value.every((item) => typeof item === "object" && item !== null && "name" in item && "type" in item && typeof item.name === "string" && typeof item.type === "string")); }); }; const isEIP712TypedData = (obj) => { if (typeof obj !== "object" || obj === null) return false; const candidate = obj; return ("domain" in candidate && "types" in candidate && "message" in candidate && "primaryType" in candidate && (0, exports.isTypedDataDomain)(candidate.domain) && isTypedMessageTypes(candidate.types) && typeof candidate.message === "object" && candidate.message !== null && typeof candidate.primaryType === "string"); }; exports.isEIP712TypedData = isEIP712TypedData; // Cheeky attempt to serialize. return true if successful! function isTransactionSerializable(data) { try { (0, viem_1.serializeTransaction)(data); return true; } catch (error) { return false; } } function isRlpHex(data) { try { (0, viem_1.parseTransaction)(data); return true; } catch (error) { return false; } }