#
Encrypt buffer
The Wax library provides a robust set of tools for handling encryption and decryption within the Hive ecosystem. This includes both methods for direct encryption/decryption as well as using the Transaction
interface to handle encrypted operations.
Public Keys in Hive
One important detail to note is that public keys within the Hive ecosystem start with the prefix STM
.
#
Using Direct Encryption and Decryption Methods
The encrypt
and decrypt
methods allow for straightforward encryption and decryption operations with explicit public keys. The first argument is your opened beekeeper wallet instance, second argument is your content as string to be encrypted/decrypted, and then public key(s) used for encryption/decryption. Below are examples covering the case with one and two keys (sender key and receiver key):
import { createHiveChain } from "@hiveio/wax";
const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData; /* Import preconfigured beekeeper data specific to snippet examples */
const hiveChain = await createHiveChain();
const content = "This is a secret message.";
// Encrypt the content - sender side
const encryptedContent = hiveChain.encrypt(wallet, content, publicKey1);
// Decrypt the content - receiver side
const decryptedContent = hiveChain.decrypt(wallet, encryptedContent);
console.log(decryptedContent); // This is a secret message.
import { createHiveChain } from "@hiveio/wax";
const { wallet, publicKey1, publicKey2 } = globalThis.snippetsBeekeeperData; /* Import preconfigured beekeeper data specific to snippet examples */
const hiveChain = await createHiveChain();
const content = "This is a secret message.";
// Encrypt the content using two keys - sender side
const encryptedContent = hiveChain.encrypt(wallet, content, publicKey1, publicKey2);
// Decrypt the content - receiver side
const decryptedContent = hiveChain.decrypt(wallet, encryptedContent);
console.log(decryptedContent); // This is a secret message.
A second public key is passed to the encryption method in this example for later use during decryption - matching public key to the private key in the wallet.
Using private keys
Public key, you are providing to the encrypt
/decrypt
method should be previously imported as private keys to the Beekeeper wallet instance, you are providing as an argument. If requested public key is not found in the wallet, exception is thrown.