Skip to content

Commit

Permalink
Drop explicit mapping
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Nov 15, 2023
1 parent 5676fd8 commit c90ee42
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 366 deletions.
2 changes: 1 addition & 1 deletion src/data_types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Validatable for AttributeNames {
self.0.len(),
MAX_ATTRIBUTES_COUNT
)
.into());
.into());
}
Ok(())
}
Expand Down
8 changes: 1 addition & 7 deletions src/data_types/w3c/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Validatable for CredentialAttributes {
"CredentialAttributes validation failed: {} value format is not supported",
attribute
)
.into())
.into());
}
}
}
Expand Down Expand Up @@ -137,12 +137,6 @@ impl CredentialAttributes {
self.0.insert(attribute, json!(value));
}

pub fn get_attribute(&self, attribute: &str) -> Result<&Value> {
self.0
.get(attribute)
.ok_or_else(|| err_msg!("Credential attribute {} not found", attribute))
}

pub fn encode(&self, encoding: &CredentialValuesEncoding) -> Result<CredentialValues> {
match encoding {
CredentialValuesEncoding::Auto => {
Expand Down
11 changes: 2 additions & 9 deletions src/data_types/w3c/presentation_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ use std::collections::HashSet;
pub struct CredentialPresentationProof {
#[serde(rename = "type")]
pub type_: PresentationProofType,
/// Uniform Resource Identifier - https://www.w3.org/TR/vc-data-model/#dfn-uri
// FIXME: Consider either removing or moving under proof_value
// In fact, it's only needed to make attributes validation on the verifier side
// Revealed attributes and predicates can be restored from credential subject, but not unrevealed attributes
pub mapping: CredentialAttributesMapping,
pub proof_value: String,
#[serde(skip_serializing_if = "Option::is_none")]
// Timestamp is needed to query revocation registry at the specific moment in time
Expand Down Expand Up @@ -40,12 +35,10 @@ impl CredentialPresentationProofValue {
impl CredentialPresentationProof {
pub fn new(
proof_value: CredentialPresentationProofValue,
mapping: CredentialAttributesMapping,
timestamp: Option<u64>,
) -> CredentialPresentationProof {
CredentialPresentationProof {
type_: PresentationProofType::AnonCredsPresentationProof2023,
mapping,
timestamp,
proof_value: proof_value.encode(),
}
Expand Down Expand Up @@ -75,10 +68,10 @@ pub struct PresentationProofValue {
}

impl PresentationProof {
pub fn new(proof_value: PresentationProofValue, nonce: String) -> PresentationProof {
pub fn new(proof_value: PresentationProofValue, challenge: String) -> PresentationProof {
PresentationProof {
type_: PresentationProofType::AnonCredsPresentationProof2023,
challenge: nonce,
challenge,
proof_value: proof_value.encode(),
}
}
Expand Down
110 changes: 89 additions & 21 deletions src/services/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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::credential::W3CCredential;
use crate::data_types::w3c::credential::{PredicateAttribute, W3CCredential};
use crate::data_types::{
credential::CredentialValues,
link_secret::LinkSecret,
Expand Down Expand Up @@ -218,27 +218,43 @@ pub fn get_non_revoked_interval(
&HashMap<RevocationRegistryDefinitionId, HashMap<u64, u64>>,
>,
) -> Option<NonRevokedInterval> {
let mut interval: Option<NonRevokedInterval> = None;
// Collapse to the most stringent local interval for the attributes / predicates,
// we can do this because there is only 1 revocation status list for this credential
// if it satisfies the most stringent interval, it will satisfy all intervals
let interval = match (attrs_nonrevoked_interval, pred_nonrevoked_interval) {
(Some(attr), None) => Some(attr),
(None, Some(pred)) => Some(pred),
(Some(mut attr), Some(pred)) => {
attr.compare_and_set(&pred);
Some(attr)
}
_ => None,
};

get_requested_non_revoked_interval(
rev_reg_id,
interval.as_ref(),
pres_req.non_revoked.as_ref(),
nonrevoke_interval_override,
)
}

if let Some(rev_reg_id) = rev_reg_id {
// Collapse to the most stringent local interval for the attributes / predicates,
// we can do this because there is only 1 revocation status list for this credential
// if it satisfies the most stringent interval, it will satisfy all intervals
interval = match (attrs_nonrevoked_interval, pred_nonrevoked_interval) {
(Some(attr), None) => Some(attr),
(None, Some(pred)) => Some(pred),
(Some(mut attr), Some(pred)) => {
attr.compare_and_set(&pred);
Some(attr)
}
_ => None,
};
pub fn get_requested_non_revoked_interval(
rev_reg_id: Option<&RevocationRegistryDefinitionId>,
local_nonrevoked_interval: Option<&NonRevokedInterval>,
global_nonrevoked_interval: Option<&NonRevokedInterval>,
nonrevoke_interval_override: Option<
&HashMap<RevocationRegistryDefinitionId, HashMap<u64, u64>>,
>,
) -> Option<NonRevokedInterval> {
let mut interval: Option<NonRevokedInterval> = local_nonrevoked_interval.cloned();

if let Some(rev_reg_id) = rev_reg_id {
// Global interval is override by the local one,
// we only need to update if local is None and Global is Some,
// do not need to update if global is more stringent
if let (Some(global), None) = (pres_req.non_revoked.clone(), interval.as_mut()) {
interval = Some(global);
if let (Some(global), None) = (global_nonrevoked_interval, interval.as_mut()) {
interval = Some(global.clone());
};

// Override Interval if an earlier `from` value is accepted by the verifier
Expand Down Expand Up @@ -280,16 +296,68 @@ impl RequestedProof {
}
}

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;
}
}
return false;
}
}

impl W3CCredential {
pub(crate) fn get_attribute(&self, requested_attribute: &str) -> Result<(String, Value)> {
for (attribute, value) in self.credential_subject.attributes.0.iter() {
if attr_common_view(attribute) == attr_common_view(requested_attribute) {
return Ok((attribute.to_owned(), value.to_owned()));
}
}
Err(err_msg!(
"Credential attribute {} not found",
requested_attribute
))
return Err(err_msg!("Credential attribute {} not found", requested_attribute));
}

pub(crate) fn has_attribute(&self, requested_attribute: &str) -> bool {
for attribute in self.credential_subject.attributes.0.keys() {
if attr_common_view(attribute) == attr_common_view(requested_attribute) {
return true;
}
}
return false;
}

pub(crate) fn attributes(&self) -> Vec<AttributeInfo> {
self.credential_subject
.attributes
.0
.iter()
.filter(|(_, value)| value.as_str().is_some())
.map(|(attribute, _)| AttributeInfo {
name: Some(attribute.to_owned()),
names: None,
restrictions: None,
non_revoked: None,
})
.collect()
}

pub(crate) fn predicates(&self) -> Result<Vec<PredicateInfo>> {
self.credential_subject
.attributes
.0
.iter()
.filter(|(_, value)| value.as_object().is_some())
.map(|(attribute, value)| {
serde_json::from_value::<PredicateAttribute>(value.to_owned())
.map(|predicate| PredicateInfo {
name: attribute.to_owned(),
p_type: predicate.p_type,
p_value: predicate.p_value,
restrictions: None,
non_revoked: None,
})
.map_err(|_| err_msg!("Unable to parse predicate from credential attribute"))
})
.collect()
}
}
Loading

0 comments on commit c90ee42

Please sign in to comment.