From cc8b10a4b08681212363bbea4887c15a7adcc847 Mon Sep 17 00:00:00 2001 From: zeroknots Date: Wed, 14 Feb 2024 13:00:15 +0700 Subject: [PATCH 1/4] feat: adding default resolverUID --- src/DataTypes.sol | 2 +- src/Registry.sol | 2 +- src/core/ModuleManager.sol | 2 -- src/core/ResolverManager.sol | 5 +++++ src/lib/ModuleDeploymentLib.sol | 2 -- src/lib/StubLib.sol | 2 +- test/ModuleRegistration.t.sol | 13 ++++++++++++- 7 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/DataTypes.sol b/src/DataTypes.sol index d5e95e07..d8464e78 100644 --- a/src/DataTypes.sol +++ b/src/DataTypes.sol @@ -34,7 +34,7 @@ struct SchemaRecord { } struct ResolverRecord { - IExternalResolver resolver; // Optional resolver. + IExternalResolver resolver; // resolver. address resolverOwner; // The address of the account used to register the resolver. } diff --git a/src/Registry.sol b/src/Registry.sol index 25e3b0d2..3beeb228 100644 --- a/src/Registry.sol +++ b/src/Registry.sol @@ -1,8 +1,8 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.24; -import { SignedAttestation } from "./core/SignedAttestation.sol"; import { IRegistry } from "./IRegistry.sol"; +import { SignedAttestation } from "./core/SignedAttestation.sol"; /** * @author zeroknots */ diff --git a/src/core/ModuleManager.sol b/src/core/ModuleManager.sol index 4da58042..12558f1f 100644 --- a/src/core/ModuleManager.sol +++ b/src/core/ModuleManager.sol @@ -131,8 +131,6 @@ abstract contract ModuleManager is IRegistry, ResolverManager { internal returns (ModuleRecord memory moduleRegistration) { - // ensure that non-zero resolverUID was provided - if (resolverUID == EMPTY_RESOLVER_UID) revert InvalidDeployment(); // ensure moduleAddress is not already registered if ($moduleAddrToRecords[moduleAddress].resolverUID != EMPTY_RESOLVER_UID) { revert AlreadyRegistered(moduleAddress); diff --git a/src/core/ResolverManager.sol b/src/core/ResolverManager.sol index f4e4f065..4c84e7d9 100644 --- a/src/core/ResolverManager.sol +++ b/src/core/ResolverManager.sol @@ -12,6 +12,11 @@ abstract contract ResolverManager is IRegistry { mapping(ResolverUID uid => ResolverRecord resolver) internal $resolvers; + constructor() { + ResolverRecord storage $resolver = $resolvers[ResolverUID.wrap(bytes32(0))]; + $resolver.resolverOwner = address(this); + } + /** * @dev Modifier to require that the caller is the owner of a resolver * diff --git a/src/lib/ModuleDeploymentLib.sol b/src/lib/ModuleDeploymentLib.sol index b9b41b8b..ab9b70da 100644 --- a/src/lib/ModuleDeploymentLib.sol +++ b/src/lib/ModuleDeploymentLib.sol @@ -71,6 +71,4 @@ library ModuleDeploymentLib { ) ); } - - error InvalidDeployment(); } diff --git a/src/lib/StubLib.sol b/src/lib/StubLib.sol index a79a6db7..4c87e9bc 100644 --- a/src/lib/StubLib.sol +++ b/src/lib/StubLib.sol @@ -108,7 +108,7 @@ library StubLib { { IExternalResolver resolverContract = $resolver.resolver; - if (address(resolverContract) != ZERO_ADDRESS) return; + if (address(resolverContract) == ZERO_ADDRESS) return; if (resolverContract.resolveModuleRegistration({ sender: msg.sender, moduleAddress: moduleAddress, record: moduleRecord }) == false) { diff --git a/test/ModuleRegistration.t.sol b/test/ModuleRegistration.t.sol index ecfad1b7..d819cd4d 100644 --- a/test/ModuleRegistration.t.sol +++ b/test/ModuleRegistration.t.sol @@ -42,7 +42,7 @@ contract ModuleRegistrationTest is BaseTest { function test_WhenRegisteringAModuleOnAnInvalidResolverUID() external prankWithAccount(moduleDev1) { MockModule newModule = new MockModule(); // It should revert. - ResolverUID invalidUID = ResolverUID.wrap(hex"00"); + ResolverUID invalidUID = ResolverUID.wrap(hex"01"); vm.expectRevert(abi.encodeWithSelector(IRegistry.InvalidResolver.selector, address(0))); registry.registerModule(invalidUID, address(newModule), ""); @@ -103,4 +103,15 @@ contract ModuleRegistrationTest is BaseTest { vm.expectRevert(); registry.deployViaFactory(address(registry), "", "", defaultResolverUID); } + + function test_WhenUsingDefaultResolverUID() public prankWithAccount(moduleDev1) { + bytes32 salt = bytes32(abi.encodePacked(address(moduleDev1.addr), bytes12(0))); + + bytes memory bytecode = type(MockModule).creationCode; + + ResolverUID nullUID = ResolverUID.wrap(bytes32(0)); + address moduleAddr = registry.deployModule(salt, nullUID, bytecode, ""); + ModuleRecord memory record = registry.findModule(moduleAddr); + assertTrue(record.resolverUID == nullUID); + } } From c976611275bedc221a4ad019eee9234042f70e5e Mon Sep 17 00:00:00 2001 From: zeroknots Date: Wed, 14 Feb 2024 13:04:20 +0700 Subject: [PATCH 2/4] chore: fix checksc --- src/core/ModuleManager.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/ModuleManager.sol b/src/core/ModuleManager.sol index 12558f1f..956e265a 100644 --- a/src/core/ModuleManager.sol +++ b/src/core/ModuleManager.sol @@ -49,6 +49,7 @@ abstract contract ModuleManager is IRegistry, ResolverManager { returns (address moduleAddress) { ResolverRecord storage $resolver = $resolvers[resolverUID]; + // ensure that existing resolverUID was provided if ($resolver.resolverOwner == ZERO_ADDRESS) revert InvalidResolver($resolver.resolver); moduleAddress = initCode.deploy(salt); @@ -72,8 +73,7 @@ abstract contract ModuleManager is IRegistry, ResolverManager { */ function registerModule(ResolverUID resolverUID, address moduleAddress, bytes calldata metadata) external { ResolverRecord storage $resolver = $resolvers[resolverUID]; - - // ensure that non-zero resolverUID was provided + // ensure that existing resolverUID was provided if ($resolver.resolverOwner == ZERO_ADDRESS) revert InvalidResolver($resolver.resolver); ModuleRecord memory record = _storeModuleRecord({ @@ -101,7 +101,7 @@ abstract contract ModuleManager is IRegistry, ResolverManager { returns (address moduleAddress) { ResolverRecord storage $resolver = $resolvers[resolverUID]; - if ($resolver.resolverOwner == ZERO_ADDRESS) revert InvalidResolverUID(resolverUID); + if ($resolver.resolverOwner == ZERO_ADDRESS) revert InvalidResolver($resolver.resolver); // prevent someone from calling a registry function pretending its a factory if (factory == address(this)) revert FactoryCallFailed(factory); // call external factory to deploy module From 5f95684c681129d2a18dfe24f2f2650c8184cf7f Mon Sep 17 00:00:00 2001 From: zeroknots Date: Wed, 14 Feb 2024 13:31:27 +0700 Subject: [PATCH 3/4] chore: removing comment --- src/Registry.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Registry.sol b/src/Registry.sol index 3beeb228..d569946c 100644 --- a/src/Registry.sol +++ b/src/Registry.sol @@ -7,7 +7,4 @@ import { SignedAttestation } from "./core/SignedAttestation.sol"; * @author zeroknots */ -contract Registry is IRegistry, SignedAttestation { -// TODO: should we create a default resolverUID thats address(0). -// this will allow the registry to be usable right after deployment without any resolver -} +contract Registry is IRegistry, SignedAttestation { } From 2d2c3cb8c9da8de45729def3b7732113d3f6b049 Mon Sep 17 00:00:00 2001 From: zeroknots Date: Wed, 14 Feb 2024 13:37:20 +0700 Subject: [PATCH 4/4] docs: source of code --- src/lib/ModuleDeploymentLib.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/ModuleDeploymentLib.sol b/src/lib/ModuleDeploymentLib.sol index ab9b70da..ea7efa27 100644 --- a/src/lib/ModuleDeploymentLib.sol +++ b/src/lib/ModuleDeploymentLib.sol @@ -5,11 +5,11 @@ pragma solidity ^0.8.24; * @title ModuleDeploymentLib * @dev A library that can be used to deploy the Registry * @author zeroknots + * source: https://github.com/0age/metamorphic/blob/master/contracts/ImmutableCreate2Factory.sol#L194-L203 */ library ModuleDeploymentLib { error InvalidSalt(); error InvalidAddress(); - // source: https://github.com/0age/metamorphic/blob/master/contracts/ImmutableCreate2Factory.sol#L194-L203 modifier containsCaller(bytes32 salt) { // prevent contract submissions from being stolen from tx.pool by requiring