2

While following the IOTA JavaScript guide, I changed the example node to a main net node - https://nodes.thetangle.org:443 and received the following error message below. How do I get messages from the main net to be displayed? I apologize if this seems a bit rudimentary but I'm simply trying to understand all this. Thanks for your response in advance.

const Iota = require('@iota/core');
const Extract = require('@iota/extract-json');

// Connect to a node
const iota = Iota.composeAPI({
  provider: 'https://nodes.iota.org:443'
});

// Define the tail transaction hash of the bundle whose messages you want to read
const tailTransactionHash =
    'UKPUW9NQYXBWXAURISOSCUNFMLSFWKNO9WJOGTEJRCTLJFEIKVBXGUVRAJKZWWMUKDCWLKBJNOYIA9999';

// Get the transaction objects in the bundle
iota.getBundle(tailTransactionHash)
  .then(bundle => {
    // Extract and parse the JSON messages from the transactions' `signatureMessageFragment` fields
    console.log(JSON.parse(Extract.extractJson(bundle)));
  })
  .catch(err => {
    console.error(err);
  });

Error Message:

PS C:\IOTA> node Hello_World_Test.js
Error: Invalid JSON encoded message
    at Object.exports.extractJson (C:\IOTA\node_modules\@iota\extract-json\out\extract-json\src\index.js:80:15)
    at C:\IOTA\Hello_World_Test.js:21:36
    at tryCatcher (C:\IOTA\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\IOTA\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (C:\IOTA\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (C:\IOTA\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (C:\IOTA\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (C:\IOTA\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (C:\IOTA\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (C:\IOTA\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\IOTA\node_modules\bluebird\js\release\async.js:15:14)
    at processImmediate (internal/timers.js:456:21)

1 Answer 1

1

The '@iota/extract-json' module provides a way to encode arbitrary JSON (of arbitrary length) into a IOTA bundle and decode it from there again. Decoding requires that the transactions have actually been built the same way.

IOTA transactions have other ways of embedding some text, the oldest one being to put them in trytes directly (only uppercase letters and 9 allowed), the next one being Converter.asciiToTrytes.

As you can see on thetangle.org, your transaction uses a simple text message (you have the options to show "Text" or "Trytes", but only "Text" shows meaningful text). In case of JSON encoded messages, there would be a "JSON" option, too.

Therefore the error message is expected. Just like your image editor program would show an error if you opened a Word document in it.

You can get the trytes of your transaction as

console.log(bundle[0].signatureMessageFragment);

and the text message as

const Converter = require('@iota/converter');

Converter.trytesToAscii(bundle[0].signatureMessageFragment.substring(0,2186))

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.