I'm trying to get merkle proof for Ethereum Smart Contract mapping field data. I created test smart contract and deployed to test network. Smart Contract Code like following code:
contract TestSmartContract {
mapping(string => string) mappingField;
address sender;
function TestSmartContract() public {
sender = msg.sender;
}
function addData(string key, string value) public {
mappingField[key] = value;
}}
I invoked smart contract addData method with test data.
"a","1"
"b","2"
"c","3"
After invoking smart contract, I tried to get value with web3 library, it works and i can get value for "a" key as "1".
var slot = "0".repeat(64);
var key = web3.fromAscii('a');
contractAddress = "0xac3f3d7286d8092be72d0011865a393ea722f609";
var keccak256 = web3.sha3(key + slot, { encoding: 'hex' });
web3.eth.getStorageAt(
contractAddress,
keccak256,
function (err, result) {
console.log(web3.toUtf8(result))
console.log(result);
}
);
As a final stage, I tried to create Merkle Proof for Key "a". I tried to use https://github.com/zmitton/eth-proof library for retrieving Merkle Proofs. But it gives "storageNode not found" error. You can find my test code below:
const Web3 = require('web3');
const EP = require('eth-proof')
var chainDataPath = '/Users/ramazan.girgin/chain-tmp/privchain/geth/chaindata/';
try {
var latestBlockHash = 'f02052990b2ea9daa445c0a4655cb3a395a84c8300cba8258b8faf4357b109c7'
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send;
var eP = new EP(
new Web3.providers.HttpProvider("http://127.0.0.1:8545"),
latestBlockHash,
chainDataPath
)
contractAddress = 'ac3f3d7286d8092be72d0011865a393ea722f609';
eP.getStorageProof(contractAddress, '0','0x61').then((result) => {
console.log(result)
}).catch((e) => {
console.log(e)
})
} catch (e) {
console.log(e)
}
Actually, ZMitton uses https://github.com/ethereumjs/merkle-patricia-tree in this library. But i think there was a problem while sending path for storageTrie.findPath function in library getStorageProof method or i can incorrect values for getStorageProof() method. In the other hand, if i call getStorageProof for sender field , it gives complete proof.
eP.getStorageProof(contractAddress, '1').then((result) => {
console.log(result)
}).catch((e) => {
console.log(e)
})
How can i retrieve merkle proof for mapping field in Smart Contract? Or how can i calculate correct path for storageTrie.findPath function parameter ?
Thanks in advance
Ramazan