zkgroup/api/auth/
auth_credential_presentation.rs

1use partial_default::PartialDefault;
2use serde::{Serialize, Serializer};
3
4use crate::api;
5use crate::auth::AuthCredentialWithPniZkcPresentation;
6use crate::common::constants::*;
7use crate::common::errors::*;
8use crate::common::simple_types::*;
9
10#[derive(derive_more::From)]
11pub enum AnyAuthCredentialPresentation {
12    V4(AuthCredentialWithPniZkcPresentation),
13}
14
15#[repr(u8)]
16#[derive(Copy, Clone, Debug, PartialDefault, derive_more::TryFrom)]
17#[try_from(repr)]
18enum PresentationVersion {
19    // V1-V3 are no longer supported.
20    #[partial_default]
21    V4 = PRESENTATION_VERSION_4,
22}
23
24impl From<PresentationVersion> for u8 {
25    fn from(value: PresentationVersion) -> Self {
26        value as u8
27    }
28}
29
30impl AnyAuthCredentialPresentation {
31    pub fn new(presentation_bytes: &[u8]) -> Result<Self, ZkGroupDeserializationFailure> {
32        let first = *presentation_bytes
33            .first()
34            .ok_or(ZkGroupDeserializationFailure::new::<Self>())?;
35        let version = PresentationVersion::try_from(first)
36            .map_err(|_| ZkGroupDeserializationFailure::new::<Self>())?;
37        match version {
38            PresentationVersion::V4 => Ok(crate::deserialize::<
39                AuthCredentialWithPniZkcPresentation,
40            >(presentation_bytes)?
41            .into()),
42        }
43    }
44
45    pub fn get_aci_ciphertext(&self) -> api::groups::UuidCiphertext {
46        match self {
47            AnyAuthCredentialPresentation::V4(presentation) => presentation.aci_ciphertext(),
48        }
49    }
50
51    pub fn get_pni_ciphertext(&self) -> api::groups::UuidCiphertext {
52        match self {
53            AnyAuthCredentialPresentation::V4(presentation) => presentation.pni_ciphertext(),
54        }
55    }
56
57    pub fn get_redemption_time(&self) -> Timestamp {
58        match self {
59            AnyAuthCredentialPresentation::V4(presentation) => presentation.redemption_time(),
60        }
61    }
62}
63
64impl Serialize for AnyAuthCredentialPresentation {
65    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
66    where
67        S: Serializer,
68    {
69        match self {
70            AnyAuthCredentialPresentation::V4(presentation) => presentation.serialize(serializer),
71        }
72    }
73}