ERC20Permit.sol 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/extensions/ERC20Permit.sol)
  3. pragma solidity ^0.8.0;
  4. import "./IERC20Permit.sol";
  5. import "../ERC20.sol";
  6. import "../../../utils/cryptography/ECDSA.sol";
  7. import "../../../utils/cryptography/EIP712.sol";
  8. import "../../../utils/Nonces.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, Nonces {
  20. // solhint-disable-next-line var-name-mixedcase
  21. bytes32 private constant _PERMIT_TYPEHASH =
  22. keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
  23. /**
  24. * @dev In previous versions `_PERMIT_TYPEHASH` was declared as `immutable`.
  25. * However, to ensure consistency with the upgradeable transpiler, we will continue
  26. * to reserve a slot.
  27. * @custom:oz-renamed-from _PERMIT_TYPEHASH
  28. */
  29. // solhint-disable-next-line var-name-mixedcase
  30. bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;
  31. /**
  32. * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
  33. *
  34. * It's a good idea to use the same `name` that is defined as the ERC20 token name.
  35. */
  36. constructor(string memory name) EIP712(name, "1") {}
  37. /**
  38. * @dev See {IERC20Permit-permit}.
  39. */
  40. function permit(
  41. address owner,
  42. address spender,
  43. uint256 value,
  44. uint256 deadline,
  45. uint8 v,
  46. bytes32 r,
  47. bytes32 s
  48. ) public virtual override {
  49. require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
  50. bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
  51. bytes32 hash = _hashTypedDataV4(structHash);
  52. address signer = ECDSA.recover(hash, v, r, s);
  53. require(signer == owner, "ERC20Permit: invalid signature");
  54. _approve(owner, spender, value);
  55. }
  56. /**
  57. * @dev See {IERC20Permit-nonces}.
  58. */
  59. function nonces(address owner) public view virtual override(IERC20Permit, Nonces) returns (uint256) {
  60. return super.nonces(owner);
  61. }
  62. /**
  63. * @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
  64. */
  65. // solhint-disable-next-line func-name-mixedcase
  66. function DOMAIN_SEPARATOR() external view override returns (bytes32) {
  67. return _domainSeparatorV4();
  68. }
  69. }