Playing with IOTA: Parsing Data For Supply chain project


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/core
npm 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 sent
message: message // The message converted into trytes
.prepareTransfers(_seed, transfers)
.then(trytes => iota.sendTrytes(trytes, 3, 14))
.then(bundle => {
console.log(‘Transfer successfully sent’) => console.log(tx))
.catch(err => {

To fetch this message :

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

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’
mamState = Mam.changeMode(mamState, mamType, mamSecret

Send a MAM message

const Publish = async data => {
// Convert the JSON to trytes and create a MAM message
const trytes = asciiToTrytes(data)
const message = Mam.create(mamState, trytes)
// Update the MAM state to the state of this latest message
mamState = message.state
// Attach the message
await 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++) {
id: index,
time: (new Date()).toLocaleString()
},product =>{

fetch all Restricted MAM messages:

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

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) {

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 root
await FetchMamRestricted.execute(root.substring(0,root.length-1012))

Find all code on my Github link:

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

Playing with IOTA: Parsing Data For Supply chain project was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.