Skip to main content

walletkit_core/
requests.rs

1//! Proof requests and responses in World ID v4.
2
3use world_id_core::requests::{
4    ProofRequest as CoreProofRequest, ProofResponse as CoreProofResponse,
5};
6
7use crate::error::WalletKitError;
8
9/// A request from the RP to the Authenticator. See [`CoreProofRequest`] for more details.
10/// This is a wrapper type to expose to foreign language bindings.
11#[derive(Debug, Clone, uniffi::Object)]
12pub struct ProofRequest(pub(crate) CoreProofRequest);
13
14#[uniffi::export]
15impl ProofRequest {
16    /// Deserializes a `ProofRequest` from a JSON string.
17    ///
18    /// # Errors
19    /// Returns an error if the JSON is invalid or cannot be parsed.
20    #[uniffi::constructor]
21    pub fn from_json(json: &str) -> Result<Self, WalletKitError> {
22        let core_request: CoreProofRequest =
23            serde_json::from_str(json).map_err(|e| WalletKitError::Generic {
24                error: format!("invalid proof request json: {e}"),
25            })?;
26        Ok(Self(core_request))
27    }
28
29    /// Serializes the proof request to a JSON string.
30    ///
31    /// # Errors
32    /// Returns an error if serialization fails.
33    pub fn to_json(&self) -> Result<String, WalletKitError> {
34        serde_json::to_string(&self.0).map_err(|e| WalletKitError::Generic {
35            error: format!("critical unexpected error serializing to json: {e}"),
36        })
37    }
38
39    /// Returns the unique identifier for this request.
40    #[must_use]
41    pub fn id(&self) -> String {
42        self.0.id.clone()
43    }
44
45    /// Returns the request format version as a `u8`.
46    #[must_use]
47    pub const fn version(&self) -> u8 {
48        self.0.version as u8
49    }
50}
51
52/// A response from the Authenticator to the RP. See [`CoreProofResponse`] for more details.
53///
54/// This is a wrapper type to expose to foreign language bindings.
55#[derive(Debug, Clone, uniffi::Object)]
56pub struct ProofResponse(pub CoreProofResponse);
57
58#[uniffi::export]
59impl ProofResponse {
60    /// Serializes the proof response to a JSON string.
61    ///
62    /// # Errors
63    /// Returns an error if serialization fails.
64    pub fn to_json(&self) -> Result<String, WalletKitError> {
65        serde_json::to_string(&self.0).map_err(|e| WalletKitError::Generic {
66            error: format!("critical unexpected error serializing to json: {e}"),
67        })
68    }
69
70    /// Returns the unique identifier for this response.
71    #[must_use]
72    pub fn id(&self) -> String {
73        self.0.id.clone()
74    }
75
76    /// Returns the response format version as a `u8`.
77    #[must_use]
78    pub const fn version(&self) -> u8 {
79        self.0.version as u8
80    }
81
82    /// Returns the top-level error message, if the entire proof request failed.
83    #[must_use]
84    pub fn error(&self) -> Option<String> {
85        self.0.error.clone()
86    }
87}
88
89impl ProofResponse {
90    /// Consumes the wrapper and returns the inner `CoreProofResponse`.
91    #[must_use]
92    pub fn into_inner(self) -> CoreProofResponse {
93        self.0
94    }
95}
96
97impl From<CoreProofRequest> for ProofRequest {
98    fn from(core_request: CoreProofRequest) -> Self {
99        Self(core_request)
100    }
101}
102
103impl From<CoreProofResponse> for ProofResponse {
104    fn from(core_response: CoreProofResponse) -> Self {
105        Self(core_response)
106    }
107}