Skip to content

Commit

Permalink
Add custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
garyghayrat committed Jan 30, 2024
1 parent d8140ae commit bce18d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/ERC6538Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pragma solidity 0.8.23;
/// @dev `ERC6538Registry` contract to map accounts to their stealth meta-address. See
/// [ERC-6538](https://eips.ethereum.org/EIPS/eip-6538) to learn more.
contract ERC6538Registry {
/// @notice Emitted when an invalid signature is provided to `registerKeysOnBehalf`.
error ERC6538Registry__InvalidSignature();

/// @notice Next nonce expected from `user` to use when signing for `registerKeysOnBehalf`.
/// @dev `registrant` may be a standard 160-bit address or any other identifier.
/// @dev `schemeId` is an integer identifier for the stealth address scheme.
Expand Down Expand Up @@ -98,16 +101,15 @@ contract ERC6538Registry {
recoveredAddress = ecrecover(dataHash, v, r, s);
}

require(
if (
(
(recoveredAddress != address(0) && recoveredAddress == registrant)
|| (
(recoveredAddress == address(0) || recoveredAddress != registrant)
&& (
IERC1271(registrant).isValidSignature(dataHash, signature)
== IERC1271.isValidSignature.selector
!= IERC1271.isValidSignature.selector
)
),
"Invalid signature"
);
)
) revert ERC6538Registry__InvalidSignature();

stealthMetaAddressOf[registrant][schemeId] = stealthMetaAddress;
emit StealthMetaAddressSet(registrant, schemeId, stealthMetaAddress);
Expand Down
4 changes: 3 additions & 1 deletion test/ERC6538Registry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {Deploy} from "script/Deploy.s.sol";
contract ERC6538RegistryTest is Test, Deploy {
SigUtils internal sigUtils;

error ERC6538Registry__InvalidSignature();

event StealthMetaAddressSet(
address indexed registrant, uint256 indexed schemeId, bytes stealthMetaAddress
);
Expand Down Expand Up @@ -153,7 +155,7 @@ contract RegisterKeysOnBehalf_Address is ERC6538RegistryTest {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);
bytes memory signature = abi.encodePacked(r, s, v);

vm.expectRevert("Invalid signature");
vm.expectRevert(ERC6538Registry__InvalidSignature.selector);
registry.registerKeysOnBehalf(registrant, schemeId, signature, stealthMetaAddress);
}

Expand Down

0 comments on commit bce18d4

Please sign in to comment.