0

I'm trying to whitelist a single *address to be able to interact with this smart contract:

pragma solidity >=0.4.22 <0.7.0;

contract VisitorTracking{
    struct Location{
        string locationName;
        string registrar;
        uint timeStamp;
    }
    string visitorName;

    mapping (address => bool) private whitelist;

    constructor() public {
        whitelist[0xADDRESS] = true;
    }

    modifier onlyWhitelisted() {
        require(whitelist[msg.sender], "Not whitelisted");
        _;
    }

    mapping (uint => Location) trail;
    uint8 locationNum;

    function addNewLocation(string memory visitor,string memory locationName,string memory registrar)public onlyWhitelisted {
        if(bytes(visitorName).length == 0){
            visitorName = visitor;
        }
        Location memory newLocation;
        newLocation.locationName = locationName;
        newLocation.registrar = registrar;
        newLocation.timeStamp = now;
        trail[locationNum] = newLocation;
        locationNum++;
    }

    function getLocationNum() public view onlyWhitelisted returns(uint8){
        return locationNum;
    }

    function getVisitorName() public view onlyWhitelisted returns(string memory){
        return visitorName;
    }

    function getLocation(uint8 locationNo) public view onlyWhitelisted returns(string memory,string memory,uint){
        return (trail[locationNo].locationName,trail[locationNo].registrar,trail[locationNo].timeStamp);
    }
}

Have spent some time playing around with different approaches but cannot get it to whitelist. Other addresses are always able to initiate and interact with the contract.

What am I doing wrong?

Below shows the address ending f67 can call the contract even though A42 is the only address hardcoded in the whitelist.

enter image description here

3
  • Are you getting errors? How are you trying to add the whitelisted address? What goes wrong? Commented Apr 29 at 18:08
  • @Lauri Peltonen No errrors. Address is hardcoded within the constructor but other addresses can still interact with the contract. e.g. add new location and view the location etc. Commented Apr 29 at 21:34
  • That's interesting. It's probably not a version issue as someone suggested, since if the constructor doesn't get called, nobody should be able to call the restricted functions. I suggest you remove code as much as possible until you're down to the bare minimum - often that helps in pinpointing the problematic place Commented Apr 30 at 8:43

2 Answers 2

0

In your Solidity contract, whitelist Ethereum addresses, not IP addresses. Pass the Ethereum address to the constructor when deploying the contract.

Constructor:

constructor(address _whitelistAddress) public {
    whitelist[_whitelistAddress] = true;
}

Use this code when deploying the contract. Pass the address you want to whitelist as an argument.

0

I've tried your code and it works as intended. Only the whitelisted address can call the functions with onlyWhitelisted modifier.

Maybe if you have a problem it could come from the version you use.

The version pragma you are using is way to wide. There may be conflicts because versions changed a lot and don't work the same way.

For instance in pragma solidity 0.4.+ the constructor was called by creating a function with the name matching the Contract's name.

pragma solidity ^0.4.22;

    contract MyContract {
        uint public value;
    
        // Constructor
        function MyContract(uint initialValue) public {
            value = initialValue;
        }
    }

While in pragma 0.7+ we do use the constructor the visibility modifier public is not used anymore

4
  • Please see the photo I added to the OP. Is this how you are trying to call the functions? Commented Apr 30 at 12:15
  • What ? Do you realise that anyone can deploy a contract ? There is no way to prevent an address from deploying a contract. You can only prevent someone from calling a function in a contract. On your picture you are just deploying 3 different contracts, but you are never calling any function of the contract. I assure you that the code works perfectly.
    – Torof
    Commented Apr 30 at 14:11
  • I do not realise, smart contracts are a new concept to me. Have a look at the new photo in the OP which shows different addresses being able to call the contract. Commented Apr 30 at 15:00
  • Any address can call a contract , you will never be able to prevent that. What you can prevent is the result of a function call. If i am not allowed, the function will revert/stop, but the function was still called. I've tried your contract on Remix, it works perfectly. Only the allowed address can call the functions and have the desired result.
    – Torof
    Commented Apr 30 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.