Skip to content

Commit

Permalink
fix: DomainRegistry (#127)
Browse files Browse the repository at this point in the history
* refactor: registerDomain, uri

* feat: erc721 hubDomainsRegistry
  • Loading branch information
pegahcarter authored Oct 23, 2024
1 parent 5051b47 commit 08f7492
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 80 deletions.
35 changes: 15 additions & 20 deletions contracts/hub/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {IMembership} from "../membership/IMembership.sol";
import {HubUtils} from "./HubUtils.sol";
import {IHub} from "./interfaces/IHub.sol";
import {ITaskManager} from "../tasks/interfaces/ITaskManager.sol";
import {IHubDomainsRegistry} from "./interfaces/IHubDomainsRegistry.sol";
import {Domain, IHubDomainsRegistry} from "./interfaces/IHubDomainsRegistry.sol";

/*
TODO:
- Are market, commitment, metadataUri modifiable?
- Are market, commitment, uri modifiable?
- Should the deployer be allowed to transfer their deployer role ownership?
- max/min values of parameters
*/
Expand Down Expand Up @@ -47,7 +47,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
uint256 public commitment;
uint256 public archetype;
uint256 public market;
string public metadataUri;
string public uri;

uint32 public initTimestamp;
uint32 public initPeriodId;
Expand All @@ -73,7 +73,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
uint256[] calldata roles_,
uint256 _market,
uint256 _commitment,
string memory _metadataUri
string memory _uri
) external initializer {
// ownership
__Ownable_init(_initialOwner);
Expand All @@ -88,7 +88,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
_setRoles(roles_);
_setMarket(_market);
_setCommitment(_commitment);
_setMetadataUri(_metadataUri);
_setUri(_uri);

initTimestamp = uint32(block.timestamp);
period0Start = IGlobalParameters(_globalParameters).period0Start();
Expand Down Expand Up @@ -229,13 +229,9 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// HUB-MANAGEMENT
// -----------------------------------------------------------

function registerDomain(
string calldata domain_,
address hubAddress_,
string calldata metadataUri_
) external onlyOwner {
function registerDomain(string calldata _name, string calldata _uri) external onlyOwner {
// also revert if not deployer
IHubDomainsRegistry(hubDomainsRegistry).registerDomain(domain_, hubAddress_, metadataUri_);
IHubDomainsRegistry(hubDomainsRegistry).registerDomain(_name, _uri, owner());
}

/// @inheritdoc IHub
Expand Down Expand Up @@ -284,9 +280,9 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
emit ArchetypeSet(input[0]);
}

function setMetadataUri(string memory metadataUri_) external {
function setUri(string memory _uri) external {
if (!isAdmin(msg.sender)) revert NotAdmin();
_setMetadataUri(metadataUri_);
_setUri(_uri);
}

/// internal
Expand All @@ -313,12 +309,12 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
emit CommitmentSet(commitment_);
}

function _setMetadataUri(string memory metadataUri_) internal {
_revertForInvalidMetadataUri(metadataUri_);
function _setUri(string memory _uri) internal {
_revertForInvalidMetadataUri(_uri);

metadataUri = metadataUri_;
uri = _uri;

emit MetadataUriSet(metadataUri_);
emit MetadataUriSet(_uri);
}

function _addUrl(string memory url) internal {
Expand Down Expand Up @@ -359,9 +355,8 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
// VIEWS
// -----------------------------------------------------------

// TODO: figure these out
function getDomain(string calldata domain) external view returns (address, string memory) {
return IHubDomainsRegistry(hubDomainsRegistry).getDomain(domain);
function getDomain() external view returns (Domain memory) {
return IHubDomainsRegistry(hubDomainsRegistry).getDomain(address(this));
}

function getUrls() external view returns (string[] memory) {
Expand Down
99 changes: 45 additions & 54 deletions contracts/hub/HubDomainsRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "./interfaces/IHubDomainsRegistry.sol";
import "./interfaces/IHubRegistry.sol";

// TODO: register properly from a hub
// TODO: is verifier needed?
contract HubDomainsRegistry is IHubDomainsRegistry, OwnableUpgradeable {
struct Domain {
string name;
address hubAddress;
address verifier;
string metadataUri;
}
// import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {Domain, IHubDomainsRegistry} from "./interfaces/IHubDomainsRegistry.sol";
import {IHubRegistry} from "./interfaces/IHubRegistry.sol";

contract HubDomainsRegistry is IHubDomainsRegistry, Initializable, ERC721Upgradeable {
mapping(address => Domain) public domains;
mapping(string => address) public nameToHub;
mapping(uint256 => address) private tokenIdToHub;

mapping(string => Domain) private domains;
mapping(address => string[]) private hubAddressToDomains;
mapping(uint256 => string) private tokenIdToDomain;
uint256 private tokenIdCounter;
uint256 private tokenId;
address private hubRegistry;

event DomainRegistered(
address indexed hubAddress,
address verifier,
string domain,
uint256 tokenId,
string metadataUri
);
event DomainRegistered(address indexed hub, uint256 indexed tokenId, string name, string uri);

constructor() {
_disableInitializers();
}

function initialize(address _hubRegistry) external initializer {
__Ownable_init(msg.sender);
function initialize(
address _hubRegistry,
string memory name_,
string memory symbol_
) external initializer {
hubRegistry = _hubRegistry;

__ERC721_init(name_, symbol_);
}

modifier onlyFromHub() {
Expand All @@ -46,53 +38,52 @@ contract HubDomainsRegistry is IHubDomainsRegistry, OwnableUpgradeable {

/// @inheritdoc IHubDomainsRegistry
function registerDomain(
string calldata domain,
address hubAddress,
string calldata metadataUri
) external override(IHubDomainsRegistry) onlyFromHub {
require(domains[domain].hubAddress == address(0), "Domain already registered");
require(hubAddressToDomains[hubAddress].length == 0, "Domain already registered");
require(_isValidDomain(domain), "Invalid domain format");

tokenIdCounter++;
domains[domain] = Domain(domain, hubAddress, msg.sender, metadataUri);
hubAddressToDomains[hubAddress].push(domain);
tokenIdToDomain[tokenIdCounter] = domain;

emit DomainRegistered(hubAddress, msg.sender, domain, tokenIdCounter, metadataUri);
string calldata _name,
string calldata _uri,
address _owner
) external onlyFromHub {
require(domains[msg.sender].tokenId == 0, "Domain already registered");
require(_isValidDomain(_name), "Invalid _name format");

uint256 tokenId_ = ++tokenId; // gas
domains[msg.sender] = Domain({tokenId: tokenId_, name: _name, uri: _uri});
nameToHub[_name] = msg.sender;
tokenIdToHub[tokenId_] = msg.sender;

_mint(_owner, tokenId_);

emit DomainRegistered({hub: msg.sender, tokenId: tokenId_, name: _name, uri: _uri});
}

/// @inheritdoc IHubDomainsRegistry
function getDomain(string calldata domain) external view returns (address, string memory) {
return (domains[domain].hubAddress, domains[domain].metadataUri);
function getDomain(address hub) external view returns (Domain memory) {
return domains[hub];
}

function verifierOf(uint256 tokenId) external view returns (address verifier) {
return domains[tokenIdToDomain[tokenId]].verifier;
function getHubByName(string memory name) external view returns (address) {
return nameToHub[name];
}

function _isValidDomain(string memory domain) internal pure returns (bool) {
bytes memory b = bytes(domain);
function _isValidDomain(string memory _name) internal pure returns (bool) {
bytes memory b = bytes(_name);
if (b.length == 0 || b.length > 20) return false; // Adjust length as needed
if (!(_endsWithHub(domain))) return false; // Ends with ".hub"
if (!(_endsWithHub(_name))) return false; // Ends with ".hub"
for (uint i; i < b.length - 4; i++) {
// Skip ".hub"
if (
!(b[i] >= 0x30 && b[i] <= 0x39) && // 0-9
!(b[i] >= 0x41 && b[i] <= 0x5A) && // A-Z
!(b[i] >= 0x61 && b[i] <= 0x7A)
!(b[i] >= 0x61 && b[i] <= 0x7A) // a-z
) {
// a-z
return false;
}
}
return true;
}

function _endsWithHub(string memory domain) internal pure returns (bool) {
bytes memory b = bytes(domain);
function _endsWithHub(string memory _name) internal pure returns (bool) {
bytes memory b = bytes(_name);
return
b.length >= 4 &&
b.length > 4 &&
b[b.length - 1] == "b" &&
b[b.length - 2] == "u" &&
b[b.length - 3] == "h" &&
Expand Down
2 changes: 1 addition & 1 deletion contracts/hub/interfaces/IHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ interface IHub {
function archetype() external view returns (uint256);
function commitment() external view returns (uint256);
function market() external view returns (uint256);
function metadataUri() external view returns (string memory);
function uri() external view returns (string memory);

// Mutative

Expand Down
16 changes: 12 additions & 4 deletions contracts/hub/interfaces/IHubDomainsRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

struct Domain {
uint256 tokenId;
string name;
string uri;
}

interface IHubDomainsRegistry {
/// @notice register an "X.hub" domain
/// @dev must be called through the hub
function registerDomain(string calldata domain, address hubAddress, string calldata metadataUri) external;
function registerDomain(string calldata _name, string calldata _uri, address _owner) external;

/// @notice get the Domain of a given hub
function getDomain(address hub) external view returns (Domain memory);

/// @notice get hub and uri associated to the "X.hub" domain
function getDomain(string calldata domain) external view returns (address, string memory);
function verifierOf(uint256 tokenId) external view returns (address verifier);
/// @notice Get the hub of a given "X.hub" name
function getHubByName(string memory name) external view returns (address);
}
2 changes: 1 addition & 1 deletion script/DeployAll.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract DeployAll is Script {
autId.transferOwnership(owner);

// init hubDomainsRegistry now that hubRegistry is deployed
hubDomainsRegistry.initialize(address(hubRegistry));
hubDomainsRegistry.initialize(address(hubRegistry), "Hub Domains Registry", "HDR");

// Setup initial tasks
Task[] memory tasks = new Task[](3);
Expand Down

0 comments on commit 08f7492

Please sign in to comment.