Skip to content

Commit

Permalink
Refactored mapping structure
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Nov 10, 2023
1 parent 3f6519b commit ea7188b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 130 deletions.
6 changes: 0 additions & 6 deletions src/data_types/pres_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 5 additions & 25 deletions src/data_types/w3c/presentation_proof.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -124,30 +124,10 @@ impl Default for PresentationProofType {
#[serde(rename_all = "camelCase")]
pub struct CredentialAttributesMapping {
#[serde(default)]
pub revealed_attributes: Vec<Attribute>,
pub revealed_attribute_groups: Vec<AttributeGroup>,
pub revealed_attributes: HashSet<String>,
pub revealed_attribute_groups: HashSet<String>,
#[serde(default)]
pub unrevealed_attributes: Vec<Attribute>,
pub unrevealed_attributes: HashSet<String>,
#[serde(default)]
pub requested_predicates: Vec<Predicate>,
}

#[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<String>,
}

#[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<String>,
}
22 changes: 0 additions & 22 deletions src/services/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -278,24 +277,3 @@ impl RequestedProof {
referents
}
}

impl CredentialAttributesMapping {
pub(crate) fn attribute_referents(&self) -> HashSet<String> {
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<String> {
let mut referents = HashSet::new();
for attribute in self.requested_predicates.iter() {
referents.insert(attribute.referent.to_string());
}
referents
}
}
42 changes: 10 additions & 32 deletions src/services/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
94 changes: 49 additions & 45 deletions src/services/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String> =
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,
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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<RequestedProof> {
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
Expand All @@ -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)
Expand Down

0 comments on commit ea7188b

Please sign in to comment.