Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace OZ EIP712 #256

Merged
merged 11 commits into from
Aug 4, 2024
Merged

Replace OZ EIP712 #256

merged 11 commits into from
Aug 4, 2024

Conversation

saucepoint
Copy link
Collaborator

@saucepoint saucepoint commented Aug 3, 2024

Related Issue

Replace OZ EIP712.sol with our own because UR uses OZ v4.7 and cannot find the import

Description of changes

Took Permit2's EIP712.sol:

  • turned _HASHED_NAME into an immutable
  • Added checks for _CACHED_THIS == address(this) (mirrors OZ's behavior) removed since its a delegate protection, see comment below
  • Added _HASHED_VERSION to DOMAIN_SEPARATOR (mirrors OZ's behavior)

RYO cryptography at 10pm on a friday, what could possibly go wrong 🤷

@saucepoint saucepoint added the posm position manager label Aug 3, 2024
Comment on lines 0 to 1
79585 No newline at end of file
79660
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

remappings.txt Show resolved Hide resolved
src/base/EIP712.sol Outdated Show resolved Hide resolved
src/base/EIP712.sol Outdated Show resolved Hide resolved
@saucepoint
Copy link
Collaborator Author

upon additional reading from Uniswap/permit2#51 and OpenZeppelin/openzeppelin-contracts#2852

we do not need to check address(this) == _CACHED_THIS in DOMAIN_SEPARATOR

  1. it saves 36 gas

  2. it breaks ERC721Permit tokens looking to delegatecall posm. we dont expect the community to tap into posm for its ERC721Permit implementation. and if they do, they're bad devs 👿

snreynolds
snreynolds previously approved these changes Aug 3, 2024
Copy link
Member

@snreynolds snreynolds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

st,small,507x507-pad,600x600,f8f8f8 u1

bytes32 private immutable _HASHED_NAME;

bytes32 private constant _TYPE_HASH =
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dont we want a version string? i thought part of the idea was that we might have multiple versions? @snreynolds

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it shaves a bit of gas and sara thinks we can version in name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it shouldnt shave anything off gas except in the constructor? after that its not calculating the typehash right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh you should hardcode this hash - solidity calculates constants at runtime not compile time so itll be recalculating every time

src/base/EIP712.sol Show resolved Hide resolved
}

/// @notice Creates an EIP-712 typed data hash
function _hashTypedData(bytes32 dataHash) internal view returns (bytes32) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i went through OZ's contracts to figure out why theirs is cheaper lol. If you rewrite this function as:

    function _hashTypedData(bytes32 dataHash) internal view returns (bytes32 digest) {
        bytes32 domainSeparator = DOMAIN_SEPARATOR();
        assembly("memory-safe") {
            let fmp := mload(0x40)
            mstore(fmp, hex"19_01")
            mstore(add(fmp, 0x02), domainSeparator)
            mstore(add(fmp, 0x22), dataHash)
            digest := keccak256(fmp, 0x42)
        }
    }

our gas will be in line with theirs again! Based on this function

Comment on lines 0 to 1
79585 No newline at end of file
79566
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IEIP712 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think IPositionManager should inherit this?

hensha256
hensha256 previously approved these changes Aug 4, 2024
@saucepoint saucepoint merged commit 0c956bf into main Aug 4, 2024
3 checks passed
@saucepoint saucepoint deleted the eip712 branch August 4, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
posm position manager
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants