1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Ethereum wallet operations service.
//!
//! This module provides convenient methods for Ethereum wallet operations including
//! message signing, transaction signing, typed data signing, and more. All methods
//! are designed to work with Privy's embedded wallet infrastructure.
use crate::{
AuthorizationContext, PrivySignedApiError,
generated::{
Error, ResponseValue,
types::{
EthereumPersonalSignRpcInput, EthereumPersonalSignRpcInputMethod,
EthereumPersonalSignRpcInputParams, EthereumPersonalSignRpcInputParamsEncoding,
EthereumSecp256k1SignRpcInput, EthereumSecp256k1SignRpcInputMethod,
EthereumSecp256k1SignRpcInputParams, EthereumSendTransactionRpcInput,
EthereumSendTransactionRpcInputMethod, EthereumSendTransactionRpcInputParams,
EthereumSendTransactionRpcInputParamsTransaction,
EthereumSign7702AuthorizationRpcInput, EthereumSign7702AuthorizationRpcInputMethod,
EthereumSign7702AuthorizationRpcInputParams, EthereumSignTransactionRpcInput,
EthereumSignTransactionRpcInputMethod, EthereumSignTransactionRpcInputParams,
EthereumSignTransactionRpcInputParamsTransaction, EthereumSignTypedDataRpcInput,
EthereumSignTypedDataRpcInputMethod, EthereumSignTypedDataRpcInputParams,
EthereumSignTypedDataRpcInputParamsTypedData, WalletRpcBody, WalletRpcResponse,
},
},
};
/// Service for Ethereum-specific wallet operations.
///
/// Provides convenient methods for common Ethereum wallet operations such as:
/// - Personal message signing (UTF-8 strings and raw bytes)
/// - secp256k1 signature generation
/// - EIP-712 typed data signing
/// - Transaction signing and broadcasting
/// - EIP-7702 authorization signing
///
/// # Examples
///
/// Basic usage:
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// // Sign a UTF-8 message
/// let result = ethereum_service
/// .sign_message(
/// "wallet_id",
/// "Hello, Ethereum!",
/// &auth_ctx,
/// None, // no idempotency key
/// )
/// .await?;
/// # Ok(())
/// # }
/// ```
pub struct EthereumService {
wallets_client: crate::subclients::WalletsClient,
}
impl EthereumService {
/// Creates a new [`EthereumService`] instance.
///
/// This is typically called internally by `WalletsClient::ethereum()`.
pub(crate) fn new(wallets_client: crate::subclients::WalletsClient) -> Self {
Self { wallets_client }
}
/// Signs a UTF-8 encoded message for an Ethereum wallet using the `personal_sign` method.
///
/// This method signs arbitrary UTF-8 text messages using Ethereum's personal message
/// signing standard. The message will be prefixed with the Ethereum signed message
/// prefix before signing.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet to use for signing
/// * `message` - The UTF-8 message string to be signed
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request to prevent duplicate operations
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the signature data.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// let signature = ethereum_service
/// .sign_message(
/// "clz2rqy4500061234abcd1234",
/// "Hello, Ethereum!",
/// &auth_ctx,
/// Some("unique-request-id-123"),
/// )
/// .await?;
///
/// println!("Message signed successfully");
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// This method will return an error if:
/// - The wallet ID is invalid or not found
/// - The authorization context is invalid
/// - Network communication fails
/// - The signing operation fails on the server
pub async fn sign_message(
&self,
wallet_id: &str,
message: &str,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body = WalletRpcBody::EthereumPersonalSignRpcInput(EthereumPersonalSignRpcInput {
address: None,
chain_type: None,
method: EthereumPersonalSignRpcInputMethod::PersonalSign,
params: EthereumPersonalSignRpcInputParams {
encoding: EthereumPersonalSignRpcInputParamsEncoding::Utf8,
message: message.to_string(),
},
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Signs a raw byte array message for an Ethereum wallet using the `personal_sign` method.
///
/// This method signs raw binary data by first encoding it as a hex string (with 0x prefix)
/// and then using Ethereum's personal message signing standard.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet to use for signing
/// * `message` - The message byte array to be signed
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the signature data.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// let message_bytes = b"Hello, bytes!";
/// let signature = ethereum_service
/// .sign_message_bytes("clz2rqy4500061234abcd1234", message_bytes, &auth_ctx, None)
/// .await?;
///
/// println!("Byte message signed successfully");
/// # Ok(())
/// # }
/// ```
pub async fn sign_message_bytes(
&self,
wallet_id: &str,
message: &[u8],
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let hex_message = format!("0x{}", hex::encode(message));
let rpc_body = WalletRpcBody::EthereumPersonalSignRpcInput(EthereumPersonalSignRpcInput {
address: None,
chain_type: None,
method: EthereumPersonalSignRpcInputMethod::PersonalSign,
params: EthereumPersonalSignRpcInputParams {
encoding: EthereumPersonalSignRpcInputParamsEncoding::Hex,
message: hex_message,
},
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Signs a message using secp256k1 signature algorithm.
///
/// This method performs low-level secp256k1 signing on a pre-computed hash.
/// The hash should be exactly 32 bytes and is typically the result of keccak256
/// hashing of the data to be signed.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet to use for signing
/// * `hash` - The hash to sign (typically 32 bytes as hex string with 0x prefix)
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the secp256k1 signature.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// // Pre-computed keccak256 hash
/// let hash = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
/// let signature = ethereum_service
/// .sign_secp256k1("clz2rqy4500061234abcd1234", hash, &auth_ctx, None)
/// .await?;
///
/// println!("Hash signed with secp256k1");
/// # Ok(())
/// # }
/// ```
///
/// # Notes
///
/// This is a lower-level signing method. For most use cases, prefer `sign_message()`
/// or `sign_typed_data()` which handle the hashing automatically.
pub async fn sign_secp256k1(
&self,
wallet_id: &str,
hash: &str,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body =
WalletRpcBody::EthereumSecp256k1SignRpcInput(EthereumSecp256k1SignRpcInput {
address: None,
chain_type: None,
method: EthereumSecp256k1SignRpcInputMethod::Secp256k1Sign,
params: EthereumSecp256k1SignRpcInputParams {
hash: hash.to_string(),
},
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Signs a 7702 authorization using the eth_sign7702Authorization RPC method.
///
/// EIP-7702 introduces account abstraction by allowing EOAs to temporarily delegate
/// control to a smart contract. This method signs the authorization that allows
/// the delegation to take place.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet to use for signing
/// * `params` - The parameters for the eth_sign7702Authorization RPC method including contract address, chain ID, and nonce
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the signed authorization data.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// let params = EthereumSign7702AuthorizationRpcInputParams {
/// chain_id: EthereumSign7702AuthorizationRpcInputParamsChainId::Integer(1),
/// contract: "0x1234567890abcdef1234567890abcdef12345678".to_string(),
/// nonce: None,
/// };
///
/// let authorization = ethereum_service
/// .sign_7702_authorization("clz2rqy4500061234abcd1234", params, &auth_ctx, None)
/// .await?;
///
/// println!("7702 authorization signed successfully");
/// # Ok(())
/// # }
/// ```
pub async fn sign_7702_authorization(
&self,
wallet_id: &str,
params: EthereumSign7702AuthorizationRpcInputParams,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body = WalletRpcBody::EthereumSign7702AuthorizationRpcInput(
EthereumSign7702AuthorizationRpcInput {
address: None,
chain_type: None,
method: EthereumSign7702AuthorizationRpcInputMethod::EthSign7702Authorization,
params,
},
);
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Signs typed data using EIP-712 standard.
///
/// EIP-712 defines a standard for typed structured data signing that provides
/// better UX and security compared to signing arbitrary strings. This method
/// implements the `eth_signTypedData_v4` RPC method.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet to use for signing
/// * `typed_data` - The typed data structure to be signed, conforming to EIP-712 format
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the signed typed data.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// // Create EIP-712 typed data structure
/// let typed_data = EthereumSignTypedDataRpcInputParamsTypedData {
/// domain: Default::default(),
/// message: Default::default(),
/// primary_type: "Mail".to_string(),
/// types: Default::default(),
/// };
///
/// let signature = ethereum_service
/// .sign_typed_data("clz2rqy4500061234abcd1234", typed_data, &auth_ctx, None)
/// .await?;
///
/// println!("Typed data signed successfully");
/// # Ok(())
/// # }
/// ```
///
/// # Notes
///
/// The typed data must conform to the EIP-712 specification with proper domain,
/// types, primaryType, and message fields. Refer to EIP-712 for the complete
/// specification of the required structure.
pub async fn sign_typed_data(
&self,
wallet_id: &str,
typed_data: EthereumSignTypedDataRpcInputParamsTypedData,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body =
WalletRpcBody::EthereumSignTypedDataRpcInput(EthereumSignTypedDataRpcInput {
address: None,
chain_type: None,
method: EthereumSignTypedDataRpcInputMethod::EthSignTypedDataV4,
params: EthereumSignTypedDataRpcInputParams { typed_data },
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Signs a transaction using the eth_signTransaction method.
///
/// This method signs an Ethereum transaction but does not broadcast it to the network.
/// The signed transaction can be broadcast later using other tools or the `send_transaction` method.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet to use for signing
/// * `transaction` - The transaction object to be signed including to, value, data, gas, etc.
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the signed transaction data.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// let transaction = EthereumSignTransactionRpcInputParamsTransaction {
/// to: Some("0x742d35Cc6635C0532925a3b8c17d6d1E9C2F7ca".to_string()),
/// value: None,
/// gas_limit: None,
/// gas_price: None,
/// nonce: None,
/// chain_id: None,
/// data: None,
/// from: None,
/// max_fee_per_gas: None,
/// max_priority_fee_per_gas: None,
/// type_: None,
/// };
///
/// let signed_tx = ethereum_service
/// .sign_transaction("clz2rqy4500061234abcd1234", transaction, &auth_ctx, None)
/// .await?;
///
/// println!("Transaction signed successfully");
/// # Ok(())
/// # }
/// ```
pub async fn sign_transaction(
&self,
wallet_id: &str,
transaction: EthereumSignTransactionRpcInputParamsTransaction,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body =
WalletRpcBody::EthereumSignTransactionRpcInput(EthereumSignTransactionRpcInput {
address: None,
chain_type: None,
method: EthereumSignTransactionRpcInputMethod::EthSignTransaction,
params: EthereumSignTransactionRpcInputParams { transaction },
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Signs and sends a transaction using the eth_sendTransaction method.
///
/// This method both signs and broadcasts an Ethereum transaction to the specified network.
/// It's a convenience method that combines signing and sending in one operation.
///
/// # Parameters
///
/// * `wallet_id` - The ID of the wallet used for the transaction
/// * `caip2` - The CAIP-2 chain ID of the Ethereum network (e.g., "eip155:1" for Ethereum Mainnet, "eip155:11155111" for Sepolia)
/// * `transaction` - The transaction object to be sent
/// * `authorization_context` - The authorization context containing JWT or private keys for request signing
/// * `idempotency_key` - Optional idempotency key for the request
///
/// # Returns
///
/// Returns a `ResponseValue<WalletRpcResponse>` containing the transaction hash or other relevant data.
///
/// # Examples
///
/// ```rust,no_run
/// # use anyhow::Result;
/// # async fn example() -> Result<()> {
/// use privy_rs::{AuthorizationContext, generated::types::*};
/// # use privy_rs::PrivyClient;
///
/// # let client = PrivyClient::new("app_id".to_string(), "app_secret".to_string())?;
/// let ethereum_service = client.wallets().ethereum();
/// let auth_ctx = AuthorizationContext::new();
///
/// let transaction = EthereumSendTransactionRpcInputParamsTransaction {
/// to: Some("0x742d35Cc6635C0532925a3b8c17d6d1E9C2F7ca".to_string()),
/// value: None,
/// gas_limit: None,
/// max_fee_per_gas: None,
/// max_priority_fee_per_gas: None,
/// data: Some("0x".to_string()),
/// chain_id: None,
/// from: None,
/// gas_price: None,
/// nonce: None,
/// type_: None,
/// };
///
/// let result = ethereum_service
/// .send_transaction(
/// "clz2rqy4500061234abcd1234",
/// "eip155:1",
/// transaction,
/// &auth_ctx,
/// None,
/// )
/// .await?;
///
/// println!("Transaction sent successfully");
/// # Ok(())
/// # }
/// ```
///
/// # Notes
///
/// - The transaction will be broadcast to the network specified by the CAIP-2 chain ID
/// - This method requires sufficient balance in the wallet to cover gas costs and transfer value
/// - The transaction will be mined and included in a block if successful
/// - Common CAIP-2 chain IDs: "eip155:1" (Ethereum), "eip155:137" (Polygon), "eip155:11155111" (Sepolia testnet)
pub async fn send_transaction(
&self,
wallet_id: &str,
caip2: &str,
transaction: EthereumSendTransactionRpcInputParamsTransaction,
authorization_context: &AuthorizationContext,
idempotency_key: Option<&str>,
) -> Result<ResponseValue<WalletRpcResponse>, PrivySignedApiError> {
let rpc_body =
WalletRpcBody::EthereumSendTransactionRpcInput(EthereumSendTransactionRpcInput {
address: None,
caip2: caip2
.parse()
.map_err(|_| Error::InvalidRequest("Invalid CAIP-2 format".to_string()))?,
chain_type: None,
method: EthereumSendTransactionRpcInputMethod::EthSendTransaction,
params: EthereumSendTransactionRpcInputParams { transaction },
sponsor: Some(false),
});
self.wallets_client
.rpc(wallet_id, authorization_context, idempotency_key, &rpc_body)
.await
}
/// Create an Alloy-compatible signer for this wallet
///
/// This returns a `PrivyAlloyWallet` that implements Alloy's signer traits,
/// allowing it to be used with any Alloy-compatible library.
///
/// # Feature Flag
/// Requires the `alloy` feature to be enabled.
///
/// # Example
/// ```no_run
/// use privy_rs::{PrivyClient, AuthorizationContext, PrivateKey};
/// use alloy_signer::SignerSync;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = PrivyClient::new_from_env()?;
/// let private_key = std::fs::read_to_string("private_key.pem")?;
/// let ctx = AuthorizationContext::new().push(PrivateKey(private_key));
///
/// let signer = client.wallets().ethereum().alloy("wallet_id", &ctx).await?;
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "alloy")]
pub async fn alloy(
&self,
wallet_id: &str,
authorization_context: &AuthorizationContext,
) -> Result<crate::alloy::PrivyAlloyWallet, crate::PrivyApiError> {
let wallet_response = self.wallets_client.get(wallet_id).await?;
let wallet = wallet_response.into_inner();
let address = wallet.address.parse().map_err(|e| {
crate::PrivyApiError::InvalidRequest(format!("Failed to parse wallet address: {e}"))
})?;
Ok(crate::alloy::PrivyAlloyWallet::new(
wallet_id.to_string(),
address,
self.wallets_client.clone(),
authorization_context.clone(),
))
}
}