From aaa0d85a55e35f95a9f7a3eda97a0d03614e16ac Mon Sep 17 00:00:00 2001 From: thefireskater <114769322+thefireskater@users.noreply.github.com> Date: Fri, 2 Jun 2023 23:14:30 -0700 Subject: [PATCH] Given selectors, return n-quad positions --- ssi-jws/src/lib.rs | 4 ++++ ssi-ldp/src/lib.rs | 11 +++++++++++ ssi-ldp/src/proof.rs | 1 + ssi-vc/src/lib.rs | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/ssi-jws/src/lib.rs b/ssi-jws/src/lib.rs index 805c9a002..094eb22f5 100644 --- a/ssi-jws/src/lib.rs +++ b/ssi-jws/src/lib.rs @@ -357,6 +357,8 @@ pub fn verify_payload( assert!(credential_subject_id != "", "credentialSubject node not found"); let mut first_claim_found = false; + + i = 0; while i < payload.messages.len() { let m = payload.messages[i].as_str(); if m.starts_with(credential_subject_id) { @@ -368,9 +370,11 @@ pub fn verify_payload( assert!(first_claim_found, "No claims in derived credential"); for j in 0..message_hashes.len() { + //eprintln!("Checking hash for {}: ", payload.messages[i].as_str()); let revealed_hash = message_hashes[j]; let target_hash = SignatureMessage::hash(payload.messages[i].as_bytes()); if revealed_hash != target_hash { + //eprintln!("Hashes do not match"); return Err(Error::InvalidSignature); } diff --git a/ssi-ldp/src/lib.rs b/ssi-ldp/src/lib.rs index 5dc29f91c..132034f59 100644 --- a/ssi-ldp/src/lib.rs +++ b/ssi-ldp/src/lib.rs @@ -648,6 +648,17 @@ fn rename_blank_node_labels(orig: &Vec) -> Vec { rewritten } +pub async fn to_nquads( + document: &(dyn LinkedDataDocument + Sync), + context_loader: &mut ContextLoader, +) -> Result, Error> { + let doc_dataset = document + .to_dataset_for_signing(None, context_loader) + .await?; + let doc_normalized = urdna2015::normalize(doc_dataset.quads().map(QuadRef::from)).into_nquads_vec(); + Ok(doc_normalized) +} + async fn to_jws_payload_v2( document: &(dyn LinkedDataDocument + Sync), proof: &Proof, diff --git a/ssi-ldp/src/proof.rs b/ssi-ldp/src/proof.rs index c9cc17bbf..a9a6616b8 100644 --- a/ssi-ldp/src/proof.rs +++ b/ssi-ldp/src/proof.rs @@ -223,6 +223,7 @@ pub struct LinkedDataProofOptions { #[serde(skip_serializing_if = "Option::is_none")] /// The nonce of the proof. pub nonce: Option, + #[serde(skip_serializing_if = "Option::is_none")] /// Indices of disclosed messages pub disclosed_message_indices: Option>, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/ssi-vc/src/lib.rs b/ssi-vc/src/lib.rs index fb5a00796..3e735e828 100644 --- a/ssi-vc/src/lib.rs +++ b/ssi-vc/src/lib.rs @@ -984,6 +984,7 @@ impl Credential { resolver: &dyn DIDResolver, context_loader: &mut ContextLoader, ) -> VerificationResult { + // this is the entrypoint called by didkit let nonce = match options.as_ref() { Some(ldp_options) => ldp_options.nonce.clone(), None => None @@ -1096,6 +1097,27 @@ impl Credential { result.checks.push(Check::Status); result } + + pub async fn get_nquad_positions(&self, selectors: &Vec, context_loader: &mut ContextLoader) -> Result, Error> { + let nquads = ssi_ldp::to_nquads(self, context_loader).await?; + let mut positions = Vec::new(); + let mut index: u32 = 2; + for nq in nquads.iter() { + let split: Vec<&str> = nq.split(" ").collect(); + let middle = split[1]; + + for s in selectors.iter() { + let suffix = "/".to_owned() + s + ">"; + if middle.ends_with(suffix.as_str()) { + positions.push(index); + break; + } + } + + index += 1; + } + Ok(positions) + } } impl CheckableStatus {