Skip to content

Commit

Permalink
Inject only required components, not entire profiles
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jun 6, 2023
1 parent 9594a8e commit edbc042
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 103 deletions.
7 changes: 6 additions & 1 deletion agents/rust/aries-vcx-agent/src/services/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ impl ServiceVerifier {
});

verifier
.verify_presentation(&self.profile, presentation, send_closure)
.verify_presentation(
&self.profile.inject_anoncreds_ledger_read(),
&self.profile.inject_anoncreds(),
presentation,
send_closure,
)
.await?;
self.verifiers
.insert(thread_id, VerifierWrapper::new(verifier, &connection_id))?;
Expand Down
88 changes: 59 additions & 29 deletions aries_vcx/src/common/proofs/verifier/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
use aries_vcx_core::anoncreds::base_anoncreds::BaseAnonCreds;
use aries_vcx_core::ledger::base_ledger::AnoncredsLedgerRead;
use std::sync::Arc;

use crate::common::proofs::verifier::verifier_internal::{
build_cred_defs_json_verifier, build_rev_reg_defs_json, build_rev_reg_json, build_schemas_json_verifier,
get_credential_info, validate_proof_revealed_attributes,
};
use crate::core::profile::profile::Profile;
use crate::errors::error::prelude::*;
use crate::utils::mockdata::mock_settings::get_mock_result_for_validate_indy_proof;

