Show attached messages

This example demonstrates how to read attached messages (not enrcrypted)

In the basic examples we still show how to use SignumJS depending the way you are using SignumJS, i.e. imported via NodeJS package manager or as minified bundle. In case you haven't read about the module system, take a quick look there:

A fully working example is available here

Following functions from SignumJS are used in this example

const {
    composeApi,
    isAttachmentVersion,
    TransactionType,
    TransactionArbitrarySubtype, 
    Address
} = require('@signumjs/core');
const {ChainTime} = require('@signumjs/util')

// Create the Api instance to connect to a blockchain node
const api = composeApi({
    nodeHost: "https://europe.signum.network"
});

// here we check for a certain attachment type, and get the text message if not encrypted
const getMessageText = transaction =>
    isAttachmentVersion(transaction, 'EncryptedMessage')
        ? '<encrypted>'
        : transaction.attachment.message;

async function listMessages(account) {
    try {
        const accountId = Address.create(account).getNumericId()

        // here we can explicitly filter by Transaction Types
        const {transactions} = await api.account.getAccountTransactions(
            {
                accountId,
                lastIndex: 20, // getting only the most recent 20
                type: TransactionType.Arbitrary,
                subtype: TransactionArbitrarySubtype.Message
            }
        );

        // now we iterate through the transactions and print in a formatted way
        transactions.forEach(t => {
            console.info(`On ${ChainTime.fromChainTimestamp(t.blockTimestamp).getDate()}`)
            console.info(`From ${t.senderRS}`)
            console.info(`To ${t.recipientRS}`)
            console.info('Message:\n', getMessageText(t), '\n---')
        })

    } catch (e) {
        handleError(e)
    }
}

Last updated