Skip to content

Add ERC: Composite EIP-712 Signatures #993

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 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a9ad9c8
Add ERC-XXXX: Composite EIP-712 Signatures
sola92 Mar 25, 2025
e2e4d3f
lints
sola92 Mar 25, 2025
0022d4d
updates
sola92 Mar 25, 2025
95ce3fc
Simplify signature by removing CompositeMessage type
sola92 Mar 27, 2025
dea21b4
fix lints
sola92 Mar 28, 2025
f5d16c3
fixes
sola92 Mar 28, 2025
0b5df96
updates
sola92 Mar 28, 2025
11d12e7
verify backwards compatibility
sola92 Mar 28, 2025
3a98549
eth_signCompositeTypedData -> eth_signTypedData_v5
sola92 Mar 28, 2025
1a095e5
fix lints
sola92 Mar 28, 2025
4ed988c
fix lints
sola92 Mar 28, 2025
6e674dd
fix lints
sola92 Mar 28, 2025
cf19e4e
fix lints
sola92 Mar 28, 2025
22828ba
fix lints
sola92 Mar 28, 2025
991df70
fix lints
sola92 Mar 28, 2025
12a62b3
fix lints
sola92 Mar 28, 2025
0294163
updates
sola92 Mar 28, 2025
7d14469
updates
sola92 Mar 28, 2025
fefafd4
updates
sola92 Mar 28, 2025
9e7b14f
updates
sola92 Mar 28, 2025
c611106
updates
sola92 Mar 28, 2025
54b260a
updates
sola92 Mar 28, 2025
3882a89
updates
sola92 Mar 28, 2025
490386c
updates
sola92 Mar 28, 2025
e125758
updates
sola92 Mar 28, 2025
b0cb002
updates
sola92 Mar 28, 2025
11b00c6
updates
sola92 Mar 28, 2025
473672a
updates
sola92 Mar 28, 2025
0705223
updates
sola92 Mar 28, 2025
47a76f8
updates
sola92 Mar 31, 2025
f23f0a4
- Fix typos
sola92 Apr 12, 2025
e306df0
updates
sola92 Apr 12, 2025
35ad9fc
updates
sola92 Apr 12, 2025
171c1cc
updates
sola92 Apr 18, 2025
bf41d3e
updates
sola92 May 4, 2025
bd08bc7
updates
sola92 May 4, 2025
5beb332
updates
sola92 May 4, 2025
54bae6a
updates
sola92 May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions assets/erc-tbd/ExampleVerifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

error Unauthorized();

contract ExampleVerifier {
bytes32 private immutable COMPOSITE_DOMAIN_SEPARATOR;
bytes32 private constant COMPOSITE_MESSAGE_TYPEHASH =
keccak256("CompositeMessage(bytes32 merkleRoot)");

bytes32 private immutable DOMAIN_SEPARATOR;
bytes32 private constant MESSAGE_TYPEHASH =
keccak256("PlaceOrder(bytes32 orderId, address user)");

constructor() {
COMPOSITE_DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId)"
),
keccak256(bytes("ERC-XXXX")),
keccak256(bytes("1.0.0")),
block.chainid
)
);

DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
),
keccak256(bytes("MyApp")),
keccak256(bytes("1.0.0")),
block.chainid,
address(this)
)
);
}

function placeOrder(
bytes32 orderId,
address user,
bytes calldata signature,
bytes32 merkleRoot,
bytes32[] calldata proof
) public {
bytes32 messageHash = keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR,
keccak256(abi.encode(MESSAGE_TYPEHASH, orderId, user))
)
);

if (
!verifyCompositeSignature(
messageHash,
proof,
merkleRoot,
signature,
user
)
) {
revert Unauthorized();
}

// DO STUFF
}

function verifyMessageInclusion(
bytes32 messageHash,
bytes32[] calldata proof,
bytes32 root
) internal pure returns (bool) {
bytes32 computedRoot = messageHash;

for (uint256 i = 0; i < proof.length; ++i) {
if (computedRoot < proof[i]) {
computedRoot = keccak256(
abi.encodePacked(computedRoot, proof[i])
);
} else {
computedRoot = keccak256(
abi.encodePacked(proof[i], computedRoot)
);
}
}

return computedRoot == root;
}

function verifyCompositeSignature(
bytes32 messageHash,
bytes32[] calldata proof,
bytes32 merkleRoot,
bytes calldata signature,
address expectedSigner
) internal view returns (bool) {
if (!verifyMessageInclusion(messageHash, proof, merkleRoot)) {
return false;
}

bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01",
COMPOSITE_DOMAIN_SEPARATOR,
keccak256(abi.encode(COMPOSITE_MESSAGE_TYPEHASH, merkleRoot))
)
);
return recover(digest, signature) == expectedSigner;
}

function recover(
bytes32 digest,
bytes memory signature
) internal pure returns (address) {
require(signature.length == 65, "Invalid signature length");

bytes32 r;
bytes32 s;
uint8 v;

assembly {
r := mload(add(signature, 32))
s := mload(add(signature, 64))
v := byte(0, mload(add(signature, 96)))
}

return ecrecover(digest, v, r, s);
}
}
Binary file added assets/erc-tbd/erc-tbd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading