1

I am making a simple MAM publish and fetch script. The first script is mam_publish which publishes the data. The other script is mam_receive which fetched the already published data.

Now, the problem is that publishing script is working correctly but fetch script is not fetching the data. It is only executing the line

console.log('\n\nFETCHING DATA!!\n\n')

I tried debugging the code, but I am unable to figure out the problem. Can someone take a look?

Here is the script for mam_publish.js

const Mam = require('@iota/mam')
const { asciiToTrytes } = require('@iota/converter')
const IOTA = require('iota.lib.js');
const mode = 'restricted'
const secretKey = 'VERYSECRETKEY'
const provider = 'https://nodes.devnet.iota.org'

let numberofSteps;
let kilometersWalked;

let mamState = Mam.init(provider)
// Set channel mode
mamState = Mam.changeMode(mamState, mode, secretKey)

const publish = async packet => {
        // Create MAM Payload - STRING OF TRYTES
        const trytes = asciiToTrytes(packet)
        const message = Mam.create(mamState, trytes)
         // Save new mamState
        mamState = message.state
         // Attach the payload
        await Mam.attach(message.payload, message.address, 3, 9)
        console.log('Published', packet, '\n');
        console.log ('Root', message.root);
        //return message.root
    }

const publishAll = async () => {
  const root = await publish({
    numberofSteps: Math.floor((Math.random()*89)+10),
    kilometersWalked: (numberofSteps/1312.335),
    message: 'Message from Alice',
    timestamp: (new Date()).toLocaleString()
  })

  await publish({
    numberofSteps: Math.floor((Math.random()*89)+10),
    kilometersWalked: (numberofSteps/1312.335),
    message: 'Message from Alice',
    timestamp: (new Date()).toLocaleString()
  })
  return root
}
publishAll()

and the script for mam_receive

const Mam = require('./lib/mam.client.js');
const { trytesToAscii } = require('@iota/converter')
const IOTA = require('iota.lib.js');
const mode = 'restricted'
const secretKey = 'VERYSECRETKEY'
const provider = 'https://nodes.devnet.iota.org'

let root = 'SBOKXANECLJFR9QHUARPNFKJKRCEEQ9YACINNUPNOVTLZJCLUZN9ANTHZAATNCYLZGMVVVOBAUETPAHOZ';

// Initialise MAM State
let mamState = Mam.init(provider);
mamState = Mam.changeMode(mamState, mode, secretKey)


const logData = data => console.log('Fetched and parsed', JSON.parse(trytesToAscii(data)), '\n')


const execute = async root => {
    await Mam.fetch(root, mode, secretKey, logData)
    // Output synchronously once fetch is completed
   const result = await Mam.fetch(root, mode, secretKey)
   result.messages.forEach(message => console.log('Fetched and parsed', JSON.parse(trytesToAscii(message)), '\n'))
    }

console.log('\n\nFETCHING DATA!!\n\n')
execute()

Any leads would be highly appreciated.

4
  • 1
    your execute() function takes a root parameter you are not passing... (you probably want to pass the global root variable for it). Also probably you want to log your result object before iterating over its (possibly empty) messages...
    – mihi
    Commented Dec 8, 2019 at 12:33
  • I tried passing the root variable in execute() function, but the result is same. By logging result you mean use console.log()?
    – degr8sid
    Commented Dec 9, 2019 at 14:26
  • 1
    yes, console.log :)
    – mihi
    Commented Dec 9, 2019 at 20:36
  • I tried making the changes that you suggested, but the result is same. It is still executing console.log('\n\nFETCHING DATA!!\n\n') but not executing the execute function even though I passed a root execute(root). Any thing else that I might be overlooking?
    – degr8sid
    Commented Dec 10, 2019 at 13:46

1 Answer 1

1

Mam.fetch is not an async function, instead use the callback. You can make any “callback style” function into an async function by wrapping the call in a return new Promise((resolve,reject)=> .... Do a bit of research about Promises, this is a very common and useful pattern in Nodejs.

But i think your problem is secretKey needs to be converted to trytes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.