Building For a Distributed and Resilient Future … Here’s Pigpen, IPFS and Ethereum

We have created a centralised digital world, and which depends on services running on servers. These servers often centralise the…

Ref: here

Building For a Distributed and Resilient Future … Here’s Pigpen, IPFS and Ethereum

We have created a centralised digital world, and which depends on services running on servers. These servers often centralise the provision of the service and also the data infrastructure. A failure of the service or for the data store can then risk the provision of the service. Along with this, firewalls can get in the way and stop access to information. So what’s the alternative? Well, IPFS (Interplanetary File System) provides a way to distribute files across the world, and where we use a hash value (the CID — Content Identifier — and which is a SHA-256 hash version of the file) to identify content. This overcomes the complex URL structures we have created.

So, why do I need services to locate a file on a disk somewhere, and then match that to a complex URL? Why can’t I just give an identifier to the content, and for the Internet to just find it?

So, I am moving much of my cipher challenge infrastructure towards IPFS and smart contracts. This will mean I do not need to store files on my server or run code on the server to generate the challenge. Let’s now take an example of a Pigpen cipher. Currently, I use my server to generate the challenge and to source the graphics for each Pigpen graphic [here]:

IPFS (InterPlanetary File System)

The IPFS implements a distributed infrastructure using P2P methods, and where there is no centralised server. As with Torrent networks, it is defined as being censorship-resistant [1] outlines that IPFS can be likened to the Web where we use content-address hyperlinks, but where a single BitTorrent swarm exchange objects within one Git repository.

IPFS breaks files up into blocks or chunks and traces and uses a Merkle DAG (Direct Acyclic Graph) to define the version control of files and a distributed hashtable. Within a traditional blockchain infrastructure, we sequentially store transactions. This can take some time to create a consensus through the building of blocks. With a DAG, each of the transactions becomes a block, and it thus speeds up the consensus mechanisms. Sergio Demian Lerner [2] outlined that in a DAG there were no fixed blocks and that each transaction brings with it, its own proof of work. Within this, he defined the usage of a fast cache for the most recent transactions, and where older transactions cannot be used as a reference.

IPFS Storage

So let’s source the graphics from IPFS and use a smart contract to generate the challenge. First, we need to run an IPFS desktop, and seed the graphics (and which are ‘a.png’, ‘b.png’, and so on):

We can either call back the graphics with the CID, or use a gateway to the source. This has sourced ‘a’ as:

QmcizbQqy4A9HRUEA34aiBSzbpk6BHJFDjAJWMvrKQRUyA

And then we can access from [here]:

https://ipfs.io/ipfs/QmcizbQqy4A9HRUEA34aiBSzbpk6BHJFDjAJWMvrKQRUyA

A ‘b’ is sourced as:

QmcVC9hK9CDKW9RC5aBNFjF521XyDCfiPR3zzzQ84NZGXy

And then we can access it from [here]:

https://ipfs.io/ipfs/QmcVC9hK9CDKW9RC5aBNFjF521XyDCfiPR3zzzQ84NZGXy

Smart contact

Now we will create a smart contract. For this we will randomly generate a word, and then map each character to the IPFS storage:

The mapping to the IPFS storage is defined with the string array of:

string [26] ipfs=["QmcizbQqy4A9HRUEA34aiBSzbpk6BHJFDjAJWMvrKQRUyA",
"QmcVC9hK9CDKW9RC5aBNFjF521XyDCfiPR3zzzQ84NZGXy",
"QmfP33Mde6wLXphucyV6Z3em8HjHhdftEBgJ4PDcfuNzgu",
"QmPXZXszrGn2c1pcL2UNK3UfqW6DYaLHZsicYo5Gktkj4W",
];

Now we can select a random word, and then determine its place in the alphabet (pos) and then map it to the Pigpen graphic:

function pigpen_cipher() view public returns (string memory) {
  bytes memory word=bytes(words[random(words.length,0)]);
string memory rtn="Find the plaintext for the Pigpen code of: ";
  for (uint256 i=0;i<word.length;i++) {
uint256 pos=uint8(word[i])-uint8(97);
rtn=string(abi.encodePacked(rtn,
"<img src='https://ipfs.io/ipfs/",ipfs[pos],"' style='height: 50px;'> " ));
}
rtn= string(abi.encodePacked(rtn, "\n\nThe answer is:\n",word));
return string(rtn);
}

First, we will create the smart contract and compile it:

We can then start our local blockchain with ganache:

And then deploy our contract locally:

We then see the miners have charged some gas:

Now we will test:

The code is:

Find the plaintext for the Pigpen code of:  <img  src='https://ipfs.io/ipfs/QmPXZXszrGn2c1pcL2UNK3UfqW6DYaLHZsicYo5Gktkj4W'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/QmR7StEGgVkURcbiwK2KBEq7kstwzawAQmp7tuAQ37dTBT'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/QmPyy7kn3TqT5EMJLKaumHxxpuQ7387Cyxvnc79pRcg44R'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/QmfTyFLuwRxEYrj2zpu6sMsCCTJV6fh7QJphHCEHpCL5T5'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/QmPVn2BEyKCBKc1SRK9KicFqyW9Sg7ohAypSBZEPJhMrqu'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/QmcizbQqy4A9HRUEA34aiBSzbpk6BHJFDjAJWMvrKQRUyA'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/Qmd5ZeGU5cJFUasQBWr7VvXbbPUWQcYnWUUQQVYkyk6XT6'  style='height: 50px;'> <img  src='https://ipfs.io/ipfs/QmPyy7kn3TqT5EMJLKaumHxxpuQ7387Cyxvnc79pRcg44R'  style='height: 50px;'>   
The answer is: displays

If we load this into a browser we get:

And which show say “displays”.

Deploy to the Ethereum test network

Once we have tested it locally, we can move it to an Ethereum test network. In this case we will use the Ropsten test network. We then change the environment for deployment to “Injected Web3” and then link a Metamask wallet, and then deploy:

We will then wait for the miners to pick up the smart contract:

And then:

We can then view the transaction for the new contract [here]:

Next we can view the contract [here]:

We now need to verify the contract:

And then with:

And then paste in the code to verify it:

And then get a successful deployment [here]:

Now we can test:

The smart contract is here:

https://ropsten.etherscan.io/address/0x1a9cdda855948fb97debc7ef8064ff8ad72fd1ba#readContract

Conclusions

IPFS offers a more resilient, and (hopefully) faster, method of sourcing content. Smart contracts, too, support a more distributed approach. Overall, IPFS offers an excellent method of accessing content, but there are still issues to solve, such as integrating encryption into the files and in making sure that files can be deleted. For standard content, though, it’s a winner!

If you are interested, we have ongoing research into the integration of IPFS into distributed systems for an EU project named GLASS, and where we aim to build a citizen-focused infrastructure for e-Governance.

If you are interested in collaborating, please get in contact, and build a more secure and resilient world.

Reference

[1] Benet, J. (2014). Ipfs-content addressed, versioned, p2p file system. arXiv preprint arXiv:1407.3561.

[2] Lerner, S. D. (2015). DagCoin: a cryptocurrency without blocks. White paper.