Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Dec 3, 2023
1 parent fb427f2 commit a79197d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 61 deletions.
51 changes: 24 additions & 27 deletions src/services/w3c/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,30 @@ impl W3CCredential {
}

pub(crate) fn has_attribute(&self, requested_attribute: &str) -> bool {
for (attribute, value) in self.credential_subject.attributes.0.iter() {
if attr_common_view(attribute) == attr_common_view(requested_attribute) {
if let CredentialAttributeValue::Attribute(_) = value {
return true;
}
let (_, value) = match self.get_case_insensitive_attribute(requested_attribute) {
Ok(value) => value,
_ => {
return false;
}
}
false
};
matches!(value, CredentialAttributeValue::Attribute(_))
}

pub(crate) fn has_predicate(&self, predicate: &PredicateInfo) -> bool {
let predicate_attribute = attr_common_view(&predicate.name);
for (attribute, value) in self.credential_subject.attributes.0.iter() {
if attr_common_view(attribute) == predicate_attribute {
if let CredentialAttributeValue::Predicate(predicates) = value {
let found = predicates.iter().find(|shared_predicate| {
shared_predicate.predicate == predicate.p_type
&& shared_predicate.value == predicate.p_value
});
if found.is_some() {
return true;
}
}
let (_, value) = match self.get_case_insensitive_attribute(&predicate.name) {
Ok(value) => value,
Err(_) => return false,
};

match value {
CredentialAttributeValue::Predicate(ref predicates) => {
predicates.iter().any(|shared_predicate| {
shared_predicate.predicate == predicate.p_type
&& shared_predicate.value == predicate.p_value
})
}
_ => false,
}
false
}

pub(crate) fn attributes(&self) -> Vec<AttributeInfo> {
Expand Down Expand Up @@ -91,12 +89,11 @@ impl W3CCredential {
}

impl Schema {
pub(crate) fn has_attribute(&self, requested_attribute: &str) -> bool {
for attribute in self.attr_names.0.iter() {
if attr_common_view(attribute) == attr_common_view(requested_attribute) {
return true;
}
}
false
pub(crate) fn has_case_insensitive_attribute(&self, requested_attribute: &str) -> bool {
let requested_attribute = attr_common_view(requested_attribute);
self.attr_names
.0
.iter()
.any(|attribute| attr_common_view(attribute) == requested_attribute)
}
}
51 changes: 18 additions & 33 deletions src/services/w3c/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn verify_presentation(
let credential_proofs = presentation
.verifiable_credential
.iter()
.map(|verifiable_credential| verifiable_credential.get_presentation_proof())
.map(W3CCredential::get_presentation_proof)
.collect::<Result<Vec<&CredentialPresentationProof>>>()?;

// These values are from the prover and cannot be trusted
Expand All @@ -57,7 +57,7 @@ pub fn verify_presentation(

let proof_data = presentation.proof.get_proof_value()?;
let mut proof = Proof {
proofs: Vec::new(),
proofs: Vec::with_capacity(presentation.verifiable_credential.len()),
aggregated_proof: proof_data.aggregated,
};

Expand Down Expand Up @@ -137,22 +137,20 @@ fn check_credential_non_revoked_interval(
>,
proof: &CredentialPresentationProof,
) -> Result<()> {
if credential.get_rev_reg_id().is_none() {
return Ok(());
}

let non_revoked_interval = get_requested_non_revoked_interval(
credential.get_rev_reg_id(),
nonrevoke_interval,
presentation_request.non_revoked.as_ref(),
nonrevoke_interval_override,
);
if let Some(rev_reg_id) = credential.get_rev_reg_id() {
let non_revoked_interval = get_requested_non_revoked_interval(
Some(rev_reg_id),
nonrevoke_interval,
presentation_request.non_revoked.as_ref(),
nonrevoke_interval_override,
);

if let Some(non_revoked_interval) = non_revoked_interval {
let timestamp = proof
.timestamp
.ok_or_else(|| err_msg!("Credential timestamp not found for revocation check"))?;
non_revoked_interval.is_valid(timestamp)?;
if let Some(non_revoked_interval) = non_revoked_interval {
let timestamp = proof
.timestamp
.ok_or_else(|| err_msg!("Credential timestamp not found for revocation check"))?;
non_revoked_interval.is_valid(timestamp)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -195,7 +193,6 @@ fn check_requested_attribute<'a>(
>,
credential_proofs: &[&CredentialPresentationProof],
) -> Result<&'a W3CCredential> {
let mut found_credential: Option<&'a W3CCredential> = None;
for (index, credential) in presentation.verifiable_credential.iter().enumerate() {
let proof = credential_proofs
.get(index)
Expand All @@ -214,16 +211,10 @@ fn check_requested_attribute<'a>(
.is_ok();

if valid_credential {
found_credential = Some(credential);
break;
return Ok(credential);
}
}

if let Some(found_credential) = found_credential {
// credential for attribute is found in revealed data
return Ok(found_credential);
}

// else consider attribute as unrevealed and try to find credential which schema includes requested attribute
for (index, credential) in presentation.verifiable_credential.iter().enumerate() {
let proof = credential_proofs
Expand All @@ -238,7 +229,7 @@ fn check_requested_attribute<'a>(
)
})?;

let valid_credential = schema.has_attribute(attribute)
let valid_credential = schema.has_case_insensitive_attribute(attribute)
&& check_credential_conditions(
credential,
presentation_request,
Expand All @@ -252,16 +243,10 @@ fn check_requested_attribute<'a>(
.is_ok();

if valid_credential {
found_credential = Some(credential);
break;
return Ok(credential);
}
}

if let Some(found_credential) = found_credential {
// credential for attribute is found in revealed data
return Ok(found_credential);
}

Err(err_msg!(
"Presentation does not contain attribute {}",
attribute
Expand Down
1 change: 0 additions & 1 deletion tests/anoncreds_demos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anoncreds::data_types::pres_request::PredicateTypes;
use anoncreds::data_types::w3c::credential::CredentialAttributeValue;
use anoncreds::data_types::w3c::credential_proof::CredentialProof;
use anoncreds::data_types::w3c::presentation_proof::{PredicateAttribute, PredicateAttributeType};
use anoncreds::data_types::w3c::uri::URI;
use anoncreds::verifier;
Expand Down

0 comments on commit a79197d

Please sign in to comment.