0

I am currently learning solidity through CryptoZombies and I came across a point where they say ,"setKittyContractAddress is external", so anyone can call it! That means anyone who called the function could change the address of the CryptoKitties contract, and break our app for all its users.

We do want the ability to update this address in our contract, but we don't want everyone to be able to update it.

To handle cases like this, one common practice that has emerged is to make contracts Ownable — meaning they have an owner (you) who has special privileges. Here is that function:

function setKittyContractAddress(address _address) external {
kittyContract = KittyInterface(_address);
}

Now my question is that can we not just use msg.sender for this purpose.

I don't quite know how to do that though.

applesomthing
  • 361
  • 3
  • 19
  • I am having a hard time understanding your purpose, usecase and the problem. Can you be more elaborate? – keser Mar 31 '22 at 10:48

1 Answers1

1

can we not just use msg.sender for this purpose

That's exactly what the OpenZeppelin Ownable does in the background. Through a series of steps, it sets the owner variable, and then validates msg.sender against it.

modifier onlyOwner() {
    require(owner() == _msgSender(), "Ownable: caller is not the owner");
    _;
}

The owner() function returns the current owner address, and the _msgSender() function is a wrapper of the msg.sender global variable.

Source: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.5.0/contracts/access/Ownable.sol#L43

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100