Skip to content

Commit

Permalink
fix(dataverse): properly extract claims from VC
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 16, 2024
1 parent b2f6d43 commit 88141ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
7 changes: 1 addition & 6 deletions contracts/okp4-dataverse/src/credential/vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,7 @@ impl<'a> VerifiableCredential<'a> {
})
.map_ok(|claim_id| Claim {
id: claim_id.iri,
content: Dataset::new(
dataset
.match_pattern(Some(claim_id.into()), None, None, None)
.copied()
.collect(),
),
content: dataset.sub_graph(claim_id.into()),
})
.collect()
}
Expand Down
34 changes: 34 additions & 0 deletions packages/okp4-rdf/src/dataset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::owned_model::OwnedQuad;
use itertools::Itertools;
use rio_api::model::{GraphName, NamedNode, Quad, Subject, Term};
use std::collections::HashSet;
use std::slice::Iter;

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -49,6 +50,39 @@ impl<'a> Dataset<'a> {
) -> QuadPatternFilter<'a, Iter<'a, Quad<'a>>> {
self.iter().skip_pattern((s, p, o, g).into())
}

pub fn sub_graph(&'a self, subject: Subject<'a>) -> Dataset<'a> {
Self::new(Self::sub_graph_from_quads(self.as_ref(), HashSet::new(), subject).0)
}

fn sub_graph_from_quads(
quads: &'a [Quad<'a>],
mut visited: HashSet<Subject<'a>>,
subject: Subject<'a>,
) -> (Vec<Quad<'a>>, HashSet<Subject<'a>>) {
let mut sub_graph = vec![];
for quad in quads
.iter()
.match_pattern((Some(subject), None, None, None).into())
{
sub_graph.push(*quad);

let maybe_node: Option<Subject<'a>> = match quad.object {
Term::NamedNode(n) => Some(n.into()),
Term::BlankNode(n) => Some(n.into()),
_ => None,
};

if let Some(s) = maybe_node.filter(|n| !visited.contains(n)) {
visited.insert(subject);
let (new_quads, new_visited) = Self::sub_graph_from_quads(quads, visited, s);
visited = new_visited;
sub_graph.extend(new_quads);
}
}

(sub_graph, visited)
}
}

#[derive(Copy, Clone)]
Expand Down

0 comments on commit 88141ba

Please sign in to comment.