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?
2 Answers
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.
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