EIP712.sol 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../cryptography/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. 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. * @dev Initializes the domain separator and parameter caches.
  35. *
  36. * The meaning of `name` and `version` is specified in
  37. * https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]:
  38. *
  39. * - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol.
  40. * - `version`: the current major version of the signing domain.
  41. *
  42. * NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
  43. * contract upgrade].
  44. */
  45. constructor(string memory name, string memory version) {
  46. bytes32 hashedName = keccak256(bytes(name));
  47. bytes32 hashedVersion = keccak256(bytes(version));
  48. bytes32 typeHash = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
  49. _HASHED_NAME = hashedName;
  50. _HASHED_VERSION = hashedVersion;
  51. _CACHED_CHAIN_ID = block.chainid;
  52. _CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
  53. _TYPE_HASH = typeHash;
  54. }
  55. /**
  56. * @dev Returns the domain separator for the current chain.
  57. */
  58. function _domainSeparatorV4() internal view returns (bytes32) {
  59. if (block.chainid == _CACHED_CHAIN_ID) {
  60. return _CACHED_DOMAIN_SEPARATOR;
  61. } else {
  62. return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);
  63. }
  64. }
  65. function _buildDomainSeparator(bytes32 typeHash, bytes32 name, bytes32 version) private view returns (bytes32) {
  66. return keccak256(
  67. abi.encode(
  68. typeHash,
  69. name,
  70. version,
  71. block.chainid,
  72. address(this)
  73. )
  74. );
  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. }