#
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 anencrypted
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.
Non-encrypted operations
startEncrypt
only enables encryption for the operation data above, other data is not encrypted.
#
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.
Test it yourself: src/typescript/encryption/encrypt-operations/one-key.ts
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);
publicKey1
used for transaction signing can (and usually does) differ from the public key used for encryption.
Test it yourself: src/typescript/encryption/encrypt-operations/multiple-keys.ts
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);
Test it yourself: src/typescript/encryption/encrypt-operations/multiple-start-encrypt.ts
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);
publicKey1
used for transaction signing can (and usually does) differ from the public key used for encryption.