Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve XSD dateTime and dateTimeStamp lexical representations. #639

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
submodules: true

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
uses: dtolnay/rust-toolchain@1.81.0

- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub struct MyCredentialSubject {
let credential = ssi::claims::vc::v1::JsonCredential::<MyCredentialSubject>::new(
Some(uri!("https://example.org/#CredentialId").to_owned()), // id
uri!("https://example.org/#Issuer").to_owned().into(), // issuer
DateTime::now(), // issuance date
DateTime::now().into(), // issuance date
NonEmptyVec::new(MyCredentialSubject {
name: "John Smith".to_owned(),
email: "john.smith@example.org".to_owned()
Expand Down
3 changes: 2 additions & 1 deletion crates/claims/crates/data-integrity/core/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub struct DataIntegrityDocument {
impl ssi_json_ld::Expandable for DataIntegrityDocument {
type Error = JsonLdError;

type Expanded<I: Interpretation, V: Vocabulary> = ssi_json_ld::ExpandedDocument<V::Iri, V::BlankId>
type Expanded<I: Interpretation, V: Vocabulary>
= ssi_json_ld::ExpandedDocument<V::Iri, V::BlankId>
where
I: Interpretation,
V: VocabularyMut,
Expand Down
11 changes: 6 additions & 5 deletions crates/claims/crates/data-integrity/core/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use ssi_core::Lexical;
use ssi_verification_methods::{ProofPurpose, ReferenceOrOwned};

use crate::{suite::ConfigurationError, CryptographicSuite, ProofConfiguration};
Expand All @@ -14,7 +15,7 @@ pub struct ProofOptions<M, T> {

/// Date a creation of the proof.
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<xsd_types::DateTimeStamp>,
pub created: Option<Lexical<xsd_types::DateTimeStamp>>,

/// Verification method.
pub verification_method: Option<ReferenceOrOwned<M>>,
Expand All @@ -25,7 +26,7 @@ pub struct ProofOptions<M, T> {

/// Specifies when the proof expires.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,
pub expires: Option<Lexical<xsd_types::DateTimeStamp>>,

#[allow(rustdoc::bare_urls)]
/// Conveys one or more security domains in which the proof is meant to be
Expand Down Expand Up @@ -79,7 +80,7 @@ impl<M, T: Default> Default for ProofOptions<M, T> {
fn default() -> Self {
Self {
context: None,
created: Some(xsd_types::DateTimeStamp::now_ms()),
created: Some(xsd_types::DateTimeStamp::now_ms().into()),
verification_method: None,
proof_purpose: ProofPurpose::default(),
expires: None,
Expand All @@ -94,7 +95,7 @@ impl<M, T: Default> Default for ProofOptions<M, T> {

impl<M, T> ProofOptions<M, T> {
pub fn new(
created: xsd_types::DateTimeStamp,
created: Lexical<xsd_types::DateTimeStamp>,
verification_method: ReferenceOrOwned<M>,
proof_purpose: ProofPurpose,
options: T,
Expand All @@ -116,7 +117,7 @@ impl<M, T> ProofOptions<M, T> {
pub fn from_method_and_options(verification_method: ReferenceOrOwned<M>, options: T) -> Self {
Self {
context: None,
created: Some(xsd_types::DateTimeStamp::now_ms()),
created: Some(xsd_types::DateTimeStamp::now_ms().into()),
verification_method: Some(verification_method),
proof_purpose: ProofPurpose::default(),
expires: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ impl<'d, 'a, S: SerializeCryptographicSuite> Expandable
for EmbeddedProofConfigurationRef<'d, 'a, S>
{
type Error = ConfigurationExpansionError;
type Expanded<I, V> = ExpandedEmbeddedProofConfiguration<V::Iri, V::BlankId>
type Expanded<I, V>
= ExpandedEmbeddedProofConfiguration<V::Iri, V::BlankId>
where
I: Interpretation,
V: VocabularyMut,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use iref::Iri;
use serde::Serialize;
use ssi_core::Lexical;
use ssi_verification_methods::{ProofPurpose, ReferenceOrOwned};
use static_iref::iri;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -31,7 +32,7 @@ pub struct ProofConfiguration<S: CryptographicSuite> {

/// Date a creation of the proof.
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<xsd_types::DateTimeStamp>,
pub created: Option<Lexical<xsd_types::DateTimeStamp>>,

/// Verification method.
#[serde(serialize_with = "S::serialize_verification_method_ref")]
Expand All @@ -42,7 +43,7 @@ pub struct ProofConfiguration<S: CryptographicSuite> {

/// Specifies when the proof expires.
#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,
pub expires: Option<Lexical<xsd_types::DateTimeStamp>>,

#[allow(rustdoc::bare_urls)]
/// Conveys one or more security domains in which the proof is meant to be
Expand Down Expand Up @@ -94,7 +95,7 @@ pub struct ProofConfiguration<S: CryptographicSuite> {
impl<S: CryptographicSuite> ProofConfiguration<S> {
pub fn new(
type_: S,
created: xsd_types::DateTimeStamp,
created: Lexical<xsd_types::DateTimeStamp>,
verification_method: ReferenceOrOwned<S::VerificationMethod>,
proof_purpose: ProofPurpose,
options: S::ProofOptions,
Expand Down Expand Up @@ -122,7 +123,7 @@ impl<S: CryptographicSuite> ProofConfiguration<S> {
Self {
context: None,
type_,
created: Some(xsd_types::DateTimeStamp::now_ms()),
created: Some(xsd_types::DateTimeStamp::now_ms().into()),
verification_method,
proof_purpose: ProofPurpose::default(),
expires: None,
Expand Down Expand Up @@ -221,10 +222,10 @@ impl<S: CryptographicSuite> ProofConfiguration<S> {
ProofConfigurationRef {
context: self.context.as_ref(),
type_: &self.type_,
created: self.created,
created: self.created.as_ref(),
verification_method: self.verification_method.borrowed(),
proof_purpose: self.proof_purpose,
expires: self.expires,
expires: self.expires.as_ref(),
domains: &self.domains,
challenge: self.challenge.as_deref(),
nonce: self.nonce.as_deref(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use educe::Educe;
use serde::Serialize;
use ssi_core::Lexical;
use ssi_verification_methods::{ProofPurpose, ReferenceOrOwnedRef};
use std::collections::BTreeMap;

Expand All @@ -20,15 +21,15 @@ pub struct ProofConfigurationRef<'a, S: CryptographicSuite> {
pub type_: &'a S,

#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<xsd_types::DateTimeStamp>,
pub created: Option<&'a Lexical<xsd_types::DateTimeStamp>>,

#[serde(serialize_with = "S::serialize_verification_method_ref_ref")]
pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>,

pub proof_purpose: ProofPurpose,

#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,
pub expires: Option<&'a Lexical<xsd_types::DateTimeStamp>>,

#[serde(
with = "crate::value_or_array",
Expand Down Expand Up @@ -59,10 +60,10 @@ impl<'a, S: CryptographicSuite> ProofConfigurationRef<'a, S> {
ProofConfiguration {
context: self.context.cloned(),
type_: self.type_.clone(),
created: self.created,
created: self.created.cloned(),
verification_method: S::clone_verification_method_ref_ref(self.verification_method),
proof_purpose: self.proof_purpose,
expires: self.expires,
expires: self.expires.cloned(),
domains: self.domains.to_owned(),
challenge: self.challenge.map(ToOwned::to_owned),
nonce: self.nonce.map(ToOwned::to_owned),
Expand Down Expand Up @@ -145,15 +146,15 @@ pub struct ProofConfigurationRefWithoutOptions<'a, S: CryptographicSuite> {
pub type_: &'a S,

#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<xsd_types::DateTimeStamp>,
pub created: Option<&'a Lexical<xsd_types::DateTimeStamp>>,

#[serde(serialize_with = "S::serialize_verification_method_ref_ref")]
pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>,

pub proof_purpose: ProofPurpose,

#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,
pub expires: Option<&'a Lexical<xsd_types::DateTimeStamp>>,

#[serde(
with = "crate::value_or_array",
Expand Down
37 changes: 26 additions & 11 deletions crates/claims/crates/data-integrity/core/src/proof/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::{
de::{DeserializeSeed, MapAccess},
Deserialize,
};
use ssi_core::{de::WithType, OneOrMany};
use ssi_core::{de::WithType, Lexical, OneOrMany};
use std::{collections::BTreeMap, marker::PhantomData};

mod field;
Expand All @@ -28,15 +28,30 @@ pub use replay_map::*;

mod configuration;

/// Converts an XSD dateTime into a XSD dateTimeStamp while preserving the
/// lexical representation.
///
/// If no offset is given in the dateTime, the UTC offset (`Z`) is added.
fn datetime_to_utc_datetimestamp(
d: Option<xsd_types::DateTime>,
) -> Option<xsd_types::DateTimeStamp> {
d.map(|d| {
xsd_types::DateTimeStamp::new(
d.date_time,
d.offset
.unwrap_or(chrono::FixedOffset::east_opt(0).unwrap()),
)
value: Option<Lexical<xsd_types::DateTime>>,
) -> Option<Lexical<xsd_types::DateTimeStamp>> {
value.map(|lexical_dt| {
let (dt, mut representation) = lexical_dt.into_parts();

let dts = xsd_types::DateTimeStamp::new(
dt.date_time,
dt.offset.unwrap_or_else(|| {
if let Some(r) = &mut representation {
// Keep most of the lexical representation, just add the
// offset.
r.push('Z');
}

chrono::FixedOffset::east_opt(0).unwrap()
}),
);

Lexical::from_parts(dts, representation)
})
}

Expand All @@ -50,10 +65,10 @@ impl<'de, T: DeserializeCryptographicSuite<'de>> Proof<T> {
.map_err(|_| serde::de::Error::custom("unexpected cryptosuite"))?;

let mut context = None;
let mut created: Option<xsd_types::DateTime> = None;
let mut created: Option<Lexical<xsd_types::DateTime>> = None;
let mut verification_method = None;
let mut proof_purpose = None;
let mut expires: Option<xsd_types::DateTime> = None;
let mut expires: Option<Lexical<xsd_types::DateTime>> = None;
let mut domains: Option<OneOrMany<String>> = None;
let mut challenge = None;
let mut nonce = None;
Expand Down
19 changes: 10 additions & 9 deletions crates/claims/crates/data-integrity/core/src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use educe::Educe;
use serde::{Deserialize, Serialize};
use ssi_claims_core::{AttachProof, ProofValidationError, ProofValidity, ResourceProvider};
use ssi_core::Lexical;
use ssi_core::{one_or_many::OneOrManyRef, OneOrMany};
use ssi_verification_methods::{ProofPurpose, ReferenceOrOwned};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -49,7 +50,7 @@ pub struct Proof<S: CryptographicSuite> {

/// Date a creation of the proof.
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<xsd_types::DateTimeStamp>,
pub created: Option<Lexical<xsd_types::DateTimeStamp>>,

/// Verification method.
#[serde(serialize_with = "S::serialize_verification_method_ref")]
Expand All @@ -60,7 +61,7 @@ pub struct Proof<S: CryptographicSuite> {

/// Specifies when the proof expires.
#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<xsd_types::DateTimeStamp>,
pub expires: Option<Lexical<xsd_types::DateTimeStamp>>,

#[allow(rustdoc::bare_urls)]
/// Conveys one or more security domains in which the proof is meant to be
Expand Down Expand Up @@ -117,7 +118,7 @@ impl<T: CryptographicSuite> Proof<T> {
/// Creates a new proof.
pub fn new(
type_: T,
created: xsd_types::DateTimeStamp,
created: Lexical<xsd_types::DateTimeStamp>,
verification_method: ReferenceOrOwned<T::VerificationMethod>,
proof_purpose: ProofPurpose,
options: T::ProofOptions,
Expand All @@ -143,10 +144,10 @@ impl<T: CryptographicSuite> Proof<T> {
ProofRef {
context: self.context.as_ref(),
type_: &self.type_,
created: self.created,
created: self.created.as_ref(),
verification_method: self.verification_method.borrowed(),
proof_purpose: self.proof_purpose,
expires: self.expires,
expires: self.expires.as_ref(),
domains: &self.domains,
challenge: self.challenge.as_deref(),
nonce: self.nonce.as_deref(),
Expand All @@ -171,10 +172,10 @@ impl<T: CryptographicSuite> Proof<T> {
ProofConfigurationRef {
context: self.context.as_ref(),
type_: &self.type_,
created: self.created,
created: self.created.as_ref(),
verification_method: self.verification_method.borrowed(),
proof_purpose: self.proof_purpose,
expires: self.expires,
expires: self.expires.as_ref(),
domains: &self.domains,
challenge: self.challenge.as_deref(),
nonce: self.nonce.as_deref(),
Expand Down Expand Up @@ -212,10 +213,10 @@ impl<S: CloneCryptographicSuite> Clone for Proof<S> {
Self {
context: self.context.clone(),
type_: self.type_.clone(),
created: self.created,
created: self.created.clone(),
verification_method: S::clone_verification_method_ref(&self.verification_method),
proof_purpose: self.proof_purpose,
expires: self.expires,
expires: self.expires.clone(),
domains: self.domains.clone(),
challenge: self.challenge.clone(),
nonce: self.nonce.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;

use ssi_core::Lexical;
use ssi_verification_methods::{ProofPurpose, ReferenceOrOwnedRef};

use crate::{CryptographicSuite, ProofConfigurationRef};
Expand All @@ -9,13 +10,13 @@ pub struct ProofRef<'a, S: CryptographicSuite> {

pub type_: &'a S,

pub created: Option<xsd_types::DateTimeStamp>,
pub created: Option<&'a Lexical<xsd_types::DateTimeStamp>>,

pub verification_method: ReferenceOrOwnedRef<'a, S::VerificationMethod>,

pub proof_purpose: ProofPurpose,

pub expires: Option<xsd_types::DateTimeStamp>,
pub expires: Option<&'a Lexical<xsd_types::DateTimeStamp>>,

pub domains: &'a [String],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ where
let new_proof = Proof {
context: base_proof.context.cloned(),
type_: Bbs2023,
created: base_proof.created,
created: base_proof.created.cloned(),
verification_method: base_proof.verification_method.cloned(),
proof_purpose: base_proof.proof_purpose,
expires: base_proof.expires,
expires: base_proof.expires.cloned(),
domains: base_proof.domains.to_vec(),
challenge: base_proof.challenge.map(ToOwned::to_owned),
nonce: base_proof.nonce.map(ToOwned::to_owned),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ _:b5 <https://windsurf.grotto-networking.com/selective#year> \"2023\"^^<http://w
];
let proof_configuration = ProofConfiguration::new(
Bbs2023,
xsd_types::DateTimeStamp::now_ms(),
xsd_types::DateTimeStamp::now_ms().into(),
ReferenceOrOwned::Reference("did:method:test".parse().unwrap()),
ProofPurpose::Assertion,
(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl<E, P> ValidateClaims<E, P> for JsonCredential {
impl ssi_json_ld::Expandable for JsonCredential {
type Error = JsonLdError;

type Expanded<I, V> = ssi_json_ld::ExpandedDocument<V::Iri, V::BlankId>
type Expanded<I, V>
= ssi_json_ld::ExpandedDocument<V::Iri, V::BlankId>
where
I: Interpretation,
V: VocabularyMut,
Expand Down
Loading
Loading