This is the first of a series of tutorials and technical updates from AnyLedger. You can follow and contribute to the project on the Github repository. This article was written together with Jozef Henzl.

AnyLedger Embedded Wallet

First of all, what is AnyLedger embedded wallet? To make it simple, it is a lightweight software library that can be called by embedded applications in order to generate and manipulate private keys, send blockchain transactions and interact with smart contracts. The embedded application itself usually runs on the top of a real-time operating system (RTOS), of which Zephyr and FreeRTOS are two examples. The physical targets are therefore energy constrained IoT devices powered by MCUs (microcontrollers): sensors, smart watches, smart locks and so on. In a nutshell, an IoT device featuring the embedded wallet can natively interact with the blockchain.

Nordic NRF52 dev kit board and a DHT11 temperature + humidity sensor

Outline of the Demo and Components

The demo describes a scenario in which a tamper-proof data storage of environmental sensor records is required. The device measures the data (temperature and humidity in our example), compiles a transaction including these data and sends it to the blockchain. Once the transaction is “mined”, it is permanently stored and any party can retrieve the history of the data from the blockchain.

Let’s list our demo components:

A temperature-humidity sensor. Here we will use the popular DHT11. This sensor is very easy to use — it requires just one wire to get the data plus VCC and GND connection.

A hardware board (or emulator, such as QEMU) such as Nordic dev kit, on which we flashed the embedded wallet firmware. In our example, the data pin of the sensor is connected to the P1.10 pin of the nRF52840 dev kit board.

Ethereum blockchain, in particular a synced Ethereum node.

A 6LoWPAN gateway, such as a Linux computer. The device uses this gateway to interact with the Ethereum node.

AnyLedger Wallet console

If you want to play with the demo wallet, you don’t even need a hardware board — Zephyr provides a virtualized target for QEMU. Please follow the build instructions from the Anyledger wallet repository.

It’s alive!

To make it easier to interact with app, blockchain and wallet, a shell-like interface is exposed by the board via its USB port connection. You can connect to it for example using minicom:

minicom -w -D /dev/ttyACM0 -b 115200

Once connected, you should see a prompt. You can use <tab> to complete the commands and arrows to browse command history — just like in the unix shell.

First we’ll take a look at the wallet module. This module allows you to change the private key, send a simple ethereum transaction and sync the account nonce with a blockchain. The most important thing to do before sending any transaction is to sync the account nonce. This will also test if the connection to the ethereum node works. In the terminal, do:

wallet sync

If you see no error message, check the nonce

wallet nonce

Now we can do a simple transfer of 100 wei

wallet transfer 0x2e83b5Ae698E1f1ab5b6F4bb0732D72F0c74D049 100

The transaction will be encoded, signed, and submitted to the ethereum node. On success, the command will print a transaction hash to the console.

Now let’s move to the temperature reporting functionality. The code polls the sensor every 30 seconds and makes the value available to other parts of the app. Do

to see what the current temperature and humidity is. To start storing the data in the blockchain, you have to start the uploader manually with the

upload start <address> <interval_seconds>

command. Once started, the data are encoded into the transaction and sent to the blockchain. To stop the service, simply do

upload stop

To see the status of the transaction and verify that the data are there, let’s check etherscan: you should see two bytes of data in the data field — first byte is temperature, the second one humidity. But to make things simpler, we have included a script that will output a list of transactions and the decoded data to a console:

$ py -m tools.etherscan_query — address 0x2e83b5Ae698E1f1ab5b6F4bb0732D72F0c74D049 — decode temp

# block temperature humidity

3216546 22 40

3216552 21 40

3216555 22 38

Why this is relevant

This is all pretty exciting, but it’s actually also relevant! The data that we just sent from the device has been signed with the private key stored on the device. An external observer seeing this data associated to a blockchain transaction can be sure of who the sender is (the device!) and when the data was sent (or better, inserted into a block) without the need to trust a central authority. Here we are making at least two assumptions: the device has enough security measurements in place to maintain exclusive ownership of the private keys and the device blockchain address was previously paired with the IoT device, meaning that blockchain address ‘xxx’ is associated to a given IoT identification number ‘yyy’ set at manufacturing time. The latter action is a one time procedure, usually done during the IoT device bootstrap.

These sensors, creating tamper proof audit trails of environmental data, can be very useful for instance in supply chain, industrial or automotive scenarios.

What’s next!?

So far we covered something pretty fundamental, that is how to send transactions. But many blockchains, for instance Ethereum, allow for more powerful and complex actions such as interacting with smart contracts. We will explore this in a future blogpost, together with an access control scenario in which the access to the device is restricted to specific entities. Until then, stay tuned and happy coding!