Posted By : Rahul
During smart contract development, a project sometimes requires connecting with other contracts in the network. However, private variables in Solidity are inaccessible to any other contract and can only be read by the contracts themselves. These data can, however, be accessed from outside the blockchain. Let us examine the process of obtaining private data from smart contracts.
The Ethereum Virtual Machine, or EVM, uses slots on the blockchain to store smart contract data in a big array with a length of 2**256. Up to 32 bytes of data can be stored in each memory slot. The state variables of smart contracts are stored by the EVM in the sequence in which they were declared in blockchain slots.
Also, Explore | Code Analysis Tools for Solidity Smart Contracts
Space efficiency is the main goal of smart contract storage. Variables are packed into one 32-byte slot if two or more can fit into it, starting from the right.
uint8 => 1 byte
uint16 => 2 bytes and so on
uint256 => 32 bytes
bool => 1 byte
address => 20 byte
bytes1 => 1 byte
bytes2 => 2 bytes
We can take the following actions to get private data from a Solidity smart contract. Here, we'll be extracting data using ethers.js.
You may also like | Top 5 Smart Contract Development Companies
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
contract MyContract {
uint256 private privateData_0;
uint256 private privateData_1;
constructor(uint256 _privateData0,uint256 _privateData1){
privateData_0 = _privateData0;
privateData_1 = _privateData1;
}
}
const { ethers, upgrades } = require("hardhat");
const { BigNumber } = require('ethers')
const { expect } = require("chai");
describe('accessing private data', function(){
let deployer
let Contract
let addr1
let contract
beforeEach(async function(){
[deployer ,addr1,addr2] = await ethers.getSigners();
Contract = await ethers.getContractFactory('MyContract');
contract = await Contract.deploy(101,102);
const data = await contract.waitForDeployment();
console.log(
`deployed to ${Contract},${contract.target},${data}`
);
});
it('contract', async function(){
const private_1 = 101;
const private_2 = 102;
const slot = 0;
const slot1 = 1;
const slot0Bytes = await ethers.provider.getStorage(contract.target, slot);
const slot0Bytes1 = await ethers.provider.getStorage(contract.target, slot1);
const slotData = parseInt(Number(slot0Bytes))
const slotData_1 = parseInt(Number(slot0Bytes1))
expect(slotData).to.be.equals(parseInt(Number(private_1)));
expect(slotData_1).to.be.equals(parseInt(Number(private_2)));
})
})
The test case that runs will confirm that the only integer read from slot 0 is 101, and the only integer read from slot 1 is 102. which both are private data.
Also, Explore | Top 5 Smart Contract Development Companies
Since we are working with public blockchains, private data in a smart contract is not private in the traditional sense. Blockchains cannot truly hide information from the outside world. We must use certain encryption algorithms if we wish to store sensitive data on-chain. For more about smart contract development, connect with our smart contract developers.
November 21, 2024 at 01:21 pm
Your comment is awaiting moderation.