Skip to content

Modify EIP712 implementation to use constants for _HASHED_NAME and _HASHED_VERSION#



Recommended changes were made in a new file.

The draft-EIP712Upgradeable.sol implementation being used from OpenZeppelin uses bytes32 private _HASHED_NAME and bytes32 private _HASHED_VERSION state variables which is gas inefficient because these could instead be constants.

Reading a state variable costs 2100 gas. Reading a constant costs nothing since the compiler replaces constants with literal values during compilation.

The _hashTypedDataV4 function from draft-EIP712Upgradeable.sol reads the state variables _HASHED_NAME and _HASHED_VERSION.

_hashTypedDataV4 is used by the matchOrders function in UniverseMarketplaceCore to verify signatures.


I recommend copying the draft-EIP712Upgradeable.sol implementation to the Universe-Marketplace repo and making the following changes:

Change this in draft-EIP712Upgradeable.sol:

bytes32 private _HASHED_NAME;
bytes32 private _HASHED_VERSION;
To this:
bytes32 private constant _HASHED_NAME = keccak256("Exchange");
bytes32 private constant _HASHED_VERSION = keccak256("2");

And delete these functions in draft-EIP712Upgradeable.sol because they aren't needed:

    function __EIP712_init(string memory name, string memory version) internal initializer {
        __EIP712_init_unchained(name, version);

    function __EIP712_init_unchained(string memory name, string memory version) internal initializer {
        bytes32 hashedName = keccak256(bytes(name));
        bytes32 hashedVersion = keccak256(bytes(version));
        _HASHED_NAME = hashedName;
        _HASHED_VERSION = hashedVersion;

And delete any function calls to the above two functions.