wallet_export/
wallet_export.rs

1//! Wallet Export Example
2//!
3//! This example demonstrates how to export a wallet's private key.
4//! ⚠️ **SECURITY WARNING**: This operation exposes sensitive private key material!
5//!
6//! It shows how to:
7//! - Initialize a Privy client with app credentials
8//! - Export wallet private keys with proper authorization
9//! - Handle export responses containing sensitive key data
10//!
11//! ## Required Environment Variables
12//! - `PRIVY_APP_ID`: Your Privy app ID
13//! - `PRIVY_APP_SECRET`: Your Privy app secret
14//! - `PRIVY_WALLET_ID`: The wallet ID to export
15//! - `PRIVY_AUTH_SIGNATURE`: Signature authorization (required)
16//!
17//! ## Usage
18//! ```bash
19//! cargo run --example wallet_export
20//! ```
21
22use anyhow::Result;
23use hex::ToHex;
24use privy_rs::{AuthorizationContext, JwtUser, PrivateKey, PrivyClient};
25use tracing_subscriber::EnvFilter;
26
27#[tokio::main]
28async fn main() -> Result<()> {
29    tracing_subscriber::fmt()
30        .with_env_filter(
31            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
32        )
33        .init();
34
35    // Get wallet ID from environment
36    let wallet_id =
37        std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
38
39    tracing::info!(
40        "initializing privy client from environment, wallet_id: {}",
41        wallet_id
42    );
43
44    tracing::info!("Generated HPKE key pair for encryption");
45
46    let private_key = std::fs::read_to_string("private_key.pem")?;
47
48    let client = PrivyClient::new_from_env()?;
49
50    let ctx = AuthorizationContext::new()
51        .push(PrivateKey(private_key))
52        .push(JwtUser(client.clone(), "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhbGV4QGFybHlvbi5kZXYiLCJpYXQiOjEwMDAwMDAwMDAwMH0.IpNgavH95CFZPjkzQW4eyxMIfJ-O_5cIaDyu_6KRXffykjYDRwxTgFJuYq0F6d8wSXf4de-vzfBRWSKMISM3rJdlhximYINGJB14mJFCD87VMLFbTpHIXcv7hc1AAYMPGhOsRkYfYXuvVopKszMvhupmQYJ1npSvKWNeBniIyOHYv4xebZD8L0RVlPvuEKTXTu-CDfs2rMwvD9g_wiBznS3uMF3v_KPaY6x0sx9zeCSxAH9zvhMMtct_Ad9kuoUncGpRzNhEk6JlVccN2Leb1JzbldxSywyS2AApD05u-GFAgFDN3P39V3qgRTGDuuUfUvKQ9S4rbu5El9Qq1CJTeA".to_string()));
53
54    // Export wallet private key (requires authorization signature)
55    let secret_key = client.wallets().export(&wallet_id, &ctx).await?;
56
57    tracing::info!("Successfully decrypted private key");
58    tracing::warn!("SECURITY WARNING: Private key exported and decrypted!");
59    println!(
60        "Decrypted private key (hex): {}",
61        secret_key.encode_hex::<String>()
62    );
63
64    Ok(())
65}