-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WOOF WOOF WOOF BARK BARK WOOF BARK * remove address(this) check * forge fmt * discard version from EIP712 * Update src/base/EIP712.sol Co-authored-by: Sara Reynolds <[email protected]> * natspec for delegatecall error * assembly optimization, pr nits * test to validate assembly --------- Co-authored-by: Sara Reynolds <[email protected]>
- Loading branch information
1 parent
d65f158
commit 0c956bf
Showing
15 changed files
with
119 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
116877 | ||
116874 |
2 changes: 1 addition & 1 deletion
2
.forge-snapshots/PositionManager_multicall_initialize_mint.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
416513 | ||
416535 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
79486 | ||
79467 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
62398 | ||
62355 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
45298 | ||
45243 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {IEIP712} from "../interfaces/IEIP712.sol"; | ||
|
||
/// @notice Generic EIP712 implementation | ||
/// @dev Maintains cross-chain replay protection in the event of a fork | ||
/// @dev Should not be delegatecall'd because DOMAIN_SEPARATOR returns the cached hash and does not recompute with the delegatecallers address | ||
/// @dev Reference: https://github.com/Uniswap/permit2/blob/main/src/EIP712.sol | ||
/// @dev Reference: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/EIP712.sol | ||
contract EIP712 is IEIP712 { | ||
// Cache the domain separator as an immutable value, but also store the chain id that it | ||
// corresponds to, in order to invalidate the cached domain separator if the chain id changes. | ||
bytes32 private immutable _CACHED_DOMAIN_SEPARATOR; | ||
uint256 private immutable _CACHED_CHAIN_ID; | ||
bytes32 private immutable _HASHED_NAME; | ||
|
||
/// @dev equal to keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)") | ||
bytes32 private constant _TYPE_HASH = 0x8cad95687ba82c2ce50e74f7b754645e5117c3a5bec8151c0726d5857980a866; | ||
|
||
constructor(string memory name) { | ||
_HASHED_NAME = keccak256(bytes(name)); | ||
|
||
_CACHED_CHAIN_ID = block.chainid; | ||
_CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(); | ||
} | ||
|
||
/// @notice Returns the domain separator for the current chain. | ||
/// @dev Uses cached version if chainid is unchanged from construction. | ||
function DOMAIN_SEPARATOR() public view override returns (bytes32) { | ||
return block.chainid == _CACHED_CHAIN_ID ? _CACHED_DOMAIN_SEPARATOR : _buildDomainSeparator(); | ||
} | ||
|
||
/// @notice Builds a domain separator using the current chainId and contract address. | ||
function _buildDomainSeparator() private view returns (bytes32) { | ||
return keccak256(abi.encode(_TYPE_HASH, _HASHED_NAME, block.chainid, address(this))); | ||
} | ||
|
||
/// @notice Creates an EIP-712 typed data hash | ||
function _hashTypedData(bytes32 dataHash) internal view returns (bytes32 digest) { | ||
// equal to keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), dataHash)); | ||
bytes32 domainSeparator = DOMAIN_SEPARATOR(); | ||
assembly ("memory-safe") { | ||
let fmp := mload(0x40) | ||
mstore(fmp, hex"1901") | ||
mstore(add(fmp, 0x02), domainSeparator) | ||
mstore(add(fmp, 0x22), dataHash) | ||
digest := keccak256(fmp, 0x42) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
interface IEIP712 { | ||
function DOMAIN_SEPARATOR() external view returns (bytes32); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import {EIP712} from "../src/base/EIP712.sol"; | ||
|
||
contract EIP712Test is EIP712, Test { | ||
constructor() EIP712("EIP712Test") {} | ||
|
||
function setUp() public {} | ||
|
||
function test_domainSeparator() public view { | ||
assertEq( | ||
DOMAIN_SEPARATOR(), | ||
keccak256( | ||
abi.encode( | ||
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"), | ||
keccak256(bytes("EIP712Test")), | ||
block.chainid, | ||
address(this) | ||
) | ||
) | ||
); | ||
} | ||
|
||
function test_hashTypedData() public view { | ||
bytes32 dataHash = keccak256(abi.encodePacked("data")); | ||
assertEq(_hashTypedData(dataHash), keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), dataHash))); | ||
} | ||
|
||
function test_rebuildDomainSeparator() public { | ||
uint256 chainId = 4444; | ||
vm.chainId(chainId); | ||
assertEq( | ||
DOMAIN_SEPARATOR(), | ||
keccak256( | ||
abi.encode( | ||
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"), | ||
keccak256(bytes("EIP712Test")), | ||
chainId, | ||
address(this) | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters