From 04720d4a1f416bddfcdcc3ded2c509c87717f557 Mon Sep 17 00:00:00 2001 From: Simon Bihel Date: Mon, 22 Jul 2024 10:34:08 +0100 Subject: [PATCH] Fix DataIntegrity `created` type (#584) --- Cargo.toml | 2 +- .../crates/data-integrity/core/src/options.rs | 8 ++++---- .../core/src/proof/configuration/mod.rs | 6 +++--- .../core/src/proof/configuration/reference.rs | 4 ++-- .../data-integrity/core/src/proof/de/mod.rs | 20 +++++++++++++++---- .../data-integrity/core/src/proof/mod.rs | 14 +++++++++---- .../core/src/proof/reference.rs | 2 +- .../src/suites/w3c/bbs_2023/signature/mod.rs | 2 +- crates/dids/methods/pkh/src/lib.rs | 10 ++++++++-- crates/dids/methods/tz/tests/did.rs | 10 ++++++++-- 10 files changed, 54 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a5b2e193c..6a174592d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,7 +86,7 @@ chrono = "0.4.24" iref = "3.1.2" static-iref = "3.0" rdf-types = "0.22.3" -xsd-types = "0.9.4" +xsd-types = "0.9.5" locspan = "0.8" json-syntax = "0.12.5" nquads-syntax = "0.19" diff --git a/crates/claims/crates/data-integrity/core/src/options.rs b/crates/claims/crates/data-integrity/core/src/options.rs index d41e26f55..2e4eec9c2 100644 --- a/crates/claims/crates/data-integrity/core/src/options.rs +++ b/crates/claims/crates/data-integrity/core/src/options.rs @@ -14,7 +14,7 @@ pub struct ProofOptions { /// Date a creation of the proof. #[serde(skip_serializing_if = "Option::is_none")] - pub created: Option, + pub created: Option, /// Verification method. pub verification_method: Option>, @@ -74,7 +74,7 @@ impl Default for ProofOptions { fn default() -> Self { Self { context: None, - created: Some(xsd_types::DateTime::now_ms()), + created: Some(xsd_types::DateTimeStamp::now_ms()), verification_method: None, proof_purpose: ProofPurpose::default(), expires: None, @@ -89,7 +89,7 @@ impl Default for ProofOptions { impl ProofOptions { pub fn new( - created: xsd_types::DateTime, + created: xsd_types::DateTimeStamp, verification_method: ReferenceOrOwned, proof_purpose: ProofPurpose, options: T, @@ -111,7 +111,7 @@ impl ProofOptions { pub fn from_method_and_options(verification_method: ReferenceOrOwned, options: T) -> Self { Self { context: None, - created: Some(xsd_types::DateTime::now_ms()), + created: Some(xsd_types::DateTimeStamp::now_ms()), verification_method: Some(verification_method), proof_purpose: ProofPurpose::default(), expires: None, diff --git a/crates/claims/crates/data-integrity/core/src/proof/configuration/mod.rs b/crates/claims/crates/data-integrity/core/src/proof/configuration/mod.rs index 648e5aff3..8537c6e35 100644 --- a/crates/claims/crates/data-integrity/core/src/proof/configuration/mod.rs +++ b/crates/claims/crates/data-integrity/core/src/proof/configuration/mod.rs @@ -31,7 +31,7 @@ pub struct ProofConfiguration { /// Date a creation of the proof. #[serde(skip_serializing_if = "Option::is_none")] - pub created: Option, + pub created: Option, /// Verification method. #[serde(serialize_with = "S::serialize_verification_method_ref")] @@ -90,7 +90,7 @@ pub struct ProofConfiguration { impl ProofConfiguration { pub fn new( type_: S, - created: xsd_types::DateTime, + created: xsd_types::DateTimeStamp, verification_method: ReferenceOrOwned, proof_purpose: ProofPurpose, options: S::ProofOptions, @@ -118,7 +118,7 @@ impl ProofConfiguration { Self { context: None, type_, - created: Some(xsd_types::DateTime::now_ms()), + created: Some(xsd_types::DateTimeStamp::now_ms()), verification_method, proof_purpose: ProofPurpose::default(), expires: None, diff --git a/crates/claims/crates/data-integrity/core/src/proof/configuration/reference.rs b/crates/claims/crates/data-integrity/core/src/proof/configuration/reference.rs index abb569922..30668a2a6 100644 --- a/crates/claims/crates/data-integrity/core/src/proof/configuration/reference.rs +++ b/crates/claims/crates/data-integrity/core/src/proof/configuration/reference.rs @@ -20,7 +20,7 @@ pub struct ProofConfigurationRef<'a, S: CryptographicSuite> { pub type_: &'a S, #[serde(skip_serializing_if = "Option::is_none")] - pub created: Option, + pub created: Option, #[serde(serialize_with = "S::serialize_verification_method_ref_ref")] pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>, @@ -141,7 +141,7 @@ pub struct ProofConfigurationRefWithoutOptions<'a, S: CryptographicSuite> { pub type_: &'a S, #[serde(skip_serializing_if = "Option::is_none")] - pub created: Option, + pub created: Option, #[serde(serialize_with = "S::serialize_verification_method_ref_ref")] pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>, diff --git a/crates/claims/crates/data-integrity/core/src/proof/de/mod.rs b/crates/claims/crates/data-integrity/core/src/proof/de/mod.rs index 3e8aa6bd0..0c06af06c 100644 --- a/crates/claims/crates/data-integrity/core/src/proof/de/mod.rs +++ b/crates/claims/crates/data-integrity/core/src/proof/de/mod.rs @@ -28,6 +28,18 @@ pub use replay_map::*; mod configuration; +fn datetime_to_utc_datetimestamp( + d: Option, +) -> Option { + d.map(|d| { + xsd_types::DateTimeStamp::new( + d.date_time, + d.offset + .unwrap_or(chrono::FixedOffset::east_opt(0).unwrap()), + ) + }) +} + impl<'de, T: DeserializeCryptographicSuite<'de>> Proof { fn deserialize_with_type(type_: Type, mut deserializer: S) -> Result where @@ -38,10 +50,10 @@ impl<'de, T: DeserializeCryptographicSuite<'de>> Proof { .map_err(|_| serde::de::Error::custom("unexpected cryptosuite"))?; let mut context = None; - let mut created = None; + let mut created: Option = None; let mut verification_method = None; let mut proof_purpose = None; - let mut expires = None; + let mut expires: Option = None; let mut domains = None; let mut challenge = None; let mut nonce = None; @@ -86,13 +98,13 @@ impl<'de, T: DeserializeCryptographicSuite<'de>> Proof { Ok(Self { context, type_: suite, - created, + created: datetime_to_utc_datetimestamp(created), verification_method: verification_method .map(|v| v.map(VerificationMethodOf::unwrap).into()) .ok_or_else(|| serde::de::Error::custom("missing `verificationMethod` property"))?, proof_purpose: proof_purpose .ok_or_else(|| serde::de::Error::custom("missing `proofPurpose` property"))?, - expires, + expires: datetime_to_utc_datetimestamp(expires), domains: domains.unwrap_or_default(), challenge, nonce, diff --git a/crates/claims/crates/data-integrity/core/src/proof/mod.rs b/crates/claims/crates/data-integrity/core/src/proof/mod.rs index 17e867d50..b57d354dd 100644 --- a/crates/claims/crates/data-integrity/core/src/proof/mod.rs +++ b/crates/claims/crates/data-integrity/core/src/proof/mod.rs @@ -48,8 +48,11 @@ pub struct Proof { pub type_: S, /// Date a creation of the proof. - #[serde(skip_serializing_if = "Option::is_none")] - pub created: Option, + #[serde( + deserialize_with = "de::deserialize_datetime_utc", + skip_serializing_if = "Option::is_none" + )] + pub created: Option, /// Verification method. #[serde(serialize_with = "S::serialize_verification_method_ref")] @@ -59,7 +62,10 @@ pub struct Proof { pub proof_purpose: ProofPurpose, /// Specifies when the proof expires. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde( + deserialize_with = "de::deserialize_datetime_utc", + skip_serializing_if = "Option::is_none" + )] pub expires: Option, #[allow(rustdoc::bare_urls)] @@ -113,7 +119,7 @@ impl Proof { /// Creates a new proof. pub fn new( type_: T, - created: xsd_types::DateTime, + created: xsd_types::DateTimeStamp, verification_method: ReferenceOrOwned, proof_purpose: ProofPurpose, options: T::ProofOptions, diff --git a/crates/claims/crates/data-integrity/core/src/proof/reference.rs b/crates/claims/crates/data-integrity/core/src/proof/reference.rs index de1faccd7..f5278d4bc 100644 --- a/crates/claims/crates/data-integrity/core/src/proof/reference.rs +++ b/crates/claims/crates/data-integrity/core/src/proof/reference.rs @@ -9,7 +9,7 @@ pub struct ProofRef<'a, S: CryptographicSuite> { pub type_: &'a S, - pub created: Option, + pub created: Option, pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>, diff --git a/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/signature/mod.rs b/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/signature/mod.rs index 74517c7b0..681cbb272 100644 --- a/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/signature/mod.rs +++ b/crates/claims/crates/data-integrity/suites/src/suites/w3c/bbs_2023/signature/mod.rs @@ -185,7 +185,7 @@ _:b5 \"2023\"^^