update_wallet/
update_wallet.rs

1//! Update Wallet Example
2//!
3//! This example demonstrates how to update a wallet's owner using signature authorization.
4//! It shows how to:
5//! - Initialize a Privy client with app credentials
6//! - Load private keys for signature authorization
7//! - Update a wallet's owner with proper authorization
8//! - Handle signature authorization errors and responses
9//!
10//! ## Required Environment Variables
11//! - `PRIVY_APP_ID`: Your Privy app ID
12//! - `PRIVY_APP_SECRET`: Your Privy app secret
13//! - `PRIVY_WALLET_ID`: The wallet ID to update
14//!
15//! ## Usage
16//! ```bash
17//! cargo run --example update_wallet
18//! ```
19
20use anyhow::Result;
21use privy_rs::{
22    AuthorizationContext, IntoKey, JwtUser, PrivateKey, PrivyApiError, PrivyClient,
23    PrivySignedApiError,
24    generated::types::{OwnerInput, UpdateWalletBody},
25};
26use tracing_subscriber::EnvFilter;
27
28#[tokio::main]
29async fn main() -> Result<()> {
30    tracing_subscriber::fmt()
31        .with_env_filter(
32            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
33        )
34        .init();
35
36    // Get wallet ID from environment
37    let wallet_id =
38        std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
39
40    tracing::info!(
41        "initializing privy client from environment, wallet_id: {}",
42        wallet_id
43    );
44
45    let file = std::fs::read_to_string("private_key.pem")?;
46
47    let key = PrivateKey(file);
48    let public_key = key.get_key().await?.public_key();
49
50    let client = PrivyClient::new_from_env()?;
51
52    let ctx = AuthorizationContext::new()
53        .push(key)
54        .push(JwtUser(client.clone(), "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhbGV4QGFybHlvbi5kZXYiLCJpYXQiOjEwMDAwMDAwMDAwMH0.IpNgavH95CFZPjkzQW4eyxMIfJ-O_5cIaDyu_6KRXffykjYDRwxTgFJuYq0F6d8wSXf4de-vzfBRWSKMISM3rJdlhximYINGJB14mJFCD87VMLFbTpHIXcv7hc1AAYMPGhOsRkYfYXuvVopKszMvhupmQYJ1npSvKWNeBniIyOHYv4xebZD8L0RVlPvuEKTXTu-CDfs2rMwvD9g_wiBznS3uMF3v_KPaY6x0sx9zeCSxAH9zvhMMtct_Ad9kuoUncGpRzNhEk6JlVccN2Leb1JzbldxSywyS2AApD05u-GFAgFDN3P39V3qgRTGDuuUfUvKQ9S4rbu5El9Qq1CJTeA".to_string()));
55
56    let wallets_client = client.wallets();
57    let wallet = wallets_client.get(&wallet_id).await?;
58
59    tracing::info!("got wallet: {:?}", wallet);
60
61    let wallet = match wallets_client
62        .update(
63            &wallet_id,
64            &ctx,
65            &UpdateWalletBody {
66                owner: Some(OwnerInput::PublicKey(public_key.to_string())),
67                ..Default::default()
68            },
69        )
70        .await
71    {
72        Ok(wallet) => wallet,
73        Err(PrivySignedApiError::Api(PrivyApiError::UnexpectedResponse(r))) => {
74            let text = r.text().await.unwrap_or_default();
75            tracing::warn!("unexpected response: {:?}", text);
76            anyhow::bail!("unexpected response")
77        }
78        Err(e) => {
79            anyhow::bail!("unexpected error: {e:?}")
80        }
81    };
82
83    tracing::info!("got updated wallet: {:?}", wallet);
84
85    Ok(())
86}