Skip to content

feat(forge): cheatcode eip712 #10570

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

Open
wants to merge 15 commits into
base: feat/eip712-with-solar
Choose a base branch
from

Conversation

0xrusowsky
Copy link
Contributor

@0xrusowsky 0xrusowsky commented May 20, 2025

Adds a new cheatcode to safely generate hashes following the EIP-712 spec.

implementation details

ref #4818

due to the lack of polymorphism, it is not possible for the cheatcode to accept a struct. Because of that, users must provide a string.

this impl allows users to either pass:

  1. the type name, which will we will attempt to retrieve from the bindings generated by forge bind-json
  2. the full string representation of the type definition. In this case, we leverage alloy's eip712 support to generate the canonical representation of the type even if users pass malformed types (unsorted or with extra whitespaces).

example usage

string constant CANONICAL = "Transaction(Person from,Person to,Asset tx)Asset(address token,uint256 amount)Person(address wallet,string name)";

contract Eip712Test is DSTest {
    Vm constant vm = Vm(HEVM_ADDRESS);

    function testEip712HashType() public {
        bytes32 canonicalHash = keccak256(bytes(CANONICAL));

        // Can figure out the canonical type from a messy string representation of the type,
        // with an invalid order and extra whitespaces
        bytes32 fromTypeDef = vm.eip712HashType(
            "Person(address wallet, string name) Asset(address token, uint256 amount) Transaction(Person from, Person to, Asset tx)"
        );
        assertEq(fromTypeDef, canonicalHash);

        // Can figure out the canonical type from the previously generated bindings with `forge bind-json`
        bytes32 fromTypeName = vm.eip712HashType("Transaction");
        assertEq(fromTypeName, canonicalHash);
    }
}

@0xrusowsky 0xrusowsky added this to the v1.3.0 milestone May 20, 2025
@0xrusowsky 0xrusowsky self-assigned this May 20, 2025
@0xrusowsky 0xrusowsky added the A-cheatcodes Area: cheatcodes label May 20, 2025
@0xrusowsky 0xrusowsky requested a review from zerosnacks as a code owner May 20, 2025 15:45
@0xrusowsky 0xrusowsky linked an issue May 20, 2025 that may be closed by this pull request
@0xrusowsky 0xrusowsky changed the base branch from master to feat/eip712-with-solar May 20, 2025 15:47
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

Please run cargo cheats twice to fix failing tests

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

looks good, left couple of comments / nits!
So if I get this right the DX would be:

  • generate bindings with forge bind-json (by default goes to utils/JsonBindings.sol but could be configurable to different pathToJsonBindings path)
  • use vm.eip712HashType(typeDefinition) or vm.eip712HashType(pathToJsonBindings, typeDefinition) to generate EIP-712 representation

@grandizzy grandizzy self-requested a review May 22, 2025 14:17
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

looks good, only some nits from my side before sending.

others, pls chime in re how the EIP-712 cheatcodes are designed to work and if you see any issue #10570 (review)
CC @klkvr

@0xrusowsky 0xrusowsky requested a review from grandizzy May 22, 2025 15:01
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

lgtm!

@0xrusowsky
Copy link
Contributor Author

0xrusowsky commented May 22, 2025

lgtm!

cool, let's wait for others to review and i'll merge it with the main feature branch.
Dani had to patch solar, so we won't be able to merge with master until there is a new release.

@grandizzy grandizzy self-requested a review May 22, 2025 15:25
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

sorry, just realized params should be calldata and not memory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants