Development
...
Recipes
Dispatch Transactions
Send Message
1min
Sending a message to another account is almost the same like Send Signa.
JS
const {Address, AddressPrefix} = require("@signumjs/core");
const {generateMasterKeys} = require("@signumjs/crypto");
const LedgerHostUrls = {
TestNet: 'http://localhost:6876',
MainNet: 'https://europe.signum.network',
}
async function sendMessage(args) {
try {
const {ledgerType, recipient, message, feeType, passphrase} = args
// we create our client depending on the parameter
const ledger = LedgerClientFactory.create({
nodeHost: LedgerHostUrls[ledgerType])
}
// we check if incoming account is either a Signum Address, or Numeric Id
// eventually, we convert to Numeric Id, if the value is invalid an error will be thrown
const recipientId = Address.create(recipient).getNumericId()
// now we check if the account exists. If it does not exist the method throws an
// exception and the further flow is interrupted.
// Using exception to assert certain conditions is IMHO more robust than checking returned
// values with if-statements. The code is much cleaner
console.info("Verifying recipient...")
await ledger.account.getAccount({accountId: recipientId})
// before we can send we need to get the private signing key from the user
const {publicKey, signPrivateKey} = generateMasterKeys(passphrase)
// as a next step, we use the systems fee suggestion mechanism to give us the current costs for the chosen
// fee type. In this example we let it flexible, but you can fix the fee costs to a minimum, i.e. 0,00735 SIGNA
const suggestedFees = await ledger.network.getSuggestedFees()
// we assume that the feeType is either ['Minimum', 'Cheap', 'Standard', 'Priority']
const selectedFeePlanck = suggestedFees[feeType.toLowerCase()]
// Now, we execute the transaction
// within the method the local signing flow is being executed, i.e.
// the private key is used only locally for signing, but never sent over the network
const {transaction} = await ledger.message.sendMessage({
recipientId,
message,
messageIsText: true,
feePlanck: selectedFeePlanck,
senderPrivateKey: signPrivateKey,
senderPublicKey: publicKey
});
// now, some final nice message
const addressPrefix = ledgerType === 'MainNet' ? AddressPrefix.MainNet : AddressPrefix.TestNet
console.info(`Successfully sent the message to ${Address.create(recipientId, addressPrefix).getReedSolomonAddress()} - Transaction Id: ${transaction}`)
} catch (e) {
// If the API returns an exception,
// the return error object is of type HttpError
handleError(e);
}
}
// // Our entry point has to be async, as our subsequent calls are.
// // This pattern keeps your app running until all async calls are executed
(async () => {
await sendSigna({
ledgerType: 'TestNet',
recipient: 'TS-QAJA-QW5Y-SWVP-4RVP4',
message: 'This is a simple message',
feeType: 'cheap',
passphrase: 'YourVerySecretPassphraseWhichCannotBeRevealedHere'
});
})();
Updated 03 Mar 2023
Did this page help you?