8

The following variables here, assuming solidity 4.24 or later.

mapping(something => something) private yourmapping;

bool private yourbool;

address private youraddress;

uint256 private yournumber;

function dosomething() private {
*do stuff*
}

I see quite a few questions about hiding stuff in solidity. I'm not interested in that. My understanding of private variables is that they do not allow any other contract to call or change those variables, meaning that only the scope within the current contract is allowed to call or modify those variables. Am I wrong?

Further, any links or other information regarding private variables would be highly useful. I have added a private function as well for bonus points. What variables are allowed to be private? What other items can be considered private?

2 Answers 2

7

According to Solidity Documentation:

Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.

In Sol, the private keyword doesn't really mean it is private to the contract consumers. What it means is for example that: a contract B imports contract A and contract A has a private variable V. From contract B, you cant change variable V.

contract A {

    string private V;

}

contract B is A{
    ...
    can't change A.V
    ...
}

hope it helps

[EDIT] - Typo

1
  • Since the data for private variables are stored in the blockchain, would there be a way to hack in Solidity a way to read other contracts private variables, like manually define a contract address and search through the default memory bins for variables?
    – Danny B
    Aug 13, 2022 at 18:10
4

You are correct. private variables are only private for the smart contract scope which means they can't be accessed or modified from other smart contracts. But their values can be read freely outside the blockchain by anyone so they don't hide data in that sense.

You can declare any variable private in the contract scope. Similarly you can declare any function private and that too is only accessible from within your contract. Whether it's a variable (of whatever type) or a function doesn't make different for the visibility - private is only accessible inside the same contract.

Any variables declared inside your functions can also be considered (a bit more) private. They are not accessible anywhere outside the function scope.

I'm not sure what kind of references you are looking for so here's something:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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