# Call to sendMessage with encrypted message

**URL:** <https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860>\
**Category:** Developers\
**Created:** [21 February 2023 21:56 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860 "2023-02-21T21:56:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [21 February 2023 21:56 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/1 "2023-02-21T21:56:35Z")

</div>

**Hello team!**

I am trying to make a call to sendMessage with the following fields:

**messageToEncrypt** : message,  
**messageToEncryptIsText** : true,  
**encryptedMessageIsPrunable** : true,

**The problem is that it doesn't return unsignedTransactionBytes to be able to sign it.**

What am I doing wrong?

If I try sending a message in plain text it does return unsignedTransactionBytes and everything works correctly.

Thank you very much for your time.

---

<div class="post-metadata">

**Author:** ![petko.petkov](https://talk.jelurida.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@petko.petkov](https://talk.jelurida.com/u/petko.petkov)\
**Post date:** [22 February 2023 10:13 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/2 "2023-02-22T10:13:45Z")

</div>

Are you providing a `secretPhrase`? The secret phrase is needed to do the encryption.

---

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [22 February 2023 10:17 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/3 "2023-02-22T10:17:31Z")

</div>

**Thank you for your response.**

Currently we always use the publicKey to sign messages, we never use the passPhrase.

Could this be the problem?  
Isn't it insecure to send requests with the key in plain text?

---

<div class="post-metadata">

**Author:** ![petko.petkov](https://talk.jelurida.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@petko.petkov](https://talk.jelurida.com/u/petko.petkov)\
**Post date:** [22 February 2023 10:50 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/4 "2023-02-22T10:50:00Z")

</div>

I guess you mean that you create the transaction bytes with pubic key? You cannot sign with pubic key 🙂

Encryption also cannot be done without pubic key, so the API returns the best it can.

---

<div class="post-metadata">

**Author:** ![petko.petkov](https://talk.jelurida.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@petko.petkov](https://talk.jelurida.com/u/petko.petkov)\
**Post date:** [22 February 2023 10:52 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/5 "2023-02-22T10:52:17Z")

</div>

Yes, it is insecure to provide your privateKey/secretPhrase in plain text or to a node you don't own. I'm not sure what is your setup and where the signing happens. Wherever it is, you can do the encryption there and then provide the encrypted data via `encryptedMessageData` and `encryptedMessageNonce`

---

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [22 February 2023 10:57 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/6 "2023-02-22T10:57:37Z")

</div>

Yes, it is just as you say. I explained myself wrong, my mistake.

**I am trying to generate the transationBytes to sign it locally.**

The problem is that the request **does not return unsignedTransactionBytes.**

**What kind of encryption does the message have?** I will encrypt it locally and send it via `encryptedMessageData` and `encryptedMessageNonce`.

I am not sure how to do this encryption.  
Checking the API I can only see `encryptTo` which also asks for the passPhrase.

Any solution?

---

<div class="post-metadata">

**Author:** ![petko.petkov](https://talk.jelurida.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@petko.petkov](https://talk.jelurida.com/u/petko.petkov)\
**Post date:** [22 February 2023 11:08 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/7 "2023-02-22T11:08:39Z")

</div>

Of course no API can do the encryption without a private key.

We use AES where the shared key is calculated from the sender's private, recipient public and the nonce. See `Crypto.getSharedKey` for more details. If you use java locally, you can depend on `ardor.jar` (here we provide it as gradle project which can be included in your project [Bitbucket](https://bitbucket.org/Jelurida/ardor-library/src/master/) ) and call e.g. `EncryptedData.encrypt` or `Account.encryptTo`. This should be available in javascript too. Not sure for other languages.

---

<div class="post-metadata">

**Author:** ![petko.petkov](https://talk.jelurida.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@petko.petkov](https://talk.jelurida.com/u/petko.petkov)\
**Post date:** [22 February 2023 11:11 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/8 "2023-02-22T11:11:45Z")

</div>

> [@6DeadSky9](#):
>
> I am trying to generate the transationBytes to sign it locally.

how do you sign it locally - this is the main question. Easiest thing is to do the encryption there.

---

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [22 February 2023 11:35 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/9 "2023-02-22T11:35:42Z")

</div>

I am using **ReactJS + ArdorJS**.

I'm checking and there is the `encryptMessage` function, I think it could be ideal.

**The problem is that when calling the function the library generates an error.**

 ![imagen](https://talk.jelurida.com/uploads/default/original/1X/0cc581bc17c738a62294bff4e254b4d5c7873a02.png)

It seems that the problem is with the `curve25519_` library. (Start on line 380)

I am trying to replicate the same operation on my own.

---

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [22 February 2023 11:37 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/10 "2023-02-22T11:37:22Z")

</div>

The process is as follows:

- I get the public key from the receiver

- I encrypt the message

**Here it already generates a failure, but the path would follow:**

- Calculate the fees

- I generate `unsignedTransactionBytes` and sign it

- Broadcast it

---

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [22 February 2023 11:51 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/11 "2023-02-22T11:51:21Z")

</div>

**UPDATE**

It was a typo. When exporting a single package there is no need to place the braces ({})

In the file ardorjs/util/curve25519\_.js in the last line:  
`export default { curve25519_ };` to `export default curve25519_;`

Now I can encrypt and send the message.  
**Everything is solved!**

Thank you very much!

---

<div class="post-metadata">

**Author:** ![petko.petkov](https://talk.jelurida.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@petko.petkov](https://talk.jelurida.com/u/petko.petkov)\
**Post date:** [22 February 2023 12:27 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/12 "2023-02-22T12:27:13Z")

</div>

OK, I pushed your fix and one more fix of the verification tests, which was preventing me to test your stuff. I pushed to my fork of the repo ([GitHub - petko-petkov-jelurida/ardorjs: Transaction signing for ardor transactions, and ardor address creations](https://github.com/petko-petkov-jelurida/ardorjs/tree/master)) and created a pull request to [GitHub - mrv777/ardorjs: Transaction signing for ardor transactions, and ardor address creations](https://github.com/mrv777/ardorjs).

I didn't test your fix, maybe check it.

Thanks!

---

<div class="post-metadata">

**Author:** ![6DeadSky9](https://talk.jelurida.com/user_avatar/talk.jelurida.com/6deadsky9/32/288_2.png) [@6DeadSky9](https://talk.jelurida.com/u/6DeadSky9)\
**Post date:** [22 February 2023 13:28 UTC](https://talk.jelurida.com/t/call-to-sendmessage-with-encrypted-message/860/13 "2023-02-22T13:28:24Z")

</div>

For me, the solution is **fully functional**.

In addition I have found other errors, you can see them in:

> [@ArdorJS library fixes](https://talk.jelurida.com/t/ardorjs-library-fixes/861):
>
> I'm working with ReactJS + ArdorJS but the library has some small bugs: ardorjs/lib/account add: import { Buffer } from "buffer"; ardorjs/lib/helpers.js add: import { Buffer } from "buffer"; ardorjs/util/curve25519\_.js export default { curve25519\_ }; to export default curve25519\_; I hope it can be useful to someone. grin

**You are welcome, we are here to build!** 🍻
