DEV Community

Cover image for Smart contract for document registry
shrey vijayvargiya
shrey vijayvargiya

Posted on

Smart contract for document registry

Developing smart contract to work for registering documents in blockchain

Under the Hood
Ever wondered why I start every story with under the hood as the first paragraph πŸ€”. Well, the reason is simple, no story in the world begins without the reason/cause/motivation/rage or whatever word is coming to your mind.

So today’s story begins and is encouraged by the process of learning about blockchain development. In fact, the last 10–12 stories are more about blockchain development because I am sharing while learning.

I feel good to learn the new stack and technology and share it with you my audience asap, it just brings satisfaction once that publish button is pressed 😁.

Getting Started
Lets begin the story, the idea is to create a smart contract to store and work with documents.

Blockchain data is immutable, consistent, open-access for everyone, permanent and most importantly secured. Because of this blockchain will certainly be the future of the applications.

And one use-case of the blockchain is storing consistent data, for example, citizens' identity documents such as Aadhar Card(for India). Currently, the data is stored in centralised servers that are managed by the government/central body making it vulnerable to attackers and many more unwanted activities.

Storing all the required documents on the blockchain has its benefits and more importantly, it will be secured.

Writing Smart Contracts
2 function with 2 variables and our smart contract is ready to use. Of course of production, you might need to make more changes and add more checkpoints and validations.

Logic
First, we will use the documents that can be stored using hash as the key. The document hash will be generated at the time of document uploading and we have tonnes of third party modules to do this for us, so no need to write the code from scratch.

The hashing algorithm will read the content of the document and create the hash using the content so that next time even if the single content is changed the entire hash will be mutated and store it all together in a new identity avoiding the vulnerability and duplicity.

The map data structure will store the hash as the key and the value will be the timestamp at which the document is stored in the blockchain.

mapping(string => uint256) documents;
Enter fullscreen mode Exit fullscreen mode

Another variable will store the owner's address, this is important for validation purposes.

Remember, you can create as many variables as you can but try to minimalist it as more variables will cost more gas at the time of the transaction or storing the document.

address ownerAddres = msg.sender;
Enter fullscreen mode Exit fullscreen mode

msg variable is the global variable in solidity or EVM. Just like in window in javascript, div in HTML.

Method to store document
Now we will write the method to store the document in the blockchain and return the timestamp at the time of creating the block.

addDocument.sol

The hash value should be hashed document, you can create it on the client-side or in the server.

Method to verify or get a document
The last part is to fetch or verify that using hash can we get the document back from the blockchain.

verifyDocument.sol

We are simply returning the data we get while iterating over the maps of documents. The date will tell that the document hash provided in the parameters of the method is correct. This also signifies that the document provided by the user of which the hash is created is correct and we have the same document copy stored in the blockchain.

Conclusion
Hence our smart contract for storing and verifying the document is completed. For production-based we certainly need to add more validations such as hash is correct, document uploaded is the required one and many more. We will also have to check if the storing hash document should not cost excessive gas otherwise the sender has to pay the gas.

Github repository

Until next time, have a good day, people.

our website - iHateReading

Top comments (0)