0

I am wondering if it is possible to have some "private" variables in a contract, by private I mean that I can read the value only if I am the owner of a specific wallet.

A simple example is today's banking: You cannot watch other people balance, only yours.

This is different from a simple coin contract on Ethereum, where you can look at the state of the contract and know the balance of every address.

This can be further extended to be able to read a variable only if your are the owner of it or part of a specific group.

Is zkSNARKs implemented it already? Is there another project that extends solidity with private variables?

3
  • 1
    You are right but in order to apply some operations on the data, there is a need to have zero-knowledge and some homomorphic encryption I guess.
    – markin2000
    Jan 7, 2018 at 19:59
  • It sounds possible to get to the "private" variables using: web3.eth.getStorageAt(contractAddress, index) . Good article about it here: medium.com/aigang-network/…
    – Tomachi
    Apr 24, 2018 at 12:31
  • @Tomachi This is exactly the API for what I have suggested, zero-knowledge is indeed required to obtain this ability, I wonder whether someone is working on that
    – markin2000
    Apr 27, 2018 at 16:20

1 Answer 1

2

State variable data in solidity is public. You'd have to encrypt the data you're storing if you don't want the data to be exposed in plain text.

3
  • I'm pretty sure that if you add private next to a state variable, then the compiler refrains from generating a 'getter' function for it, hence you cannot read it from outside the contract (via web3 for example). Jan 7, 2018 at 9:10
  • Solidity will refrain from creating a getter, but the contract will still store the data in the clear, meaning it is technically still possible to read the private variable. Jan 7, 2018 at 14:57
  • @HarryWright: Well, it sounds similar to the fact that you can read a private variable in any native (unmanaged) language, by simply reading its memory address during runtime. Still, the access level can be used in order to prevent you from doing it "during compilation". My point being, the answers seems correct, but reasoning it with State variable data in solidity is public feels somewhat flawed. Jan 7, 2018 at 17:12

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