Skip to main content

walletkit_core/
proof.rs

1use base64::{prelude::BASE64_URL_SAFE_NO_PAD, Engine};
2use world_id_core::primitives::OwnershipProof as CoreOwnershipProof;
3
4use crate::{error::WalletKitError, FieldElement};
5
6/// A WIP-103 Ownership Proof available to foreign bindings
7#[derive(Debug, Clone, uniffi::Object)]
8pub struct OwnershipProof(pub(crate) CoreOwnershipProof);
9
10#[uniffi::export]
11impl OwnershipProof {
12    /// Encodes the proof as raw bytes.
13    ///
14    /// # Errors
15    /// An encoding error is theoretically possible, should not happen in practice.
16    pub fn encode(&self) -> Result<Vec<u8>, WalletKitError> {
17        let mut buffer = Vec::new();
18        ciborium::into_writer(&self.0, &mut buffer).map_err(|_| {
19            WalletKitError::SerializationError {
20                error: "unexpected error serializing `OwnershipProof`".to_string(),
21            }
22        })?;
23        Ok(buffer)
24    }
25
26    /// Encodes the proof as base-64 encoded bytes.
27    ///
28    /// # Errors
29    /// An encoding error is theoretically possible, should not happen in practice.
30    pub fn encode_b64(&self) -> Result<String, WalletKitError> {
31        Ok(BASE64_URL_SAFE_NO_PAD.encode(self.encode()?))
32    }
33
34    /// The root hash of the Merkle root used for inclusion in the `WorldIDRegistry`.
35    #[must_use]
36    pub fn merkle_root(&self) -> FieldElement {
37        self.0.merkle_root.into()
38    }
39}