I have a scenario where I need to store N no. of document hashes in a smart contract with some key(id). And also fetch the particular document hash by it's key. Any help on designing the structure for storing would be appreciated. Thanks
Asked
Active
Viewed 315 times
1 Answers
1
Here's a rough version:
pragma solidity ^0.4.24;
contract Test {
mapping(uint256 => string) hashes;
function addDoc(uint256 id, string hash) public {
hashes[id] = hash;
}
function getDoc(uint256 id) public view returns(string) {
return hashes[id];
}
}
Here you can store and retrieve the hashes based on a uint256 id. The mapping will store as many id<->hash values as you need.
What you should do next is change the string types to be something else (bytes of some length) and consider adding some mass-add function if you need to store multiple hashes at the same time.
Lauri Peltonen
- 29,391
- 3
- 20
- 57
string[]parameter and loop over the contents to add each separate item. Or, even better, usebytes: https://ethereum.stackexchange.com/questions/17094/how-to-store-ipfs-hash-using-bytes – Lauri Peltonen Sep 10 '18 at 10:41