2

I have a method in solidity and wants to return the sent string only for the owner address.

function greet(string str) returns(string) {
    if (msg.sender == owner) {
    return str;
    } else {
        return "Sorry I respond only to my owner";
    }
}

In Rinkeby testnet, I am able to set the address directly without unlock or signing with private key web3.eth.defaultAccount = '0xC2107dFbCf6a70865f30fc439b1850bE434231c9';

and when i greet.call('Hello').... method I get the passed string returned perfectly and gets back 'Hello'

So my question is can anyone use my public address in web3.eth.defaultAccount and call the greet method ? I want only owner with private key can pass the test (msg.sender == owner). How can others without private key get through it ?

1 Answer 1

1

In rinkeby this is only possible if:

  • The account has previously been unlocked when starting geth with the - - unlock version

  • The account has previously been unlocked from the geth command line by typing personal.unlockAccount(...)

  • The method is of type pure, view or constant (being this latter one deprecated)

so to answer shortly, only those with the private key can actually do it.

2
  • The account was not unlocked in geth command. I created this account from myetherwallet. In my node.js code, I use web3 and connect directly to new Web3.providers.HttpProvider('rinkeby.infura.io/') For the first time I had only created a contract with this account by sending a rawsignedtransaction in the beginning by signing it with my privatekey.
    – antjohns
    Commented Dec 14, 2017 at 22:41
  • 1
    I believe your usage falls into the third bullet point. You're calling a view function, which means you do not need to send a transaction and thus don't need a private key (since there's nothing to sign).
    – user19510
    Commented Dec 14, 2017 at 23:10

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.