diff --git a/contracts/okp4-dataverse/src/did/crypto.rs b/contracts/okp4-dataverse/src/did/crypto.rs new file mode 100644 index 00000000..e6c9c405 --- /dev/null +++ b/contracts/okp4-dataverse/src/did/crypto.rs @@ -0,0 +1,35 @@ +use crate::did::consts::RDF_TYPE; +use crate::ContractError; +use itertools::Itertools; +use okp4_rdf::dataset::{Dataset, QuadIterator}; +use rio_api::model::Term; + +pub struct Proof<'a> { + type_: String, + inner: Dataset<'a>, +} + +impl<'a> TryFrom> for Proof<'a> { + type Error = ContractError; + + fn try_from(dataset: Dataset<'a>) -> Result { + Ok(Self { + type_: dataset + .match_pattern(None, Some(RDF_TYPE), None, None) + .objects() + .exactly_one() + .map_err(|_| { + ContractError::InvalidCredential( + "Credential proof can must have only one type".to_string(), + ) + }) + .and_then(|o| match o { + Term::NamedNode(n) => Ok(n.iri.to_string()), + _ => Err(ContractError::InvalidCredential( + "Credential proof type must be a named node".to_string(), + )), + })?, + inner: dataset, + }) + } +}