solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Support EIP-712 typehash in `type`

Open k06a opened this issue 1 year ago • 24 comments

Abstract

Let's add support of type(State).typehash for structs, returning bytes32 hashed stringed structure.

Motivation

EIP-712 type hashes are pretty popular among smart contract of different companies. I believe it makes sense to support it on Solidity language layer.

Specification

bytes32 constant public STATE_TYPEHASH = keccak256(
    "State("
        "uint256 nonce,"
        "uint256 totalSpent,"
        "uint256 totalRefunded"
    ")"
);

=>

struct State {
    uint256 nonce;
    uint256 totalSpent;
    uint256 totalRefunded;
}

bytes32 constant public STATE_TYPEHASH = type(State).typehash;

Backwards Compatibility

Not sure

k06a avatar Apr 26 '23 12:04 k06a

I really like that idea. I feel like instead of calling it State, the semantics Topic would be nicer. Also, I would like to add a further request for commonly known type hashes such as the one for permit.

Instead of doing the following (or the approach in the feature request above):

bytes32 private constant _PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

one could simply query the type hash like the following without importing the EIP-2612 interface itself (i.e. Permit is already built-in):

bytes32 private constant _PERMIT_TYPEHASH = type(Permit).typehash

This would require restricting the namespace for type but I feel that it's becoming less error-prone like that due to common mistakes made such as the usage of uint alias in the hash.

pcaversaccio avatar Apr 26 '23 13:04 pcaversaccio

@pcaversaccio type(X) is already type-sensitive: https://docs.soliditylang.org/en/v0.8.19/units-and-global-variables.html

k06a avatar Apr 27 '23 12:04 k06a

@k06a We discussed this in the call. We're fine with proceeding here. Would appreciate if you can lead the implementation, we can give some pointers on missing things, and review it.

hrkrshnn avatar May 03 '23 13:05 hrkrshnn

@hrkrshnn great, thanks!

k06a avatar May 04 '23 14:05 k06a

What do you think of adding runtime function to compute hash of the exact struct entity:

State memory state;
type(State).hashStruct(state);

k06a avatar May 06 '23 21:05 k06a

@k06a I like it. Can you open a new issue for it?

hrkrshnn avatar May 10 '23 13:05 hrkrshnn

@hrkrshnn done: https://github.com/ethereum/solidity/issues/14208

k06a avatar May 10 '23 13:05 k06a

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.

github-actions[bot] avatar Aug 09 '23 12:08 github-actions[bot]

This is still relevant.

pcaversaccio avatar Aug 09 '23 12:08 pcaversaccio

Wen merge @hrkrshnn? 😁

k06a avatar Aug 09 '23 13:08 k06a

Apologies @k06a, I've been heads down on @spearbit lately. I'll find some time on the weekend to review this.

hrkrshnn avatar Aug 09 '23 13:08 hrkrshnn

up up up :-)

this will be a game-changer.

valle-xyz avatar Sep 29 '23 21:09 valle-xyz

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.

github-actions[bot] avatar Dec 29 '23 12:12 github-actions[bot]

This is still relevant. Don't close.

pcaversaccio avatar Dec 29 '23 12:12 pcaversaccio

Would love to see this!

sebastiantf avatar Mar 09 '24 08:03 sebastiantf

+1

n4ru avatar Mar 09 '24 10:03 n4ru

@hrkrshnn status?

pegahcarter avatar Mar 28 '24 20:03 pegahcarter

Sorry @pegahcarter. I'm heads down on @cantinaxyz and @spearbit and won't be able to spend time on this.

hrkrshnn avatar Mar 28 '24 20:03 hrkrshnn

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.

github-actions[bot] avatar Jun 27 '24 12:06 github-actions[bot]

I guess this is still relevant, so don't close.

pcaversaccio avatar Jun 27 '24 12:06 pcaversaccio

I think we just need someone to champion the dev.

pegahcarter avatar Jun 27 '24 17:06 pegahcarter

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.

github-actions[bot] avatar Sep 26 '24 12:09 github-actions[bot]

I guess this is still relevant, so don't close.

pcaversaccio avatar Sep 26 '24 13:09 pcaversaccio

@hrkrshnn how about now? :pray:

pegahcarter avatar Sep 27 '24 06:09 pegahcarter