near-ca-test
Version:
An SDK for controlling Ethereum Accounts from a Near Account.
83 lines (82 loc) • 2.83 kB
JavaScript
import { isAddress, isHex, parseTransaction, serializeTransaction, } from "viem";
export function isSignMethod(method) {
return (typeof method === "string" &&
[
"eth_sign",
"personal_sign",
"eth_sendTransaction",
"eth_signTypedData",
"eth_signTypedData_v4",
].includes(method));
}
export 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" ||
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" && isAddress(value)));
case "salt":
return typeof value === "undefined" || typeof value === "string";
default:
return false; // Reject unknown properties
}
});
};
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"));
});
};
export 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 &&
isTypedDataDomain(candidate.domain) &&
isTypedMessageTypes(candidate.types) &&
typeof candidate.message === "object" &&
candidate.message !== null &&
typeof candidate.primaryType === "string");
};
// Cheeky attempt to serialize. return true if successful!
export function isTransactionSerializable(data) {
try {
serializeTransaction(data);
return true;
}
catch (error) {
return false;
}
}
export function isRlpHex(data) {
try {
parseTransaction(data);
return true;
}
catch (error) {
return false;
}
}