Development
...
Dispatch Transactions
Send Encrypted Message
2 min
sending an encrypted message to another is account is almost identical to docid\ saugsy22lp r9poooth8j , but with two very important differences you need to use the agreementprivatekey you need the recipients public key (see also docid\ rygzpmxxyfxewl83fkg2b ) encrypted messaging uses a https //en wikipedia org/wiki/diffie%e2%80%93hellman key exchange to guarantee that no secret must be sent over the network that's why encrypted messages are only readable by the sender and the receiver smart contracts do not have public keys, so they are not able to process encrypted messages 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 // the agreementprivatekey is necessary for encrypting the message const {publickey, signprivatekey, agreementprivatekey} = 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 // for p2p encrypted messages the agreementprivatekey and the recipients public key is needed const {transaction} = await ledger message sendencryptedmessage({ recipientid, message, messageistext true, feeplanck selectedfeeplanck, senderprivatekey signprivatekey, senderagreementkey agreementprivatekey, senderpublickey publickey, recipientpublickey }) // 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' }); })();