0

This question already has an answer here:

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?

marked as duplicate by Richard Horrocks, Ismael, manuhalo, Roman Frolov, atomh33ls Jan 9 '18 at 17:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 '18 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 '18 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 '18 at 16:20
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.

  • 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). – goodvibration Jan 7 '18 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. – Harry Wright Jan 7 '18 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. – goodvibration Jan 7 '18 at 17:12

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