Skip to content

Commit

Permalink
Merge pull request #6 from ensdomains/rootregistry2
Browse files Browse the repository at this point in the history
RootRegistry: Add support for setting URI
  • Loading branch information
Arachnid authored Sep 13, 2024
2 parents d6f5f7d + cd24132 commit 6f59ef9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
19 changes: 16 additions & 3 deletions contracts/src/registry/RootRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import {BaseRegistry} from "./BaseRegistry.sol";
contract RootRegistry is LockableRegistry, AccessControl {
bytes32 public constant TLD_ISSUER_ROLE = keccak256("TLD_ISSUER_ROLE");

mapping(uint256 tokenId=>string) uris;

constructor(IRegistryDatastore _datastore) LockableRegistry(_datastore) {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

function uri(uint256 /*id*/ ) public pure override returns (string memory) {
return "";
function uri(uint256 tokenId ) public view override returns (string memory) {
return uris[tokenId];
}

/**
Expand All @@ -27,15 +29,18 @@ contract RootRegistry is LockableRegistry, AccessControl {
* @param owner The new owner of the TLD token.
* @param registry The address of the registry to use.
* @param flags Flags to set.
* @param _uri URI for TLD metadata.
*/
function mint(string calldata label, address owner, IRegistry registry, uint96 flags)
function mint(string calldata label, address owner, IRegistry registry, uint96 flags, string memory _uri)
external
onlyRole(TLD_ISSUER_ROLE)
returns(uint256 tokenId)
{
tokenId = uint256(keccak256(bytes(label)));
_mint(owner, tokenId, 1, "");
datastore.setSubregistry(tokenId, address(registry), flags);
uris[tokenId] = _uri;
emit URI(_uri, tokenId);
emit NewSubname(label);
}

Expand All @@ -62,6 +67,14 @@ contract RootRegistry is LockableRegistry, AccessControl {
return _lock(tokenId, flags);
}

function setUri(uint256 tokenId, string memory _uri)
external
onlyTokenOwner(tokenId)
{
emit URI(_uri, tokenId);
uris[tokenId] = _uri;
}

function supportsInterface(bytes4 interfaceId) public view override(BaseRegistry, AccessControl) returns (bool) {
return super.supportsInterface(interfaceId);
}
Expand Down
40 changes: 31 additions & 9 deletions contracts/test/RootRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract TestRootRegistry is Test, ERC1155Holder {
vm.expectEmit(true, true, true, true);
emit TransferSingle(address(this), address(0), address(this), expectedId, 1);

uint256 tokenId = registry.mint("test2", address(this), registry, 0);
uint256 tokenId = registry.mint("test2", address(this), registry, 0, "");
vm.assertEq(tokenId, expectedId);
uint96 flags = registry.flags(tokenId);
vm.assertEq(flags, 0);
Expand All @@ -38,15 +38,15 @@ contract TestRootRegistry is Test, ERC1155Holder {
vm.expectEmit(true, true, true, true);
emit TransferSingle(address(this), address(0), address(this), expectedId, 1);

uint256 tokenId = registry.mint("test2", address(this), registry, flags);
uint256 tokenId = registry.mint("test2", address(this), registry, flags, "");
vm.assertEq(tokenId, expectedId);
uint96 actualFlags = registry.flags(tokenId);
vm.assertEq(flags, actualFlags);
}

function test_lock_name() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED() | registry.FLAG_RESOLVER_LOCKED();
uint256 tokenId = registry.mint("test2", address(this), registry, 0);
uint256 tokenId = registry.mint("test2", address(this), registry, 0, "");
uint96 actualFlags = registry.lock(tokenId, flags);
vm.assertEq(flags, actualFlags);
uint96 actualFlags2 = registry.flags(tokenId);
Expand All @@ -56,40 +56,62 @@ contract TestRootRegistry is Test, ERC1155Holder {
function test_cannot_unlock_name() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED() | registry.FLAG_RESOLVER_LOCKED();

uint256 tokenId = registry.mint("test2", address(this), registry, flags);
uint256 tokenId = registry.mint("test2", address(this), registry, flags, "");
uint96 newFlags = registry.lock(tokenId, 0);
vm.assertEq(flags, newFlags);
uint96 newFlags2 = registry.flags(tokenId);
vm.assertEq(flags, newFlags2);
}

function test_set_subregistry() public {
uint256 tokenId = registry.mint("test", address(this), registry, 0);
uint256 tokenId = registry.mint("test", address(this), registry, 0, "");
registry.setSubregistry(tokenId, IRegistry(address(this)));
vm.assertEq(address(registry.getSubregistry("test")), address(this));
}

function testFail_cannot_set_locked_subregistry() public {
uint96 flags = registry.FLAG_SUBREGISTRY_LOCKED();
uint256 tokenId = registry.mint("test", address(this), registry, flags);
uint256 tokenId = registry.mint("test", address(this), registry, flags, "");
registry.setSubregistry(tokenId, IRegistry(address(this)));
}

function test_set_resolver() public {
uint256 tokenId = registry.mint("test", address(this), registry, 0);
uint256 tokenId = registry.mint("test", address(this), registry, 0, "");
registry.setResolver(tokenId, address(this));
vm.assertEq(address(registry.getResolver("test")), address(this));
}

function testFail_cannot_set_locked_resolver() public {
uint96 flags = registry.FLAG_RESOLVER_LOCKED();
uint256 tokenId = registry.mint("test", address(this), registry, flags);
uint256 tokenId = registry.mint("test", address(this), registry, flags, "");
registry.setResolver(tokenId, address(this));
}

function testFail_cannot_set_locked_flags() public {
uint96 flags = registry.FLAG_FLAGS_LOCKED();
uint256 tokenId = registry.mint("test", address(this), registry, flags);
uint256 tokenId = registry.mint("test", address(this), registry, flags, "");
registry.lock(tokenId, registry.FLAG_RESOLVER_LOCKED());
}

function test_set_uri() public {
string memory uri = "https://example.com/";
uint256 tokenId = registry.mint("test2", address(this), registry, 0, uri);
string memory actualUri = registry.uri(tokenId);
vm.assertEq(actualUri, uri);

uri = "https://ens.domains/";
registry.setUri(tokenId, uri);
actualUri = registry.uri(tokenId);
vm.assertEq(actualUri, uri);
}

function testFail_cannot_set_unauthorized_uri() public {
string memory uri = "https://example.com/";
uint256 tokenId = registry.mint("test2", address(registry), registry, 0, uri);
string memory actualUri = registry.uri(tokenId);
vm.assertEq(actualUri, uri);

uri = "https://ens.domains/";
registry.setUri(tokenId, uri);
}
}
1 change: 1 addition & 0 deletions contracts/test/fixtures/deployEnsFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function deployEnsFixture() {
accounts[0].address,
ethRegistry.address,
1n,
"https://example.com/"
]);

return {
Expand Down

0 comments on commit 6f59ef9

Please sign in to comment.