From ea7188bf85210662cae7f9869779331d61175791 Mon Sep 17 00:00:00 2001 From: "artem.ivanov" Date: Fri, 10 Nov 2023 16:20:24 +0300 Subject: [PATCH] Refactored mapping structure Signed-off-by: artem.ivanov --- src/data_types/pres_request.rs | 6 -- src/data_types/w3c/presentation_proof.rs | 30 ++------ src/services/helpers.rs | 22 ------ src/services/prover.rs | 42 +++-------- src/services/verifier.rs | 94 ++++++++++++------------ 5 files changed, 64 insertions(+), 130 deletions(-) diff --git a/src/data_types/pres_request.rs b/src/data_types/pres_request.rs index 8075b109..d41f5b86 100644 --- a/src/data_types/pres_request.rs +++ b/src/data_types/pres_request.rs @@ -206,12 +206,6 @@ pub enum PredicateTypes { LT, } -impl Default for PredicateTypes { - fn default() -> Self { - PredicateTypes::GE - } -} - impl fmt::Display for PredicateTypes { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { diff --git a/src/data_types/w3c/presentation_proof.rs b/src/data_types/w3c/presentation_proof.rs index d27c6f9c..25c36ac2 100644 --- a/src/data_types/w3c/presentation_proof.rs +++ b/src/data_types/w3c/presentation_proof.rs @@ -1,6 +1,6 @@ -use crate::data_types::pres_request::PredicateTypes; use crate::utils::base64; use anoncreds_clsignatures::{AggregatedProof, SubProof}; +use std::collections::HashSet; #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -124,30 +124,10 @@ impl Default for PresentationProofType { #[serde(rename_all = "camelCase")] pub struct CredentialAttributesMapping { #[serde(default)] - pub revealed_attributes: Vec, - pub revealed_attribute_groups: Vec, + pub revealed_attributes: HashSet, + pub revealed_attribute_groups: HashSet, #[serde(default)] - pub unrevealed_attributes: Vec, + pub unrevealed_attributes: HashSet, #[serde(default)] - pub requested_predicates: Vec, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct Attribute { - pub referent: String, - pub name: String, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct AttributeGroup { - pub referent: String, - pub names: Vec, -} - -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct Predicate { - pub referent: String, - pub name: String, - pub p_type: PredicateTypes, - pub p_value: i32, + pub predicates: HashSet, } diff --git a/src/services/helpers.rs b/src/services/helpers.rs index a57e9f24..244a729c 100644 --- a/src/services/helpers.rs +++ b/src/services/helpers.rs @@ -5,7 +5,6 @@ use crate::cl::{ use crate::data_types::presentation::RequestedProof; use crate::data_types::rev_reg_def::RevocationRegistryDefinitionId; use crate::data_types::schema::Schema; -use crate::data_types::w3c::presentation_proof::CredentialAttributesMapping; use crate::data_types::{ credential::CredentialValues, link_secret::LinkSecret, @@ -278,24 +277,3 @@ impl RequestedProof { referents } } - -impl CredentialAttributesMapping { - pub(crate) fn attribute_referents(&self) -> HashSet { - let mut referents = HashSet::new(); - for attribute in self.revealed_attributes.iter() { - referents.insert(attribute.referent.to_string()); - } - for attribute in self.revealed_attribute_groups.iter() { - referents.insert(attribute.referent.to_string()); - } - referents - } - - pub(crate) fn predicate_referents(&self) -> HashSet { - let mut referents = HashSet::new(); - for attribute in self.requested_predicates.iter() { - referents.insert(attribute.referent.to_string()); - } - referents - } -} diff --git a/src/services/prover.rs b/src/services/prover.rs index e606f8e2..90885c41 100644 --- a/src/services/prover.rs +++ b/src/services/prover.rs @@ -33,8 +33,8 @@ use crate::utils::validation::Validatable; use crate::data_types::rev_reg_def::RevocationRegistryDefinitionId; use crate::data_types::w3c::credential_proof::{CredentialProof, CredentialSignature}; use crate::data_types::w3c::presentation_proof::{ - Attribute, AttributeGroup, CredentialAttributesMapping, CredentialPresentationProof, - CredentialPresentationProofValue, Predicate, PresentationProof, PresentationProofValue, + CredentialAttributesMapping, CredentialPresentationProof, CredentialPresentationProofValue, + PresentationProof, PresentationProofValue, }; use anoncreds_clsignatures::{ CredentialSignature as CLCredentialSignature, NonCredentialSchema, ProofBuilder, @@ -1109,43 +1109,21 @@ fn build_mapping<'p>( referent ) })?; - if let Some(ref name) = requested_attribute.name { - let attribute = Attribute { - referent: referent.to_string(), - name: name.to_string(), - }; + if requested_attribute.name.is_some() { if *reveal { - mapping.revealed_attributes.push(attribute) + mapping.revealed_attributes.insert(referent.to_string()); } else { - mapping.unrevealed_attributes.push(attribute) + mapping.unrevealed_attributes.insert(referent.to_string()); } } - if let Some(ref names) = requested_attribute.names { - mapping.revealed_attribute_groups.push(AttributeGroup { - referent: referent.to_string(), - names: names.clone(), - }) + if requested_attribute.names.is_some() { + mapping + .revealed_attribute_groups + .insert(referent.to_string()); } } - for referent in credential.requested_predicates.iter() { - let predicate_info = pres_req - .requested_predicates - .get(referent) - .cloned() - .ok_or_else(|| { - err_msg!( - "Attribute with referent \"{}\" not found in ProofRequests", - referent - ) - })?; - let predicate = Predicate { - referent: referent.to_string(), - name: predicate_info.name, - p_type: predicate_info.p_type, - p_value: predicate_info.p_value, - }; - mapping.requested_predicates.push(predicate) + mapping.predicates.insert(referent.to_string()); } Ok(mapping) diff --git a/src/services/verifier.rs b/src/services/verifier.rs index 6bc5b856..7b5a376c 100644 --- a/src/services/verifier.rs +++ b/src/services/verifier.rs @@ -164,7 +164,7 @@ pub fn verify_w3c_presentation( )?; // Ensures the restrictions set out in the request is met - let requested_proof = build_requested_proof_from_w3c_presentation(presentation)?; + let requested_proof = build_requested_proof_from_w3c_presentation(presentation, pres_req)?; verify_requested_restrictions( pres_req, schemas, @@ -195,14 +195,15 @@ pub fn verify_w3c_presentation( .revocation_registry .as_ref(); - let attributes = credential_proof.mapping.attribute_referents(); - let predicates = credential_proof.mapping.predicate_referents(); + let mut revealed_attribute: HashSet = + credential_proof.mapping.revealed_attributes.clone(); + revealed_attribute.extend(credential_proof.mapping.revealed_attribute_groups.clone()); _add_sub_proof( &mut proof_verifier, pres_req, - &attributes, - &predicates, + &revealed_attribute, + &credential_proof.mapping.predicates, &non_credential_schema, schema_id, schemas, @@ -998,24 +999,18 @@ fn collect_received_attrs_and_predicates_from_w3c_presentation( timestamp: None, }; for revealed_attribute in &presentation_proof.mapping.revealed_attributes { - revealed.insert(revealed_attribute.referent.to_string(), identifier.clone()); + revealed.insert(revealed_attribute.to_string(), identifier.clone()); } for revealed_attribute_group in &presentation_proof.mapping.revealed_attribute_groups { - revealed.insert( - revealed_attribute_group.referent.to_string(), - identifier.clone(), - ); + revealed.insert(revealed_attribute_group.to_string(), identifier.clone()); } for unrevealed_attribute in &presentation_proof.mapping.unrevealed_attributes { - unrevealed.insert( - unrevealed_attribute.referent.to_string(), - identifier.clone(), - ); + unrevealed.insert(unrevealed_attribute.to_string(), identifier.clone()); } - for predicate in &presentation_proof.mapping.requested_predicates { - predicates.insert(predicate.referent.to_string(), identifier.clone()); + for predicate in &presentation_proof.mapping.predicates { + predicates.insert(predicate.to_string(), identifier.clone()); } } @@ -1024,38 +1019,53 @@ fn collect_received_attrs_and_predicates_from_w3c_presentation( fn build_requested_proof_from_w3c_presentation( presentation: &W3CPresentation, + presentation_request: &PresentationRequestPayload, ) -> Result { let mut requested_proof = RequestedProof::default(); for (index, credential) in presentation.verifiable_credential.iter().enumerate() { + let sub_proof_index = index as u32; let proof = credential.get_presentation_proof()?; - for revealed_attribute in proof.mapping.revealed_attributes.iter() { + for referent in proof.mapping.revealed_attributes.iter() { + let requested_attribute = presentation_request + .requested_attributes + .get(referent) + .cloned() + .ok_or_else(|| err_msg!("Requested Attribute {} not found in request", referent))?; + + let name = requested_attribute + .name + .ok_or_else(|| err_msg!("Requested Attribute expected to have a name attribute"))?; + let raw = credential .credential_subject .attributes .0 - .get(&revealed_attribute.name) - .ok_or_else(|| { - err_msg!( - "Attribute {} not found in credential", - &revealed_attribute.name - ) - })?; + .get(&name) + .ok_or_else(|| err_msg!("Attribute {} not found in credential", &name))?; requested_proof.revealed_attrs.insert( - revealed_attribute.referent.clone(), + referent.clone(), RevealedAttributeInfo { - sub_proof_index: index as u32, + sub_proof_index, raw: raw.to_string(), - encoded: "".to_string(), // encoded value not needed? + encoded: "".to_string(), // encoded value not needed }, ); } - for revealed_attribute in proof.mapping.revealed_attribute_groups.iter() { + for referent in proof.mapping.revealed_attribute_groups.iter() { + let requested_attribute = presentation_request + .requested_attributes + .get(referent) + .cloned() + .ok_or_else(|| err_msg!("Requested Attribute {} not found in request", referent))?; + let names = requested_attribute.names.ok_or_else(|| { + err_msg!("Requested Attribute expected to have a names attribute") + })?; let mut group_info = RevealedAttributeGroupInfo { - sub_proof_index: index as u32, + sub_proof_index, values: HashMap::new(), }; - for name in revealed_attribute.names.iter() { + for name in names.iter() { let raw = credential .credential_subject .attributes @@ -1072,23 +1082,17 @@ fn build_requested_proof_from_w3c_presentation( } requested_proof .revealed_attr_groups - .insert(revealed_attribute.referent.clone(), group_info); + .insert(referent.to_string(), group_info); } - for revealed_attribute in proof.mapping.unrevealed_attributes.iter() { - requested_proof.unrevealed_attrs.insert( - revealed_attribute.referent.clone(), - SubProofReferent { - sub_proof_index: index as u32, - }, - ); + for referent in proof.mapping.unrevealed_attributes.iter() { + requested_proof + .unrevealed_attrs + .insert(referent.to_string(), SubProofReferent { sub_proof_index }); } - for revealed_attribute in proof.mapping.requested_predicates.iter() { - requested_proof.predicates.insert( - revealed_attribute.referent.clone(), - SubProofReferent { - sub_proof_index: index as u32, - }, - ); + for referent in proof.mapping.predicates.iter() { + requested_proof + .predicates + .insert(referent.to_string(), SubProofReferent { sub_proof_index }); } } Ok(requested_proof)