EIP712.sol 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./ECDSA.sol";
  4. /**
  5. * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
  6. *
  7. * The encoding specified in the EIP is very generic, and such a generic implementation in Solidity is not feasible,
  8. * thus this contract does not implement the encoding itself. Protocols need to implement the type-specific encoding
  9. * they need in their contracts using a combination of `abi.encode` and `keccak256`.
  10. *
  11. * This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding
  12. * scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA
  13. * ({_hashTypedDataV4}).
  14. *
  15. * The implementation of the domain separator was designed to be as efficient as possible while still properly updating
  16. * the chain id to protect against replay attacks on an eventual fork of the chain.
  17. *
  18. * NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method
  19. * https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask].
  20. *
  21. * _Available since v3.4._
  22. */
  23. abstract contract EIP712 {
  24. /* solhint-disable var-name-mixedcase */
  25. // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
  26. // invalidate the cached domain separator if the chain id changes.
  27. bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
  28. uint256 private immutable _CACHED_CHAIN_ID;
  29. address private immutable _CACHED_THIS;
  30. bytes32 private immutable _HASHED_NAME;
  31. bytes32 private immutable _HASHED_VERSION;
  32. bytes32 private immutable _TYPE_HASH;
  33. /* solhint-enable var-name-mixedcase */
  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) {
  47. bytes32 hashedName = keccak256(bytes(name));
  48. bytes32 hashedVersion = keccak256(bytes(version));
  49. bytes32 typeHash = keccak256(
  50. "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
  51. );
  52. _HASHED_NAME = hashedName;
  53. _HASHED_VERSION = hashedVersion;
  54. _CACHED_CHAIN_ID = block.chainid;
  55. _CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
  56. _CACHED_THIS = address(this);
  57. _TYPE_HASH = typeHash;
  58. }
  59. /**
  60. * @dev Returns the domain separator for the current chain.
  61. */
  62. function _domainSeparatorV4() internal view returns (bytes32) {
  63. if (address(this) == _CACHED_THIS && block.chainid == _CACHED_CHAIN_ID) {
  64. return _CACHED_DOMAIN_SEPARATOR;
  65. } else {
  66. return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);
  67. }
  68. }
  69. function _buildDomainSeparator(
  70. bytes32 typeHash,
  71. bytes32 nameHash,
  72. bytes32 versionHash
  73. ) private view returns (bytes32) {
  74. return keccak256(abi.encode(typeHash, nameHash, versionHash, block.chainid, address(this)));
  75. }
  76. /**
  77. * @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this
  78. * function returns the hash of the fully encoded EIP712 message for this domain.
  79. *
  80. * This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example:
  81. *
  82. * ```solidity
  83. * bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
  84. * keccak256("Mail(address to,string contents)"),
  85. * mailTo,
  86. * keccak256(bytes(mailContents))
  87. * )));
  88. * address signer = ECDSA.recover(digest, signature);
  89. * ```
  90. */
  91. function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
  92. return ECDSA.toTypedDataHash(_domainSeparatorV4(), structHash);
  93. }
  94. }