Sitelet https://github.com/Vectorized/solady/blob/main/docs/utils/eip712.md
Skip to content

Latest commit

 

History

History
156 lines (116 loc) · 3.88 KB

File metadata and controls

156 lines (116 loc) · 3.88 KB

EIP712

Contract for EIP-712 typed structured data hashing and signing.

Note, this implementation:

  • Uses address(this) for the verifyingContract field.
  • Does NOT use the optional EIP-712 salt.
  • Does NOT use any EIP-712 extensions. This is for simplicity and to save gas. If you need to customize, please fork / modify accordingly.

Constants And Immutables

_DOMAIN_TYPEHASH

bytes32 internal constant _DOMAIN_TYPEHASH =
    0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f

keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)").

_DOMAIN_TYPEHASH_SANS_CHAIN_ID

bytes32 internal constant _DOMAIN_TYPEHASH_SANS_CHAIN_ID =
    0x91ab3d17e3a50a9d89e63fd30b92be7f5336b03b287bb946787a83a9d62a2766

keccak256("EIP712Domain(string name,string version,address verifyingContract)").
This is only used in _hashTypedDataSansChainId.

_DOMAIN_TYPEHASH_SANS_CHAIN_ID_AND_VERIFYING_CONTRACT

bytes32 internal constant
    _DOMAIN_TYPEHASH_SANS_CHAIN_ID_AND_VERIFYING_CONTRACT =
        0xb03948446334eb9b2196d5eb166f69b9d49403eb4a12f36de8d3f9f3cb8e15c3

keccak256("EIP712Domain(string name,string version)").
This is only used in _hashTypedDataSansChainIdAndVerifyingContract.

_DOMAIN_TYPEHASH_SANS_VERIFYING_CONTRACT

bytes32 internal constant _DOMAIN_TYPEHASH_SANS_VERIFYING_CONTRACT =
    0xc2f8787176b8ac6bf7215b4adcc1e069bf4ab82d9ab1df05a57a91d425935b6e

keccak256("EIP712Domain(string name,string version,uint256 chainId)").
This is only used in _hashTypedDataSansVerifyingContract.

Hashing Operations

_domainSeparator()

function _domainSeparator()
    internal
    view
    virtual
    returns (bytes32 separator)

Returns the EIP-712 domain separator.

_hashTypedData(bytes32)

function _hashTypedData(bytes32 structHash)
    internal
    view
    virtual
    returns (bytes32 digest)

Returns the hash of the fully encoded EIP-712 message for this domain,
given structHash, as defined in
https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct.
The hash can be used together with ECDSA-recover to obtain the signer of a message:

bytes32 digest = _hashTypedData(keccak256(abi.encode(   
    keccak256("Mail(address to,string contents)"),   
    mailTo,   
    keccak256(bytes(mailContents))   
)));   
address signer = ECDSA.recover(digest, signature);   

_hashTypedDataSansChainId(bytes32)

function _hashTypedDataSansChainId(bytes32 structHash)
    internal
    view
    virtual
    returns (bytes32 digest)

Variant of _hashTypedData that excludes the chain ID.
Included for the niche use case of cross-chain workflows.

_hashTypedDataSansChainIdAndVerifyingContract(bytes32)

function _hashTypedDataSansChainIdAndVerifyingContract(bytes32 structHash)
    internal
    view
    virtual
    returns (bytes32 digest)

Variant of _hashTypedData that excludes the chain ID and verifying contract.
Included for the niche use case of cross-chain and multi-verifier workflows.

_hashTypedDataSansVerifyingContract(bytes32)

function _hashTypedDataSansVerifyingContract(bytes32 structHash)
    internal
    view
    virtual
    returns (bytes32 digest)

Variant of _hashTypedData that excludes the chain ID and verifying contract.
Included for the niche use case of multi-verifier workflows.

EIP-5267 Operations

eip712Domain()

function eip712Domain()
    public
    view
    virtual
    returns (
        bytes1 fields,
        string memory name,
        string memory version,
        uint256 chainId,
        address verifyingContract,
        bytes32 salt,
        uint256[] memory extensions
    )

See: https://eips.ethereum.org/EIPS/eip-5267