| layout | post |
|---|---|
| title | easily read private variables |
| date | 2018-03-29 09:30 |
I put this together to demonstrate that complete lack of "privacy" provided by labelling a variable private in solidity.
If you want to read a private variable in a solidity contract, and you have the source code, you can do it easily. Here's how:
- You'll need the following:
- The contract source code
- The contract address
- metamask
- Go to https://remix.ethereum.org
- Paste in the source code
- Change the label from
privatetopublic
// uint256 private secretNumber;
// uint256 public secretNumber;
- Go to the 'run' tab in remix.
- Change the environment to "injected web3", this will get you a JSON rpc connection to ethereum, or whichever testnet the contract is on.
- You'll get a bunch of buttons for interacting with the contract. Changing the label to
publicwill have given you one that gets the value.
FYI adding the public keyword to the source adds a getter in the bytecode.
Because:
So, it’s like you can pretend there are these extra convenience functions in the blockchain
Tomisin @ToJen 16:59
@maurelian wouldn't that make it a different contract then? I haven't actually deployed yet @foogunlana
Maurelian @maurelian 17:02
It’s almost exactly the same contract, and it will store the mapping data in the same locations. The only difference is that the local version will have some extra bytecode for retrieving that data.