EIP712.sol 4.6 KB

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