Skip to content

Commit

Permalink
Type requested credentials (#1130)
Browse files Browse the repository at this point in the history
Type requested credentials (#1130)

* Do not convert proof req to value unnecessarily
* Start typing RequestedCredentials
* prover_create_proof takes RequestedCredentials
* Delete _get_credential
* Do not use value for cred_by_attr

---------

Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee authored Feb 22, 2024
1 parent f632719 commit 424189e
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 312 deletions.
4 changes: 2 additions & 2 deletions aries/aries_vcx/src/common/proofs/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn generate_indy_proof(
ledger: &impl AnoncredsLedgerRead,
anoncreds: &impl BaseAnonCreds,
credentials: &SelectedCredentials,
self_attested_attrs: &HashMap<String, String>,
self_attested_attrs: HashMap<String, String>,
proof_req_data_json: PresentationRequest,
) -> VcxResult<Presentation> {
trace!(
Expand Down Expand Up @@ -53,7 +53,7 @@ pub async fn generate_indy_proof(
.prover_create_proof(
wallet,
proof_req_data_json,
&requested_credentials,
requested_credentials,
&settings::DEFAULT_LINK_SECRET_ALIAS.to_string(),
schemas_json,
credential_defs_json,
Expand Down
82 changes: 41 additions & 41 deletions aries/aries_vcx/src/common/proofs/prover/prover_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anoncreds_types::data_types::{
messages::{
cred_selection::SelectedCredentials,
pres_request::{NonRevokedInterval, PresentationRequest},
presentation::{RequestedAttribute, RequestedCredentials, RequestedPredicate},
},
};
use aries_vcx_core::{
Expand All @@ -14,10 +15,10 @@ use aries_vcx_core::{
errors::error::AriesVcxCoreErrorKind,
ledger::base_ledger::AnoncredsLedgerRead,
};
use serde_json::Value;

use crate::errors::error::prelude::*;

// TODO: Move to anoncreds_types
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct CredInfoProver {
pub referent: String,
Expand Down Expand Up @@ -211,60 +212,58 @@ pub async fn build_rev_states_json(

pub fn build_requested_credentials_json(
credentials_identifiers: &Vec<CredInfoProver>,
self_attested_attrs: &HashMap<String, String>,
self_attested_attrs: HashMap<String, String>,
proof_req: &PresentationRequest,
) -> VcxResult<String> {
) -> VcxResult<RequestedCredentials> {
trace!(
"build_requested_credentials_json >> credentials_identifiers: {:?}, self_attested_attrs: \
{:?}, proof_req: {:?}",
credentials_identifiers,
self_attested_attrs,
proof_req
);
let mut rtn: Value = json!({
"self_attested_attributes":{},
"requested_attributes":{},
"requested_predicates":{}
});
// do same for predicates and self_attested
if let Value::Object(ref mut map) = rtn["requested_attributes"] {
for cred_info in credentials_identifiers {
if proof_req
.value()
.requested_attributes
.get(&cred_info.referent)
.is_some()
{
let insert_val = json!({"cred_id": cred_info.credential_referent, "revealed": cred_info.revealed.unwrap_or(true), "timestamp": cred_info.timestamp});
map.insert(cred_info.referent.to_owned(), insert_val);
}

let mut rtn = RequestedCredentials::default();

for cred_info in credentials_identifiers {
if proof_req
.value()
.requested_attributes
.get(&cred_info.referent)
.is_some()
{
rtn.requested_attributes.insert(
cred_info.referent.to_owned(),
RequestedAttribute {
cred_id: cred_info.credential_referent.to_owned(),
timestamp: cred_info.timestamp,
revealed: cred_info.revealed.unwrap_or(true),
},
);
}
}

if let Value::Object(ref mut map) = rtn["requested_predicates"] {
for cred_info in credentials_identifiers {
if proof_req
.value()
.requested_predicates
.get(&cred_info.referent)
.is_some()
{
let insert_val = json!({"cred_id": cred_info.credential_referent, "timestamp": cred_info.timestamp});
map.insert(cred_info.referent.to_owned(), insert_val);
}
for cred_info in credentials_identifiers {
if proof_req
.value()
.requested_predicates
.get(&cred_info.referent)
.is_some()
{
rtn.requested_predicates.insert(
cred_info.referent.to_owned(),
RequestedPredicate {
cred_id: cred_info.credential_referent.to_owned(),
timestamp: cred_info.timestamp,
},
);
}
}

// handle if the attribute is not revealed
let self_attested_attrs: Value = serde_json::to_value(self_attested_attrs).map_err(|err| {
AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidJson,
format!("Cannot deserialize self attested attributes: {}", err),
)
})?;
rtn["self_attested_attributes"] = self_attested_attrs;
rtn.self_attested_attributes = self_attested_attrs;

Ok(rtn.to_string())
Ok(rtn)
}

#[cfg(test)]
Expand Down Expand Up @@ -320,6 +319,7 @@ pub mod pool_tests {
#[cfg(test)]
pub mod unit_tests {
use aries_vcx_core::ledger::indy::pool::test_utils::get_temp_dir_path;
use serde_json::Value;
use test_utils::{
constants::{
address_cred_def_id, address_schema_id, cred_def_id, schema_id, ADDRESS_CRED_DEF_ID,
Expand Down Expand Up @@ -685,11 +685,11 @@ pub mod unit_tests {
let proof_req: PresentationRequest = serde_json::from_value(proof_req).unwrap();
let requested_credential = build_requested_credentials_json(
&creds,
&serde_json::from_str(&self_attested_attrs).unwrap(),
serde_json::from_str(&self_attested_attrs).unwrap(),
&proof_req,
)
.unwrap();
assert_eq!(test.to_string(), requested_credential);
assert_eq!(test, serde_json::to_value(requested_credential).unwrap());
}

#[tokio::test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl ProverSM {
ledger,
anoncreds,
&credentials,
&self_attested_attrs,
self_attested_attrs,
)
.await
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl PresentationRequestReceived {
ledger: &impl AnoncredsLedgerRead,
anoncreds: &impl BaseAnonCreds,
credentials: &SelectedCredentials,
self_attested_attrs: &HashMap<String, String>,
self_attested_attrs: HashMap<String, String>,
) -> VcxResult<Presentation> {
let proof_req_data_json = serde_json::from_str(&get_attach_as_string!(
&self
Expand Down
Loading

0 comments on commit 424189e

Please sign in to comment.