|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity >=0.6.0 <0.8.0; |
| 4 | + |
| 5 | +/** |
| 6 | + * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data. |
| 7 | + * |
| 8 | + * The encoding specified in the EIP is very generic, and such a generic implementation in Solidity is not feasible, |
| 9 | + * thus this contract does not implement the encoding itself. Protocols need to implement the type-specific encoding |
| 10 | + * they need in their contracts using a combination of `abi.encode` and `keccak256`. |
| 11 | + * |
| 12 | + * This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding |
| 13 | + * scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA |
| 14 | + * ({_hashTypedDataV4}). |
| 15 | + * |
| 16 | + * The implementation of the domain separator was designed to be as efficient as possible while still properly updating |
| 17 | + * the chain id to protect against replay attacks on an eventual fork of the chain. |
| 18 | + * |
| 19 | + * NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method |
| 20 | + * https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask]. |
| 21 | + */ |
| 22 | +abstract contract EIP712 { |
| 23 | + /* solhint-disable var-name-mixedcase */ |
| 24 | + // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to |
| 25 | + // invalidate the cached domain separator if the chain id changes. |
| 26 | + bytes32 private immutable _CACHED_DOMAIN_SEPARATOR; |
| 27 | + uint256 private immutable _CACHED_CHAIN_ID; |
| 28 | + |
| 29 | + bytes32 private immutable _HASHED_NAME; |
| 30 | + bytes32 private immutable _HASHED_VERSION; |
| 31 | + bytes32 private immutable _TYPE_HASH; |
| 32 | + /* solhint-enable var-name-mixedcase */ |
| 33 | + |
| 34 | + /** |
| 35 | + * @dev Initializes the domain separator and parameter caches. |
| 36 | + * |
| 37 | + * The meaning of `name` and `version` is specified in |
| 38 | + * https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]: |
| 39 | + * |
| 40 | + * - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol. |
| 41 | + * - `version`: the current major version of the signing domain. |
| 42 | + * |
| 43 | + * NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart |
| 44 | + * contract upgrade]. |
| 45 | + */ |
| 46 | + constructor(string memory name, string memory version) internal { |
| 47 | + bytes32 hashedName = keccak256(bytes(name)); |
| 48 | + bytes32 hashedVersion = keccak256(bytes(version)); |
| 49 | + bytes32 typeHash = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); |
| 50 | + _HASHED_NAME = hashedName; |
| 51 | + _HASHED_VERSION = hashedVersion; |
| 52 | + _CACHED_CHAIN_ID = _getChainId(); |
| 53 | + _CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion); |
| 54 | + _TYPE_HASH = typeHash; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dev Returns the domain separator for the current chain. |
| 59 | + */ |
| 60 | + function _domainSeparatorV4() internal view returns (bytes32) { |
| 61 | + if (_getChainId() == _CACHED_CHAIN_ID) { |
| 62 | + return _CACHED_DOMAIN_SEPARATOR; |
| 63 | + } else { |
| 64 | + return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function _buildDomainSeparator(bytes32 typeHash, bytes32 name, bytes32 version) private view returns (bytes32) { |
| 69 | + return keccak256( |
| 70 | + abi.encode( |
| 71 | + typeHash, |
| 72 | + name, |
| 73 | + version, |
| 74 | + _getChainId(), |
| 75 | + address(this) |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this |
| 82 | + * function returns the hash of the fully encoded EIP712 message for this domain. |
| 83 | + * |
| 84 | + * This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example: |
| 85 | + * |
| 86 | + * ```solidity |
| 87 | + * bytes32 digest = _hashTypedDataV4(keccak256(abi.encode( |
| 88 | + * keccak256("Mail(address to,string contents)"), |
| 89 | + * mailTo, |
| 90 | + * keccak256(bytes(mailContents)) |
| 91 | + * ))); |
| 92 | + * address signer = ECDSA.recover(digest, signature); |
| 93 | + * ``` |
| 94 | + */ |
| 95 | + function _hashTypedDataV4(bytes32 structHash) internal view returns (bytes32) { |
| 96 | + return keccak256(abi.encodePacked("\x19\x01", _domainSeparatorV4(), structHash)); |
| 97 | + } |
| 98 | + |
| 99 | + function _getChainId() private pure returns (uint256 chainId) { |
| 100 | + // solhint-disable-next-line no-inline-assembly |
| 101 | + assembly { |
| 102 | + chainId := chainid() |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments