Skip to content

Commit

Permalink
Declutter issuance&presentation protocols (#945)
Browse files Browse the repository at this point in the history
Declutter issuance&presentation protocols (#945)

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas authored Aug 24, 2023
1 parent 068954a commit 72744e1
Show file tree
Hide file tree
Showing 31 changed files with 1,060 additions and 4,692 deletions.
204 changes: 25 additions & 179 deletions aries_vcx/src/handlers/issuance/holder.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use std::collections::HashMap;
use std::sync::Arc;

use aries_vcx_core::anoncreds::base_anoncreds::BaseAnonCreds;
use aries_vcx_core::ledger::base_ledger::AnoncredsLedgerRead;
use aries_vcx_core::wallet::base_wallet::BaseWallet;
use messages::msg_fields::protocols::cred_issuance::issue_credential::IssueCredential;
use messages::msg_fields::protocols::cred_issuance::offer_credential::OfferCredential;
use messages::msg_fields::protocols::cred_issuance::propose_credential::ProposeCredential;
use messages::msg_fields::protocols::cred_issuance::CredentialIssuance;
use messages::msg_fields::protocols::revocation::revoke::Revoke;
use messages::AriesMessage;
use std::sync::Arc;

use agency_client::agency_client::AgencyClient;
use aries_vcx_core::anoncreds::base_anoncreds::BaseAnonCreds;
use aries_vcx_core::ledger::base_ledger::AnoncredsLedgerRead;
use aries_vcx_core::wallet::base_wallet::BaseWallet;

use crate::common::credentials::get_cred_rev_id;
use crate::errors::error::prelude::*;
use crate::handlers::connection::mediated_connection::MediatedConnection;
use crate::handlers::revocation_notification::receiver::RevocationNotificationReceiver;
use crate::protocols::issuance::actions::CredentialIssuanceAction;
use crate::protocols::issuance::holder::state_machine::{HolderSM, HolderState};
use crate::protocols::SendClosure;

Expand Down Expand Up @@ -98,10 +95,6 @@ impl Holder {
self.holder_sm.is_terminal_state()
}

pub fn find_message_to_handle(&self, messages: HashMap<String, AriesMessage>) -> Option<(String, AriesMessage)> {
self.holder_sm.find_message_to_handle(messages)
}

pub fn get_state(&self) -> HolderState {
self.holder_sm.get_state()
}
Expand Down Expand Up @@ -193,178 +186,31 @@ impl Holder {
}
}

pub async fn step(
pub async fn process_aries_msg(
&mut self,
ledger: &Arc<dyn AnoncredsLedgerRead>,
anoncreds: &Arc<dyn BaseAnonCreds>,
message: CredentialIssuanceAction,
message: AriesMessage,
send_message: Option<SendClosure>,
) -> VcxResult<()> {
self.holder_sm = self
.holder_sm
.clone()
.handle_message(ledger, anoncreds, message, send_message)
.await?;
let holder_sm = match message {
AriesMessage::CredentialIssuance(CredentialIssuance::OfferCredential(offer)) => {
self.holder_sm.clone().receive_offer(offer)?
}
AriesMessage::CredentialIssuance(CredentialIssuance::IssueCredential(credential)) => {
let send_message = send_message.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
"Attempted to call undefined send_message callback",
))?;
self.holder_sm
.clone()
.receive_credential(ledger, anoncreds, credential, send_message)
.await?
}
AriesMessage::ReportProblem(report) => self.holder_sm.clone().receive_problem_report(report)?,
_ => self.holder_sm.clone(),
};
self.holder_sm = holder_sm;
Ok(())
}

pub async fn update_state(
&mut self,
ledger: &Arc<dyn AnoncredsLedgerRead>,
anoncreds: &Arc<dyn BaseAnonCreds>,
wallet: &Arc<dyn BaseWallet>,
agency_client: &AgencyClient,
connection: &MediatedConnection,
) -> VcxResult<HolderState> {
trace!("Holder::update_state >>>");
if self.is_terminal_state() {
return Ok(self.get_state());
}
let send_message = connection.send_message_closure(Arc::clone(wallet)).await?;

let messages = connection.get_messages(agency_client).await?;
if let Some((uid, msg)) = self.find_message_to_handle(messages) {
self.step(ledger, anoncreds, msg.into(), Some(send_message)).await?;
connection.update_message_status(&uid, agency_client).await?;
}
Ok(self.get_state())
}
}

