foundry_wallets/wallet_browser/
types.rs

1use alloy_dyn_abi::TypedData;
2use alloy_primitives::{Address, Bytes, ChainId, TxHash};
3use alloy_rpc_types::TransactionRequest;
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6
7/// Response format for API endpoints.
8/// - `Ok(T)` serializes as: {"status":"ok","data": ...}
9/// - `Ok(())` serializes as: {"status":"ok"}  (no data key)
10/// - `Error { message }` as: {"status":"error","message":"..."}
11#[derive(Serialize, Deserialize, Debug)]
12#[serde(tag = "status", content = "data", rename_all = "lowercase")]
13pub(crate) enum BrowserApiResponse<T = ()> {
14    Ok(T),
15    Error { message: String },
16}
17
18impl BrowserApiResponse<()> {
19    /// Create a successful response with no data.
20    pub fn ok() -> Self {
21        Self::Ok(())
22    }
23}
24
25impl<T> BrowserApiResponse<T> {
26    /// Create a successful response with the given data.
27    pub fn with_data(data: T) -> Self {
28        Self::Ok(data)
29    }
30
31    /// Create an error response with the given message.
32    pub fn error(msg: impl Into<String>) -> Self {
33        Self::Error { message: msg.into() }
34    }
35}
36
37/// Represents a transaction request sent to the browser wallet.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(deny_unknown_fields)]
40pub struct BrowserTransactionRequest {
41    /// The unique identifier for the transaction.
42    pub id: Uuid,
43    /// The transaction request details.
44    pub request: TransactionRequest,
45}
46
47/// Represents a transaction response sent from the browser wallet.
48#[derive(Debug, Clone, Serialize, Deserialize)]
49#[serde(deny_unknown_fields)]
50pub(crate) struct BrowserTransactionResponse {
51    /// The unique identifier for the transaction, must match the request ID sent earlier.
52    pub id: Uuid,
53    /// The transaction hash if the transaction was successful.
54    pub hash: Option<TxHash>,
55    /// The error message if the transaction failed.
56    pub error: Option<String>,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60#[serde(deny_unknown_fields)]
61pub enum SignType {
62    /// Standard personal sign: `eth_sign` / `personal_sign`
63    PersonalSign,
64    /// EIP-712 typed data sign: `eth_signTypedData_v4`
65    SignTypedDataV4,
66}
67
68#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
69#[serde(deny_unknown_fields)]
70pub struct SignRequest {
71    /// The message to be signed.
72    pub message: String,
73    /// The address that should sign the message.
74    pub address: Address,
75}
76
77/// Represents a signing request sent to the browser wallet.
78#[derive(Debug, Clone, Serialize, Deserialize)]
79#[serde(deny_unknown_fields, rename_all = "camelCase")]
80pub struct BrowserSignRequest {
81    /// The unique identifier for the signing request.
82    pub id: Uuid,
83    /// The type of signing operation.
84    pub sign_type: SignType,
85    /// The sign request details.
86    pub request: SignRequest,
87}
88
89/// Represents a typed data signing request sent to the browser wallet.
90#[derive(Debug, Clone, Serialize, Deserialize)]
91#[serde(deny_unknown_fields, rename_all = "camelCase")]
92pub struct BrowserSignTypedDataRequest {
93    /// The unique identifier for the signing request.
94    pub id: Uuid,
95    /// The address that should sign the typed data.
96    pub address: Address,
97    /// The typed data to be signed.
98    pub typed_data: TypedData,
99}
100
101/// Represents a signing response sent from the browser wallet.
102#[derive(Debug, Clone, Serialize, Deserialize)]
103#[serde(deny_unknown_fields)]
104pub(crate) struct BrowserSignResponse {
105    /// The unique identifier for the signing request, must match the request ID sent earlier.
106    pub id: Uuid,
107    /// The signature if the signing was successful.
108    pub signature: Option<Bytes>,
109    /// The error message if the signing failed.
110    pub error: Option<String>,
111}
112
113/// Represents an active connection to a browser wallet.
114#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
115pub struct Connection {
116    /// The address of the connected wallet.
117    pub address: Address,
118    /// The chain ID of the connected wallet.
119    pub chain_id: ChainId,
120}
121
122impl Connection {
123    /// Create a new connection instance.
124    pub fn new(address: Address, chain_id: ChainId) -> Self {
125        Self { address, chain_id }
126    }
127}