Serializing string arrays in solidity

Written by vasa | Published 2018/06/02
Tech Story Tags: javascript | solidity | ethereum | technology | arrays

TLDRvia the TL;DR App

TL;DR This GitHub repository is all you need.

Solidity is still in development and that’s why it still lacks in a lot of features that a good programming language should have. One of the features which solidity is missing is that it cannot return or take multi-dimensional arrays as input. Well, that does not seem important but when we talk about strings, we realize that they are just arbitrary-length byte array(i.e. byte[]). So when you create a string array, you are creating a two-dimensional byte array(i.e. byte[][]). This is where we all get stuck. Kinda like a trap…

it’s a trap!

That’s enough of talking, let’s get to work. The easiest and the most efficient way to get over this problem is to serialize the strings in the array(converting the strings in the array into byte form) and send all of them in one go. After we receive the string array in the byte form we can either pass the byte array as input to another solidity function or send it to user-end(node.js api) where we will de-serialize it back to the string array. Let’s see how it works.

Step 1: Serialize & De-serialize using solidity

For serializing/de-serializing in solidity we will use Seriality which can be used to serialize and de-serialize any data type in solidity. Thanks to pouladzade.

You will get some output like this (for startindex=1, endindex=2).

0x737472696e6731000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007737472696e6732000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007737472696e6733000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007

Now, pass this bytes buffer into getString()and you will get your strings back!

Note: You have to include Seriality files to your project to make the contract working. You can do that from here.

Step 2: De-serialize the byte buffer using javascript

Now, using javascript we can get our string array back.

Putting the above byte output in the js function we get the following output.

[“string1”, “string2”]

Voila, you successfully got your array back!

easy as that!

This was just about the strings. You can do a lot of stuff using Seriality.

Learned something? Click the 👏 to say “thanks!” and help others find this article.

Hold down the clap button if you liked the content! It helps me gain exposure .

Want to learn more? Checkout my previous articles.

Quantum Computing: Is it the end of blockchain?_Experts are suggesting quantum computing may render blockchain obsolete. As the tech giants such as Google and IBM are…_hackernoon.com

EOS 101: Getting started with EOS, Part 1_The only blockchain which has blocktime of less than a second: 0.5 sec!_hackernoon.com

Difference between SideChains and State Channels_A complete comparison of the two scaling methods._hackernoon.com

Setting up your first distributed private storage network on IPFS: Part 1_IPFS Private Storage Network Series_medium.com

13 sidechain projects every blockchain developer should know about_The whole world is going through the blockchain revolution. But wait…is this really what we dreamed of? Present…_medium.com

Clap 50 times and follow me on Twitter: @vasa_develop


Written by vasa | Entrepreneur | Co-founder @tbc_inc, an MIT CIC incubated startup | Speaker |https://vaibhavsaini.com
Published by HackerNoon on 2018/06/02