私有变量安全性
When developing smart contracts, it's crucial to understand that marking variables as private does not make them confidential. This tutorial explains why private variables are still accessible and provides best practices for handling sensitive information in smart contracts.
In Solidity, the private keyword only prevents other contracts from accessing the variable directly through code. 然而,所有存储在区块链上的数据都是公开可见的,包括私有变量。
考虑这个具有私有变量的简单智能合约:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract PrivateDataExample {
uint256 private secretNumber;
constructor(uint256 _secret) {
secretNumber = _secret;
}
}
While this variable cannot be accessed by other smart contracts, anyone can read its value by directly querying the blockchain storage.