Skip to content

Commit 29eeb28

Browse files
k06aAmxxfrangio
authored
Fix EIP712 for delegatecalls (#2852)
Co-authored-by: Hadrien Croubois <[email protected]> Co-authored-by: Francisco Giordano <[email protected]>
1 parent 75d422f commit 29eeb28

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* `Ownable`: add an internal `_transferOwnership(address)`. ([#2568](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2568))
66
* `AccessControl`: add internal `_grantRole(bytes32,address)` and `_revokeRole(bytes32,address)`. ([#2568](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2568))
77
* `AccessControl`: mark `_setupRole(bytes32,address)` as deprecated in favor of `_grantRole(bytes32,address)`. ([#2568](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2568))
8+
* `EIP712`: cache `address(this)` to immutable storage to avoid potential issues if a vanilla contract is used in a delegatecall context. ([#2852](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2852))
89

910
## 4.3.2 (2021-09-14)
1011

contracts/utils/cryptography/draft-EIP712.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abstract contract EIP712 {
2929
// invalidate the cached domain separator if the chain id changes.
3030
bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
3131
uint256 private immutable _CACHED_CHAIN_ID;
32+
address private immutable _CACHED_THIS;
3233

3334
bytes32 private immutable _HASHED_NAME;
3435
bytes32 private immutable _HASHED_VERSION;
@@ -58,14 +59,15 @@ abstract contract EIP712 {
5859
_HASHED_VERSION = hashedVersion;
5960
_CACHED_CHAIN_ID = block.chainid;
6061
_CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
62+
_CACHED_THIS = address(this);
6163
_TYPE_HASH = typeHash;
6264
}
6365

6466
/**
6567
* @dev Returns the domain separator for the current chain.
6668
*/
6769
function _domainSeparatorV4() internal view returns (bytes32) {
68-
if (block.chainid == _CACHED_CHAIN_ID) {
70+
if (address(this) == _CACHED_THIS && block.chainid == _CACHED_CHAIN_ID) {
6971
return _CACHED_DOMAIN_SEPARATOR;
7072
} else {
7173
return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);

0 commit comments

Comments
 (0)