In this previous project I talked about "Pebble": a new IoT dev-board designed by IoTeX & Nordic Semiconductor, that is characterized by two main features: Data richness, and Blockchain communication abilities.
In this project I wanted to experiment with the Blockchain feature of this interesting board, and build a basic decentralised IoTData-Marketplace where Device-Owners can monetise the data they collect by selling them to data-customers, agreeing upon the rules of a Smart Contract deployed on the IoTeX Blockchain. We will sell temperature, humidity and baro(for example, it's useful to build weather indices):
Here is a demo UI for the project: https://iotexlab.io/datamarket, if you can't wait to start building, feel free to jump to section 2 below!
1.1 IOT DATA MARKETPLACES"IoT Data Marketplace" is not a new concept: such services already exist, both centralized and decentralized ones, and they allow to collect sensor data from data providers (e.g. from smart home appliances in people’s homes and smart cities), whereas companies can buy this machine data from the marketplaces in real time, for example to understand specific consumers behaviour, or to estimate the traffic on a specific city route.
1.2. DECENTRALIZATIONBlockchain technology has already proven to be a valid decentralized alternative to those "intermediaries" that too often have proved to be not reliable, subject to hackers attacks, or even have abused their privileged position as a "man-in-the middle" by collecting, misusing or selling the data at their disposal, often censoring the services they provide at their own discretion. Building an IoT data marketplace, Blockchain is again an excellent alternative to centralized solutions: allowing for the creation of a trusted, unstoppable, secure service, and without the need to maintain the necessary infrastructure that is required for the equivalent centralised services.
1.3. PEBBLE & EDGE ORACLESAlthough blockchain provides us with a valid platform to manage our data marketplace securely, in this context blockchain has a serious limitation, which is the reliability, or the "verifiability", of the data entered in the marketplace: while the blockchain can guarantee the immutability of the data that is stored into it, who guarantees that such data is trusted at the very source '?
This is actually a known limitation of Blockchain, that is currently being solved by entities called "EdgeOracles": they are fundamentally "Gateway Smart COntracts" that live at the boundary of the blockchain, as they receive data from the real world, and can be queried by the blockchain, with the special characteristic tha that given a specific request (e.g. expressed in terms of time and/or space) they will always provide the same "fact", along with the proof for that fact being real.
Creating an edge oracle is not easy, and here is where the IoTeX Pebble comes to our aid!
1.4. SECURE HARDWAREPebble can not only collect data from its sensors: it can also sign that data, using a tamper-proof secure element called a "Trusted Execution Environment" (TEE). This cryptographically signed data can then be transmitted to any MQTT v3.1.1 endpoint to be archived, or directly to a blockchain smart contract (the Edge Oracle): in both cases the data will carry the signature from the device that produced it, as the verifiable proof of the it's source.
2. LET'S BUILD!So, without further ado, let's see what we want to build and what we need to make it!
2.1 HOW IT WORKSDevice owners want to measure IoT data and sell it to interested "buyers" at a specified price. Buyers in turn want to browse available data providers, choose the ones that fit their needs and their budget, and subscribe to receive the data from that device for a certain period of time, to be sent to a specified MQTT endpoint.
So what we need is:
- A Pebble tracker device for us (as device owners!) to collect and transmit tamper-proof location and environmental data points
- A smart contract to act as the data marketplace, where device owners can register their devices, and data buyers can subscribe for data streams
- An MQTT endpoint where the data should be sent once we (as the buyers!) have made a subscription
- A web UI to allow for easy interaction with the smart contract!
This is a simple web interface showing the list of registered devices along with their specs. The UI allows for buyers to subscribe for a specific device data, and allows device owners to register new devices and claim their earnings!
PEBBLEAs Pebble it's still in beta version, if you are interested in obtaining a demo unit you can get in contact with IoTeX at hello@iotex.io, or just drop a message in the Telegram developer chat at t.me/iotexdev: if your idea is interesting enough, you may also be eligible for a developer grant ;-) Firstly, I 3D printed the case for Pebble, that I created for this previous project, then assembled the board and the battery inside of it, and placed it out of my window: the case is definitely not waterproof, but the position protected enough for the experiment!
Pebble will, by default, send all the data to the MQTT backend trypebble.io:8080, the public endpoint hosted by the IoTeX Foundation. Since Pebble uses a secure element to sign its data, we need the public key of the device to be able to verify the data, so let's open the dashboard at http://trypebble.io:8080/devices, identify the IMEI of our device in the list, and check the latest telemetry to take note of the rsa_n
and rsa_e
values:
THE SMART CONTRACT
To complement Pebble, the IoTeX Foundation has also developed Prophecy: a demo smart contract that enables simple peer-to-peer buy & sell of data streams, so that's what we will use:
Clone the contract:
git clone https://github.com/iotexproject/pebble-prophecy.git
cd pebble-prophecy/contracts/contracts
to compile and deploy the contract we will need the ioctl
IoTeX command line client, and a IoTeX wallet with some testnet-IOTX balance:
Install ioctl
(learn more about ioctl
):
curl https://raw.githubusercontent.com/iotexproject/iotex-core/master/install-cli.sh | sh
ioctl config set endpoint api.testnet.iotex.one:443
Create a blockchain account and show the address:
ioctl account createadd mywallet
ioctl account list
io1r4m2hx0a7qk43kuvd35jcf36pf8ylnm6ekvr3v - mywallet
Obtain some Testnet-IOTX tokens here: https://faucet.iotex.io/ - send them you your just-created wallet and verify your new balance:
ioctl account balance mywallet
io1r4m2hx0a7qk43kuvd35jcf36pf8ylnm6ekvr3v: 1000 IOTX
Ready to deploy the contract!
ioctl contract compile Prophecy --abi-out a.abi --bin-out a.bin
ioctl contract deploy bin a.bin --endpoint api.iotex.one:443 -s mywallet
Take note of the action ID in the deploy
command output, visit the IoTeX Testnet explorer and input the tx id to discover what's the blockchain address where the Prophecy contract has been deployed to:
Once the smart contract is deployed and we got the contract address, we should make sure that our Pebble is actually monitoring our contract: there is no quick way to change this setting so I edited the file src/mwtt/contract_endpoint.c
of the Pebble firmware to replace the default contract address with mine, and followed the official docs to rebuild the firmware.
Finally, we are ready to interact with the IoT data marketplace! We will first use ioctl
again to test that everything works. As we are the data marketplace owners (we deployed the contract!!) we can manage the contract rules and whitelist what devices can register (ioctl contracts call reference):
Whitelisting devices for registration:
ioctl contract invoke function io1xmn76c7tg9u0gacrp9l2ps2cnltnyttz7cq6l3 a.abi preRegister --with-arguments '{"_deviceId":"0x0000000000000000000000000000000000333532363536313030373834343135"}' -l 10000000 -s mywallet
Once a device is whitelisted, it can be registered to the marketplace as a data provider: the _deviceID
argument is the hex-encoded IMEI of your device, the _rsaPubkeyN
and _rsaPubkeyE
are the public key numbers of the device, and _price
is the price for our data, expressed in IOTX per IoTeX Block (1 IoTeX block lasts about 5s).
ioctl contract invoke function io1xmn76c7tg9u0gacrp9l2ps2cnltnyttz7cq6l3 a.abi a.abi registerDevice --with-arguments '{"_deviceId":"0x0000000000000000000000000000000000333532363536313030373834343135","_freq":"10","_price":"1","_spec":"mydevice.specs","_rsaPubkeyN": "B4CB45541BBBE9CF3DC64D33F6E0E3F922472EE09992A47D540A220B11C022C81D9CFEEE16611C422C629E66D81661B3A1A3D8464A57D61DAEDF7CEB85B2132D33355C54D8E1FBA9D751F70BC9FDA355D008BBB94CCBE64F5EA4658EA4907614D77447616FB3D1A903FB8285596446EDF5FF830F2BE657F0DAF4F63E0C2B922C48C5DE828D9A4642D875385FEE431170F60DC6E98ED7FD50FC20CC067A795E10F4B916908DCAA272EA240F1BB82871A2A3F4FFF0D028133F4220E0C829F592D93B2E52F04F4553E36DF9FC6A958959F91361ABD206476063AC1535C427384AACAA2B15873731A7681B29E4D7F2A525DE681E410E156BDBDAB57489DCDD56BD71","_rsaPubkeyE":"0x010001"}' -l 10000000 -s mywallet
and finally a buyer can subscribe to that device with the command below. To preserve the buyer's privacy, the _storageEndpoint
is the RSA encrypted value of the MQTT endpoint where the data should be sent to (see this example for how to encrypt the endpoint), _storageToken
is the optional (encrypted) token if it's required by the MQTT endpoint for storage, and _duration is the interval of time we want the subscription to last for (in terms of IoTeX blocks).
ioctl contract invoke function io1xmn76c7tg9u0gacrp9l2ps2cnltnyttz7cq6l3 a.abi subscribe --with-arguments '{"_deviceId":"0x0000000000000000000000000000000000333532363536313030373834343135","_duration":"100","_storageEPoint":"0x890e6f22a4d4f512759a748deb7a7afe435615bf81fc97f9147e2c801c1aa49719cc7c9002c4e034d6e9cae8a1fa528c208a6038c296e0de833211251c5a2e41f4a2247cfd05b7b3a9312b1c76421f47f1526e1763ce04bafa519a860c9e10504cd02c3d20b95fc49bd575f1763f566dcd413cb302e2ef993ea86e3ea07198586e842c9e7ef4beb06341838370ce7e1a840ef93f3530df01e516a16f23ee2ccf0b83f4717334d23c834ff1880fffb316c4c6251ac3ec7b96b9182bc3da340595453464f65249eebe8e99c96d6864acda636981a775daadb7cc09963413631d1f427ccc3019bcf0d5d734189cdfe3fff3d490552de8dddf638cbd5054d6c8f8d3","_storageToken":""}' -l 10000000 -s mywallet
THE WEB APP
Since IoTeX provides SDKs to interact with the blockchain for many different languages, we can easily build a web UI to visualize the data marketplace contract. One example is visible at: iotexlab.io/datammarket. To develop such a web app is as easy as cloning the IoTeX dApp starter at:
https://github.com/iotexproject/iotex-dapp-sample
where the relevant code to interact with the Prophecy contract is attached!
Comments
Please log in or sign up to comment.