# Encrypt operations

Wax provides a simple interface for encrypting operation data within a transaction using the Transaction interface.

# Operations That Can Be Encrypted

Currently, Hive supports encrypting the following operation data:

  • comment - Encrypts the body field.
  • custom_json - Encrypts the json field. Custom JSON encryption is unique as it wraps the encrypted data in an encrypted key.
  • transfer - Encrypts the memo field.
  • transfer_to_savings - Encrypts the memo field.
  • transfer_from_savings - Encrypts the memo field.
  • recurrent_transfer - Encrypts the memo field.

# Encrypting Operations within a Transaction

These examples demonstrate how to handle encryption and decryption using both direct methods and the Transaction interface in the Wax library.

import { createHiveChain } from "@hiveio/wax";

const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData; /* Import preconfigured beekeeper data specific to snippet examples */

const hiveChain = await createHiveChain();

// Create a transaction
const tx = await hiveChain.createTransaction();

// Start the encryption chain
tx.startEncrypt(publicKey1)
  .pushOperation({ // Add encrypted operation
    transfer: {
      from_account: "alice",
      to_account: "bob",
      amount: hiveChain.hiveCoins(5.100), // Send 5.100 HIVE (Note: Coins, not satoshis)
      memo: "This memo will be encrypted"
    }
  })
  .stopEncrypt(); // Stop the encryption chain

// Sign and build the transaction
tx.sign(wallet, publicKey1);

console.log(tx.transaction);
import { createHiveChain } from "@hiveio/wax";

const { wallet, publicKey1, publicKey2 } = globalThis.snippetsBeekeeperData; /* Import preconfigured beekeeper data specific to snippet examples */

const hiveChain = await createHiveChain();

// Create a transaction
const tx = await hiveChain.createTransaction();

// Start the encryption chain with two keys
tx.startEncrypt(publicKey1, publicKey2)
  .pushOperation({ // Add encrypted operations
    transfer: {
      from_account: "alice",
      to_account: "bob",
      amount: hiveChain.hiveCoins(5), // Send 5.000 HIVE (Note: Coins, not satoshis)
      memo: "This memo will be encrypted with two keys"
    }
  })
  .stopEncrypt(); // Stop the encryption chain

// Sign and build the transaction
tx.sign(wallet, publicKey1);

console.log(tx.transaction);
import { createHiveChain } from "@hiveio/wax";

const { wallet, publicKey1, publicKey2 } = globalThis.snippetsBeekeeperData; /* Import preconfigured beekeeper data specific to snippet examples */

const hiveChain = await createHiveChain();

// Create a transaction
const tx = await hiveChain.createTransaction();

// Start the encryption chain with two keys
tx.startEncrypt(publicKey1, publicKey2)
  .pushOperation({ // Add encrypted operations
    transfer: {
      from_account: "alice",
      to_account: "bob",
      amount: hiveChain.hiveCoins(5.100), // Send 5.100 HIVE (Note: Coins, not satoshis)
      memo: "This memo will be encrypted with two keys"
    }
  })
  .stopEncrypt() // Stop the current encryption chain
  .startEncrypt(publicKey1) // Start the encryption chain again, but with one key only
  .pushOperation({ // Add other encrypted operations
    transfer: {
      from_account: "alice",
      to_account: "bob",
      amount: hiveChain.hiveCoins(10.050), // Send 10.050 HIVE (Note: Coins, not satoshis)
      memo: "This memo will be encrypted with one key only"
    }
  })
  .stopEncrypt(); // Stop the encryption chain again (optionally)

// Sign and build the transaction
tx.sign(wallet, publicKey1);

console.log(tx.transaction);