@nktkas/hyperliquid
Version:
Unofficial Hyperliquid API SDK for all major JS runtimes, written in TypeScript and provided with tests
44 lines (43 loc) • 1.95 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.signTypedDataWithWindowEthereum = signTypedDataWithWindowEthereum;
exports.isAbstractWindowEthereum = isAbstractWindowEthereum;
/** Signs typed data using `window.ethereum` (EIP-1193) with `eth_signTypedData_v4` (EIP-712). */
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. */
function isAbstractWindowEthereum(client) {
return typeof client === "object" && client !== null &&
"request" in client && typeof client.request === "function" &&
client.request.length >= 1;
}
});