Introduction

Recent predictions for the potential growth of the Internet of Things (IoT) up to 20 billions devices by 2020 are staggering. Some other sources even say that is a modest forecast. Regardless of the exact number of devices out there, all of us will have a chance to witness the world getting even more connected and smaller in the following years. This exponential growth is currently being challenged by two big treats, security and scalability and none of them are strightforward to tackle.

IoT and blockchain?

The proliferation of IoT will bring some magical user experiences, especially once combined with other emerging technology trends. Some of these trends, namely blockchain, could potentially address the security challenges in IoT space due to its specific set of built-in traits.

Blockchain is able to provide a unique identity to the IoT device together with an ability to broadcast messages (called transactions) which have unfalsifiable source of origin embedded into them. These traits come as a result of applying state of the art cryptographic algorithms.

This blog post will be exploring the potential of combining IoT and blockchain which would open up some very interesting user scenarios. These two technologies working side-by-side will enable us to expose physical assets to the capabilities like value exchange and Smart Contracts. Think about privately owned solar panels which could sell the surplus of energy on on-demand basis.

Another idea behind this blog post is to lower the barrier of entry for cryptography. Blockchain is a very dynamic environment with lots of research activity at the moment. Getting to know the blockchain fundamentals would give developers superpowers once they start exposing their apps to blockchain ecosystem.

There is a common misconception in the IoT community that the interaction with the blockchain requires running a “full node” on the device itself. Even though that is technically feasible in specific cases, it is inefficient in terms of computing power and consumed energy. Alternatives involve running a “light node” or using completely different distributed ledger technology (DLT), for example tangle (IOTA) which is supposedly less resource demanding due to its novel Proof-of-work concept.

We will be exploring another approach in exposing IoT to the blockchain ecosystem. The concept of wallet is a well known to the blockchain community as a digital analogy to physical wallet which is used to store bank notes. In case of blockchain, a wallet represents a digital entity which does not directly contain value but rather gives access to funds to the owner of the private key. Wallet itself is a very lightweight component which opens up the doors for blockchain interaction. Given its low footprint, wallet is a good candidate to become a part of IoT device itself.

How do I build one?

In the following sections we will go through steps of building a blockchain wallet on an embedded device. More specifically, we will be looking at the anatomy of the Ethereum wallet and how we can build one on Particle Photon IoT platform.

What is the private key?

Different blockchains might have different private key infrastructure, but the most common one (used in Ethereum, Bitcoin, etc) has the size of 256 bits. It is basically a very, very, very big random number. You can create one yourself just by flipping a coin 256 times. Its size and randomness basically guarantee its uniqueness. Owner of the private key is able to sign transactions which get afterwards propagated to blockchain network. Check out this Youtube video about the amazing math behind the concept of private keys.

When building a wallet, we should make sure that the private key generator is truly random. Apparently, generating truly random numbers is not that easy as one might expect. Luckily, some IoT platforms (including Particle Photon) offer truly random number generators (TRNG) through hardware firmware.

In our example down below, we will be reusing hard-coded private key due to practical reasons. Creating one of the fly would require moving some Ether to related address each time it is generated, which is cumbersome.

Once we create private key using the TRNG, we are able to create and sign transactions which then can be used to transact value or to interact with Smart Contracts.

The private key is also necessary to generate a public address which can be used to send funds to the IoT device.

The workflow for creating the address from the private key

The process itself is outside the scope of this post, but you can read more about it in Mastering Ethereum book by Andreas M. Antonopoulos.

Ethereum wallet library for Photon

I have created a reusable Ethereum wallet library for Particle Photon which is inspired by some other Arduino based wallet implementations like this one and this one.

For the impatient ones, you can directly go to Particle Desktop or Web IDE and search for library called embedded-ethereum-wallet.

You can check out the source code and the related example at my Github repo:

Combing IoT and blockchain in Supply Chain

In order to demonstrate a use case for IoT and blockchain, we will be looking at applying the library from above in the supply chain workflow. Most of the countries have a legislation on how pharmaceutics are being transported from the producers to the end destination. This legislation defines, among other things, the environmental conditions (temperature, humidity, etc.) under which medicines can be transported. Let’s see what would it take conceptualy to expose temperature data to all stakeholders in this supply chain scenario in such way that parties involved do not need to trust each other.

The overall idea is that the IoT device (Particle Photon) would capture the current temperature with the dedicated sensor (DS18B20) and then somehow record it on the Ethereum Smart Contract making it in that way accessible to other parties in the supply chain. Keeping the temperature value on the Smart Contract would result in its immutability which is beneficial for auditing purposes. In real world scenario, we would avoid storing data directly on Smart Contract due to the costs but rather we would keep the hash of it and the actual content could be saved on some other distributed file storage system (IPFS for example). The whole process has quite a few moving pieces, so bare with me on this one.

Temperature sensor DS18B20 connected to Particle Photon

Getting sensor data on blockchain

Everything starts with sensor on IoT device capturing the current temperature. Once that happens, we would create a transaction containing details about the Smart Contract we are about to interact with and the actual value from the temperature sensor.

Our Smart Contract structure is quite simple and it’s only able to store the most recent temperature coming from the sensor.

This is followed by signing the transaction with the private key and encoding it. Ethereum is using something that is called RLP encoding which results in transaction being encoded in binary format. You can find out in details on how the transaction creation, signing and encoding work again in Mastering Ethereum book. At this point, the transaction is ready to leave the IoT device and to be broadcasted to the blockchain network. In order to do that, we would need to get the signed transaction off the IoT device and send it to Ethereum “full node”.

Transaction signed and encoded. Now what?

We will use INFURA to transmit our signed transaction to Ethereum network. INFURA is exposing Ethereum network through HTTPS based RESTful service. This is great for mobile, desktop and web apps but it’s quite heavyweight for embedded devices like Particle Photon which has 1MB flash and 128kb of RAM.

In order to bridge IoT device and Ethereum node behind INFURA, we will use IoT industry standard protocol for small footprint message exchange called MQTT (there is an open source library for Particle Photon). IoT device would then publish message with the transaction payload to MQTT broker. On the other side of the broker, there will be a MQTT client which listens for those messages and passes them to INFURA. Be aware that MQTT is running on TCP/IP and that there should be additional effort done in order to make it secure. Consider using TLS enforced message exchange on both clients and broker (TLS version of MQTT library for Particle).

There are many options out there when choosing MQTT broker. I’ve settled with EMQ. It’s quite straightforward to setup in the cloud environment and it has a nice dashboard for tracking connected clients. Small heads up for the MQTT library on Particle Photon. Default message size (MQTT_MAX_PACKET_SIZE) needs to be increased in order for encoded transaction to fit into message payload. Otherwise, the transaction content would be cut off.

This whole workflow can be represented as follows:

Workflow for getting temperature readings from IoT sensor to blockchain

The source code for this project can be found at:

Conclusion

In this blog post, we have introduced Ethereum wallet library for Particle Photon which can be used to create private keys, addresses and sign transactions. We used this library in the end in a concrete industry scenario (supply chain) where we measure the temperature with a sensor and store it on the blockchain.

If you liked this post, check out my previous post on how Ethereum and IPFS could be used to fight corruption.