4

I've created a smart contract with ERC20 standards, I am using Ethereum TestRPC, Web3js and remix along with ABI/contract address to interact with contract functions. But I have created a modifier inside contract and applied to few functions (like functions start and stop to start/stop contract any time).

Anyone can interact with my contract using above tools, but my question is how can I specify that I'm the admin/owner of contract, So I can call Start and Stop functions?

Any help would be appreciated.

3 Answers 3

4

Please don't be confuse on accessing function using web3js. According to your question..


created a modifier inside contract and applied to few functions May be you are using modifier as below (no hard and fast)


pragma solidity 0.4.22;

contract Token{

    address owneraddress;
    modifier onlyowner {
        require(owneraddress == msg.sender);
        _;
    }

    function Token(){
        owneraddress = msg.sender ;
    }

    function start() onlyowner {
      //your logic here  
    }

    function stop() onlyowner {
     //your logic here
    }
}

This restrict to other address except owner . Here owner is assigned as address by which the contract is deployed, Because here constructor is called once when contract is deployed. so you don't need to worry about any other can access this function except owner address.

Now the case is, If you give contract code and contract address to any one then also other account cannot access this function.

Any one can make there own Interface and access these function and again the result will be same. So this conclude that you do not need to restrict through web3js API.

If you want to specify in web3js part that you are the owner and only you want to access those own able function then there is no worth of giving your contract address and code to anyone, They will use there way so you don't have any control on it. If you are making Dapp and other user use your dapp then you can do in web3js layer. And this can be done as :-

web3.DeployedContractRef.start( { from: owneraddress});

web3.DeployedContractRef.stop( { from: owneraddress});

It might help you !

1

Add the following contract:

contract Ownable {
    address public owner;

    // Sets the sender address as the `owner`.
    function Ownable() internal {
        owner = msg.sender;
    }

    // Throws an exception if called by any account other than the `owner`.
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }
}

In your contract, inherit from Ownable and apply onlyOwner on any function that you want.

For example:

import './Ownable.sol';

contract MyContract is Ownable {
    function start() external onlyOwner {
        ...
    }

    function stop() external onlyOwner {
        ...
    }
}
2
  • Thanks for the explanation, Actually my question was how can I call contract functions which only be executed by owner when interacting with contract using web3js, how can I specify that I'm the owner during call to start/stop functions using web3js. Apr 27, 2018 at 9:12
  • @Surinderkumar: Pass {from: web3.eth.accounts[0]} inside the function call. For example, await myContract.start({from: web3.accounts[0]}). By the way, it is passed to the function by default anyway, so if anything, you probably want to make sure in your test that the function cannot be called by a non-owner (using any other account, for example, web3.eth.accounts[1], or some random address like "0x1234567812345678123456781234567812345678"). Apr 27, 2018 at 9:59
0

You have several possibilites :

  • Use a password instead of checking if msg.sender is the owner
  • Call the functions using another service such as MEW/Remix or sending directly the hash of the function in the tx
  • If you have the private key, you can add the owner address in web3.eth.accounts as specified here, then you can call the functions as you wanted
2
  • Thanks @Eli, Yes I have private keys of account which created the contract. I have checked your link, I don't want to create new account using web3js. I just want to call functions start/stop, because these two functions only belongs to my address. I don't really understand your first two options but I think your 3rd option can serve my purpose. Let me explore, thanks for your help. Apr 27, 2018 at 9:56
  • It's used for generating a new account object, not creating a new address.. You can use web3.eth.accounts.privateKeyToAccount(privateKey); too. Apr 27, 2018 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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