Skip to content

Instantly share code, notes, and snippets.

@ginika-chinonso
Last active February 4, 2023 02:26
Show Gist options
  • Save ginika-chinonso/124418cca1e531f55839b17b72a7c28e to your computer and use it in GitHub Desktop.
Save ginika-chinonso/124418cca1e531f55839b17b72a7c28e to your computer and use it in GitHub Desktop.
Mini ENS smart contract with extended functionality
/*
Extended ENS Assignment
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ENS {
address public owner;
mapping(address => bytes) public AddressToBytesNames;
mapping(bytes => address) public bytesNameToAddress;
string[] public allNames;
event NameRegistered(address, string);
event NameChange(address, string);
event NameDeleted(address, string);
constructor(){
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner, "Only owner can call this function");
_;
}
function register(address _address, string memory _name) public {
bytes memory _empty;
bytes memory callerName = AddressToBytesNames[_address];
require(keccak256(abi.encodePacked(callerName)) == keccak256(abi.encodePacked(_empty)), "This address has an existing name");
require(bytesNameToAddress[bytes(_name)] == address(0), "This name has been registered");
AddressToBytesNames[_address] = bytes(_name);
bytesNameToAddress[bytes(_name)] = _address;
allNames.push(_name);
emit NameRegistered(_address, _name);
}
function getOwner(string memory _name) public view returns(address) {
return bytesNameToAddress[bytes(_name)];
}
function getNamefromAddr(address _address) public view returns(string memory) {
return string(AddressToBytesNames[_address]);
}
function changename(string memory _newName) public {
bytes memory callerName = AddressToBytesNames[msg.sender];
bytes memory _empty;
require(keccak256(abi.encodePacked(callerName)) != keccak256(abi.encodePacked(_empty)), "You dont have any name assigned to you");
require(bytesNameToAddress[bytes(_newName)] == address(0), "This name has been registered");
delete(AddressToBytesNames[msg.sender]);
delete(bytesNameToAddress[callerName]);
for(uint i = 0; i < allNames.length; i++){
if (keccak256(abi.encodePacked(allNames[i])) == keccak256(abi.encodePacked(callerName))) {
allNames[i] = _newName;
}
}
AddressToBytesNames[msg.sender] = bytes(_newName);
bytesNameToAddress[bytes(_newName)] = msg.sender;
emit NameChange(msg.sender, _newName);
}
function getAllRegNames() public view returns(string[] memory){
return allNames;
}
function getTotalNamesReg() public view returns(uint){
return allNames.length;
}
function deleteName(string memory _name) onlyOwner public {
address _address = getOwner(_name);
delete(AddressToBytesNames[_address]);
delete(bytesNameToAddress[bytes(_name)]);
for(uint i = 0; i < allNames.length; i++){
if (keccak256(abi.encodePacked(allNames[i] )) == keccak256(abi.encodePacked(_name))) {
string memory _lastName = allNames[allNames.length - 1];
allNames[i] = _lastName;
allNames.pop();
emit NameDeleted(_address, _name);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment