Skip to content

Commit

Permalink
moved hub-domain contracts to contracts/contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jabyl committed Jun 21, 2024
1 parent 5c36a34 commit d594054
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
69 changes: 69 additions & 0 deletions contracts/hub-contracts/HubDomainsRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./IHubDomains.sol";
import "./IHubDomainsRegistry.sol";

contract HubDomainsRegistry is IHubDomains, IHubDomainsRegistry, Ownable {
struct Domain {
string name;
address owner;
string metadataUri;
}

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

event DomainRegistered(address indexed owner, string domain, uint256 tokenId);

function registerDomain(string calldata domain, string calldata metadataUri) external override(IHubDomains, IHubDomainsRegistry) {
require(domains[domain].owner == address(0), "Domain already registered");
require(_isValidDomain(domain), "Invalid domain format");

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

emit DomainRegistered(msg.sender, domain, tokenIdCounter);
}

function resolveDomain(string calldata domain) external view override(IHubDomains, IHubDomainsRegistry) returns (address) {
return domains[domain].owner;
}

function getDomainMetadata(string calldata domain) external view override(IHubDomains, IHubDomainsRegistry) returns (string memory) {
return domains[domain].metadataUri;
}

function ownerOf(uint256 tokenId) external view override(IHubDomains, IHubDomainsRegistry) returns (address owner) {
return domains[tokenIdToDomain[tokenId]].owner;
}

function _isValidDomain(string memory domain) internal pure returns (bool) {
bytes memory b = bytes(domain);
if (b.length == 0 || b.length > 20) return false; // Adjust length as needed
if (!(_endsWithHub(domain))) 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)) { // a-z
return false;
}
}
return true;
}

function _endsWithHub(string memory domain) internal pure returns (bool) {
bytes memory b = bytes(domain);
return b.length >= 4 &&
b[b.length - 1] == 'b' &&
b[b.length - 2] == 'u' &&
b[b.length - 3] == 'h' &&
b[b.length - 4] == '.';
}
}
11 changes: 11 additions & 0 deletions contracts/hub-contracts/IHubDomains.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IHubDomains {
event DomainRegistered(address indexed owner, string domain, uint256 tokenId);

function registerDomain(string calldata domain, string calldata metadataUri) external;
function resolveDomain(string calldata domain) external view returns (address);
function getDomainMetadata(string calldata domain) external view returns (string memory);
function ownerOf(uint256 tokenId) external view returns (address owner);
}
9 changes: 9 additions & 0 deletions contracts/hub-contracts/IHubDomainsRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IHubDomainsRegistry {
function registerDomain(string calldata domain, string calldata metadataUri) external;
function resolveDomain(string calldata domain) external view returns (address);
function getDomainMetadata(string calldata domain) external view returns (string memory);
function ownerOf(uint256 tokenId) external view returns (address owner);
}
20 changes: 20 additions & 0 deletions contracts/hub-contracts/PublicResolver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./HubDomainsRegistry.sol";

contract PublicResolver {
HubDomainsRegistry public hubDomainsRegistry;

constructor(address _hubDomainsRegistry) {
hubDomainsRegistry = HubDomainsRegistry(_hubDomainsRegistry);
}

function resolveDomain(string calldata domain) external view returns (address) {
return hubDomainsRegistry.resolveDomain(domain);
}

function getDomainMetadata(string calldata domain) external view returns (string memory) {
return hubDomainsRegistry.getDomainMetadata(domain);
}
}

0 comments on commit d594054

Please sign in to comment.