From 604025400f9be5c32581bb6ab03a46bbc09c5562 Mon Sep 17 00:00:00 2001 From: Renan Souza Date: Tue, 13 Jun 2023 23:54:09 -0300 Subject: [PATCH] Add EIP-712 `name` and `version` getters (#4303) Co-authored-by: Hadrien Croubois Co-authored-by: ernestognw --- .changeset/little-falcons-build.md | 5 ++ contracts/utils/cryptography/EIP712.sol | 30 ++++++++- scripts/upgradeable/upgradeable.patch | 90 +++++++++++++------------ test/utils/cryptography/EIP712.test.js | 8 +++ 4 files changed, 88 insertions(+), 45 deletions(-) create mode 100644 .changeset/little-falcons-build.md diff --git a/.changeset/little-falcons-build.md b/.changeset/little-falcons-build.md new file mode 100644 index 00000000000..b310a8ae697 --- /dev/null +++ b/.changeset/little-falcons-build.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': minor +--- + +`EIP712`: Add internal getters for the name and version strings diff --git a/contracts/utils/cryptography/EIP712.sol b/contracts/utils/cryptography/EIP712.sol index b9c9c9d195d..2628014f12c 100644 --- a/contracts/utils/cryptography/EIP712.sol +++ b/contracts/utils/cryptography/EIP712.sol @@ -130,12 +130,38 @@ abstract contract EIP712 is IERC5267 { { return ( hex"0f", // 01111 - _name.toStringWithFallback(_nameFallback), - _version.toStringWithFallback(_versionFallback), + _EIP712Name(), + _EIP712Version(), block.chainid, address(this), bytes32(0), new uint256[](0) ); } + + /** + * @dev The name parameter for the EIP712 domain. + * + * NOTE: By default this function reads _name which is an immutable value. + * It only reads from storage if necessary (in case the value is too large to fit in a ShortString). + * + * _Available since v5.0._ + */ + // solhint-disable-next-line func-name-mixedcase + function _EIP712Name() internal view returns (string memory) { + return _name.toStringWithFallback(_nameFallback); + } + + /** + * @dev The version parameter for the EIP712 domain. + * + * NOTE: By default this function reads _version which is an immutable value. + * It only reads from storage if necessary (in case the value is too large to fit in a ShortString). + * + * _Available since v5.0._ + */ + // solhint-disable-next-line func-name-mixedcase + function _EIP712Version() internal view returns (string memory) { + return _version.toStringWithFallback(_versionFallback); + } } diff --git a/scripts/upgradeable/upgradeable.patch b/scripts/upgradeable/upgradeable.patch index b29e7d12bed..554775ffe8e 100644 --- a/scripts/upgradeable/upgradeable.patch +++ b/scripts/upgradeable/upgradeable.patch @@ -59,10 +59,10 @@ index ff596b0c..00000000 - - diff --git a/README.md b/README.md -index 9fc95518..53130e3c 100644 +index aba99171..6656267b 100644 --- a/README.md +++ b/README.md -@@ -16,17 +16,20 @@ +@@ -19,17 +19,20 @@ :building_construction: **Want to scale your decentralized application?** Check out [OpenZeppelin Defender](https://openzeppelin.com/defender) — a secure platform for automating and monitoring your operations. @@ -85,7 +85,7 @@ index 9fc95518..53130e3c 100644 ### Usage -@@ -35,10 +38,11 @@ Once installed, you can use the contracts in the library by importing them: +@@ -38,10 +41,11 @@ Once installed, you can use the contracts in the library by importing them: ```solidity pragma solidity ^0.8.19; @@ -101,7 +101,7 @@ index 9fc95518..53130e3c 100644 } ``` diff --git a/contracts/finance/VestingWallet.sol b/contracts/finance/VestingWallet.sol -index bb70d19f..38513771 100644 +index 5b7e1b15..1ca745d6 100644 --- a/contracts/finance/VestingWallet.sol +++ b/contracts/finance/VestingWallet.sol @@ -18,6 +18,8 @@ import "../utils/Context.sol"; @@ -114,7 +114,7 @@ index bb70d19f..38513771 100644 contract VestingWallet is Context { event EtherReleased(uint256 amount); diff --git a/contracts/governance/extensions/GovernorVotes.sol b/contracts/governance/extensions/GovernorVotes.sol -index 64431711..885f0e42 100644 +index 5d8318f4..ef3cde55 100644 --- a/contracts/governance/extensions/GovernorVotes.sol +++ b/contracts/governance/extensions/GovernorVotes.sol @@ -10,6 +10,8 @@ import "../../interfaces/IERC5805.sol"; @@ -127,7 +127,7 @@ index 64431711..885f0e42 100644 abstract contract GovernorVotes is Governor { IERC5805 public immutable token; diff --git a/contracts/package.json b/contracts/package.json -index 55e70b17..ceefb984 100644 +index 4d0f576b..822fd471 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,5 +1,5 @@ @@ -135,7 +135,7 @@ index 55e70b17..ceefb984 100644 - "name": "@openzeppelin/contracts", + "name": "@openzeppelin/contracts-upgradeable", "description": "Secure Smart Contract library for Solidity", - "version": "4.8.2", + "version": "4.9.0", "files": [ @@ -13,7 +13,7 @@ }, @@ -147,7 +147,7 @@ index 55e70b17..ceefb984 100644 "keywords": [ "solidity", diff --git a/contracts/token/ERC20/extensions/ERC20Capped.sol b/contracts/token/ERC20/extensions/ERC20Capped.sol -index 16f830d1..9ef98148 100644 +index cda07265..d314148c 100644 --- a/contracts/token/ERC20/extensions/ERC20Capped.sol +++ b/contracts/token/ERC20/extensions/ERC20Capped.sol @@ -7,6 +7,8 @@ import "../ERC20.sol"; @@ -160,20 +160,20 @@ index 16f830d1..9ef98148 100644 abstract contract ERC20Capped is ERC20 { uint256 private immutable _cap; diff --git a/contracts/token/ERC20/extensions/ERC20Permit.sol b/contracts/token/ERC20/extensions/ERC20Permit.sol -index a357199b..9dc8e894 100644 +index 9379e445..e02f0644 100644 --- a/contracts/token/ERC20/extensions/ERC20Permit.sol +++ b/contracts/token/ERC20/extensions/ERC20Permit.sol -@@ -18,6 +18,8 @@ import "../../../utils/Counters.sol"; +@@ -18,6 +18,8 @@ import "../../../utils/Nonces.sol"; * need to send a transaction, and thus is not required to hold Ether at all. * * _Available since v3.4._ + * + * @custom:storage-size 51 */ - abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 { - using Counters for Counters.Counter; + abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces { + // solhint-disable-next-line var-name-mixedcase diff --git a/contracts/token/ERC20/extensions/ERC20Wrapper.sol b/contracts/token/ERC20/extensions/ERC20Wrapper.sol -index bfe782e4..7264fe32 100644 +index bf2b225c..0e5b3628 100644 --- a/contracts/token/ERC20/extensions/ERC20Wrapper.sol +++ b/contracts/token/ERC20/extensions/ERC20Wrapper.sol @@ -14,6 +14,8 @@ import "../utils/SafeERC20.sol"; @@ -186,7 +186,7 @@ index bfe782e4..7264fe32 100644 abstract contract ERC20Wrapper is ERC20 { IERC20 private immutable _underlying; diff --git a/contracts/utils/cryptography/EIP712.sol b/contracts/utils/cryptography/EIP712.sol -index 6a4e1cad..55d8eced 100644 +index 2628014f..7d5193c8 100644 --- a/contracts/utils/cryptography/EIP712.sol +++ b/contracts/utils/cryptography/EIP712.sol @@ -4,7 +4,6 @@ @@ -268,7 +268,7 @@ index 6a4e1cad..55d8eced 100644 } /** -@@ -129,14 +114,80 @@ abstract contract EIP712 is IERC5267 { +@@ -128,6 +113,10 @@ abstract contract EIP712 is IERC5267 { uint256[] memory extensions ) { @@ -278,34 +278,34 @@ index 6a4e1cad..55d8eced 100644 + return ( hex"0f", // 01111 -- _name.toStringWithFallback(_nameFallback), -- _version.toStringWithFallback(_versionFallback), -+ _EIP712Name(), -+ _EIP712Version(), - block.chainid, - address(this), - bytes32(0), - new uint256[](0) - ); - } -+ -+ /** -+ * @dev The name parameter for the EIP712 domain. -+ * + _EIP712Name(), +@@ -142,26 +131,62 @@ abstract contract EIP712 is IERC5267 { + /** + * @dev The name parameter for the EIP712 domain. + * +- * NOTE: By default this function reads _name which is an immutable value. +- * It only reads from storage if necessary (in case the value is too large to fit in a ShortString). +- * +- * _Available since v5.0._ + * NOTE: This function reads from storage by default, but can be redefined to return a constant value if gas costs + * are a concern. -+ */ -+ function _EIP712Name() internal virtual view returns (string memory) { + */ +- // solhint-disable-next-line func-name-mixedcase +- function _EIP712Name() internal view returns (string memory) { +- return _name.toStringWithFallback(_nameFallback); ++ function _EIP712Name() internal view virtual returns (string memory) { + return _name; -+ } -+ -+ /** -+ * @dev The version parameter for the EIP712 domain. -+ * + } + + /** + * @dev The version parameter for the EIP712 domain. + * +- * NOTE: By default this function reads _version which is an immutable value. +- * It only reads from storage if necessary (in case the value is too large to fit in a ShortString). + * NOTE: This function reads from storage by default, but can be redefined to return a constant value if gas costs + * are a concern. + */ -+ function _EIP712Version() internal virtual view returns (string memory) { ++ function _EIP712Version() internal view virtual returns (string memory) { + return _version; + } + @@ -332,9 +332,13 @@ index 6a4e1cad..55d8eced 100644 + + /** + * @dev The hash of the version parameter for the EIP712 domain. -+ * + * +- * _Available since v5.0._ + * NOTE: In previous versions this function was virtual. In this version you should override `_EIP712Version` instead. -+ */ + */ +- // solhint-disable-next-line func-name-mixedcase +- function _EIP712Version() internal view returns (string memory) { +- return _version.toStringWithFallback(_versionFallback); + function _EIP712VersionHash() internal view returns (bytes32) { + string memory version = _EIP712Version(); + if (bytes(version).length > 0) { @@ -349,13 +353,13 @@ index 6a4e1cad..55d8eced 100644 + return keccak256(""); + } + } -+ } + } } diff --git a/package.json b/package.json -index 8458dd61..b4672240 100644 +index c070915f..9a513cac 100644 --- a/package.json +++ b/package.json -@@ -36,7 +36,7 @@ +@@ -33,7 +33,7 @@ }, "repository": { "type": "git", @@ -365,7 +369,7 @@ index 8458dd61..b4672240 100644 "keywords": [ "solidity", diff --git a/test/utils/cryptography/EIP712.test.js b/test/utils/cryptography/EIP712.test.js -index 54a4e772..ba4602ed 100644 +index 7ea535b7..32e3a370 100644 --- a/test/utils/cryptography/EIP712.test.js +++ b/test/utils/cryptography/EIP712.test.js @@ -47,26 +47,6 @@ contract('EIP712', function (accounts) { diff --git a/test/utils/cryptography/EIP712.test.js b/test/utils/cryptography/EIP712.test.js index 54a4e772372..7ea535b7f7d 100644 --- a/test/utils/cryptography/EIP712.test.js +++ b/test/utils/cryptography/EIP712.test.js @@ -98,6 +98,14 @@ contract('EIP712', function (accounts) { await this.eip712.verify(signature, wallet.getAddressString(), message.to, message.contents); }); + + it('name', async function () { + expect(await this.eip712.$_EIP712Name()).to.be.equal(name); + }); + + it('version', async function () { + expect(await this.eip712.$_EIP712Version()).to.be.equal(version); + }); }); } });