pub struct PrivyClient {
pub jwt_exchange: JwtExchange,
/* private fields */
}Expand description
Privy client for interacting with the Privy API.
This provides access to global operations like user and wallet management.
For wallet-specific operations, use TypedWallet<T> instances created via
the wallet() method.
§Errors
The api calls that require a signature to run will return a PrivySignedApiError
while the others will return a normal PrivyApiError.
Fields§
§jwt_exchange: JwtExchangeA store of all jwt operations for this client
Implementations§
Source§impl PrivyClient
impl PrivyClient
Sourcepub fn new_from_env() -> Result<Self, PrivyCreateError>
pub fn new_from_env() -> Result<Self, PrivyCreateError>
Create a new PrivyClient from environment variables
§Errors
This can fail for three reasons, either the app_id or app_secret are not
valid headers, or that the underlying http client could not be created, or
that the environment variables are not set.
Examples found in repository?
24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Initialize client from environment variables
32 let client = PrivyClient::new_from_env()?;
33
34 tracing::info!("initialized privy client from environment");
35
36 let user = client.users().delete("cmf56qacr01qpl90brxql83lx").await?;
37
38 tracing::info!("deleted user: {:?}", user);
39
40 Ok(())
41}More examples
24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Get wallet ID from environment and initialize client
32 let wallet_id =
33 std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
34 let client = PrivyClient::new_from_env()?;
35
36 tracing::info!(
37 "initialized privy client from environment, wallet_id: {}",
38 wallet_id
39 );
40
41 let wallet = client.wallets().get(&wallet_id).await?;
42
43 tracing::info!("got wallet: {:?}", wallet);
44
45 Ok(())
46}24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Get transaction ID from environment and initialize client
32 let transaction_id = std::env::var("PRIVY_TRANSACTION_ID")
33 .expect("PRIVY_TRANSACTION_ID environment variable not set");
34 let client = PrivyClient::new_from_env()?;
35
36 tracing::info!(
37 "initialized privy client from environment, transaction_id: {}",
38 transaction_id
39 );
40
41 let transaction = client.transactions().get(&transaction_id).await?;
42
43 tracing::info!("got transaction: {:?}", transaction);
44
45 Ok(())
46}25async fn main() -> Result<()> {
26 tracing_subscriber::fmt()
27 .with_env_filter(
28 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
29 )
30 .init();
31
32 // Get JWT from environment and initialize client
33 let user_jwt =
34 std::env::var("PRIVY_USER_JWT").expect("PRIVY_USER_JWT environment variable not set");
35 let client = PrivyClient::new_from_env()?;
36
37 tracing::info!("initialized privy client from environment");
38
39 let jwt_auth = client
40 .wallets()
41 .authenticate_with_jwt(&AuthenticateBody {
42 user_jwt,
43 encryption_type: None,
44 recipient_public_key: None,
45 })
46 .await?;
47
48 tracing::info!("got jwt auth: {:?}", jwt_auth);
49
50 Ok(())
51}27async fn main() -> Result<()> {
28 tracing_subscriber::fmt()
29 .with_env_filter(
30 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
31 )
32 .init();
33
34 // Initialize client from environment variables
35 let client = PrivyClient::new_from_env()?;
36
37 tracing::info!("initialized privy client from environment");
38
39 let wallet = client
40 .wallets()
41 .create(
42 None,
43 &CreateWalletBody {
44 chain_type: WalletChainType::Solana,
45 additional_signers: None,
46 owner: None,
47 owner_id: None,
48 policy_ids: vec![],
49 },
50 )
51 .await?;
52
53 tracing::info!("got new wallet: {:?}", wallet);
54
55 Ok(())
56}25async fn main() -> Result<()> {
26 tracing_subscriber::fmt()
27 .with_env_filter(
28 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
29 )
30 .init();
31
32 // Get additional environment variables and initialize client
33 let wallet_id =
34 std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
35 let public_key =
36 std::env::var("PRIVY_PUBLIC_KEY").expect("PRIVY_PUBLIC_KEY environment variable not set");
37 let client = PrivyClient::new_from_env()?;
38
39 tracing::info!(
40 "initialized privy client from environment, wallet_id: {}, public_key: {}",
41 wallet_id,
42 public_key
43 );
44
45 let wallets = client.wallets().list(None, None, Some(5.0), None).await?;
46
47 tracing::info!("got wallets: {:?}", wallets);
48
49 Ok(())
50}Sourcepub fn new_with_options(
app_id: String,
app_secret: String,
options: PrivyClientOptions,
) -> Result<Self, PrivyCreateError>
pub fn new_with_options( app_id: String, app_secret: String, options: PrivyClientOptions, ) -> Result<Self, PrivyCreateError>
Create a new PrivyClient with a custom url
§Errors
This can fail for two reasons, either the app_id or app_secret are not
valid headers, or that the underlying http client could not be created.
Source§impl PrivyClient
impl PrivyClient
Sourcepub fn wallets(&self) -> WalletsClient
pub fn wallets(&self) -> WalletsClient
Access the wallets client
Examples found in repository?
24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Get wallet ID from environment and initialize client
32 let wallet_id =
33 std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
34 let client = PrivyClient::new_from_env()?;
35
36 tracing::info!(
37 "initialized privy client from environment, wallet_id: {}",
38 wallet_id
39 );
40
41 let wallet = client.wallets().get(&wallet_id).await?;
42
43 tracing::info!("got wallet: {:?}", wallet);
44
45 Ok(())
46}More examples
25async fn main() -> Result<()> {
26 tracing_subscriber::fmt()
27 .with_env_filter(
28 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
29 )
30 .init();
31
32 // Get JWT from environment and initialize client
33 let user_jwt =
34 std::env::var("PRIVY_USER_JWT").expect("PRIVY_USER_JWT environment variable not set");
35 let client = PrivyClient::new_from_env()?;
36
37 tracing::info!("initialized privy client from environment");
38
39 let jwt_auth = client
40 .wallets()
41 .authenticate_with_jwt(&AuthenticateBody {
42 user_jwt,
43 encryption_type: None,
44 recipient_public_key: None,
45 })
46 .await?;
47
48 tracing::info!("got jwt auth: {:?}", jwt_auth);
49
50 Ok(())
51}27async fn main() -> Result<()> {
28 tracing_subscriber::fmt()
29 .with_env_filter(
30 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
31 )
32 .init();
33
34 // Initialize client from environment variables
35 let client = PrivyClient::new_from_env()?;
36
37 tracing::info!("initialized privy client from environment");
38
39 let wallet = client
40 .wallets()
41 .create(
42 None,
43 &CreateWalletBody {
44 chain_type: WalletChainType::Solana,
45 additional_signers: None,
46 owner: None,
47 owner_id: None,
48 policy_ids: vec![],
49 },
50 )
51 .await?;
52
53 tracing::info!("got new wallet: {:?}", wallet);
54
55 Ok(())
56}25async fn main() -> Result<()> {
26 tracing_subscriber::fmt()
27 .with_env_filter(
28 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
29 )
30 .init();
31
32 // Get additional environment variables and initialize client
33 let wallet_id =
34 std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
35 let public_key =
36 std::env::var("PRIVY_PUBLIC_KEY").expect("PRIVY_PUBLIC_KEY environment variable not set");
37 let client = PrivyClient::new_from_env()?;
38
39 tracing::info!(
40 "initialized privy client from environment, wallet_id: {}, public_key: {}",
41 wallet_id,
42 public_key
43 );
44
45 let wallets = client.wallets().list(None, None, Some(5.0), None).await?;
46
47 tracing::info!("got wallets: {:?}", wallets);
48
49 Ok(())
50}30async fn main() -> Result<()> {
31 tracing_subscriber::fmt()
32 .with_env_filter(
33 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
34 )
35 .init();
36
37 // Get wallet ID from environment and initialize client
38 let wallet_id =
39 std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
40 let client = PrivyClient::new_from_env()?;
41
42 tracing::info!(
43 "initialized privy client from environment, wallet_id: {}",
44 wallet_id
45 );
46
47 // Get SOL balance on Solana
48 let balance = client
49 .wallets()
50 .balance()
51 .get(
52 &wallet_id,
53 &GetWalletBalanceAsset::String(GetWalletBalanceAssetString::Sol),
54 &GetWalletBalanceChain::String(GetWalletBalanceChainString::Solana),
55 Some(GetWalletBalanceIncludeCurrency::Usd),
56 )
57 .await?;
58
59 tracing::info!("got wallet balance: {:?}", balance);
60
61 Ok(())
62}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 and initialize client
37 let wallet_id =
38 std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
39 let client = PrivyClient::new_from_env()?;
40
41 tracing::info!(
42 "initialized privy client from environment, wallet_id: {}",
43 wallet_id
44 );
45
46 // Get SOL transactions on Solana mainnet
47 let transactions = client
48 .wallets()
49 .transactions()
50 .get(
51 &wallet_id,
52 &WalletTransactionsAsset::String(WalletTransactionsAssetString::Sol),
53 WalletTransactionsChain::Base,
54 None, // No cursor for first page
55 Some(10.0), // Limit to 10 transactions,
56 None,
57 )
58 .await?;
59
60 tracing::info!("got wallet transactions: {:?}", transactions);
61
62 Ok(())
63}Sourcepub fn users(&self) -> UsersClient
pub fn users(&self) -> UsersClient
Access the users client
Examples found in repository?
24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Initialize client from environment variables
32 let client = PrivyClient::new_from_env()?;
33
34 tracing::info!("initialized privy client from environment");
35
36 let user = client.users().delete("cmf56qacr01qpl90brxql83lx").await?;
37
38 tracing::info!("deleted user: {:?}", user);
39
40 Ok(())
41}More examples
24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Get search term from environment and initialize client
32 let search_term =
33 std::env::var("PRIVY_SEARCH_TERM").unwrap_or_else(|_| "alex@arlyon.dev".to_string());
34 let client = PrivyClient::new_from_env()?;
35
36 tracing::info!(
37 "initialized privy client from environment, search_term: {}",
38 search_term
39 );
40
41 // Search for users by email address or other criteria
42 let search_result = client
43 .users()
44 // TODO: search term rename is not working
45 .search(&SearchUsersBody::Variant0 { search_term })
46 .await?;
47
48 tracing::info!("search result: {:?}", search_result);
49
50 Ok(())
51}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 // Initialize client from environment variables
37 let client = PrivyClient::new_from_env()?;
38
39 tracing::info!("initialized privy client from environment");
40
41 let user = client
42 .users()
43 .create(&CreateUserBody {
44 linked_accounts: vec![
45 LinkedAccountInput::EmailInput(LinkedAccountEmailInput {
46 address: "alex@arlyon.dev".into(),
47 type_: LinkedAccountEmailInputType::Email,
48 }),
49 LinkedAccountInput::CustomJwtInput(LinkedAccountCustomJwtInput {
50 custom_user_id: "alex@arlyon.dev".try_into().unwrap(),
51 type_: LinkedAccountCustomJwtInputType::CustomAuth,
52 }),
53 ],
54 custom_metadata: None,
55 wallets: vec![],
56 })
57 .await?;
58
59 tracing::info!("got new user: {:?}", user);
60
61 Ok(())
62}Sourcepub fn policies(&self) -> PoliciesClient
pub fn policies(&self) -> PoliciesClient
Access the policies client
Sourcepub fn transactions(&self) -> TransactionsClient
pub fn transactions(&self) -> TransactionsClient
Access the transactions client
Examples found in repository?
24async fn main() -> Result<()> {
25 tracing_subscriber::fmt()
26 .with_env_filter(
27 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
28 )
29 .init();
30
31 // Get transaction ID from environment and initialize client
32 let transaction_id = std::env::var("PRIVY_TRANSACTION_ID")
33 .expect("PRIVY_TRANSACTION_ID environment variable not set");
34 let client = PrivyClient::new_from_env()?;
35
36 tracing::info!(
37 "initialized privy client from environment, transaction_id: {}",
38 transaction_id
39 );
40
41 let transaction = client.transactions().get(&transaction_id).await?;
42
43 tracing::info!("got transaction: {:?}", transaction);
44
45 Ok(())
46}Sourcepub fn key_quorums(&self) -> KeyQuorumsClient
pub fn key_quorums(&self) -> KeyQuorumsClient
Access the key_quorums client
Examples found in repository?
35async fn main() -> Result<()> {
36 tracing_subscriber::fmt()
37 .with_env_filter(
38 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
39 )
40 .init();
41
42 // Initialize client from environment variables
43 let client = PrivyClient::new_from_env()?;
44
45 tracing::info!("initialized privy client from environment");
46
47 // Generate unique identifiers for this example run
48 let timestamp = std::time::SystemTime::now()
49 .duration_since(std::time::SystemTime::UNIX_EPOCH)?
50 .as_secs();
51 let wallet_idempotency_key = format!("quorum-wallet-{}", Uuid::new_v4());
52
53 tracing::info!("Starting quorum wallet example");
54
55 // Step 1: Generate three P-256 private keys for quorum
56 tracing::info!("Generating three P-256 private keys for 2-of-3 quorum");
57 let mut rng = rand::thread_rng();
58 let key1 = SecretKey::<p256::NistP256>::random(&mut rng);
59 let key2 = SecretKey::<p256::NistP256>::random(&mut rng);
60 let key3 = SecretKey::<p256::NistP256>::random(&mut rng);
61
62 let pubkey1 = key1.public_key().to_string();
63 let pubkey2 = key2.public_key().to_string();
64 let pubkey3 = key3.public_key().to_string();
65
66 tracing::info!("Generated public keys:");
67 tracing::info!("Key 1: {}", pubkey1);
68 tracing::info!("Key 2: {}", pubkey2);
69 tracing::info!("Key 3: {}", pubkey3);
70
71 // Step 2: Create a 2-of-3 key quorum
72 tracing::info!("Creating 2-of-3 key quorum");
73
74 let quorum_display_name =
75 CreateKeyQuorumBodyDisplayName::try_from(format!("Quorum Example {timestamp}").as_str())?;
76
77 let quorum_body = CreateKeyQuorumBody {
78 authorization_threshold: Some(2.0), // 2-of-3 threshold
79 display_name: Some(quorum_display_name),
80 public_keys: vec![pubkey1, pubkey2, pubkey3],
81 user_ids: vec![],
82 };
83
84 let key_quorum = client.key_quorums().create(&quorum_body).await?;
85
86 tracing::info!("Created key quorum with ID: {}", key_quorum.id);
87 tracing::info!("Quorum threshold: {:?}", key_quorum.authorization_threshold);
88
89 // Step 3: Create a new Ethereum wallet owned by the quorum
90 tracing::info!(
91 "Creating new Ethereum wallet owned by quorum with idempotency key: {}",
92 wallet_idempotency_key
93 );
94
95 let create_body = CreateWalletBody {
96 chain_type: WalletChainType::Ethereum,
97 additional_signers: None,
98 owner: None,
99 owner_id: Some(key_quorum.id.parse().unwrap()),
100 policy_ids: vec![],
101 };
102
103 let wallet = client
104 .wallets()
105 .create(Some(&wallet_idempotency_key), &create_body)
106 .await?;
107
108 tracing::info!("Created wallet with ID: {}", wallet.id);
109 tracing::info!("Wallet address: {}", wallet.address);
110 tracing::info!("Wallet owner ID: {:?}", wallet.owner_id);
111
112 // Step 4: Test signing with only one key (should fail)
113 tracing::info!("Testing wallet export with only one key (should fail due to quorum threshold)");
114
115 let single_key_ctx = AuthorizationContext::new().push(PrivateKey(
116 key1.to_sec1_pem(der::pem::LineEnding::LF)
117 .unwrap()
118 .as_str()
119 .to_owned(),
120 ));
121
122 let single_key_result = client.wallets().export(&wallet.id, &single_key_ctx).await;
123
124 match single_key_result {
125 Err(err) => {
126 tracing::info!(
127 "✓ Single key authorization correctly failed as expected: {:?}",
128 err
129 );
130 }
131 Ok(_) => {
132 tracing::error!("✗ Single key authorization should have failed but succeeded!");
133 return Err(anyhow::anyhow!(
134 "Single key authorization should have failed due to quorum threshold"
135 ));
136 }
137 }
138
139 // Step 5: Test signing with two keys (should succeed)
140 tracing::info!("Testing wallet export with two keys (should succeed)");
141
142 let two_key_ctx = single_key_ctx.push(PrivateKey(
143 key2.to_sec1_pem(der::pem::LineEnding::LF)
144 .unwrap()
145 .as_str()
146 .to_owned(),
147 ));
148
149 let two_key_result = client.wallets().export(&wallet.id, &two_key_ctx).await;
150
151 match two_key_result {
152 Ok(export_result) => {
153 tracing::info!("✓ Two key authorization succeeded as expected");
154 tracing::info!("Exported private key length: {} bytes", export_result.len());
155 }
156 Err(err) => {
157 tracing::error!("✗ Two key authorization failed unexpectedly: {:?}", err);
158 return Err(anyhow::anyhow!(
159 "Two key authorization should have succeeded"
160 ));
161 }
162 }
163
164 Ok(())
165}Sourcepub fn fiat(&self) -> FiatClient
pub fn fiat(&self) -> FiatClient
Access the fiat client
Trait Implementations§
Source§impl Clone for PrivyClient
impl Clone for PrivyClient
Source§fn clone(&self) -> PrivyClient
fn clone(&self) -> PrivyClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more