Encrypt operations
Wax provides a simple interface for encrypting operation data within a transaction using the ITransaction interface. Operation encrypting allows to push operations in transparent way, just by pushing markers when to start and stop encryption.
Operations That Can Be Encrypted
Currently, Hive supports encrypting the following operation data:
-
comment_operation
- Encrypts the
bodyfield. -
custom_json_operation
- Encrypts the
jsonfield. Custom JSON encryption is unique as it wraps the encrypted data in anencryptedkey. -
transfer_operation
- Encrypts the
memofield. -
transfer_to_savings_operation
- Encrypts the
memofield. -
transfer_from_savings_operation
- Encrypts the
memofield. -
recurrent_transfer_operation
- Encrypts the
memofield.
Encrypting Operations within a Transaction
These examples demonstrate how to handle encryption and decryption using both direct methods and the ITransaction interface in the Wax library.
Actual encryption is performed while signing transaction!
One key encryption
Non-encrypted operations
startEncrypt only enables encryption for the operation data above, other data is not encrypted.
import { createHiveChain } from "@hiveio/wax";
/* Import preconfigured beekeeper data specific to snippet examples */
const { signer1, publicKey1 } = globalThis.snippetsBeekeeperData;
const hiveChain = await createHiveChain();
// Create a transaction
const tx = await hiveChain.createTransaction();
// Start the encryption chain
tx.startEncrypt(publicKey1)
.pushOperation({ // Add encrypted operation
transfer_operation: {
from: "alice",
to: "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
await signer1.signTransaction(tx);
console.log(tx.transaction);
publicKey1 used for transaction signing can (and usually does) differ from the public key used for encryption.
Not implemented yet — planned for a future release.
use runner::{SnippetsBeekeeperData, snippets_beekeeper_data};
use wax::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
/* Import preconfigured beekeeper data specific to snippet examples */
let SnippetsBeekeeperData { signer1, public_key1, .. } = snippets_beekeeper_data();
let hive_chain = create_hive_chain(None)?;
// Create a transaction
let mut tx = hive_chain.create_transaction(None).await?;
// Start the encryption chain
tx.start_encrypt(&public_key1, None)
// Add encrypted operation
.push_operation(hive_chain.create_operation(Value::TransferOperation(
Transfer {
from_account: "alice".into(),
to_account: "bob".into(),
// Send 5.100 HIVE (Note: Coins, not satoshis)
amount: hive_chain.hive_coins(5.100)?,
memo: "This memo will be encrypted".into(),
},
)))
.stop_encrypt()?; // Stop the encryption chain
// Sign and build the transaction
tx.perform_operation_encryption(&signer1)?;
tx.sign(&signer1, &public_key1)?;
println!("{:#?}", tx.transaction());
Ok(())
}
Two keys encryption
import { createHiveChain } from "@hiveio/wax";
/* Import preconfigured beekeeper data specific to snippet examples */
const { signer1, publicKey1, publicKey2 } = globalThis.snippetsBeekeeperData;
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_operation: {
from: "alice",
to: "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
await signer1.signTransaction(tx);
console.log(tx.transaction);
Not implemented yet — planned for a future release.
use runner::{SnippetsBeekeeperData, snippets_beekeeper_data};
use wax::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
/* Import preconfigured beekeeper data specific to snippet examples */
let SnippetsBeekeeperData { signer1, public_key1, public_key2, .. } = snippets_beekeeper_data();
let hive_chain = create_hive_chain(None)?;
// Create a transaction
let mut tx = hive_chain.create_transaction(None).await?;
// Start the encryption chain with two keys
tx.start_encrypt(&public_key1, Some(&public_key2))
// Add encrypted operations
.push_operation(hive_chain.create_operation(Value::TransferOperation(
Transfer {
from_account: "alice".into(),
to_account: "bob".into(),
// Send 5.000 HIVE (Note: Coins, not satoshis)
amount: hive_chain.hive_coins(5)?,
memo: "This memo will be encrypted with two keys".into(),
},
)))
.stop_encrypt()?; // Stop the encryption chain
// Sign and build the transaction
tx.perform_operation_encryption(&signer1)?;
tx.sign(&signer1, &public_key1)?;
println!("{:#?}", tx.transaction());
Ok(())
}
Mixed encryption
import { createHiveChain } from "@hiveio/wax";
/* Import preconfigured beekeeper data specific to snippet examples */
const { signer1, publicKey1, publicKey2 } = globalThis.snippetsBeekeeperData;
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_operation: {
from: "alice",
to: "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_operation: {
from: "alice",
to: "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
await signer1.signTransaction(tx);
console.log(tx.transaction);
publicKey1 used for transaction signing can (and usually does) differ from the public key used for encryption.
Not implemented yet — planned for a future release.
use runner::{SnippetsBeekeeperData, snippets_beekeeper_data};
use wax::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
/* Import preconfigured beekeeper data specific to snippet examples */
let SnippetsBeekeeperData { signer1, public_key1, public_key2, .. } = snippets_beekeeper_data();
let hive_chain = create_hive_chain(None)?;
// Create a transaction
let mut tx = hive_chain.create_transaction(None).await?;
// Start the encryption chain with two keys
tx.start_encrypt(&public_key1, Some(&public_key2))
// Add encrypted operations
.push_operation(hive_chain.create_operation(Value::TransferOperation(
Transfer {
from_account: "alice".into(),
to_account: "bob".into(),
// Send 5.100 HIVE (Note: Coins, not satoshis)
amount: hive_chain.hive_coins(5.100)?,
memo: "This memo will be encrypted with two keys".into(),
},
)))
.stop_encrypt()? // Stop the current encryption chain
// Start the encryption chain again, but with one key only
.start_encrypt(&public_key1, None)
// Add other encrypted operations
.push_operation(hive_chain.create_operation(Value::TransferOperation(
Transfer {
from_account: "alice".into(),
to_account: "bob".into(),
// Send 10.050 HIVE (Note: Coins, not satoshis)
amount: hive_chain.hive_coins(10.050)?,
memo: "This memo will be encrypted with one key only".into(),
},
)))
.stop_encrypt()?; // Stop the encryption chain again (optionally)
// Sign and build the transaction
tx.perform_operation_encryption(&signer1)?;
tx.sign(&signer1, &public_key1)?;
println!("{:#?}", tx.transaction());
Ok(())
}