From 502a8970f5c4f8ffc003fc7e3d52ebd2cc2f4327 Mon Sep 17 00:00:00 2001 From: Phoenix Date: Wed, 21 Sep 2022 20:10:31 +0530 Subject: [PATCH] format code and make contract ownable with custom hashDigit length --- contract/Ethereum.sol | 58 ++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/contract/Ethereum.sol b/contract/Ethereum.sol index fb09863..d9ab72f 100644 --- a/contract/Ethereum.sol +++ b/contract/Ethereum.sol @@ -8,30 +8,61 @@ contract ENS { event Sent_Eth(address sender, address receiever, uint256 amount); + //Owner of the contract will have extra previliges + address private immutable owner; + // Mapping system that connects name to address - mapping(string => address) user; + mapping(string => address) private user; // A variable to check if the address has a name based of the state of its boolen - mapping(address => bool) public wallet_Address; + mapping(address => bool) private wallet_Address; // A variable to store a 5 digit minified address as we will use it to protect against errors that can be generated from capitalization as James and james are two different things entirely - mapping(uint => address) minified_Address; + mapping(uint => address) private minified_Address; //We will be using this variable to store the amount of digit we will be using for minified address and since it is a constant we store it directly with 5 - uint hashdigit = 5; + uint public hashdigit = 5; //Modulus variable to make it 10 ^ 8 - uint hashModulus = 10 ** hashdigit; + uint private hashModulus = 10 ** hashdigit; + + constructor(uint _hashdigit) { + if(_hashdigit > 0) { + hashdigit = _hashdigit; + hashModulus = 10 ** hashdigit; + } + owner = msg.sender; + } + + modifier onlyOwner() { + require(msg.sender == owner,'Admin access is required to call this method'); + _; + } + /** - @dev allows unregistered users to create a profile with a username - @param _username username of user - */ + * @dev allow owner to update the hashDigit + * @param _hashdigit hashDigit of the unique identifier + */ + function updateHashDigits(uint _hashdigit) onlyOwner public { + require(_hashdigit > 0, "Invalid input provided"); + + hashdigit = _hashdigit; + hashModulus = 10 ** hashdigit; + } + + function getOwner() public view returns(address) { + return owner; + } + /** + @dev allows unregistered users to create a profile with a username + @param _username username of user + */ // This is three step process where you would sumbit username, use the same name to get a uniue digit code and also verify your digit code/ unique id - function submitUsername(string memory _username) public returns(uint) { + function submitUsername(string memory _username) public { require(bytes(_username).length > 0, "Empty username"); require( wallet_Address[msg.sender] == false, @@ -47,18 +78,15 @@ contract ENS { ); minified_Address[min_Digits] = msg.sender; wallet_Address[msg.sender] = true; - return min_Digits; + emit NewUser(_username, msg.sender); } // Input the name to obtain your unique ID function getDigitCode(string memory _username) public view returns (uint){ - - uint pack_string = uint(keccak256(abi.encodePacked(_username))); - - uint min_Digits = pack_string % hashModulus; - + uint pack_string = uint(keccak256(abi.encodePacked(_username))); + uint min_Digits = pack_string % hashModulus; return min_Digits; }