Skip to content

Commit

Permalink
refactor(dataverse): reoganize vc related code
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 5, 2024
1 parent bd826f9 commit b4fe886
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions contracts/okp4-dataverse/src/credential/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod proof;
mod rdf_markers;
mod vc;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::did::consts::RDF_TYPE;
use crate::credential::rdf_markers::RDF_TYPE;
use crate::ContractError;
use itertools::Itertools;
use okp4_rdf::dataset::{Dataset, QuadIterator};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::did::consts::*;
use crate::did::crypto::Proof;
use crate::credential::proof::Proof;
use crate::credential::rdf_markers::*;
use crate::ContractError;
use itertools::Itertools;
use okp4_rdf::dataset::Dataset;
use okp4_rdf::dataset::QuadIterator;
use okp4_rdf::dataset::{Dataset, QuadPattern};
use rio_api::model::{BlankNode, Literal, NamedNode, Subject, Term};

pub struct VerifiableCredential<'a> {
Expand Down Expand Up @@ -35,12 +35,13 @@ impl<'a> TryFrom<&'a Dataset<'a>> for VerifiableCredential<'a> {
fn try_from(dataset: &'a Dataset<'a>) -> Result<Self, Self::Error> {
let id = Self::extract_identifier(&dataset)?;

let mut proofs = vec![];
let mut unsecured_filter: Vec<QuadPattern<'_>> = vec![];
for (proof, graph) in Self::extract_proofs(dataset, id)? {
proofs.push(proof);
unsecured_filter.push((None, None, None, Some(Some(graph.into()))).into())
}
let (proofs, proof_graphs): (Vec<Proof<'a>>, Vec<BlankNode<'a>>) =
Self::extract_proofs(dataset, id)?.into_iter().unzip();

let unsecured_filter = proof_graphs
.into_iter()
.map(|g| (None, None, None, Some(Some(g.into()))).into())
.collect();

Ok(Self {
id: id.iri,
Expand All @@ -53,7 +54,7 @@ impl<'a> TryFrom<&'a Dataset<'a>> for VerifiableCredential<'a> {
proof: proofs,
unsecured_document: Dataset::new(
dataset
.into_iter()
.iter()
.skip_patterns(unsecured_filter)
.map(|quad| *quad)
.collect(),
Expand Down
3 changes: 0 additions & 3 deletions contracts/okp4-dataverse/src/did/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/okp4-dataverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)]

pub mod contract;
mod did;
mod credential;
mod error;
pub mod msg;
pub mod state;
Expand Down
17 changes: 6 additions & 11 deletions packages/okp4-rdf/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ impl<'a> Dataset<'a> {
Self { quads }
}

pub fn iter(&self) -> Iter<Quad<'a>> {
self.quads.iter()
}

pub fn match_pattern(
&'a self,
s: Option<Subject<'a>>,
p: Option<NamedNode<'a>>,
o: Option<Term<'a>>,
g: Option<Option<GraphName<'a>>>,
) -> QuadPatternFilter<'a, Iter<'a, Quad<'a>>> {
self.quads.iter().match_pattern((s, p, o, g).into())
self.iter().match_pattern((s, p, o, g).into())
}

pub fn skip_pattern(
Expand All @@ -29,7 +33,7 @@ impl<'a> Dataset<'a> {
o: Option<Term<'a>>,
g: Option<Option<GraphName<'a>>>,
) -> QuadPatternFilter<'a, Iter<'a, Quad<'a>>> {
self.quads.iter().skip_pattern((s, p, o, g).into())
self.iter().skip_pattern((s, p, o, g).into())
}
}

Expand All @@ -41,15 +45,6 @@ impl<'a> From<&'a [Quad<'a>]> for Dataset<'a> {
}
}

impl<'a> IntoIterator for &'a Dataset<'a> {
type Item = &'a Quad<'a>;
type IntoIter = Iter<'a, Quad<'a>>;

fn into_iter(self) -> Self::IntoIter {
self.quads.iter()
}
}

#[derive(Copy, Clone)]
pub struct QuadPattern<'a> {
subject: Option<Subject<'a>>,
Expand Down

0 comments on commit b4fe886

Please sign in to comment.