Last active
February 3, 2023 12:42
-
-
Save marvelous-007/5aa5048a0f10edddb332c8419f1c9918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.17; | |
// @title ENS: Ethereum Naming system | |
// @author Johnson MArvelous (Prime) | |
// @use a local blockchain i.e Ganache cli for a faster transaction and a better user experience | |
contract ENS_Registry { | |
// @notice this data type holds the mapped address and the owner of the domain name | |
struct ENS { | |
address owner; | |
address mapAddress; | |
} | |
// @notice this maps a node -> ENSrecord | |
// @dev the hashing algorithm used is keccak256 | |
mapping(bytes32 => ENS) public ENSRecord; | |
// @notice this function maps a domain name to an address | |
// @params _domain is the domain name | |
// @params _address is the address mapped to the domain name | |
// @dev person who deployed the smart contract is the defualt owner | |
function addDomainName(string calldata _domain, address _mapAddress) public { | |
ENS storage newDomain = ENSRecord[node(_domain)]; | |
require(newDomain.mapAddress == address(0), "Domain is aleady in use"); | |
newDomain.owner = msg.sender; | |
newDomain.mapAddress = _mapAddress; | |
emit Log(_domain, _mapAddress, "The domain is mapped to this address"); | |
} | |
// @notice this function transfer ownership of the domain name to another address | |
// @param _domain is the domain name | |
// @param _newOwner is the address of the new owner | |
// @dev only the owner can call the transferOwnership func | |
function transferOwnership(string calldata _domain, address _newOwner) public onlyOwner(_domain){ | |
ENS storage myRecord = ENSRecord[node(_domain)]; | |
myRecord.owner = _newOwner; | |
emit Log(_domain, _newOwner, "The owner of the domain name is transfered!"); | |
} | |
// @notice this func transfers ownership to anothe address | |
// @params _domain is the domain name | |
// @params _newAddress is the current address of the owner | |
// @dev only the owner can call this function | |
function changeAddress(string calldata _domain, address _newAddress) public onlyOwner(_domain) { | |
ENS storage myRecord = ENSRecord[node(_domain)]; | |
myRecord.mapAddress = _newAddress; | |
emit Log(_domain, _newAddress, "The address of the domain name is has been changed!"); | |
} | |
// @notice this function convey the address assigned to the domain name | |
// @param _domain is the domain name | |
// @return mapAddress of the owner | |
function getAddress(string calldata _domain) public view returns(address) { | |
return ENSRecord[node(_domain)].mapAddress; | |
} | |
// @notice this regulates access to a function to only owners | |
// @params _domain is the domain name | |
modifier onlyOwner(string calldata _domain) { | |
require(msg.sender == ENSRecord[node(_domain)].owner); | |
_; | |
} | |
// @notice This functions hashes the domain name | |
// @params _domain is the domain name | |
// @return the node uses the keccack256 hashing algorithm | |
function node(string calldata _domain) public pure returns(bytes32) { | |
return keccak256(bytes(_domain)); | |
} | |
// @notice this function emtis after every function is executed | |
event Log(string _domain, address indexed _address, string message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment