// US-002: EIP-712 signing engine
use alloy_sol_types::{eip712_domain, sol, SolStruct};

sol! {
    struct Message {
        string msg;
    }
}

/// EIP-712 domain for AsterDex. `chain_id` must match the deployment environment (testnet: 714).
///
/// Call once at `Credentials` construction and cache the result — building the
/// domain struct on every signature is unnecessary work.
pub(crate) fn get_domain(chain_id: u64) -> alloy_sol_types::Eip712Domain {
    eip712_domain! {
        name: "AsterSignTransaction",
        version: "1",
        chain_id: chain_id,
        verifying_contract: alloy_primitives::address!(
            "0000000000000000000000000000000000000000"
        ),
    }
}

/// Compute EIP-712 signing hash for a URL-encoded param string.
///
/// Accepts a pre-computed domain reference so callers can cache the domain
/// instead of rebuilding it on every request.
pub(crate) fn signing_hash(
    param_string: &str,
    domain: &alloy_sol_types::Eip712Domain,
) -> alloy_primitives::B256 {
    let msg = Message { msg: param_string.to_string() };
    msg.eip712_signing_hash(domain)
}
