Skip to content

Commit

Permalink
Make IO for issuer offer-sending opt-in
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 Aug 20, 2023
1 parent 434f453 commit 88a7f2f
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 133 deletions.
10 changes: 3 additions & 7 deletions aries_vcx/src/handlers/issuance/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,9 @@ impl Issuer {
Ok(offer.into())
}

pub fn mark_credential_offer_msg_sent(&mut self) -> VcxResult<()> {
self.issuer_sm = self.issuer_sm.clone().mark_credential_offer_msg_sent()?;
Ok(())
}

#[deprecated]
pub async fn send_credential_offer(&mut self, send_message: SendClosure) -> VcxResult<()> {
self.issuer_sm = self.issuer_sm.clone().send_credential_offer(send_message).await?;
self.issuer_sm.clone().send_credential_offer(send_message).await?;
Ok(())
}

Expand All @@ -176,7 +172,7 @@ impl Issuer {
anoncreds: &Arc<dyn BaseAnonCreds>,
send_message: SendClosure,
) -> VcxResult<()> {
self.issuer_sm = self.issuer_sm.clone().send_credential(anoncreds, send_message).await?;
self.issuer_sm = self.issuer_sm.clone().build_credential(anoncreds, send_message).await?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/issuance/mediated_issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn issuer_find_messages_to_handle(
return Some((uid, message));
}
}
IssuerState::OfferSent => match &message {
IssuerState::OfferSet => match &message {
AriesMessage::CredentialIssuance(CredentialIssuance::RequestCredential(msg)) => {
if matches_opt_thread_id!(msg, sm.get_thread_id().unwrap().as_str()) {
return Some((uid, message));
Expand Down
1 change: 0 additions & 1 deletion aries_vcx/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl From<IssuerState> for u32 {
IssuerState::Initial => 0,
IssuerState::ProposalReceived => 1,
IssuerState::OfferSet => 2,
IssuerState::OfferSent => 3,
IssuerState::RequestReceived => 4,
IssuerState::CredentialSent => 5,
IssuerState::Finished => 6,
Expand Down
43 changes: 9 additions & 34 deletions aries_vcx/src/protocols/issuance/issuer/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::protocols::common::build_problem_report_msg;
use crate::protocols::issuance::issuer::states::credential_sent::CredentialSentState;
use crate::protocols::issuance::issuer::states::finished::FinishedState;
use crate::protocols::issuance::issuer::states::initial::InitialIssuerState;
use crate::protocols::issuance::issuer::states::offer_sent::OfferSentState;
use crate::protocols::issuance::issuer::states::offer_set::OfferSetState;
use crate::protocols::issuance::issuer::states::proposal_received::ProposalReceivedState;
use crate::protocols::issuance::issuer::states::requested_received::RequestReceivedState;
Expand All @@ -41,7 +40,6 @@ pub enum IssuerFullState {
Initial(InitialIssuerState),
OfferSet(OfferSetState),
ProposalReceived(ProposalReceivedState),
OfferSent(OfferSentState),
RequestReceived(RequestReceivedState),
CredentialSent(CredentialSentState),
Finished(FinishedState),
Expand All @@ -52,7 +50,6 @@ pub enum IssuerState {
Initial,
OfferSet,
ProposalReceived,
OfferSent,
RequestReceived,
CredentialSent,
Finished,
Expand All @@ -66,7 +63,6 @@ impl Display for IssuerFullState {
IssuerFullState::Initial(_) => f.write_str("Initial"),
IssuerFullState::OfferSet(_) => f.write_str("OfferSet"),
IssuerFullState::ProposalReceived(_) => f.write_str("ProposalReceived"),
IssuerFullState::OfferSent(_) => f.write_str("OfferSent"),
IssuerFullState::RequestReceived(_) => f.write_str("RequestReceived"),
IssuerFullState::CredentialSent(_) => f.write_str("CredentialSent"),
IssuerFullState::Finished(_) => f.write_str("Finished"),
Expand Down Expand Up @@ -201,7 +197,6 @@ impl IssuerSM {
Some(offer_info) => offer_info.rev_reg_id.clone(),
_ => None,
},
IssuerFullState::OfferSent(state) => state.rev_reg_id.clone(),
IssuerFullState::RequestReceived(state) => state.rev_reg_id.clone(),
IssuerFullState::CredentialSent(state) => {
state
Expand Down Expand Up @@ -262,7 +257,6 @@ impl IssuerSM {
IssuerFullState::Initial(_) => IssuerState::Initial,
IssuerFullState::ProposalReceived(_) => IssuerState::ProposalReceived,
IssuerFullState::OfferSet(_) => IssuerState::OfferSet,
IssuerFullState::OfferSent(_) => IssuerState::OfferSent,
IssuerFullState::RequestReceived(_) => IssuerState::RequestReceived,
IssuerFullState::CredentialSent(_) => IssuerState::CredentialSent,
IssuerFullState::Finished(ref status) => match status.status {
Expand Down Expand Up @@ -318,33 +312,13 @@ impl IssuerSM {
pub fn get_credential_offer_msg(&self) -> VcxResult<OfferCredential> {
match &self.state {
IssuerFullState::OfferSet(state) => Ok(state.offer.clone()),
IssuerFullState::OfferSent(state) => Ok(state.offer.clone()),
_ => Err(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
format!("Can not get_credential_offer in current state {}.", self.state),
)),
}
}

pub fn mark_credential_offer_msg_sent(self) -> VcxResult<Self> {
let Self {
state,
source_id,
thread_id,
} = self;
let state = match state {
IssuerFullState::OfferSet(state) => IssuerFullState::OfferSent(state.into()),
IssuerFullState::OfferSent(state) => IssuerFullState::OfferSent(state),
_ => {
return Err(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
format!("Can not mark_as_offer_sent in current state {}.", state),
))
}
};
Ok(Self::step(source_id, thread_id, state))
}

pub fn receive_proposal(self, proposal: ProposeCredential) -> VcxResult<Self> {
// verify_thread_id(
// &self.thread_id,
Expand All @@ -356,7 +330,7 @@ impl IssuerSM {
let state = IssuerFullState::ProposalReceived(ProposalReceivedState::new(proposal, None));
(state, thread_id)
}
IssuerFullState::OfferSent(_) => {
IssuerFullState::OfferSet(_) => {
let state = IssuerFullState::ProposalReceived(ProposalReceivedState::new(proposal, None));
(state, self.thread_id.clone())
}
Expand All @@ -372,17 +346,18 @@ impl IssuerSM {
})
}

pub async fn send_credential_offer(self, send_message: SendClosure) -> VcxResult<Self> {
Ok(match self.state {
#[deprecated]
pub async fn send_credential_offer(self, send_message: SendClosure) -> VcxResult<()> {
match self.state {
IssuerFullState::OfferSet(ref state_data) => {
let cred_offer_msg = state_data.offer.clone().into();
send_message(cred_offer_msg).await?;
self.mark_credential_offer_msg_sent()?
}
_ => {
return Err(AriesVcxError::from_msg(AriesVcxErrorKind::NotReady, "Invalid action"));
}
})
};
Ok(())
}

pub fn receive_request(self, request: RequestCredential) -> VcxResult<Self> {
Expand All @@ -391,7 +366,7 @@ impl IssuerSM {
// &CredentialIssuanceAction::CredentialRequest(request.clone()),
// )?;
let state = match self.state {
IssuerFullState::OfferSent(state_data) => IssuerFullState::RequestReceived((state_data, request).into()),
IssuerFullState::OfferSet(state_data) => IssuerFullState::RequestReceived((state_data, request).into()),
s => {
warn!("Unable to receive credential request in state {}", s);
s
Expand All @@ -400,7 +375,7 @@ impl IssuerSM {
Ok(Self { state, ..self })
}

pub async fn send_credential(
pub async fn build_credential(
self,
anoncreds: &Arc<dyn BaseAnonCreds>,
send_message: SendClosure,
Expand Down Expand Up @@ -461,7 +436,7 @@ impl IssuerSM {
// &CredentialIssuanceAction::ProblemReport(problem_report.clone()),
// )?;
let state = match self.state {
IssuerFullState::OfferSent(state_data) => IssuerFullState::Finished((state_data, problem_report).into()),
IssuerFullState::OfferSet(state_data) => IssuerFullState::Finished((state_data, problem_report).into()),
IssuerFullState::CredentialSent(state_data) => IssuerFullState::Finished((state_data).into()),
s => {
warn!("Unable to receive credential ack in state {}", s);
Expand Down
9 changes: 5 additions & 4 deletions aries_vcx/src/protocols/issuance/issuer/states/initial.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use messages::msg_fields::protocols::cred_issuance::offer_credential::OfferCredential;

use crate::{handlers::util::OfferInfo, protocols::issuance::issuer::states::offer_sent::OfferSentState};
use crate::{handlers::util::OfferInfo, protocols::issuance::issuer::states::offer_set::OfferSetState};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
pub struct InitialIssuerState {}

impl From<(OfferInfo, OfferCredential)> for OfferSentState {
impl From<(OfferInfo, OfferCredential)> for OfferSetState {
fn from((offer_info, offer): (OfferInfo, OfferCredential)) -> Self {
trace!("SM is now in OfferSent state");
OfferSentState {
OfferSetState {
offer,
cred_data: offer_info.credential_json,
credential_json: offer_info.credential_json,
cred_def_id: offer_info.cred_def_id,
rev_reg_id: offer_info.rev_reg_id,
tails_file: offer_info.tails_file,
}
Expand Down
1 change: 0 additions & 1 deletion aries_vcx/src/protocols/issuance/issuer/states/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub(super) mod credential_sent;
pub(super) mod finished;
pub(super) mod initial;
pub(super) mod offer_sent;
pub(super) mod offer_set;
pub(super) mod proposal_received;
pub(super) mod requested_received;
59 changes: 0 additions & 59 deletions aries_vcx/src/protocols/issuance/issuer/states/offer_sent.rs

This file was deleted.

45 changes: 41 additions & 4 deletions aries_vcx/src/protocols/issuance/issuer/states/offer_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use crate::protocols::issuance::issuer::states::offer_sent::OfferSentState;
use messages::msg_fields::protocols::cred_issuance::offer_credential::OfferCredential;
use messages::msg_fields::protocols::cred_issuance::request_credential::RequestCredential;
use messages::msg_fields::protocols::report_problem::ProblemReport;

use crate::handlers::util::Status;
use crate::protocols::issuance::issuer::state_machine::RevocationInfoV1;
use crate::protocols::issuance::issuer::states::finished::FinishedState;
use crate::protocols::issuance::issuer::states::requested_received::RequestReceivedState;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OfferSetState {
Expand Down Expand Up @@ -28,14 +34,45 @@ impl OfferSetState {
}
}

impl From<OfferSetState> for OfferSentState {
impl From<OfferSetState> for FinishedState {
fn from(state: OfferSetState) -> Self {
trace!("SM is now in OfferSent state");
OfferSentState {
trace!("SM is now in Finished state");
FinishedState {
cred_id: None,
revocation_info_v1: Some(RevocationInfoV1 {
cred_rev_id: None,
rev_reg_id: state.rev_reg_id,
tails_file: state.tails_file,
}),
status: Status::Undefined,
}
}
}

impl From<(OfferSetState, RequestCredential)> for RequestReceivedState {
fn from((state, request): (OfferSetState, RequestCredential)) -> Self {
trace!("SM is now in Request Received state");
RequestReceivedState {
offer: state.offer,
cred_data: state.credential_json,
rev_reg_id: state.rev_reg_id,
tails_file: state.tails_file,
request,
}
}
}

impl From<(OfferSetState, ProblemReport)> for FinishedState {
fn from((state, err): (OfferSetState, ProblemReport)) -> Self {
trace!("SM is now in Finished state");
FinishedState {
cred_id: None,
revocation_info_v1: Some(RevocationInfoV1 {
cred_rev_id: None,
rev_reg_id: state.rev_reg_id,
tails_file: state.tails_file,
}),
status: Status::Failed(err),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{handlers::util::OfferInfo, protocols::issuance::issuer::states::offer_sent::OfferSentState};
use crate::{handlers::util::OfferInfo, protocols::issuance::issuer::states::offer_set::OfferSetState};
use messages::msg_fields::protocols::cred_issuance::{
offer_credential::OfferCredential, propose_credential::ProposeCredential,
};
Expand All @@ -18,12 +18,13 @@ impl ProposalReceivedState {
}
}

impl From<(OfferCredential, OfferInfo)> for OfferSentState {
impl From<(OfferCredential, OfferInfo)> for OfferSetState {
fn from((offer, offer_info): (OfferCredential, OfferInfo)) -> Self {
trace!("SM is now in OfferSent state");
OfferSentState {
trace!("SM is now in OfferSet state");
OfferSetState {
offer,
cred_data: offer_info.credential_json,
credential_json: offer_info.credential_json,
cred_def_id: offer_info.cred_def_id,
rev_reg_id: offer_info.rev_reg_id,
tails_file: offer_info.tails_file,
}
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/tests/test_creds_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ mod tests {
let mut issuer =
accept_cred_proposal(&mut institution, &institution_to_consumer, rev_reg_id, Some(tails_dir)).await;
decline_offer(&mut consumer, &consumer_to_institution, &mut holder).await;
assert_eq!(IssuerState::OfferSent, issuer.get_state());
assert_eq!(IssuerState::OfferSet, issuer.get_state());
tokio::time::sleep(Duration::from_millis(1000)).await;
issuer_update_with_mediator(
&mut issuer,
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/tests/utils/devsetup_faber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Faber {
)
.await
.unwrap();
assert_eq!(IssuerState::OfferSent, self.issuer_credential.get_state());
assert_eq!(IssuerState::OfferSet, self.issuer_credential.get_state());
}

pub async fn send_credential(&mut self) {
Expand Down
Loading

0 comments on commit 88a7f2f

Please sign in to comment.