Smart contract for document registry
Tags: blockchain, web3, programming, tutorial
Published: 2022-11-10 08:40:34

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;
```

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;
```

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.

[Add Document](https://gist.github.com/shreyvijayvargiya/a8d6ab893e6c10b1b9d1b1ffe47e9f63#file-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.

[Verify Document](https://gist.github.com/shreyvijayvargiya/1f3157f3eab5bb3aed7972e3c4360198#file-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.

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.

[Registry Document](https://gist.github.com/shreyvijayvargiya/a6d08701dec6f162b92a38346cea21ef#file-registrydocument-sol)

**Until next time, have a good day, people.
Keep developing
[iHateReading](ihatereading.in)**