[Ethereum] How to implement owner check in Solidity

contract-designecrecoverkeccaksolidity

Is there any EIP that specifies how to implement function in contract, that let's you call any other function as contract owner, with provided signature?

I don't want to reinvent wheel.

For example:

I want to create token contract, in which any address with ethers (for gas) can push transfer tokens transactions, as other address (sender) with its signature in which there may not be any ethers for gas.


  function transferFrom(address recipient, uint256 amount, uint8 _v, bytes32 _r, bytes32 _s) public {
    address recipient = msg.sender;
    bytes32 hash = keccak256('Transfer tokens:', recipient, amount);
    address caller = ecrecover(hash, _v, _r, _s);
...
  }

I need something like this.

My question is. Is there any EIP for that or maybe more generic EIP not only for token transfers?

Best Answer

contract Ownable {

  address public owner;

  modifier onlyOwner {
    require(msg.sender == owner, "Ownable: You are not the owner, Bye.");
    _;
  }

  constructor () public {
    owner = msg.sender;
  }
}

contract Owned is Ownable {

  function doSomething() public onlyOwner {
    // caller is the owner if they got this far.
    // carry on
  }
}

See the @OpenZeppelin/contracts access control contracts for a well-crafted implementation of this solution.

Hope it helps.

Edit:

  • added Open Zepplin Link
Related Topic