use phala_tee_deploy_rs::{Encryptor, Error, Result, TeeDeployerBuilder};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::env;
#[tokio::main]
async fn main() -> Result<()> {
dotenv::dotenv().ok();
println!("🔷 OPERATOR (Phase 1): Setting up infrastructure and obtaining public key");
let mut deployer = TeeDeployerBuilder::new()
.with_api_key(env::var("PHALA_CLOUD_API_KEY").expect("PHALA_CLOUD_API_KEY must be set"))
.with_api_endpoint(
env::var("PHALA_CLOUD_API_ENDPOINT")
.unwrap_or_else(|_| "https://cloud-api.phala.network/api/v1".to_string()),
)
.build()?;
println!("🔍 Discovering available TEEPods...");
let teepods = deployer.discover_teepod().await?;
println!("✅ Selected TEEPod with ID: {}", teepods.nodes[0].teepod_id);
println!("📄 Creating VM configuration...");
let docker_compose = r#"
version: '3'
services:
app:
image: nginx:alpine
ports:
- "8080:80"
environment:
- DB_PASSWORD
- API_SECRET
"#;
let vm_config = deployer.create_vm_config(
docker_compose,
"secure-workflow-example",
Some(1), Some(1024), Some(10), )?;
let vm_config_json = serde_json::to_value(&vm_config).unwrap();
println!("🔑 Requesting encryption public key...");
let pubkey_response = deployer.get_pubkey_for_config(&vm_config_json).await?;
let public_key = pubkey_response.app_env_encrypt_pubkey;
let salt = pubkey_response.app_id_salt;
let app_id = pubkey_response.app_id.clone();
println!("✅ Public key obtained: {}", public_key);
println!("✅ Salt obtained: {}", salt);
println!("✅ App ID: {}", app_id);
println!("\n======== SECURE CHANNEL ========");
println!("Operator securely shares the public key with the user");
println!("======== SECURE CHANNEL ========\n");
println!("🔶 USER: Encrypting sensitive environment variables");
let user_env_vars = vec![
(
"DB_PASSWORD".to_string(),
"super-secret-db-password".to_string(),
),
("API_SECRET".to_string(), "user-private-api-key".to_string()),
];
println!("🔐 Encrypting environment variables...");
let encrypted_env = Encryptor::encrypt_env_vars(&user_env_vars, &public_key)?;
println!("✅ Environment variables encrypted successfully");
println!("\n======== SECURE CHANNEL ========");
println!("User securely shares the encrypted environment variables with the operator");
println!("But never reveals the plaintext values");
println!("======== SECURE CHANNEL ========\n");
println!("🔷 OPERATOR (Phase 2): Deploying with encrypted environment variables");
println!("🚀 Deploying application...");
let deployment = deployer
.deploy_with_encrypted_env(vm_config_json, encrypted_env, &public_key, &salt)
.await?;
let full_app_id = format!("app_{}", app_id);
println!("\n✅ Deployment successful!");
println!(" Deployment ID: {}", deployment.id);
println!(" App ID: {}", app_id);
println!(" Full Application Identifier: {}", full_app_id);
println!(" Status: {}", deployment.status);
println!("\n✨ You can check the network information for your deployment using:");
println!(" cargo run --example network_info {}", full_app_id);
Ok(())
}