Use
Memos
Memo.encode encrypts a private message and Memo.decode decrypts it.
Rules
- Only text that starts with
#is encrypted; other text is returned unchanged. - Encrypt with the sender's memo private key and the recipient's memo public key.
- Decrypt with either side's memo private key.
Encrypt
import { Memo, PrivateKey } from 'hive-tx'
const senderMemoKey = PrivateKey.from('5J...sender-memo-key')
const encrypted = Memo.encode(senderMemoKey, 'STM...recipient-memo-public-key', '#Hello')
To encrypt by account name, use encodeForAccount; it looks up the recipient's memo key:
const encrypted = await Memo.encodeForAccount(senderMemoKey, 'bob', '#Hello')
Put the result in a transfer's memo field.
Decrypt
const recipientMemoKey = PrivateKey.from('5J...recipient-memo-key')
const text = Memo.decode(recipientMemoKey, encryptedMemo) // starts with '#'
Caution
Memos use AES-CBC with a key shared between the two memo keys. The memo key is separate from the posting and active keys; do not use those to encrypt.