Skip to content

Instantly share code, notes, and snippets.

@faytey
Last active February 3, 2023 12:56
Show Gist options
  • Select an option

  • Save faytey/85a807b7101efa7b3df540f5ebc3841b to your computer and use it in GitHub Desktop.

Select an option

Save faytey/85a807b7101efa7b3df540f5ebc3841b to your computer and use it in GitHub Desktop.
Testing out the creation of a simple decentralised domain naming service
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @author Faith M. Roberts
/// @title A Simple Decentralized Domain Naming Service Contract.
contract Faytey7Dns {
address public owner;
constructor(){
owner = msg.sender;
}
/// @dev Setting up the complex variable
struct Domain {
string DName;
address Address;
string Duration;
bool exists;
}
// Mapping the struct to a variable
mapping (string => Domain) public nameservice;
mapping (address => bool) public Account;
// Events to be emitted in the contract whenever there's a function call.
event LogRegisteredDomain(string _DName, address _Address, string message);
event LogDomainLookupStatus(bool);
event LogReassignedDomainName(string _DName, string _NewDomain, string message);
// Function to register new domain.
function registerDomain(string memory _DName, address _Address, string memory _Duration) public returns(bool){
require(!Account[msg.sender], "This Account Already Exists!!!");
require(nameservice[_DName].exists == false, "Domain already exists!!!");
Domain memory _newDomain = Domain(_DName,_Address,_Duration, true);
nameservice[_DName] = _newDomain;
emit LogRegisteredDomain(_DName,_Address, "Successfully registered");
Account[msg.sender] = true;
return true;
}
// Function to look up proposed names availability
function domainNameLookup(string memory _DName) public view returns (address, string memory){
require(nameservice[_DName].exists == true, "This Domain is currently Available");
return(nameservice[_DName].Address,nameservice[_DName].Duration);
}
// Modifiers can be used to change the body of a function.
// If this modifier is used, it will prepend a check that only passes
// if the function is called from the owners address.
modifier OnlyOwner(){
require(isOwner());
_;
}
// This function is used to confirm the caller of
// the reassign address name is the owner.
function isOwner() public view returns(bool){
return msg.sender == owner;
}
//Function to change the domain name to an available name
function reassignAddressName(string memory _DName, string memory _NewDomain) public OnlyOwner returns(bool) {
require(nameservice[_DName].exists == true, "This Domain is currently Available");
nameservice[_DName].DName = _NewDomain;
emit LogReassignedDomainName(_DName, _NewDomain, "Successfully reassigned");
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment