Playing with IOTA: Parsing Data For Supply chain project

Yehia Tarek
Oct 1, 2019 · 4 min read

Hello Everyone, I am new at IOTA development and I decided to make a supply chain system using IOTA the project will force organizations to use Eco-Friendly material by giving rate for each organization of how much they consume the Eco-Friendly materials and keep tracking the product till we get it back for recycling. If you aren’t familiar with IOTA I recommend you read the documentation and you should know Nodejs basics.

While designing my project idea I made a simple prototype for establishing a secure connection to track all products.

What I will do?

1-Create a customized ID refer to Eco-Friendly materials producer, The Buyer and the order of the product( example: first created product, second created product..etc)

2-This ID will be like a bar-code on each product.

3-Each time the bar-code reader detected a product. It will send a JSON Object with the product ID, current bar-code reader ID and the current time. So I could track a large number of products.

How My prototype establishes communication?

1-Send a restricted MAM message with content ‘Initialize Root’ and return the root address.

2-Send all Products as a JSON object using restricted MAM.

3-Send the root address as a public transaction.

4- Fetch root address From public transaction.

5-Parse it to all devices with the side key to fetch all Products JSON objects.

Code Implementation:

First, we need to install the iota packages

npm install @iota/corenpm install @iota/converter


you could use yarn add

Now, create your JavaScript file touch SendPublicTransaction

Import Packages and connect it to the blockchain.

const iotaLibrary = require(‘@iota/core’)const Converter = require(‘@iota/converter’)const iota = iotaLibrary.composeAPI({provider: ‘'})

To send public data, I assume you are familiar with seeds and addresses if note check this link. We need to create a transfer object with the address destination and the message content and for sure the transmitted value on our case will be zero.

const message = Converter.asciiToTrytes(_message)const transfers = [{value: 0,address: _address, // Where the data is being sentmessage: message // The message converted into trytes}]iota.prepareTransfers(_seed, transfers).then(trytes => iota.sendTrytes(trytes, 3, 14)).then(bundle => {console.log(‘Transfer successfully sent’) => console.log(tx))}).catch(err => {console.log(err)})}

To fetch this message :

const FetchPublicTransaction = async (_address) =>{return new Promise(function(resolve, reject) {iota.findTransactionObjects({ addresses: [_address] },function(error, response) {if (error) {reject(error);} else {console.log(‘Encoded message:’)console.log(response[0].signatureMessageFragment)// Modify trytes into a consumable lengthconst trytes = response[0].signatureMessageFragment.slice(0, -1)//Convert trytes to plan text//Convert trytes to plan textconst data = Converter.trytesToAscii(trytes)console.log(‘Decoded message:’)console.log(data)resolve(data)}})})}

If you are facing difficulties in understanding this code check this video created from the IOTA foundation explaining it in detail. I added some promises if you don’t know what is it to check this link.

Then we have the restricted MAM code

Initialize MAM

const mamType = ‘restricted’const mamSecret = ‘DONTSHARETHISPASSWORD’mamState = Mam.changeMode(mamState, mamType, mamSecret

Send a MAM message

const Publish = async data => {// Convert the JSON to trytes and create a MAM messageconst trytes = asciiToTrytes(data)const message = Mam.create(mamState, trytes)// Update the MAM state to the state of this latest messagemamState = message.state// Attach the messageawait Mam.attach(message.payload, message.address, 3, 9)return message.root}

Send more then one Message using foreach

const PublishAll = (noOfMessages)=>{const products = []for (let index = 0; index < noOfMessages; index++) {products.push({id: index,time: (new Date()).toLocaleString()})},product =>{SendMamRestricted.execute(JSON.stringify(product))})}

fetch all Restricted MAM messages:

const FetchMam = async (_root) => {// Callback used to pass data + returns next_rootconsole.log(“inside FetchMamRestricted function root is”,_root) // outptu ‘somthing’const resp = await Mam.fetch(_root, mamType, mamSecret, logData)console.log(resp)}

And that it now all you need to do is to connect all code.

sendID.js File:

const SendMamRestricted = require(“./SendMamRestricted.js”)const SendPublicTransaction= require(“./SendPublicTransaction.js”)const PublishAll = require(‘./PublishAll.js’)const seed = ‘any 81 capital char and 9’const address = ‘any 81 capital char and 9’SendMamRestricted.execute(“initialize root”).then(function(result) {SendPublicTransaction.execute(seed,address,result)PublishAll.execute(10)})

fetchID.js File:

const FetchPublicTransaction = require(‘./FetchPublicTransaction.js’)const FetchMamRestricted = require(‘./FetchMamRestricted.js’)const address = ‘any random 81 capital char and 9’const main = async()=>{const root = await FetchPublicTransaction.execute(address)// retrun rootawait FetchMamRestricted.execute(root.substring(0,root.length-1012))}main()

Find all code on my Github link:

If you have any question please ask me. Thank you!

Get Best Software Deals Directly In Your Inbox

Sign up for Coinmonks

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.


Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project —

Yehia Tarek

Written by

A Full-Stack Blockchain Developer,


Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project —

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store