Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
2nd April 2024
9 min read
Associate Consultant L2- Development
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.
Rahul Maurya
Rahul is a highly skilled Backend Developer with expertise in Node.js, JavaScript, Python, HTML, CSS, React.js, Redux, Git, MySQL, MongoDB, GitHub, Heroku, Vercel, and Render. He has undergone multiple courses to enhance his skill set, including Data Structures & Algorithms. Rahul has invested considerable effort in studying and implementing new concepts in data and development, with a particular focus on leveraging these insights to effectively address existing challenges.
Associate Consultant L2- Development
By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.
We would love to
hear from you!
Innovate with confidence!