Last active
February 3, 2023 16:59
-
-
Save Ucheokolo/6cbfbdc7ba01dcf91566c8bcd63d7574 to your computer and use it in GitHub Desktop.
A simple Ens name verification smart contract: Web3bridge Assignment
This file contains hidden or 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
| @Uche | |
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.0; | |
| contract ensdomain { | |
| mapping(address => bytes) public addressToBytesName; | |
| mapping(bytes => address) public bytesNameToAddress; | |
| string[] public nameList; | |
| uint256 public totalRegAddress = 0; | |
| event resgisterAlert(address, string); | |
| event nameChange(address, string); | |
| function register(address _address, string memory _name) public { | |
| bytes memory _empty; | |
| bytes memory callerName = addressToBytesName[_address]; | |
| require( | |
| keccak256(abi.encodePacked(callerName)) == | |
| keccak256(abi.encodePacked(_empty)), | |
| "This address has an existing name" | |
| ); | |
| require( | |
| bytesNameToAddress[bytes(_name)] == address(0), | |
| "This name is Taken" | |
| ); | |
| addressToBytesName[_address] = bytes(_name); | |
| bytesNameToAddress[bytes(_name)] = _address; | |
| nameList.push(_name); | |
| totalRegAddress += 1; | |
| emit resgisterAlert(_address, _name); | |
| } | |
| function getOwner(string memory _name) public view returns (address) { | |
| return (bytesNameToAddress[bytes(_name)]); | |
| } | |
| function getNameFromAddress(address _address) | |
| public | |
| view | |
| returns (string memory) | |
| { | |
| return string(addressToBytesName[_address]); | |
| } | |
| function changeName(string memory _newName) public { | |
| bytes memory callerName = addressToBytesName[msg.sender]; | |
| bytes memory _empty; | |
| require( | |
| keccak256(abi.encodePacked(callerName)) != | |
| keccak256(abi.encodePacked(_empty)), | |
| "You dont have any name assigned to you" | |
| ); | |
| delete (addressToBytesName[msg.sender]); | |
| delete (bytesNameToAddress[callerName]); | |
| totalRegAddress -= 1; | |
| register(msg.sender, _newName); | |
| emit nameChange(msg.sender, _newName); | |
| } | |
| function allNames() public view returns(string[] memory){ | |
| return nameList; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment