"Phone Book Contract" Example Version 4

In my previous post, any account could add and delete from the contract.
so in this post i will be using "Modifiers" to check that only the creator of the contract can add and remove data but any one can view the phonebook

Modifiers can be used to change the body of a function.

Modifiers let you wrap additional functionality to a method, so they're kind of like the decorator pattern in OOP.



pragma solidity ^0.4.0;

contract PhoneBookAresh{
    //Declare a struct similar to C++ 
    struct Person{
        string fullName;
        string number;
    }
    
    //Events
    event Log(string name, string number);
    
    //An Array of structs similar to C++ (flexible storage) and index start at 0
    Person[] MyPhoneBook;
    //whos phonebook it is. The Value is the Hex value of the account
    address creator; 
    
    //this is a constructor, notice it has the same name as the Contract name
    function PhoneBookAresh() public {
        creator = msg.sender;
    }
    
    //modifier
    modifier onlyBy(address _account)
    {
        require(msg.sender == _account);
        // Do not forget the "_;"! It will
        // be replaced by the actual function
        // body when the modifier is used.
        _;
    }
    
    //Adds an item to the Phonebook using PUSH
    //Only the creator can add phone numbers
    function AddPhone(string _fullName, string _number) public onlyBy(creator)
    {
        //if name doesnt exist. still uses GAS to go thru the loop
        if (CheckName(_fullName) == false)
        {
            MyPhoneBook.push(Person(_fullName,_number));
            Log (_fullName,_number);
        }
        else
        {
            //revert; will return unused gas whereas throw will continue to consume all available gas.
            revert();
        }
    }
    
    //used to remove or set the value to empty/zero
    //only the creator can remove the person from the phonebook
    function RemovePerson(uint _index) public onlyBy(creator){
        Log (MyPhoneBook[_index].fullName,MyPhoneBook[_index].number);
        delete MyPhoneBook[_index];
    }
    
    function GetPerson(uint _index) public view returns (string)          
    {
        return MyPhoneBook[_index].fullName;
    }
    
    function CheckName(string _fullName) public view returns (bool)
    {
        uint PhoneBookLength = MyPhoneBook.length;
         //Must be uint and not int
        for (uint i=0; i< PhoneBookLength; i++)
        {
            //As in Java, the == operator does not compare the literals of two strings.
            if (stringsEqual(MyPhoneBook[i].fullName,_fullName))
            {
                return true;
            }
        }
        return false;
    }
    function GetPhoneNumber(string _fullName) public view returns (string)
    {
        uint PhoneBookLength = MyPhoneBook.length;
    
        //Must be uint and not int
        for (uint i=0; i< PhoneBookLength; i++)
        {
            //As in Java, the == operator does not compare the literals of two strings.
            if (stringsEqual(MyPhoneBook[i].fullName,_fullName))
            {
                return MyPhoneBook[i].number;
            }
        }
    }
    
    //Manually comparing strings function used as a utility function
    //source https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol
    
    //View This is generally the replacement for constant. It indicates that the function will not alter the storage state in any way.
    //Pure This is even more restrictive, indicating that it won't even read the storage state.
    function stringsEqual(string storage _a, string memory _b)  internal view returns (bool) {
        bytes storage a = bytes(_a);
        bytes memory b = bytes(_b);
        if (a.length != b.length)
            return false;
        // @todo unroll this loop
        for (uint i = 0; i < a.length; i ++)
            if (a[i] != b[i])
                return false;
        return true;
    }
}

Below Screenshot of the Remix





Updated 11/1/2017 Hash function of a struct

Comments

Popular posts from this blog

Solidity Hash of Structs (Testing hash uniqueness)

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Parity installation on Ubuntu Virtual Machine