0


Let's say that we have a contract that not anyone can access to view function :

contract AccessControl {
    mapping (address => bool) authorized;

    function getSomething () public view returns (string) { 
        // control if the caller is authorized
        return "hello";
    }
}

I tried this but it works only on javascript-vm (remix)

contract AccessControl {
    mapping (address => bool) authorized;

    function getSomething () public view returns (string) { 
        require(authorized[msg.sender]);
        return "hello";
    }
}

Is there another solution?

  • Contract looks OK. What problems do you have with it? – Mikhail Vladimirov Apr 3 at 13:58
0

First of all, blockchain is public, and everyone may read code of your contract and storage of your contract, so there could not be anything private there.

Access control you suggest will only work when function is called from within another smart contract.

  • so isn't possible ? – maroodb Apr 3 at 14:35
  • For public blockchain you cannot prevent off-chain applications and humans from reading data of your contract. – Mikhail Vladimirov Apr 3 at 14:37
  • I wont prevent them from reading data, I want to prevent them from calling a function – maroodb Apr 3 at 14:47
  • You cannot prevent people from calling your function off-chain, and there is no much sense to call view function on-chain, unless it is called from another contract. – Mikhail Vladimirov Apr 3 at 14:49
  • Ok, So I will add a digital signature to verify access – maroodb Apr 3 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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