privy-rs 0.1.0-alpha.5

Privy SDK for Rust
Documentation
//! Wallet Export Example
//!
//! This example demonstrates how to export a wallet's private key.
//! ⚠️ **SECURITY WARNING**: This operation exposes sensitive private key material!
//!
//! It shows how to:
//! - Initialize a Privy client with app credentials
//! - Export wallet private keys with proper authorization
//! - Handle export responses containing sensitive key data
//!
//! ## Required Environment Variables
//! - `PRIVY_APP_ID`: Your Privy app ID
//! - `PRIVY_APP_SECRET`: Your Privy app secret
//! - `PRIVY_WALLET_ID`: The wallet ID to export
//! - `PRIVY_AUTH_SIGNATURE`: Signature authorization (required)
//!
//! ## Usage
//! ```bash
//! cargo run --example wallet_export
//! ```

use anyhow::Result;
use hex::ToHex;
use privy_rs::{AuthorizationContext, JwtUser, PrivateKey, PrivyClient};
use tracing_subscriber::EnvFilter;

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
        )
        .init();

    // Get wallet ID from environment
    let wallet_id =
        std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");

    tracing::info!(
        "initializing privy client from environment, wallet_id: {}",
        wallet_id
    );

    tracing::info!("Generated HPKE key pair for encryption");

    let private_key = std::fs::read_to_string("private_key.pem")?;

    let client = PrivyClient::new_from_env()?;

    let ctx = AuthorizationContext::new()
        .push(PrivateKey::new(private_key))
        .push(JwtUser(client.clone(), "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhbGV4QGFybHlvbi5kZXYiLCJpYXQiOjEwMDAwMDAwMDAwMH0.IpNgavH95CFZPjkzQW4eyxMIfJ-O_5cIaDyu_6KRXffykjYDRwxTgFJuYq0F6d8wSXf4de-vzfBRWSKMISM3rJdlhximYINGJB14mJFCD87VMLFbTpHIXcv7hc1AAYMPGhOsRkYfYXuvVopKszMvhupmQYJ1npSvKWNeBniIyOHYv4xebZD8L0RVlPvuEKTXTu-CDfs2rMwvD9g_wiBznS3uMF3v_KPaY6x0sx9zeCSxAH9zvhMMtct_Ad9kuoUncGpRzNhEk6JlVccN2Leb1JzbldxSywyS2AApD05u-GFAgFDN3P39V3qgRTGDuuUfUvKQ9S4rbu5El9Qq1CJTeA".to_string()));

    // Export wallet private key (requires authorization signature)
    let secret_key = client.wallets().export(&wallet_id, &ctx).await?;

    tracing::info!("Successfully decrypted private key");
    tracing::warn!("SECURITY WARNING: Private key exported and decrypted!");
    println!(
        "Decrypted private key (hex): {}",
        secret_key.encode_hex::<String>()
    );

    Ok(())
}