Development
...
Dispatch Transactions
Send Message
1 min
sending a message to another account is almost the same like docid\ wqq2xtdjm6rfkn ilwusw 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' }); })();