Skip to content

Instantly share code, notes, and snippets.

@ginika-chinonso
Last active February 4, 2023 01:25
Show Gist options
  • Save ginika-chinonso/7297ce12609bb968983be4b833b2e926 to your computer and use it in GitHub Desktop.
Save ginika-chinonso/7297ce12609bb968983be4b833b2e926 to your computer and use it in GitHub Desktop.
Mini ENS smart contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ENS {
mapping(address => bytes) public AddressToBytesNames;
mapping(bytes => address) public bytesNameToAddress;
uint public TotalRegAddr = 0;
event registerAlert(address, string);
event NameChange(address, string);
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;
TotalRegAddr += 1;
emit registerAlert(_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");
delete(AddressToBytesNames[msg.sender]);
delete(bytesNameToAddress[callerName]);
TotalRegAddr -= 1;
register(msg.sender, _newName);
emit NameChange(msg.sender, _newName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment