Skip to content

Commit

Permalink
Add standalone type hash definition and remove regitrant from it
Browse files Browse the repository at this point in the history
  • Loading branch information
garyghayrat committed Jan 30, 2024
1 parent bce18d4 commit a9b7db1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
12 changes: 5 additions & 7 deletions src/ERC6538Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ contract ERC6538Registry {
/// @dev `nonce` will be incremented after each valid `registerKeysOnBehalf` call.
mapping(address user => uint256) public nonceOf;

/// @notice The EIP-712 type hash used in `registerKeysOnBehalf`.
bytes32 public constant ERC6538REGISTRY_ENTRY_TYPE_HASH =
keccak256("Erc6538RegistryEntry(uint256 schemeId,bytes stealthMetaAddress,uint256 nonce)");

/// @dev The chain ID where this contract is initially deployed.
uint256 internal immutable INITIAL_CHAIN_ID;

Expand Down Expand Up @@ -76,13 +80,7 @@ contract ERC6538Registry {
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256(
"RegisterKeysOnBehalf(address registrant,uint256 schemeId,bytes stealthMetaAddress,uint256 nonce)"
),
registrant,
schemeId,
stealthMetaAddress,
nonceOf[registrant]++
ERC6538REGISTRY_ENTRY_TYPE_HASH, schemeId, stealthMetaAddress, nonceOf[registrant]++
)
)
)
Expand Down
20 changes: 8 additions & 12 deletions test/ERC6538Registry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {
) external {
(address alice, uint256 alicePk) = makeAddrAndKey(name);
SigUtils.RegistrantInfo memory registrantInfo =
SigUtils.RegistrantInfo(alice, schemeId, stealthMetaAddress, 0 /* nonce */ );
SigUtils.RegistrantInfo(schemeId, stealthMetaAddress, 0 /* nonce */ );
bytes32 hash = sigUtils.getTypedDataHash(registrantInfo);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -88,7 +88,7 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {
address registrant = address(erc1271MockContract);

SigUtils.RegistrantInfo memory registrantInfo =
SigUtils.RegistrantInfo(registrant, schemeId, stealthMetaAddress, 0 /* nonce */ );
SigUtils.RegistrantInfo(schemeId, stealthMetaAddress, 0 /* nonce */ );
bytes32 hash = sigUtils.getTypedDataHash(registrantInfo);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -109,7 +109,7 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {

for (uint256 nonce = 0; nonce < numOfUpdates; nonce++) {
SigUtils.RegistrantInfo memory registrantInfo =
SigUtils.RegistrantInfo(alice, schemeId, stealthMetaAddress, nonce);
SigUtils.RegistrantInfo(schemeId, stealthMetaAddress, nonce);
bytes32 hash = sigUtils.getTypedDataHash(registrantInfo);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -126,9 +126,9 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {
uint256 schemeId,
bytes memory stealthMetaAddress
) external {
(address alice, uint256 alicePk) = makeAddrAndKey(name);
( /*address alice */ , uint256 alicePk) = makeAddrAndKey(name);
SigUtils.RegistrantInfo memory registrantInfo =
SigUtils.RegistrantInfo(alice, schemeId, stealthMetaAddress, 0 /* nonce */ );
SigUtils.RegistrantInfo(schemeId, stealthMetaAddress, 0 /* nonce */ );
bytes32 hash = sigUtils.getTypedDataHash(registrantInfo);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -150,7 +150,7 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {
address registrant = address(erc1271MockContract);

SigUtils.RegistrantInfo memory registrantInfo =
SigUtils.RegistrantInfo(registrant, schemeId, stealthMetaAddress, 0 /* nonce */ );
SigUtils.RegistrantInfo(schemeId, stealthMetaAddress, 0 /* nonce */ );
bytes32 hash = sigUtils.getTypedDataHash(registrantInfo);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -168,7 +168,7 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {
vm.assume(nonce != 0);
(address alice, uint256 alicePk) = makeAddrAndKey(name);
SigUtils.RegistrantInfo memory registrantInfo =
SigUtils.RegistrantInfo(alice, schemeId, stealthMetaAddress, nonce);
SigUtils.RegistrantInfo(schemeId, stealthMetaAddress, nonce);
bytes32 hash = sigUtils.getTypedDataHash(registrantInfo);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);
Expand Down Expand Up @@ -302,7 +302,6 @@ contract SigUtils {
}

struct RegistrantInfo {
address registrant;
uint256 schemeId;
bytes stealthMetaAddress;
uint256 nonce;
Expand All @@ -312,10 +311,7 @@ contract SigUtils {
function getStructHash(RegistrantInfo memory _info) internal pure returns (bytes32) {
return keccak256(
abi.encode(
keccak256(
"RegisterKeysOnBehalf(address registrant,uint256 schemeId,bytes stealthMetaAddress,uint256 nonce)"
),
_info.registrant,
keccak256("Erc6538RegistryEntry(uint256 schemeId,bytes stealthMetaAddress,uint256 nonce)"),
_info.schemeId,
_info.stealthMetaAddress,
_info.nonce
Expand Down

0 comments on commit a9b7db1

Please sign in to comment.