I want to write solidity contract which stores hash table as mapping(uint => string) documents data structure in ethereum blockchain.
Now, each time I run a function "storeDocument" in this contract, I store a students' details in mapping as ID => Hash_of_stu_data and run the contract. Likewise, I have to store many students' details in same mapping data structure by creating several transactions. So, it is stored permanantly in blockchain.
So, now there is another function "verifyDocument" in same contract that returns boolean value if given hash is matched on providing ID as key and hash anytime by anyone on a different transaction. So, it has to access same documents mapping data structure, compare against the value and return bool value.
How do I verify a particular student's hash based on key value in table in another transaction ? Since it is permanently stored, what should I do ? This happens only if store all records and verify in the same transaction. This is possible if accessing data from Blockchain that is stored in mapping data structure in same transaction. But, I want this table to be updated with new entries everytime I create a transaction to store a key value pair as we do in database.
I am able to access mapping table data in the same transaction. But I want to store it permanently as we do so in database to verify my hash at any time. You can assume this contract is for checking the integrity of any document.
This is sample code:
pragma solidity >=0.4.0 <0.6.0;
contract Example {
mapping(uint => string) documents;
function storeDocument(uint id, string docHash) public
{
documents[id] = docHash;
}
function verifyDocument(uint id, string hashToVerify) view public returns (bool)
{
if(keccak256(abi.encodePacked(documents[id])) == keccak256(abi.encodePacked(hashToVerify )) )
{
return true;
}
else
{
return false;
}
}
}
Please help me. Please mention if question isn't asked properly. Thank you.