// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title RWAEIP712
 * @notice EIP-712 domain and type hashes for index publisher attestations (off-chain or on-chain verify).
 */
library RWAEIP712 {
    bytes32 public constant INDEX_VALUE_TYPEHASH = keccak256(
        "IndexValueAttestation(string indexTicker,uint256 newValue,uint256 nonce,uint256 deadline)"
    );

    function domainSeparator(
        string memory name,
        string memory version,
        uint256 chainId,
        address verifyingContract
    ) internal pure returns (bytes32) {
        bytes32 typeHash = keccak256(
            "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
        );
        return keccak256(
            abi.encode(
                typeHash,
                keccak256(bytes(name)),
                keccak256(bytes(version)),
                chainId,
                verifyingContract
            )
        );
    }

    function hashIndexAttestation(
        bytes32 domain,
        string memory indexTicker,
        uint256 newValue,
        uint256 nonce,
        uint256 deadline
    ) internal pure returns (bytes32) {
        bytes32 structHash = keccak256(
            abi.encode(INDEX_VALUE_TYPEHASH, keccak256(bytes(indexTicker)), newValue, nonce, deadline)
        );
        return keccak256(abi.encodePacked("\x19\x01", domain, structHash));
    }
}