pub async fn validate_indy_proof(
profile: &Arc<dyn Profile>,
ledger: &Arc<dyn AnoncredsLedgerRead>,
anoncreds: &Arc<dyn BaseAnonCreds>,
proof_json: &str,
proof_req_json: &str,
) -> VcxResult<bool> {
if let Some(mock_result) = get_mock_result_for_validate_indy_proof() {
return mock_result;
}

let anoncreds = Arc::clone(profile).inject_anoncreds();
validate_proof_revealed_attributes(proof_json)?;

let credential_data = get_credential_info(proof_json)?;

let credential_defs_json = build_cred_defs_json_verifier(profile, &credential_data)
let credential_defs_json = build_cred_defs_json_verifier(ledger, &credential_data)
.await
.unwrap_or(json!({}).to_string());
let schemas_json = build_schemas_json_verifier(profile, &credential_data)
let schemas_json = build_schemas_json_verifier(ledger, &credential_data)
.await
.unwrap_or(json!({}).to_string());
let rev_reg_defs_json = build_rev_reg_defs_json(profile, &credential_data)
let rev_reg_defs_json = build_rev_reg_defs_json(ledger, &credential_data)
.await
.unwrap_or(json!({}).to_string());
let rev_regs_json = build_rev_reg_json(profile, &credential_data)
let rev_regs_json = build_rev_reg_json(ledger, &credential_data)
.await
.unwrap_or(json!({}).to_string());

Expand Down Expand Up @@ -117,9 +117,14 @@ pub mod unit_tests {
.unwrap();

assert_eq!(
validate_indy_proof(&setup.profile, &prover_proof_json, &proof_req_json.to_string())
.await
.unwrap(),
validate_indy_proof(
&setup.profile.inject_indy_ledger_read(),
&setup.profile.inject_anoncreds(),
&prover_proof_json,
&proof_req_json
)
.await
.unwrap(),
true
);
})
Expand Down Expand Up @@ -194,19 +199,29 @@ pub mod unit_tests {
.await
.unwrap();
assert_eq!(
validate_indy_proof(&setup.profile, &prover_proof_json, &proof_req_json)
.await
.unwrap_err()
.kind(),
validate_indy_proof(
&setup.profile.inject_anoncreds_ledger_read(),
&setup.profile.inject_anoncreds(),
&prover_proof_json,
&proof_req_json
)
.await
.unwrap_err()
.kind(),
AriesVcxErrorKind::ProofRejected
);

let mut proof_req_json: serde_json::Value = serde_json::from_str(&proof_req_json).unwrap();
proof_req_json["requested_attributes"]["attribute_0"]["restrictions"] = json!({});
assert_eq!(
validate_indy_proof(&setup.profile, &prover_proof_json, &proof_req_json.to_string())
.await
.unwrap(),
validate_indy_proof(
&setup.profile.inject_anoncreds_ledger_read(),
&setup.profile.inject_anoncreds(),
&prover_proof_json,
&proof_req_json.to_string()
)
.await
.unwrap(),
true
);
})
Expand Down Expand Up @@ -282,9 +297,14 @@ pub mod unit_tests {
.await
.unwrap();
assert_eq!(
validate_indy_proof(&setup.profile, &prover_proof_json, &proof_req_json)
.await
.unwrap(),
validate_indy_proof(
&setup.profile.inject_anoncreds_ledger_read(),
&setup.profile.inject_anoncreds(),
&prover_proof_json,
&proof_req_json
)
.await
.unwrap(),
true
);

Expand All @@ -294,10 +314,15 @@ pub mod unit_tests {
let prover_proof_json = serde_json::to_string(&proof_obj).unwrap();

assert_eq!(
validate_indy_proof(&setup.profile, &prover_proof_json, &proof_req_json)
.await
.unwrap_err()
.kind(),
validate_indy_proof(
&setup.profile.inject_anoncreds_ledger_read(),
&setup.profile.inject_anoncreds(),
&prover_proof_json,
&proof_req_json
)
.await
.unwrap_err()
.kind(),
AriesVcxErrorKind::InvalidProof
);
}
Expand All @@ -307,10 +332,15 @@ pub mod unit_tests {
let prover_proof_json = serde_json::to_string(&proof_obj).unwrap();

assert_eq!(
validate_indy_proof(&setup.profile, &prover_proof_json, &proof_req_json)
.await
.unwrap_err()
.kind(),
validate_indy_proof(
&setup.profile.inject_anoncreds_ledger_read(),
&setup.profile.inject_anoncreds(),
&prover_proof_json,
&proof_req_json
)
.await
.unwrap_err()
.kind(),
AriesVcxErrorKind::InvalidProof
);
}
Expand Down
16 changes: 8 additions & 8 deletions aries_vcx/src/common/proofs/verifier/verifier_internal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use aries_vcx_core::errors::error::AriesVcxCoreErrorKind;
use aries_vcx_core::ledger::base_ledger::AnoncredsLedgerRead;
use serde_json;
use serde_json::Value;

Expand Down Expand Up @@ -97,11 +98,10 @@ pub fn validate_proof_revealed_attributes(proof_json: &str) -> VcxResult<()> {
}

pub async fn build_cred_defs_json_verifier(
profile: &Arc<dyn Profile>,
ledger: &Arc<dyn AnoncredsLedgerRead>,
credential_data: &[CredInfoVerifier],
) -> VcxResult<String> {
debug!("building credential_def_json for proof validation");
let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let mut credential_json = json!({});

for cred_info in credential_data.iter() {
Expand All @@ -124,12 +124,11 @@ pub async fn build_cred_defs_json_verifier(
}

pub async fn build_schemas_json_verifier(
profile: &Arc<dyn Profile>,
ledger: &Arc<dyn AnoncredsLedgerRead>,
credential_data: &[CredInfoVerifier],
) -> VcxResult<String> {
debug!("building schemas json for proof validation");

let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let mut schemas_json = json!({});

for cred_info in credential_data.iter() {
Expand All @@ -153,12 +152,11 @@ pub async fn build_schemas_json_verifier(
}

pub async fn build_rev_reg_defs_json(
profile: &Arc<dyn Profile>,
ledger: &Arc<dyn AnoncredsLedgerRead>,
credential_data: &[CredInfoVerifier],
) -> VcxResult<String> {
debug!("building rev_reg_def_json for proof validation");

let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let mut rev_reg_defs_json = json!({});

for cred_info in credential_data.iter() {
Expand All @@ -180,10 +178,12 @@ pub async fn build_rev_reg_defs_json(
Ok(rev_reg_defs_json.to_string())
}

pub async fn build_rev_reg_json(profile: &Arc<dyn Profile>, credential_data: &[CredInfoVerifier]) -> VcxResult<String> {
pub async fn build_rev_reg_json(
ledger: &Arc<dyn AnoncredsLedgerRead>,
credential_data: &[CredInfoVerifier],
) -> VcxResult<String> {
debug!("building rev_reg_json for proof validation");

let ledger = Arc::clone(profile).inject_anoncreds_ledger_read();
let mut rev_regs_json = json!({});

for cred_info in credential_data.iter() {
Expand Down
14 changes: 8 additions & 6 deletions aries_vcx/src/handlers/connection/mediated_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use serde_json::Value;
use agency_client::agency_client::AgencyClient;
use agency_client::api::downloaded_message::DownloadedMessage;
use agency_client::MessageStatusCode;
use aries_vcx_core::wallet::base_wallet::BaseWallet;
use url::Url;
use uuid::Uuid;

Expand Down Expand Up @@ -776,14 +777,13 @@ impl MediatedConnection {
.await
}

pub async fn send_message_closure(&self, profile: &Arc<dyn Profile>) -> VcxResult<SendClosure> {
pub async fn send_message_closure(&self, wallet: Arc<dyn BaseWallet>) -> VcxResult<SendClosure> {
trace!("send_message_closure >>>");
let did_doc = self.their_did_doc().ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::NotReady,
"Cannot send message: Remote Connection information is not set",
))?;
let sender_vk = self.pairwise_info().pw_vk.clone();
let wallet = profile.inject_wallet();
Ok(Box::new(move |message: AriesMessage| {
Box::pin(send_message(wallet, sender_vk.clone(), did_doc.clone(), message))
}))
Expand Down Expand Up @@ -819,13 +819,13 @@ impl MediatedConnection {
pub async fn send_generic_message(&self, profile: &Arc<dyn Profile>, message: &str) -> VcxResult<String> {
trace!("MediatedConnection::send_generic_message >>> message: {:?}", message);
let message = Self::build_basic_message(message);
let send_message = self.send_message_closure(profile).await?;
let send_message = self.send_message_closure(profile.inject_wallet()).await?;
send_message(message).await.map(|_| String::new())
}

pub async fn send_a2a_message(&self, profile: &Arc<dyn Profile>, message: &AriesMessage) -> VcxResult<String> {
trace!("MediatedConnection::send_a2a_message >>> message: {:?}", message);
let send_message = self.send_message_closure(profile).await?;
let send_message = self.send_message_closure(profile.inject_wallet()).await?;
send_message(message.clone()).await.map(|_| String::new())
}

Expand All @@ -835,7 +835,9 @@ impl MediatedConnection {
comment: Option<String>,
) -> VcxResult<TrustPingSender> {
let mut trust_ping = TrustPingSender::build(true, comment);
trust_ping.send_ping(self.send_message_closure(profile).await?).await?;
trust_ping
.send_ping(self.send_message_closure(profile.inject_wallet()).await?)
.await?;
Ok(trust_ping)
}

Expand All @@ -859,7 +861,7 @@ impl MediatedConnection {
));
}
};
let send_message = self.send_message_closure(profile).await?;
let send_message = self.send_message_closure(profile.inject_wallet()).await?;
send_message(build_handshake_reuse_msg(&oob).into()).await
}

Expand Down
4 changes: 2 additions & 2 deletions aries_vcx/src/handlers/issuance/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Holder {
notification: Revoke,
) -> VcxResult<()> {
if self.holder_sm.is_revokable(profile).await? {
let send_message = connection.send_message_closure(profile).await?;
let send_message = connection.send_message_closure(profile.inject_wallet()).await?;
// TODO: Store to remember notification was received along with details
RevocationNotificationReceiver::build(self.get_rev_reg_id()?, self.get_cred_rev_id(profile).await?)
.handle_revocation_notification(notification, send_message)
Expand Down Expand Up @@ -207,7 +207,7 @@ impl Holder {
if self.is_terminal_state() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(profile).await?;
let send_message = connection.send_message_closure(profile.inject_wallet()).await?;

let messages = connection.get_messages(agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/issuance/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl Issuer {
if self.is_terminal_state() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(profile).await?;
let send_message = connection.send_message_closure(profile.inject_wallet()).await?;
let messages = connection.get_messages(agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
self.step(profile, msg.into(), Some(send_message)).await?;
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/proof_presentation/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Prover {
if !self.progressable_by_message() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(profile).await?;
let send_message = connection.send_message_closure(profile.inject_wallet()).await?;

let messages = connection.get_messages(agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
Expand Down
Loading

0 comments on commit edbc042

Please sign in to comment.