Vault
Client Methods
Example
use autonomi::Client;
use autonomi::client::payment::PaymentOption;
use autonomi::{SecretKey, Bytes};
use autonomi::vault::app_name_to_vault_content_type;
#[tokio::main]
async fn main() {
// initialize a local client and test wallet
let client = Client::init_local().await.unwrap();
let wallet = get_funded_wallet();
let payment_option = PaymentOption::Wallet(wallet.clone());
// each key can have a vault which will be addressed at the public key (easy to find!)
let main_key = SecretKey::random();
// the content type can be any integer, it is used to identify the type of data in the vault
let content_type = app_name_to_vault_content_type("Choose a unique name for your app here");
let original_content = Bytes::from_static(b"Store any data you want in the vault");
// if it doesn't already exist, the vault will be created
let cost = client
.write_bytes_to_vault(
original_content.clone(),
payment_option.clone(),
&main_key,
content_type,
)
.await
.unwrap();
println!("Vault creation cost: {cost}");
// fetch the vault and decrypt it
let (fetched_content, fetched_content_type) = client.fetch_and_decrypt_vault(&main_key).await.unwrap();
// it should match the original content
assert_eq!(fetched_content_type, content_type);
assert_eq!(fetched_content, original_content);
// the vault can be updated at will
let new_content = Bytes::from_static(b"Update the vault with new data");
let cost = client
.write_bytes_to_vault(
new_content.clone(),
payment_option.clone(),
&main_key,
content_type,
)
.await
.unwrap();
println!("Vault update cost should be 0: {cost}");
}