pub mod test_utils {
use agency_client::agency_client::AgencyClient;
use messages::msg_fields::protocols::cred_issuance::CredentialIssuance;
use messages::AriesMessage;

use crate::errors::error::prelude::*;
use crate::handlers::connection::mediated_connection::MediatedConnection;

pub async fn get_credential_offer_messages(
agency_client: &AgencyClient,
connection: &MediatedConnection,
) -> VcxResult<String> {
let credential_offers: Vec<AriesMessage> = connection
.get_messages(agency_client)
.await?
.into_iter()
.filter_map(|(_, a2a_message)| match a2a_message {
AriesMessage::CredentialIssuance(CredentialIssuance::OfferCredential(_)) => Some(a2a_message),
_ => None,
})
.collect();

Ok(json!(credential_offers).to_string())
}
}

// #[cfg(test)]
// pub mod unit_tests {

// use crate::common::test_utils::mock_profile;
// use crate::utils::devsetup::SetupMocks;
// use messages::protocols::issuance::credential::test_utils::_credential;
// use messages::protocols::issuance::credential_offer::test_utils::_credential_offer;
// use messages::protocols::issuance::credential_proposal::test_utils::_credential_proposal_data;
// use messages::protocols::issuance::credential_request::test_utils::_my_pw_did;

// use super::*;

// pub fn _send_message() -> Option<SendClosure> {
// Some(Box::new(|_: A2AMessage| Box::pin(async { VcxResult::Ok(()) })))
// }

// fn _holder_from_offer() -> Holder {
// Holder::create_from_offer("test_source_id", _credential_offer()).unwrap()
// }

// fn _holder() -> Holder {
// Holder::create("test_source_id").unwrap()
// }

// impl Holder {
// async fn to_finished_state(mut self) -> Holder {
// self.step(
// &mock_profile(),
// CredentialIssuanceAction::CredentialProposalSend(_credential_proposal_data()),
// _send_message(),
// )
// .await
// .unwrap();
// self.step(
// &mock_profile(),
// CredentialIssuanceAction::CredentialOffer(_credential_offer()),
// _send_message(),
// )
// .await
// .unwrap();
// self.step(
// &mock_profile(),
// CredentialIssuanceAction::CredentialRequestSend(_my_pw_did()),
// _send_message(),
// )
// .await
// .unwrap();
// self.step(
// &mock_profile(),
// CredentialIssuanceAction::Credential(_credential()),
// _send_message(),
// )
// .await
// .unwrap();
// self
// }
// }

// #[tokio::test]
// async fn exchange_credential_from_proposal_without_negotiation() {
// let _setup = SetupMocks::init();
// let holder = _holder().to_finished_state().await;
// assert_eq!(HolderState::Finished, holder.get_state());
// }

// #[tokio::test]
// async fn exchange_credential_from_proposal_with_negotiation() {
// let _setup = SetupMocks::init();
// let mut holder = _holder();
// assert_eq!(HolderState::Initial, holder.get_state());

// holder
// .send_proposal(_credential_proposal_data(), _send_message().unwrap())
// .await
// .unwrap();
// assert_eq!(HolderState::ProposalSent, holder.get_state());

// let messages = map!(
// "key_1".to_string() => A2AMessage::CredentialOffer(_credential_offer())
// );
// let (_, msg) = holder.find_message_to_handle(messages).unwrap();
// holder.step(&mock_profile(), msg.into(), _send_message()).await.unwrap();
// assert_eq!(HolderState::OfferReceived, holder.get_state());

// holder
// .send_proposal(_credential_proposal_data(), _send_message().unwrap())
// .await
// .unwrap();
// assert_eq!(HolderState::ProposalSent, holder.get_state());

// let messages = map!(
// "key_1".to_string() => A2AMessage::CredentialOffer(_credential_offer())
// );
// let (_, msg) = holder.find_message_to_handle(messages).unwrap();
// holder.step(&mock_profile(), msg.into(), _send_message()).await.unwrap();
// assert_eq!(HolderState::OfferReceived, holder.get_state());

// holder
// .send_request(&mock_profile(), _my_pw_did(), _send_message().unwrap())
// .await
// .unwrap();
// assert_eq!(HolderState::RequestSent, holder.get_state());

// let messages = map!(
// "key_1".to_string() => A2AMessage::Credential(_credential())
// );
// let (_, msg) = holder.find_message_to_handle(messages).unwrap();
// holder.step(&mock_profile(), msg.into(), _send_message()).await.unwrap();
// assert_eq!(HolderState::Finished, holder.get_state());
// }
// }
Loading

0 comments on commit 72744e1

Please sign in to comment.