Problems when IPFS and Blockchain are combined

Completely decentralized Blogging application using IPFS and block-chain.

abhilash reddy
Coinmonks
Published in
4 min readNov 13, 2018

--

As promised in my earlier blog . I this blog i share some ideas about building a blogging application using IPFS and block-chain which is completely decentralized. The best part is, its authentication is done using block-chain.

Overview about what is IPFS :

IPFS is a distributed file system which uses content based addressing system. That means in regular HTTP protocol we access our files using IP-address of server and then server at this IP-address will fetch us required files from database. But in IPFS our entire document is converted into a single cryptographic hash and this hash is used to access our files in future.

Rather than explaining about this DAPP directly, let us understand it in comparison with regular blogging application.

Data Storage :

In regular blogging applications, there will be a server to serve blogs and if the user writes or edits a blog it will get stored in database. But in DAPP when user writes or edits a blog the entire content is stored in IPFS and a base58 encoded cryptographic hash is returned for accessing this blog in future.

Authentication :

Every one knows about authentication for website services. Basically the entire authentication process is used to make sure that the actions preformed on a specific account is done by a single authorized user . In the same way the hash received from IPFS is signed with private key by the user and is stored on block-chain as a part of ledger.

So basically after writing a blog i am signing it and saying to everyone in the network to remember that this blog belongs to me.

Technical Problems :

While building this DAPP I have face some specific technical problems. Among them, one appeared to be significant. Lets know how to solve it.

The String Problem :

  • We write a solidity contract named blog, to store the IPFS hash of each new blog. Structure used to store content of each user is …
  • Each user have a mapping like this ..

(user public address) -> struct containing all blog hashes of a user

  • Generally when data is stored in IPFS ,it returns a base58 encoded string .

This will log the base58 encoded string :

QmdE4B1fET12wrd67zPtN82fcm1zN3QAMKSPXMbseU5DWB

  • this sting is sent to solidity and added to the list of blogs of every user.
  • Now if I want to retrieve blogs of any specific user, I need to return the string array which I have stored.
  • But in solidity we can only return array of primitive data types like integers, bytes, address. But we have stored our hash data of blogs in a string array which cannot be returned.
  • So there is an alternative.

Solution :

  • As the string format is not comfortable to store we can convert the string to bytes and store in bytes format

QmdE4B1fET12wrd67zPtN82fcm1zN3QAMKSPXMbseU5DWB

  • If the above base 58 encoded string is converted to hexadecimal ,we get a 34 bytes hexadecimal string.

NOTE : Below string is hexadecimal meaning every two characters equals a byte

1220dd2ec4d8f062614722a9f5330b591bba9376971a10b79fc514cf9dcd6caaeb3c

  • But maximum allowed size a bytes element in solidity is 32 bytes .But this string is of length 34 bytes.
  • In above hexadecimal string first byte 12 represents that we are using sha-256 algorithm for reducing the entire content to this unique string.
  • second byte 20 represents that it is of length 32 bytes. See if 20 is converted to decimal it gives 32 .
  • So basically we can omit first two bytes (ie : 1220)while storing the string. It means that storing this is sufficient .

dd2ec4d8f062614722a9f5330b591bba9376971a10b79fc514cf9dcd6caaeb3c

  • This string is of 32 bytes and we can store it in bytes32 datatype of the solidity.
  • Now we can easily return the bytes32 array whenever needed.

If you like to explore more about this conversion please visit this website. If you are interested in using this in your application you can find the source code in this website .You can also find some pseudo-code and more detailed explanation regarding conversion of base58 to hexadeximal here

You can find the code for the entire bloging application in my github repository, including the explanation of how to run it.

By the way feel free to comment if you have any doubts about this blog or give me a clap if you like my blog as a token of appreciation :-)

I will be back sooner with an another blog about block-chain next week. Until then Byeeeee.

Get Best Software Deals Directly In Your Inbox

--

--