0

I deployed this contract and also interacted with it using ganache.

     pragma solidity ^0.4.0;
    contract Counter {
     int private count = 0;
     function incrementCounter() public {
      count += 1;
      }
      function decrementCounter() public {
    count -= 1;
      }
      function getCount() public constant returns (int) {
    return count;
      }
    }

However, when interacting with the contract, any account on Ganache can interact with it and use the function increment. How can I define only one account from Ganache to be able to change this function ?? I just wanna to add some security to the contract?

This is link for the smartcontract I follow: https://medium.com/crypto-currently/build-your-first-smart-contract-fc36a8ff50ca

3 Answers 3

1

Have a look at the Ownable library https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/ownership/Ownable.sol . It gives you a modifier called onlyOwner which you can add to all the functions where you need to restrict the access.

Using the onlyOwner modifier blocks access from anyone but from the owner. Owner is by default the one who created the contract.

There are also other alternatives to this but Ownable is probably the most widely-used and accepted solution.

1

You can add a constraint to your functions, like this:

pragma solidity ^0.4.0;
    contract Counter {

        int private count = 0;
        address public admin;

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

        function incrementCounter() public {
            require(msg.sender==admin);
            count += 1;
        }

        function decrementCounter() public {
            require(msg.sender==admin);
            count -= 1;
        }

        function getCount() public constant returns (int) {
            return count;
        }
    }

In the constructor, the admin is set to be the account that deployed the contract. Then, the require(msg.sender==admin) will check if the account trying to execute the functions is the admin of your system, if not, it will throw and the function will not be executed.

hope this helps.

0

You just need to add a modifier to the contract, then include the modifier wherever you want the added security. Below, the modifier isAdmin has been added to the incrementCounter and decrementCounter functions.

pragma solidity ^0.4.0;

contract Counter {
    int private count = 0;

    modifier isAdmin() {
        require(msg.sender == 0xYOUR-ADDRESS-HERE);
        _;
    }

    function incrementCounter() public isAdmin {
        count += 1;
    }

    function decrementCounter() public isAdmin {
        count -= 1;
    }

    function getCount() public constant returns (int) {
        return count;
    }
}
4
  • I tried your solution but it gives me this error ": Expected primary expression. require(msg.sender == 0x0x081D55360Acf2990791656c195Ee94124d84B186) ^-^" . How can I solve that error ? Commented Aug 14, 2018 at 17:13
  • you have an extra 0x, should be 0x081D55360Acf2990791656c195Ee94124d84B186
    – Shomari
    Commented Aug 15, 2018 at 2:54
  • I added the public key corresponding to the private key which is used to deploy the contract, I get that error with the function that uses the mpdifier " the tx doesn't have the correct nonce. account has nonce of: 3 tx has nonce of: 2" Commented Aug 17, 2018 at 3:27
  • Public key?? I'm not very familiar with Ganache, but that sounds like a problem with your client. Probably best to ask a new question.
    – Shomari
    Commented Aug 17, 2018 at 17:54

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.