Skip to content

Commit

Permalink
Add DataIntegrityDocument type.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Jun 25, 2024
1 parent e8c63bf commit c33c39f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
66 changes: 66 additions & 0 deletions crates/claims/crates/data-integrity/core/src/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::{borrow::Cow, collections::BTreeMap, hash::Hash};

use rdf_types::VocabularyMut;
use serde::{Deserialize, Serialize};
use ssi_claims_core::{Proof, Validate};
use ssi_core::OneOrMany;
use ssi_json_ld::{AnyJsonLdEnvironment, JsonLdError, JsonLdNodeObject, JsonLdObject};

/// Any Data-Integrity-compatible document.
///
/// The only assumption made by this type is that the JSON-LD `@type` attribute
/// is aliased to `type`, which is common practice (for instance with
/// Verifiable Credentials).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataIntegrityDocument {
#[serde(rename = "@context", skip_serializing_if = "Option::is_none")]
pub context: Option<ssi_json_ld::syntax::Context>,

#[serde(
rename = "type",
alias = "@type",
default,
skip_serializing_if = "OneOrMany::is_empty"
)]
pub types: OneOrMany<String>,

#[serde(flatten)]
pub properties: BTreeMap<String, ssi_json_ld::syntax::Value>,
}

impl<E, V, L> ssi_rdf::Expandable<E> for DataIntegrityDocument
where
E: AnyJsonLdEnvironment<Vocabulary = V, Loader = L>,
V: VocabularyMut,
V::Iri: Clone + Eq + Hash,
V::BlankId: Clone + Eq + Hash,
L: json_ld::Loader<V::Iri>,
L::Error: std::fmt::Display,
{
type Error = JsonLdError<L::Error>;

type Expanded = json_ld::ExpandedDocument<V::Iri, V::BlankId>;

async fn expand(&self, environment: &mut E) -> Result<Self::Expanded, Self::Error> {
let json = ssi_json_ld::CompactJsonLd(json_syntax::to_value(self).unwrap());
json.expand(environment).await
}
}

impl JsonLdObject for DataIntegrityDocument {
fn json_ld_context(&self) -> Option<Cow<ssi_json_ld::syntax::Context>> {
self.context.as_ref().map(Cow::Borrowed)
}
}

impl JsonLdNodeObject for DataIntegrityDocument {
fn json_ld_type(&self) -> ssi_json_ld::JsonLdTypes {
ssi_json_ld::JsonLdTypes::new(&[], Cow::Borrowed(self.types.as_slice()))
}
}

impl<E, P: Proof> Validate<E, P> for DataIntegrityDocument {
fn validate(&self, _env: &E, _proof: &P::Prepared) -> ssi_claims_core::ClaimsValidity {
Ok(())
}
}
2 changes: 2 additions & 0 deletions crates/claims/crates/data-integrity/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ops::{Deref, DerefMut};
pub mod canonicalization;
mod de;
mod decode;
mod document;
pub mod hashing;
mod options;
mod proof;
Expand All @@ -23,6 +24,7 @@ pub use suite::{
DeserializeCryptographicSuite, SerializeCryptographicSuite, StandardCryptographicSuite,
};

pub use document::*;
#[doc(hidden)]
pub use ssi_rdf;

Expand Down
2 changes: 1 addition & 1 deletion crates/claims/crates/vc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub mod syntax;
pub mod v1;
pub mod v2;

pub use syntax::{AnySpecializedJsonCredential, AnyJsonCredential, AnyJsonPresentation};
pub use syntax::{AnyJsonCredential, AnyJsonPresentation, AnySpecializedJsonCredential};
7 changes: 7 additions & 0 deletions crates/core/src/one_or_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ impl<T> OneOrMany<T> {
}
}

pub fn as_slice(&self) -> &[T] {
match self {
Self::One(t) => std::slice::from_ref(t),
Self::Many(l) => l.as_slice(),
}
}

pub fn first(&self) -> Option<&T> {
match self {
Self::One(value) => Some(value),
Expand Down

0 comments on commit c33c39f

Please sign in to comment.