ssi_data_integrity_core/proof/configuration/
reference.rs

1use educe::Educe;
2use serde::Serialize;
3use ssi_core::Lexical;
4use ssi_verification_methods::{ProofPurpose, ReferenceOrOwnedRef};
5use std::collections::BTreeMap;
6
7use crate::{
8    CloneCryptographicSuite, CryptographicSuite, ProofConfiguration, SerializeCryptographicSuite,
9};
10
11#[derive(Educe, Serialize)]
12#[educe(Clone, Copy)]
13#[serde(bound = "S: SerializeCryptographicSuite", rename_all = "camelCase")]
14pub struct ProofConfigurationRef<'a, S: CryptographicSuite> {
15    /// Proof context.
16    #[serde(rename = "@context", default, skip_serializing_if = "Option::is_none")]
17    pub context: Option<&'a ssi_json_ld::syntax::Context>,
18
19    /// Proof type.
20    #[serde(flatten, serialize_with = "S::serialize_type")]
21    pub type_: &'a S,
22
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub created: Option<&'a Lexical<xsd_types::DateTimeStamp>>,
25
26    #[serde(serialize_with = "S::serialize_verification_method_ref_ref")]
27    pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>,
28
29    pub proof_purpose: ProofPurpose,
30
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub expires: Option<&'a Lexical<xsd_types::DateTimeStamp>>,
33
34    #[serde(
35        with = "crate::value_or_array",
36        skip_serializing_if = "<[String]>::is_empty",
37        rename = "domain"
38    )]
39    pub domains: &'a [String],
40
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub challenge: Option<&'a str>,
43
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub nonce: Option<&'a str>,
46
47    #[serde(flatten, serialize_with = "S::serialize_proof_options")]
48    pub options: &'a S::ProofOptions,
49
50    /// Extra properties.
51    #[serde(flatten)]
52    pub extra_properties: &'a BTreeMap<String, json_syntax::Value>,
53}
54
55impl<'a, S: CryptographicSuite> ProofConfigurationRef<'a, S> {
56    pub fn to_owned(&self) -> ProofConfiguration<S>
57    where
58        S: CloneCryptographicSuite,
59    {
60        ProofConfiguration {
61            context: self.context.cloned(),
62            type_: self.type_.clone(),
63            created: self.created.cloned(),
64            verification_method: S::clone_verification_method_ref_ref(self.verification_method),
65            proof_purpose: self.proof_purpose,
66            expires: self.expires.cloned(),
67            domains: self.domains.to_owned(),
68            challenge: self.challenge.map(ToOwned::to_owned),
69            nonce: self.nonce.map(ToOwned::to_owned),
70            options: S::clone_proof_options(self.options),
71            extra_properties: self.extra_properties.clone(),
72        }
73    }
74
75    pub fn map<T: CryptographicSuite>(
76        self,
77        map_type: impl FnOnce(&'a S) -> &'a T,
78        map_verification_method: impl FnOnce(&'a S::VerificationMethod) -> &'a T::VerificationMethod,
79        map_options: impl FnOnce(&'a S::ProofOptions) -> &'a T::ProofOptions,
80    ) -> ProofConfigurationRef<'a, T> {
81        ProofConfigurationRef {
82            context: self.context,
83            type_: map_type(self.type_),
84            created: self.created,
85            verification_method: self.verification_method.map(map_verification_method),
86            proof_purpose: self.proof_purpose,
87            expires: self.expires,
88            domains: self.domains,
89            challenge: self.challenge,
90            nonce: self.nonce,
91            options: map_options(self.options),
92            extra_properties: self.extra_properties,
93        }
94    }
95
96    pub fn try_map<T: CryptographicSuite, E>(
97        self,
98        map_type: impl FnOnce(&'a S) -> Result<&'a T, E>,
99        map_verification_method: impl FnOnce(
100            &'a S::VerificationMethod,
101        ) -> Result<&'a T::VerificationMethod, E>,
102        map_options: impl FnOnce(&'a S::ProofOptions) -> Result<&'a T::ProofOptions, E>,
103    ) -> Result<ProofConfigurationRef<'a, T>, E> {
104        Ok(ProofConfigurationRef {
105            context: self.context,
106            type_: map_type(self.type_)?,
107            created: self.created,
108            verification_method: self.verification_method.try_map(map_verification_method)?,
109            proof_purpose: self.proof_purpose,
110            expires: self.expires,
111            domains: self.domains,
112            challenge: self.challenge,
113            nonce: self.nonce,
114            options: map_options(self.options)?,
115            extra_properties: self.extra_properties,
116        })
117    }
118
119    pub fn without_proof_options(self) -> ProofConfigurationRefWithoutOptions<'a, S> {
120        ProofConfigurationRefWithoutOptions {
121            context: self.context,
122            type_: self.type_,
123            created: self.created,
124            verification_method: self.verification_method,
125            proof_purpose: self.proof_purpose,
126            expires: self.expires,
127            domains: self.domains,
128            challenge: self.challenge,
129            nonce: self.nonce,
130            extra_properties: self.extra_properties,
131        }
132    }
133}
134
135/// Proof configuration without the suite specific options.
136#[derive(Educe, Serialize)]
137#[educe(Clone, Copy)]
138#[serde(bound = "S: SerializeCryptographicSuite", rename_all = "camelCase")]
139pub struct ProofConfigurationRefWithoutOptions<'a, S: CryptographicSuite> {
140    /// Proof context.
141    #[serde(rename = "@context", default, skip_serializing_if = "Option::is_none")]
142    pub context: Option<&'a ssi_json_ld::syntax::Context>,
143
144    /// Proof type.
145    #[serde(flatten, serialize_with = "S::serialize_type")]
146    pub type_: &'a S,
147
148    #[serde(skip_serializing_if = "Option::is_none")]
149    pub created: Option<&'a Lexical<xsd_types::DateTimeStamp>>,
150
151    #[serde(serialize_with = "S::serialize_verification_method_ref_ref")]
152    pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>,
153
154    pub proof_purpose: ProofPurpose,
155
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub expires: Option<&'a Lexical<xsd_types::DateTimeStamp>>,
158
159    #[serde(
160        with = "crate::value_or_array",
161        skip_serializing_if = "<[String]>::is_empty",
162        rename = "domain"
163    )]
164    pub domains: &'a [String],
165
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub challenge: Option<&'a str>,
168
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pub nonce: Option<&'a str>,
171
172    /// Extra properties.
173    #[serde(flatten)]
174    pub extra_properties: &'a BTreeMap<String, json_syntax::Value>,
175}