df-hyperliquid-core 0.2.6

Hyperliquid SDK for Rust with full API parity: info, exchange, websockets, HIP-3, HIP-4, HyperEVM and CoreWriter. Exact decimals, the fastest websocket parsing of the SDKs benchmarked, the fastest order signing with the secp256k1 feature, native and wasm.
Documentation
//! The domain a struct is signed under: the exchange's for an L1 action's phantom agent, or the
//! signature chain's for a user-signed action.

use crate::client::network::{L1_CHAIN_ID, Network};
use crate::utils::address::Address;
use crate::utils::json::Json;
use crate::utils::sign::eip712::word::{address_word, word};
use crate::utils::sign::keccak::{Keccak256, keccak256};

/// The EIP-712 type string of a domain, hashed as the first word of [`Domain::separator`].
pub const DOMAIN_TYPE: &str = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)";

/// An EIP-712 domain with the four fields of [`DOMAIN_TYPE`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Domain {
    /// `name`: `Exchange` or `HyperliquidSignTransaction`.
    pub name: &'static str,
    /// `version`: `1` for both.
    pub version: &'static str,
    /// `chainId`.
    pub chain_id: u64,
    /// `verifyingContract`: the zero address for both.
    pub verifying_contract: Address,
}

impl Domain {
    /// What an L1 action's phantom agent is signed under, on both networks.
    /// The chain id is the literal 1337, whatever chain the wallet is on; the
    /// network is in the phantom agent's `source`.
    pub const EXCHANGE: Domain =
        Domain { name: "Exchange", version: "1", chain_id: L1_CHAIN_ID, verifying_contract: Address::ZERO };

    /// What a user-signed action is signed under: the chain the action names
    /// as its `signatureChainId`.
    #[must_use]
    pub const fn user_signed(network: Network) -> Domain {
        Domain {
            name: "HyperliquidSignTransaction",
            version: "1",
            chain_id: network.signature_chain_id(),
            verifying_contract: Address::ZERO,
        }
    }

    /// The domain separator: keccak256 of the type hash and the four encoded fields.
    #[must_use]
    pub fn separator(&self) -> [u8; 32] {
        Keccak256::new()
            .chain(&keccak256(DOMAIN_TYPE.as_bytes()))
            .chain(&keccak256(self.name.as_bytes()))
            .chain(&keccak256(self.version.as_bytes()))
            .chain(&word(self.chain_id))
            .chain(&address_word(self.verifying_contract))
            .finalize()
    }

    /// The `domain` object of wallet typed data: `chainId` a number, as viem sends it.
    pub(super) fn to_json(self) -> Json {
        Json::Object(vec![
            ("name".to_owned(), Json::String(self.name.to_owned())),
            ("version".to_owned(), Json::String(self.version.to_owned())),
            ("chainId".to_owned(), Json::Number(self.chain_id.to_string())),
            ("verifyingContract".to_owned(), Json::String(self.verifying_contract.to_hex())),
        ])
    }
}

#[cfg(test)]
#[path = "tests/domain.rs"]
mod tests;