0

I am currently working on a smart contract and using modifiers. I want to restrict access to a function only to a single account. I can do this with 'msg.sender' but to my knowledge this limits only to the account that deployed the contract. I want only the account 'web3.eth.accounts[2]' to access the function. Is this possible in solidity?

1
  • require(msg.sender == address_you_want);
    – OWADVL
    Nov 6, 2018 at 10:25

3 Answers 3

0

Msg.sender actually is the address of the caller of the function. This can be anyone: either a smart contract or an external account. You can hard-code in your smart contract which addresses you want to be able to call certain functions, or specify them using the constructor.

0

Restricting access is a common pattern for contracts. Note that you can never restrict any human or computer from reading the content of your transactions or your contract’s state. You can make it a bit harder by using encryption, but if your contract is supposed to read the data, so will everyone else.

Take a look documentation https://solidity.readthedocs.io/en/v0.4.24/common-patterns.html#restricting-access

0

You could write a modifier that prevents you from accessing the function. Using the openZeppelin ownable contract, you could get the _owner address(which is you, after you've deployed the smart contract), then this might help

modifier RestrictOwner(address _address) {
    require(msg.sender !== _owner);
    _;
}

function play() public RestrictOwner {
     // contains logic
}
1
  • Few notes, it doesn’t prevents from you, it prevents from accounts who aren't the owner. In addition, please show him how to inherit from the contract you mentioned
    – matank001
    Oct 8, 2022 at 14:41

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.