draft-ERC20Permit.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.0-rc.1 (token/ERC20/extensions/draft-ERC20Permit.sol)
  3. pragma solidity ^0.8.0;
  4. import "./draft-IERC20Permit.sol";
  5. import "../ERC20.sol";
  6. import "../../../utils/cryptography/draft-EIP712.sol";
  7. import "../../../utils/cryptography/ECDSA.sol";
  8. import "../../../utils/Counters.sol";
  9. /**
  10. * @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
  11. * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
  12. *
  13. * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
  14. * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
  15. * need to send a transaction, and thus is not required to hold Ether at all.
  16. *
  17. * _Available since v3.4._
  18. */
  19. abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
  20. using Counters for Counters.Counter;
  21. mapping(address => Counters.Counter) private _nonces;
  22. // solhint-disable-next-line var-name-mixedcase
  23. bytes32 private immutable _PERMIT_TYPEHASH =
  24. keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
  25. /**
  26. * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
  27. *
  28. * It's a good idea to use the same `name` that is defined as the ERC20 token name.
  29. */
  30. constructor(string memory name) EIP712(name, "1") {}
  31. /**
  32. * @dev See {IERC20Permit-permit}.
  33. */
  34. function permit(
  35. address owner,
  36. address spender,
  37. uint256 value,
  38. uint256 deadline,
  39. uint8 v,
  40. bytes32 r,
  41. bytes32 s
  42. ) public virtual override {
  43. require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
  44. bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
  45. bytes32 hash = _hashTypedDataV4(structHash);
  46. address signer = ECDSA.recover(hash, v, r, s);
  47. require(signer == owner, "ERC20Permit: invalid signature");
  48. _approve(owner, spender, value);
  49. }
  50. /**
  51. * @dev See {IERC20Permit-nonces}.
  52. */
  53. function nonces(address owner) public view virtual override returns (uint256) {
  54. return _nonces[owner].current();
  55. }
  56. /**
  57. * @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
  58. */
  59. // solhint-disable-next-line func-name-mixedcase
  60. function DOMAIN_SEPARATOR() external view override returns (bytes32) {
  61. return _domainSeparatorV4();
  62. }
  63. /**
  64. * @dev "Consume a nonce": return the current value and increment.
  65. *
  66. * _Available since v4.1._
  67. */
  68. function _useNonce(address owner) internal virtual returns (uint256 current) {
  69. Counters.Counter storage nonce = _nonces[owner];
  70. current = nonce.current();
  71. nonce.increment();
  72. }
  73. }