@nktkas/hyperliquid
Version:
Unofficial Hyperliquid API SDK for all major JS runtimes, written in TypeScript and provided with tests
30 lines (29 loc) • 1.27 kB
JavaScript
/** Signs typed data using `window.ethereum` (EIP-1193) with `eth_signTypedData_v4` (EIP-712). */
export async function signTypedDataWithWindowEthereum(ethereum, domain, types, message) {
const accounts = await ethereum.request({ method: "eth_requestAccounts", params: [] });
if (!Array.isArray(accounts) || accounts.length === 0) {
throw new Error("No Ethereum accounts available");
}
const from = accounts[0];
const dataToSign = JSON.stringify({
domain,
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
...types,
},
primaryType: Object.keys(types)[0],
message,
});
return await ethereum.request({ method: "eth_signTypedData_v4", params: [from, dataToSign] });
}
/** Checks if the given value is an abstract `window.ethereum` object. */
export function isAbstractWindowEthereum(client) {
return typeof client === "object" && client !== null &&
"request" in client && typeof client.request === "function" &&
client.request.length >= 1;
}