Skip to content

Commit

Permalink
trying tuple msg approach
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <[email protected]>
  • Loading branch information
gmulhearn-anonyome committed Oct 17, 2023
1 parent 792f9ed commit c709d5b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 141 deletions.
62 changes: 23 additions & 39 deletions aries_vcx/src/protocols/issuance_v2/state_machines/holder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,21 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<ProposalPrepared<T>> {
pub async fn with_proposal(
input_data: &T::CreateProposalInput,
preview: Option<CredentialPreviewV2>,
) -> VcxResult<Self> {
) -> VcxResult<(Self, ProposeCredentialV2)> {
let attachment_data = T::create_proposal_attachment_content(input_data).await?;
let attachments_format_and_data =
vec![(T::get_proposal_attachment_format(), attachment_data)];
let proposal =
create_proposal_message_from_attachments(attachments_format_and_data, preview, None);

Ok(HolderV2 {
let holder = HolderV2 {
thread_id: get_thread_id_or_message_id!(proposal),
state: ProposalPrepared {
proposal,
_marker: PhantomData,
},
})
}
};

/// Get the prepared proposal message which should be sent to the issuer.
pub fn get_proposal(&self) -> &ProposeCredentialV2 {
&self.state.proposal
Ok((holder, proposal))
}

/// Receive an incoming [OfferCredentialV2] message for this protocol. On success, the
Expand Down Expand Up @@ -193,7 +189,7 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<OfferReceived<T>> {
self,
input_data: &T::CreateProposalInput,
preview: Option<CredentialPreviewV2>,
) -> VcxSMTransitionResult<HolderV2<ProposalPrepared<T>>, Self> {
) -> VcxSMTransitionResult<(HolderV2<ProposalPrepared<T>>, ProposeCredentialV2), Self> {
let attachment_data = match T::create_proposal_attachment_content(input_data).await {
Ok(msg) => msg,
Err(error) => {
Expand All @@ -211,13 +207,14 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<OfferReceived<T>> {
Some(self.thread_id.clone()),
);

Ok(HolderV2 {
let holder = HolderV2 {
state: ProposalPrepared {
proposal,
_marker: PhantomData,
},
thread_id: self.thread_id,
})
};

Ok((holder, proposal))
}

/// Respond to an offer by preparing a request (to accept the offer). The request is prepared in
Expand All @@ -231,7 +228,7 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<OfferReceived<T>> {
pub async fn prepare_credential_request(
self,
input_data: &T::CreateRequestInput,
) -> VcxSMTransitionResult<HolderV2<RequestPrepared<T>>, Self> {
) -> VcxSMTransitionResult<(HolderV2<RequestPrepared<T>>, RequestCredentialV2), Self> {
let offer_message = &self.state.offer;

let (attachment_data, output_metadata) =
Expand All @@ -254,13 +251,14 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<OfferReceived<T>> {

let new_state = RequestPrepared {
request_preparation_metadata: output_metadata,
request,
};

Ok(HolderV2 {
let holder = HolderV2 {
state: new_state,
thread_id: self.thread_id,
})
};

Ok((holder, request))
}
}

Expand All @@ -278,7 +276,7 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<RequestPrepared<T>> {
/// support this.
pub async fn with_request(
input_data: &T::CreateRequestInput,
) -> VcxResult<HolderV2<RequestPrepared<T>>> {
) -> VcxResult<(HolderV2<RequestPrepared<T>>, RequestCredentialV2)> {
let (attachment_data, output_metadata) =
T::create_request_attachment_content_independent_of_offer(input_data).await?;

Expand All @@ -290,18 +288,14 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<RequestPrepared<T>> {

let new_state = RequestPrepared {
request_preparation_metadata: output_metadata,
request,
};

Ok(HolderV2 {
let holder = HolderV2 {
thread_id,
state: new_state,
})
}
};

/// Get the prepared request message which should be sent to the issuer.
pub fn get_request(&self) -> &RequestCredentialV2 {
&self.state.request
Ok((holder, request))
}

/// Receive a credential in response to a request message that was sent to the issuer.
Expand Down Expand Up @@ -359,7 +353,7 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<CredentialReceived<T>> {

// TODO - consider enum variants for (HolderV2<AckPrepared>, HoldverV2<Completed>)
/// Transition into the [Complete] state, by preparing an Ack message, only if required.
pub fn prepare_ack_if_required(self) -> HolderV2<Complete<T>> {
pub fn prepare_ack_if_required(self) -> (HolderV2<Complete<T>>, Option<AckCredentialV2>) {
let should_ack = self.state.credential.decorators.please_ack.is_some();

let ack = if should_ack {
Expand All @@ -377,22 +371,14 @@ impl<T: HolderCredentialIssuanceFormat> HolderV2<CredentialReceived<T>> {
} else {
None
};
HolderV2 {
let holder = HolderV2 {
state: Complete {
ack,
_marker: PhantomData,
},
thread_id: self.thread_id,
}
}
}
};

impl<T: HolderCredentialIssuanceFormat> HolderV2<Complete<T>> {
/// Get the prepared Ack message in response to the received credential, ready to be sent to the
/// issuer. If an acknowledgement is not required by the issuer, then [None] is returned and
/// nothing is required to be sent.
pub fn get_ack(&self) -> Option<&AckCredentialV2> {
self.state.ack.as_ref()
(holder, ack)
}
}

Expand Down Expand Up @@ -459,16 +445,14 @@ mod tests {
ctx2.expect()
.returning(|| MaybeKnown::Unknown(String::from("format")));

let holder =
let (_holder, proposal) =
HolderV2::<ProposalPrepared<MockHolderCredentialIssuanceFormat>>::with_proposal(
&String::from("in"),
None,
)
.await
.unwrap();

let proposal = holder.get_proposal();

let formats = proposal.content.formats.clone();
let attachments = proposal.content.filters_attach.clone();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use std::marker::PhantomData;

use messages::msg_fields::protocols::cred_issuance::v2::ack::AckCredentialV2;

use crate::protocols::issuance_v2::formats::holder::HolderCredentialIssuanceFormat;

pub struct Complete<T: HolderCredentialIssuanceFormat> {
pub(crate) ack: Option<AckCredentialV2>,
pub(crate) _marker: PhantomData<T>,
}

impl<T: HolderCredentialIssuanceFormat> Complete<T> {
pub fn new(ack: Option<AckCredentialV2>) -> Self {
pub fn new() -> Self {
Self {
ack,
_marker: PhantomData,
}
}

pub fn get_ack(&self) -> Option<&AckCredentialV2> {
self.ack.as_ref()
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use std::marker::PhantomData;

use messages::msg_fields::protocols::cred_issuance::v2::propose_credential::ProposeCredentialV2;

use crate::protocols::issuance_v2::formats::holder::HolderCredentialIssuanceFormat;

pub struct ProposalPrepared<T: HolderCredentialIssuanceFormat> {
pub(crate) proposal: ProposeCredentialV2,
pub(crate) _marker: PhantomData<T>,
}

impl<T: HolderCredentialIssuanceFormat> ProposalPrepared<T> {
pub fn new(proposal: ProposeCredentialV2) -> Self {
pub fn new() -> Self {
Self {
proposal,
_marker: PhantomData,
}
}

pub fn get_proposal(&self) -> &ProposeCredentialV2 {
&self.proposal
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
use messages::msg_fields::protocols::cred_issuance::v2::request_credential::RequestCredentialV2;

use crate::protocols::issuance_v2::formats::holder::HolderCredentialIssuanceFormat;

pub struct RequestPrepared<T: HolderCredentialIssuanceFormat> {
pub(crate) request: RequestCredentialV2,
pub(crate) request_preparation_metadata: T::CreatedRequestMetadata,
}

impl<T: HolderCredentialIssuanceFormat> RequestPrepared<T> {
pub fn new(
request: RequestCredentialV2,
request_preparation_metadata: T::CreatedRequestMetadata,
) -> Self {
pub fn new(request_preparation_metadata: T::CreatedRequestMetadata) -> Self {
Self {
request,
request_preparation_metadata,
}
}

pub fn get_request(&self) -> &RequestCredentialV2 {
&self.request
}

pub fn get_request_preparation_metadata(&self) -> &T::CreatedRequestMetadata {
&self.request_preparation_metadata
}
Expand Down
43 changes: 16 additions & 27 deletions aries_vcx/src/protocols/issuance_v2/state_machines/issuer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<T: IssuerCredentialIssuanceFormat> IssuerV2<ProposalReceived<T>> {
input_data: &T::CreateOfferInput,
preview: CredentialPreviewV2,
replacement_id: Option<String>,
) -> VcxSMTransitionResult<IssuerV2<OfferPrepared<T>>, Self> {
) -> VcxSMTransitionResult<(IssuerV2<OfferPrepared<T>>, OfferCredentialV2), Self> {
let (attachment_data, offer_metadata) =
match T::create_offer_attachment_content(input_data).await {
Ok(data) => data,
Expand All @@ -149,15 +149,14 @@ impl<T: IssuerCredentialIssuanceFormat> IssuerV2<ProposalReceived<T>> {
Some(self.thread_id.clone()),
);

let new_state = OfferPrepared {
offer_metadata,
offer,
};
let new_state = OfferPrepared { offer_metadata };

Ok(IssuerV2 {
let issuer = IssuerV2 {
state: new_state,
thread_id: self.thread_id,
})
};

Ok((issuer, offer))
}
}

Expand All @@ -171,7 +170,7 @@ impl<T: IssuerCredentialIssuanceFormat> IssuerV2<OfferPrepared<T>> {
input_data: &T::CreateOfferInput,
preview: CredentialPreviewV2,
replacement_id: Option<String>,
) -> VcxResult<Self> {
) -> VcxResult<(Self, OfferCredentialV2)> {
let (attachment_data, offer_metadata) =
T::create_offer_attachment_content(input_data).await?;

Expand All @@ -185,20 +184,14 @@ impl<T: IssuerCredentialIssuanceFormat> IssuerV2<OfferPrepared<T>> {

let thread_id = get_thread_id_or_message_id!(offer);

let new_state = OfferPrepared {
offer_metadata,
offer,
};
let new_state = OfferPrepared { offer_metadata };

Ok(IssuerV2 {
let issuer = IssuerV2 {
state: new_state,
thread_id,
})
}
};

/// Get the prepared offer message which should be sent to the holder.
pub fn get_offer(&self) -> &OfferCredentialV2 {
&self.state.offer
Ok((issuer, offer))
}

/// Receive an incoming [ProposeCredentialV2] message for this protocol. On success, the
Expand Down Expand Up @@ -324,7 +317,7 @@ impl<T: IssuerCredentialIssuanceFormat> IssuerV2<RequestReceived<T>> {
input_data: &T::CreateCredentialInput,
please_ack: Option<bool>, // defaults to false
replacement_id: Option<String>,
) -> VcxSMTransitionResult<IssuerV2<CredentialPrepared<T>>, Self> {
) -> VcxSMTransitionResult<(IssuerV2<CredentialPrepared<T>>, IssueCredentialV2), Self> {
let request = &self.state.request;

let res = match &self.state.from_offer_metadata {
Expand Down Expand Up @@ -360,23 +353,19 @@ impl<T: IssuerCredentialIssuanceFormat> IssuerV2<RequestReceived<T>> {
let new_state = CredentialPrepared {
from_offer_metadata: self.state.from_offer_metadata,
credential_metadata: cred_metadata,
credential,
please_ack,
};

Ok(IssuerV2 {
let issuer = IssuerV2 {
state: new_state,
thread_id: self.thread_id,
})
};

Ok((issuer, credential))
}
}

impl<T: IssuerCredentialIssuanceFormat> IssuerV2<CredentialPrepared<T>> {
/// Get the prepared credential message which should be sent to the holder.
pub fn get_credential(&self) -> &IssueCredentialV2 {
&self.state.credential
}

/// Get details about the credential that was prepared.
/// The details are specific to the [IssuerCredentialIssuanceFormat] being used.
pub fn get_credential_creation_metadata(&self) -> &T::CreatedCredentialMetadata {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
use messages::msg_fields::protocols::cred_issuance::v2::issue_credential::IssueCredentialV2;

use crate::protocols::issuance_v2::formats::issuer::IssuerCredentialIssuanceFormat;

pub struct CredentialPrepared<T: IssuerCredentialIssuanceFormat> {
pub(crate) from_offer_metadata: Option<T::CreatedOfferMetadata>,
pub(crate) credential_metadata: T::CreatedCredentialMetadata,
pub(crate) credential: IssueCredentialV2,
pub(crate) please_ack: bool,
}

impl<T: IssuerCredentialIssuanceFormat> CredentialPrepared<T> {
pub fn new(
from_offer_metadata: Option<T::CreatedOfferMetadata>,
credential_metadata: T::CreatedCredentialMetadata,
credential: IssueCredentialV2,
please_ack: bool,
) -> Self {
Self {
from_offer_metadata,
credential_metadata,
credential,
please_ack,
}
}
Expand All @@ -32,10 +27,6 @@ impl<T: IssuerCredentialIssuanceFormat> CredentialPrepared<T> {
&self.credential_metadata
}

pub fn get_credential(&self) -> &IssueCredentialV2 {
&self.credential
}

pub fn get_please_ack(&self) -> bool {
self.please_ack
}
Expand Down
Loading

0 comments on commit c709d5b

Please sign in to comment.