From 6a054c6c7fd5270fcdefe672455ef1616711957f Mon Sep 17 00:00:00 2001 From: George Mulhearn Date: Wed, 26 Jun 2024 11:48:20 +1000 Subject: [PATCH] any-wrapper approach Signed-off-by: George Mulhearn --- .../src/controllers/did_exchange.rs | 14 +++--- .../src/controllers/didcomm.rs | 13 ++++-- .../src/handlers/did_exchange.rs | 39 +++++----------- .../did_exchange/state_machine/generic/mod.rs | 14 +++--- .../did_exchange/state_machine/mod.rs | 8 ++-- .../state_machine/requester/helpers.rs | 28 +++++++---- .../requester/request_sent/mod.rs | 13 +++--- .../responder/response_sent/mod.rs | 7 ++- aries/aries_vcx/tests/test_did_exchange.rs | 4 +- aries/messages/src/misc/mod.rs | 26 ++++++++--- .../protocols/did_exchange/v1_0/complete.rs | 37 ++++++++------- .../protocols/did_exchange/v1_0/mod.rs | 20 ++------ .../did_exchange/v1_0/problem_report.rs | 34 ++++++++------ .../protocols/did_exchange/v1_0/request.rs | 39 ++++++++++------ .../protocols/did_exchange/v1_1/complete.rs | 37 ++++++++------- .../protocols/did_exchange/v1_1/mod.rs | 20 ++------ .../did_exchange/v1_1/problem_report.rs | 32 +++++++------ .../protocols/did_exchange/v1_1/request.rs | 34 ++++++++++---- .../protocols/did_exchange/v1_x/complete.rs | 46 +++++++++++++------ .../did_exchange/v1_x/problem_report.rs | 44 ++++++++++++------ .../protocols/did_exchange/v1_x/request.rs | 42 +++++++++++++---- 21 files changed, 320 insertions(+), 231 deletions(-) diff --git a/aries/agents/aath-backchannel/src/controllers/did_exchange.rs b/aries/agents/aath-backchannel/src/controllers/did_exchange.rs index 607c64e87e..501a40f320 100644 --- a/aries/agents/aath-backchannel/src/controllers/did_exchange.rs +++ b/aries/agents/aath-backchannel/src/controllers/did_exchange.rs @@ -5,7 +5,7 @@ use aries_vcx_agent::aries_vcx::{ did_parser_nom::Did, messages::{ msg_fields::protocols::did_exchange::{ - v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, v1_x::request::Request, DidExchange, + v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, v1_x::request::AnyRequest, DidExchange, }, AriesMessage, }, @@ -60,7 +60,7 @@ impl HarnessAgent { .to_string()) } - pub fn queue_didexchange_request(&self, request: Request) -> HarnessResult<()> { + pub fn queue_didexchange_request(&self, request: AnyRequest) -> HarnessResult<()> { info!("queue_didexchange_request >> request: {:?}", request); let mut msg_buffer = self.didx_msg_buffer.write().map_err(|_| { HarnessError::from_msg( @@ -162,9 +162,11 @@ impl HarnessAgent { })? }; let request = match request { - AriesMessage::DidExchange(DidExchange::V1_0(DidExchangeV1_0::Request(request))) - | AriesMessage::DidExchange(DidExchange::V1_1(DidExchangeV1_1::Request(request))) => { - request + AriesMessage::DidExchange(DidExchange::V1_0(DidExchangeV1_0::Request(r))) => { + AnyRequest::V1_0(r) + } + AriesMessage::DidExchange(DidExchange::V1_1(DidExchangeV1_1::Request(r))) => { + AnyRequest::V1_1(r) } _ => { return Err(HarnessError::from_msg( @@ -174,7 +176,7 @@ impl HarnessAgent { } }; - let request_thread = &request.decorators.thread; + let request_thread = &request.inner().decorators.thread; let opt_invitation = match request_thread.as_ref().and_then(|th| th.pthid.as_ref()) { Some(pthid) => { diff --git a/aries/agents/aath-backchannel/src/controllers/didcomm.rs b/aries/agents/aath-backchannel/src/controllers/didcomm.rs index 739722c6d9..ef8637f928 100644 --- a/aries/agents/aath-backchannel/src/controllers/didcomm.rs +++ b/aries/agents/aath-backchannel/src/controllers/didcomm.rs @@ -6,7 +6,10 @@ use aries_vcx_agent::aries_vcx::{ msg_fields::protocols::{ connection::Connection, cred_issuance::{v1::CredentialIssuanceV1, CredentialIssuance}, - did_exchange::{v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, DidExchange}, + did_exchange::{ + v1_0::DidExchangeV1_0, v1_1::DidExchangeV1_1, v1_x::request::AnyRequest, + DidExchange, + }, notification::Notification, present_proof::{v1::PresentProofV1, PresentProof}, }, @@ -197,9 +200,11 @@ impl HarnessAgent { async fn handle_did_exchange_msg(&self, msg: DidExchange) -> HarnessResult<()> { match msg { - DidExchange::V1_0(DidExchangeV1_0::Request(request)) - | DidExchange::V1_1(DidExchangeV1_1::Request(request)) => { - self.queue_didexchange_request(request)?; + DidExchange::V1_0(DidExchangeV1_0::Request(request)) => { + self.queue_didexchange_request(AnyRequest::V1_0(request))?; + } + DidExchange::V1_1(DidExchangeV1_1::Request(request)) => { + self.queue_didexchange_request(AnyRequest::V1_1(request))?; } DidExchange::V1_0(DidExchangeV1_0::Response(response)) => { let res = self diff --git a/aries/agents/aries-vcx-agent/src/handlers/did_exchange.rs b/aries/agents/aries-vcx-agent/src/handlers/did_exchange.rs index dffbe37a20..958f4bdd4c 100644 --- a/aries/agents/aries-vcx-agent/src/handlers/did_exchange.rs +++ b/aries/agents/aries-vcx-agent/src/handlers/did_exchange.rs @@ -6,7 +6,7 @@ use aries_vcx::{ messages::{ msg_fields::protocols::{ did_exchange::v1_x::{ - complete::Complete, problem_report::ProblemReport, request::Request, + complete::Complete, problem_report::ProblemReport, request::AnyRequest, response::AnyResponse, }, out_of_band::invitation::Invitation as OobInvitation, @@ -87,31 +87,16 @@ impl DidcommHandlerDidExchange { // /agent/command/did-exchange/{id} where {id} is actually {pthid}. // We should have internal strategy to manage threads ourselves, and build necessary // extensions/mappings/accommodations in AATH backchannel - warn!("send_request >>> request: {}", request); - let pthid = request - .clone() - .decorators - .thread - .ok_or_else(|| { - AgentError::from_msg( - AgentErrorKind::InvalidState, - "Request did not contain a thread", - ) - })? - .pthid; - + warn!("send_request >>> request: {:?}", request); + let req_thread = request.inner().decorators.thread.as_ref().ok_or_else(|| { + AgentError::from_msg( + AgentErrorKind::InvalidState, + "Request did not contain a thread", + ) + })?; + let pthid = req_thread.pthid.clone(); // todo: messages must provide easier way to access this without all the shenanigans - let thid = request - .clone() - .decorators - .thread - .ok_or_else(|| { - AgentError::from_msg( - AgentErrorKind::InvalidState, - "Request did not contain a thread id", - ) - })? - .thid; + let thid = req_thread.thid.clone(); let ddo_their = requester.their_did_doc(); let ddo_our = requester.our_did_document(); @@ -139,13 +124,13 @@ impl DidcommHandlerDidExchange { // rather than being supplied by upper layers pub async fn handle_msg_request( &self, - request: Request, + request: AnyRequest, invitation: Option, ) -> AgentResult<(String, Option, String, String)> { // todo: type the return type // Todo: messages should expose fallible API to get thid (for any aries msg). It's common // pattern - let thread = request.decorators.thread.as_ref(); + let thread = request.inner().decorators.thread.as_ref(); let thid = thread .ok_or_else(|| { diff --git a/aries/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs b/aries/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs index cf4301f006..08de2843fc 100644 --- a/aries/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs +++ b/aries/aries_vcx/src/protocols/did_exchange/state_machine/generic/mod.rs @@ -6,9 +6,11 @@ use did_parser_nom::Did; use did_peer::peer_did::{numalgos::numalgo4::Numalgo4, PeerDid}; use did_resolver_registry::ResolverRegistry; use messages::{ - msg_fields::protocols::did_exchange::{ - v1_1::request::Request, - v1_x::{complete::Complete, problem_report::ProblemReport, response::AnyResponse}, + msg_fields::protocols::did_exchange::v1_x::{ + complete::{AnyComplete, Complete}, + problem_report::ProblemReport, + request::AnyRequest, + response::AnyResponse, }, msg_types::protocols::did_exchange::DidExchangeTypeV1, }; @@ -94,7 +96,7 @@ impl GenericDidExchange { our_peer_did: &PeerDid, our_label: String, version: DidExchangeTypeV1, - ) -> Result<(Self, Request), AriesVcxError> { + ) -> Result<(Self, AnyRequest), AriesVcxError> { let TransitionResult { state, output } = DidExchangeRequester::::construct_request( resolver_registry, @@ -114,7 +116,7 @@ impl GenericDidExchange { pub async fn handle_request( wallet: &impl BaseWallet, resolver_registry: Arc, - request: Request, + request: AnyRequest, our_peer_did: &PeerDid, invitation_key: Option, ) -> Result<(Self, AnyResponse), AriesVcxError> { @@ -137,7 +139,7 @@ impl GenericDidExchange { self, response: AnyResponse, resolver_registry: Arc, - ) -> Result<(Self, Complete), (Self, AriesVcxError)> { + ) -> Result<(Self, AnyComplete), (Self, AriesVcxError)> { match self { GenericDidExchange::Requester(requester_state) => match requester_state { RequesterState::RequestSent(request_sent_state) => { diff --git a/aries/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs b/aries/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs index e4aee62f37..c54f331aff 100644 --- a/aries/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs +++ b/aries/aries_vcx/src/protocols/did_exchange/state_machine/mod.rs @@ -11,9 +11,8 @@ use did_doc::schema::did_doc::DidDocument; use messages::{ decorators::{thread::Thread, timing::Timing}, msg_fields::protocols::did_exchange::v1_x::problem_report::{ - ProblemCode, ProblemReport, ProblemReportContent, ProblemReportDecorators, + AnyProblemReport, ProblemCode, ProblemReport, ProblemReportContent, ProblemReportDecorators, }, - msg_types::protocols::did_exchange::DidExchangeTypeV1, }; use uuid::Uuid; @@ -39,11 +38,10 @@ impl DidExchange { self, reason: String, problem_code: Option, - ) -> TransitionResult, ProblemReport> { + ) -> TransitionResult, AnyProblemReport> { let content = ProblemReportContent::builder() .problem_code(problem_code) .explain(Some(reason.clone())) - .version(DidExchangeTypeV1::new_v1_1()) .build(); let decorators = ProblemReportDecorators::builder() .thread( @@ -69,7 +67,7 @@ impl DidExchange { our_did_document: self.our_did_document, their_did_document: self.their_did_document, }, - output: problem_report, + output: AnyProblemReport::V1_1(problem_report), } } diff --git a/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/helpers.rs b/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/helpers.rs index f985a40afc..cd00d4cf91 100644 --- a/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/helpers.rs +++ b/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/helpers.rs @@ -7,8 +7,8 @@ use messages::{ }, msg_fields::protocols::{ did_exchange::v1_x::{ - complete::{Complete, CompleteDecorators}, - request::{Request, RequestContent, RequestDecorators}, + complete::{AnyComplete, Complete, CompleteDecorators}, + request::{AnyRequest, Request, RequestContent, RequestDecorators}, }, out_of_band::invitation::{Invitation, OobService}, }, @@ -27,7 +27,7 @@ pub fn construct_request( our_did: String, our_label: String, version: DidExchangeTypeV1, -) -> Request { +) -> AnyRequest { let msg_id = Uuid::new_v4().to_string(); let thid = msg_id.clone(); let thread = match invitation_id { @@ -44,13 +44,17 @@ pub fn construct_request( .did_doc(None) .goal(Some("To establish a connection".into())) // Rejected if non-empty by acapy .goal_code(Some(MaybeKnown::Known(ThreadGoalCode::AriesRelBuild))) // Rejected if non-empty by acapy - .version(version) .build(); - Request::builder() + let req = Request::builder() .id(msg_id) .content(content) .decorators(decorators) - .build() + .build(); + + match version { + DidExchangeTypeV1::V1_1(_) => AnyRequest::V1_1(req), + DidExchangeTypeV1::V1_0(_) => AnyRequest::V1_0(req), + } } pub fn construct_didexchange_complete( @@ -58,7 +62,7 @@ pub fn construct_didexchange_complete( invitation_id: Option, request_id: String, version: DidExchangeTypeV1, -) -> Complete { +) -> AnyComplete { let thread = match invitation_id { Some(invitation_id) => Thread::builder() .thid(request_id) @@ -69,12 +73,16 @@ pub fn construct_didexchange_complete( let decorators = CompleteDecorators::builder() .thread(thread) .timing(Timing::builder().out_time(Utc::now()).build()) - .version(version) .build(); - Complete::builder() + let msg = Complete::builder() .id(Uuid::new_v4().to_string()) .decorators(decorators) - .build() + .build(); + + match version { + DidExchangeTypeV1::V1_1(_) => AnyComplete::V1_1(msg), + DidExchangeTypeV1::V1_0(_) => AnyComplete::V1_0(msg), + } } /// We are going to support only DID service values in did-exchange protocol unless there's explicit diff --git a/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs b/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs index 99bbffd484..30c351239d 100644 --- a/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs +++ b/aries/aries_vcx/src/protocols/did_exchange/state_machine/requester/request_sent/mod.rs @@ -5,9 +5,8 @@ use did_peer::peer_did::{numalgos::numalgo4::Numalgo4, PeerDid}; use did_resolver::traits::resolvable::resolution_output::DidResolutionOutput; use did_resolver_registry::ResolverRegistry; use messages::{ - msg_fields::protocols::did_exchange::{ - v1_1::request::Request, - v1_x::{complete::Complete, response::AnyResponse}, + msg_fields::protocols::did_exchange::v1_x::{ + complete::AnyComplete, request::AnyRequest, response::AnyResponse, }, msg_types::protocols::did_exchange::DidExchangeTypeV1, }; @@ -33,7 +32,7 @@ impl DidExchangeRequester { our_peer_did: &PeerDid, our_label: String, version: DidExchangeTypeV1, - ) -> Result, AriesVcxError> { + ) -> Result, AriesVcxError> { debug!( "DidExchangeRequester::construct_request >> their_did: {}, our_peer_did: \ {}", @@ -52,13 +51,13 @@ impl DidExchangeRequester { ); debug!( - "DidExchangeRequester::construct_request << prepared request: {}", + "DidExchangeRequester::construct_request << prepared request: {:?}", request ); Ok(TransitionResult { state: DidExchangeRequester::from_parts( RequestSent { - request_id: request.id.clone(), + request_id: request.inner().id.clone(), invitation_id, }, their_did_document, @@ -72,7 +71,7 @@ impl DidExchangeRequester { self, response: AnyResponse, resolver_registry: Arc, - ) -> Result, Complete>, TransitionError> + ) -> Result, AnyComplete>, TransitionError> { debug!( "DidExchangeRequester::receive_response >> response: {:?}", diff --git a/aries/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs b/aries/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs index 4fa2c20392..524eee4f77 100644 --- a/aries/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs +++ b/aries/aries_vcx/src/protocols/did_exchange/state_machine/responder/response_sent/mod.rs @@ -6,7 +6,9 @@ use did_peer::peer_did::{numalgos::numalgo4::Numalgo4, PeerDid}; use did_resolver_registry::ResolverRegistry; use messages::{ msg_fields::protocols::did_exchange::v1_x::{ - complete::Complete, request::Request, response::AnyResponse, + complete::Complete, + request::{AnyRequest, Request}, + response::AnyResponse, }, msg_types::protocols::did_exchange::DidExchangeTypeV1, }; @@ -29,7 +31,7 @@ impl DidExchangeResponder { pub async fn receive_request( wallet: &impl BaseWallet, resolver_registry: Arc, - request: Request, + request: AnyRequest, our_peer_did: &PeerDid, invitation_key: Option, ) -> Result, AnyResponse>, AriesVcxError> @@ -40,6 +42,7 @@ impl DidExchangeResponder { request, our_peer_did, invitation_key ); let version = request.get_version(); + let request = request.into_inner(); let their_ddo = resolve_ddo_from_request(&resolver_registry, &request).await?; let our_did_document = our_peer_did.resolve_did_doc()?; diff --git a/aries/aries_vcx/tests/test_did_exchange.rs b/aries/aries_vcx/tests/test_did_exchange.rs index 1af5f167ae..ad1b2ae096 100644 --- a/aries/aries_vcx/tests/test_did_exchange.rs +++ b/aries/aries_vcx/tests/test_did_exchange.rs @@ -135,7 +135,7 @@ async fn did_exchange_test() -> Result<(), Box> { .await .unwrap(); info!( - "Invitee processes invitation, builds up request {}", + "Invitee processes invitation, builds up request {:?}", &request ); @@ -168,7 +168,7 @@ async fn did_exchange_test() -> Result<(), Box> { .await .unwrap(); - let responder = responder.receive_complete(complete).unwrap(); + let responder = responder.receive_complete(complete.into_inner()).unwrap(); info!("Asserting did document of requester"); assert_key_agreement( diff --git a/aries/messages/src/misc/mod.rs b/aries/messages/src/misc/mod.rs index b264511e6f..ae768dd633 100644 --- a/aries/messages/src/misc/mod.rs +++ b/aries/messages/src/misc/mod.rs @@ -70,25 +70,37 @@ pub mod test_utils { assert_eq!(Protocol::from(protocol_type), deserialized) } - pub fn test_msg(content: T, decorators: U, msg_kind: V, mut expected: Value) + pub fn test_msg(content: T, decorators: U, msg_kind: V, expected: Value) where AriesMessage: From>, V: MessageKind, Protocol: From, { let id = "test".to_owned(); - let msg_type = build_msg_type(msg_kind); - - let obj = expected.as_object_mut().expect("JSON object"); - obj.insert("@id".to_owned(), json!(id)); - obj.insert("@type".to_owned(), json!(msg_type)); let msg = MsgParts::::builder() .id(id) .content(content) .decorators(decorators) .build(); - let msg = AriesMessage::from(msg); + + test_constructed_msg(msg, msg_kind, expected); + } + + pub fn test_constructed_msg(complete: M, msg_kind: V, mut expected: Value) + where + AriesMessage: From, + V: MessageKind, + Protocol: From, + { + let id = "test".to_owned(); + let msg_type = build_msg_type(msg_kind); + + let obj = expected.as_object_mut().expect("JSON object"); + obj.insert("@id".to_owned(), json!(id)); + obj.insert("@type".to_owned(), json!(msg_type)); + + let msg = AriesMessage::from(complete); test_serde(msg, expected); } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/complete.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/complete.rs index 1065fb8dcb..1cab9fbbec 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/complete.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/complete.rs @@ -6,7 +6,7 @@ use crate::{ /// Alias type for DIDExchange v1.0 Complete message. /// Note that since this inherits from the V1.X message, the direct serialization -/// of this message type is not recommended, as version metadata will be lost. +/// of this message type is not recommended, as it will be indistinguisable from V1.1. /// Instead, this type should be converted to/from an AriesMessage pub type Complete = MsgParts; @@ -23,7 +23,8 @@ mod tests { timing::tests::make_extended_timing, }, misc::test_utils, - msg_types::protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_0}, + msg_fields::protocols::did_exchange::v1_x::complete::AnyComplete, + msg_types::protocols::did_exchange::DidExchangeTypeV1_0, }; #[test] @@ -36,17 +37,17 @@ mod tests { } }); - let decorators = CompleteDecorators::builder() - .thread(thread) - .version(DidExchangeTypeV1::new_v1_0()) - .build(); + let decorators = CompleteDecorators::builder().thread(thread).build(); - test_utils::test_msg( - NoContent, - decorators, - DidExchangeTypeV1_0::Complete, - expected, + let msg = AnyComplete::V1_0( + Complete::builder() + .id("test".to_owned()) + .content(NoContent) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_0::Complete, expected); } #[test] @@ -54,7 +55,6 @@ mod tests { let decorators = CompleteDecorators { thread: make_extended_thread(), timing: Some(make_extended_timing()), - version: DidExchangeTypeV1::new_v1_0(), }; let expected = json!({ @@ -62,11 +62,14 @@ mod tests { "~timing": serde_json::to_value(make_extended_timing()).unwrap() }); - test_utils::test_msg( - NoContent, - decorators, - DidExchangeTypeV1_0::Complete, - expected, + let msg = AnyComplete::V1_0( + Complete::builder() + .id("test".to_owned()) + .content(NoContent) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_0::Complete, expected); } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/mod.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/mod.rs index 9b518b0009..37a5adde35 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/mod.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/mod.rs @@ -16,10 +16,7 @@ use super::{v1_x::response::ResponseDecorators, DidExchange}; use crate::{ misc::utils::{into_msg_with_type, transit_to_aries_msg}, msg_fields::traits::DelayedSerde, - msg_types::{ - protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_0}, - MsgKindType, MsgWithType, - }, + msg_types::{protocols::did_exchange::DidExchangeTypeV1_0, MsgKindType, MsgWithType}, }; #[derive(Clone, Debug, From, PartialEq)] @@ -44,21 +41,12 @@ impl DelayedSerde for DidExchangeV1_0 { let kind = protocol.kind_from_str(kind_str); match kind.map_err(D::Error::custom)? { - DidExchangeTypeV1_0::Request => Request::deserialize(deserializer).map(|mut c| { - c.content.version = DidExchangeTypeV1::new_v1_0(); - c.into() - }), + DidExchangeTypeV1_0::Request => Request::deserialize(deserializer).map(From::from), DidExchangeTypeV1_0::Response => Response::deserialize(deserializer).map(From::from), DidExchangeTypeV1_0::ProblemReport => { - ProblemReport::deserialize(deserializer).map(|mut c| { - c.content.version = DidExchangeTypeV1::new_v1_0(); - c.into() - }) + ProblemReport::deserialize(deserializer).map(From::from) } - DidExchangeTypeV1_0::Complete => Complete::deserialize(deserializer).map(|mut c| { - c.decorators.version = DidExchangeTypeV1::new_v1_0(); - c.into() - }), + DidExchangeTypeV1_0::Complete => Complete::deserialize(deserializer).map(From::from), } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/problem_report.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/problem_report.rs index db06040f23..6c8a3352cd 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/problem_report.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/problem_report.rs @@ -24,8 +24,10 @@ mod tests { thread::tests::make_extended_thread, timing::tests::make_extended_timing, }, misc::test_utils, - msg_fields::protocols::did_exchange::v1_x::problem_report::ProblemCode, - msg_types::protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_0}, + msg_fields::protocols::did_exchange::v1_x::problem_report::{ + AnyProblemReport, ProblemCode, + }, + msg_types::protocols::did_exchange::DidExchangeTypeV1_0, }; #[test] @@ -33,7 +35,6 @@ mod tests { let content = ProblemReportContent::builder() .problem_code(None) .explain(None) - .version(DidExchangeTypeV1::new_v1_0()) .build(); let decorators = ProblemReportDecorators::new(make_extended_thread()); @@ -42,12 +43,15 @@ mod tests { "~thread": decorators.thread }); - test_utils::test_msg( - content, - decorators, - DidExchangeTypeV1_0::ProblemReport, - expected, + let msg = AnyProblemReport::V1_0( + ProblemReport::builder() + .id("test".to_owned()) + .content(content) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_0::ProblemReport, expected); } #[test] @@ -55,7 +59,6 @@ mod tests { let content = ProblemReportContent::builder() .problem_code(Some(ProblemCode::RequestNotAccepted)) .explain(Some("test_conn_problem_report_explain".to_owned())) - .version(DidExchangeTypeV1::new_v1_0()) .build(); let mut decorators = ProblemReportDecorators::new(make_extended_thread()); @@ -70,11 +73,14 @@ mod tests { "~l10n": decorators.localization }); - test_utils::test_msg( - content, - decorators, - DidExchangeTypeV1_0::ProblemReport, - expected, + let msg = AnyProblemReport::V1_0( + ProblemReport::builder() + .id("test".to_owned()) + .content(content) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_0::ProblemReport, expected); } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/request.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/request.rs index 713ba45fd8..ed8a1eca0b 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/request.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_0/request.rs @@ -5,7 +5,7 @@ use crate::{ /// Alias type for DIDExchange v1.0 Request message. /// Note that since this inherits from the V1.X message, the direct serialization -/// of this Request is not recommended, as version metadata will be lost. +/// of this Request is not recommended, as it will be indistinguisable from Request V1.1. /// Instead, this type should be converted to/from an AriesMessage pub type Request = MsgParts; @@ -25,8 +25,11 @@ mod tests { timing::tests::make_extended_timing, }, misc::test_utils, - msg_fields::protocols::did_exchange::v1_0::request::{Request, RequestDecorators}, - msg_types::protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_0}, + msg_fields::protocols::did_exchange::{ + v1_0::request::{Request, RequestDecorators}, + v1_x::request::AnyRequest, + }, + msg_types::protocols::did_exchange::DidExchangeTypeV1_0, }; pub fn request_content() -> RequestContent { @@ -47,7 +50,6 @@ mod tests { ) .build(), ), - version: DidExchangeTypeV1::new_v1_0(), } } @@ -59,10 +61,7 @@ mod tests { .decorators(RequestDecorators::default()) .build(); let printed_json = format!("{}", msg); - let mut parsed_request: Request = serde_json::from_str(&printed_json).unwrap(); - // the serialized format of [Request] directly will not retain the version metadata, - // that information is the responsibility of higher up layers. - parsed_request.content.version = DidExchangeTypeV1::new_v1_0(); + let parsed_request: Request = serde_json::from_str(&printed_json).unwrap(); assert_eq!(msg, parsed_request); } @@ -76,12 +75,16 @@ mod tests { "did": content.did, "did_doc~attach": content.did_doc, }); - test_utils::test_msg( - content, - RequestDecorators::default(), - DidExchangeTypeV1_0::Request, - expected, + + let msg = AnyRequest::V1_0( + Request::builder() + .id("test".to_owned()) + .content(content) + .decorators(RequestDecorators::default()) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_0::Request, expected); } #[test] @@ -102,6 +105,14 @@ mod tests { "~timing": decorators.timing }); - test_utils::test_msg(content, decorators, DidExchangeTypeV1_0::Request, expected); + let msg = AnyRequest::V1_0( + Request::builder() + .id("test".to_owned()) + .content(content) + .decorators(decorators) + .build(), + ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_0::Request, expected); } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/complete.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/complete.rs index ddddff23e5..7b9e3db321 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/complete.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/complete.rs @@ -6,7 +6,7 @@ use crate::{ /// Alias type for DIDExchange v1.1 Complete message. /// Note that since this inherits from the V1.X message, the direct serialization -/// of this message type is not recommended, as version metadata will be lost. +/// of this message type is not recommended, as it will be indistinguisable from V1.0. /// Instead, this type should be converted to/from an AriesMessage pub type Complete = MsgParts; @@ -23,7 +23,8 @@ mod tests { timing::tests::make_extended_timing, }, misc::test_utils, - msg_types::protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_1}, + msg_fields::protocols::did_exchange::v1_x::complete::AnyComplete, + msg_types::protocols::did_exchange::DidExchangeTypeV1_1, }; #[test] @@ -36,17 +37,17 @@ mod tests { } }); - let decorators = CompleteDecorators::builder() - .thread(thread) - .version(DidExchangeTypeV1::new_v1_1()) - .build(); + let decorators = CompleteDecorators::builder().thread(thread).build(); - test_utils::test_msg( - NoContent, - decorators, - DidExchangeTypeV1_1::Complete, - expected, + let msg = AnyComplete::V1_1( + Complete::builder() + .id("test".to_owned()) + .content(NoContent) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_1::Complete, expected); } #[test] @@ -54,7 +55,6 @@ mod tests { let decorators = CompleteDecorators { thread: make_extended_thread(), timing: Some(make_extended_timing()), - version: DidExchangeTypeV1::new_v1_1(), }; let expected = json!({ @@ -62,11 +62,14 @@ mod tests { "~timing": serde_json::to_value(make_extended_timing()).unwrap() }); - test_utils::test_msg( - NoContent, - decorators, - DidExchangeTypeV1_1::Complete, - expected, + let msg = AnyComplete::V1_1( + Complete::builder() + .id("test".to_owned()) + .content(NoContent) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_1::Complete, expected); } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/mod.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/mod.rs index b7747ae096..698c7a8bba 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/mod.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/mod.rs @@ -16,10 +16,7 @@ use super::{v1_x::response::ResponseDecorators, DidExchange}; use crate::{ misc::utils::{into_msg_with_type, transit_to_aries_msg}, msg_fields::traits::DelayedSerde, - msg_types::{ - protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_1}, - MsgKindType, MsgWithType, - }, + msg_types::{protocols::did_exchange::DidExchangeTypeV1_1, MsgKindType, MsgWithType}, }; #[derive(Clone, Debug, From, PartialEq)] @@ -44,21 +41,12 @@ impl DelayedSerde for DidExchangeV1_1 { let kind = protocol.kind_from_str(kind_str); match kind.map_err(D::Error::custom)? { - DidExchangeTypeV1_1::Request => Request::deserialize(deserializer).map(|mut c| { - c.content.version = DidExchangeTypeV1::new_v1_1(); - c.into() - }), + DidExchangeTypeV1_1::Request => Request::deserialize(deserializer).map(From::from), DidExchangeTypeV1_1::Response => Response::deserialize(deserializer).map(From::from), DidExchangeTypeV1_1::ProblemReport => { - ProblemReport::deserialize(deserializer).map(|mut c| { - c.content.version = DidExchangeTypeV1::new_v1_1(); - c.into() - }) + ProblemReport::deserialize(deserializer).map(From::from) } - DidExchangeTypeV1_1::Complete => Complete::deserialize(deserializer).map(|mut c| { - c.decorators.version = DidExchangeTypeV1::new_v1_1(); - c.into() - }), + DidExchangeTypeV1_1::Complete => Complete::deserialize(deserializer).map(From::from), } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/problem_report.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/problem_report.rs index ec7625842c..86a998da92 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/problem_report.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/problem_report.rs @@ -25,9 +25,9 @@ mod tests { }, misc::test_utils, msg_fields::protocols::did_exchange::v1_x::problem_report::{ - ProblemCode, ProblemReportDecorators, + AnyProblemReport, ProblemCode, ProblemReportDecorators, }, - msg_types::protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_1}, + msg_types::protocols::did_exchange::DidExchangeTypeV1_1, }; #[test] @@ -35,7 +35,6 @@ mod tests { let content = ProblemReportContent::builder() .problem_code(None) .explain(None) - .version(DidExchangeTypeV1::new_v1_1()) .build(); let decorators = ProblemReportDecorators::new(make_extended_thread()); @@ -44,12 +43,15 @@ mod tests { "~thread": decorators.thread }); - test_utils::test_msg( - content, - decorators, - DidExchangeTypeV1_1::ProblemReport, - expected, + let msg = AnyProblemReport::V1_1( + ProblemReport::builder() + .id("test".to_owned()) + .content(content) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_1::ProblemReport, expected); } #[test] @@ -57,7 +59,6 @@ mod tests { let content = ProblemReportContent::builder() .problem_code(Some(ProblemCode::RequestNotAccepted)) .explain(Some("test_conn_problem_report_explain".to_owned())) - .version(DidExchangeTypeV1::new_v1_1()) .build(); let mut decorators = ProblemReportDecorators::new(make_extended_thread()); @@ -72,11 +73,14 @@ mod tests { "~l10n": decorators.localization }); - test_utils::test_msg( - content, - decorators, - DidExchangeTypeV1_1::ProblemReport, - expected, + let msg = AnyProblemReport::V1_1( + ProblemReport::builder() + .id("test".to_owned()) + .content(content) + .decorators(decorators) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_1::ProblemReport, expected); } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/request.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/request.rs index 9cf901690a..44456ea24e 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/request.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_1/request.rs @@ -5,7 +5,7 @@ use crate::{ /// Alias type for DIDExchange v1.1 request message. /// Note that since this inherits from the V1.X message, the direct serialization -/// of this message type is not recommended, as version metadata will be lost. +/// of this message type is not recommended, as it will be indistinguisable from Request V1.0. /// Instead, this type should be converted to/from an AriesMessage pub type Request = MsgParts; @@ -25,8 +25,11 @@ mod tests { timing::tests::make_extended_timing, }, misc::test_utils, - msg_fields::protocols::did_exchange::v1_1::request::{Request, RequestDecorators}, - msg_types::protocols::did_exchange::{DidExchangeTypeV1, DidExchangeTypeV1_1}, + msg_fields::protocols::did_exchange::{ + v1_1::request::{Request, RequestDecorators}, + v1_x::request::AnyRequest, + }, + msg_types::protocols::did_exchange::DidExchangeTypeV1_1, }; pub fn request_content() -> RequestContent { @@ -47,7 +50,6 @@ mod tests { ) .build(), ), - version: DidExchangeTypeV1::new_v1_1(), } } @@ -73,12 +75,16 @@ mod tests { "did": content.did, "did_doc~attach": content.did_doc, }); - test_utils::test_msg( - content, - RequestDecorators::default(), - DidExchangeTypeV1_1::Request, - expected, + + let msg = AnyRequest::V1_1( + Request::builder() + .id("test".to_owned()) + .content(content) + .decorators(RequestDecorators::default()) + .build(), ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_1::Request, expected); } #[test] @@ -99,6 +105,14 @@ mod tests { "~timing": decorators.timing }); - test_utils::test_msg(content, decorators, DidExchangeTypeV1_1::Request, expected); + let msg = AnyRequest::V1_1( + Request::builder() + .id("test".to_owned()) + .content(content) + .decorators(decorators) + .build(), + ); + + test_utils::test_constructed_msg(msg, DidExchangeTypeV1_1::Request, expected); } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/complete.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/complete.rs index dc223cada3..f225d817f1 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/complete.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/complete.rs @@ -14,7 +14,7 @@ use crate::{ /// Alias type for the shared DIDExchange v1.X complete message type. /// Note the direct serialization of this message type is not recommended, -/// as version metadata will be lost. +/// as it will be indistinguisable between V1.1 & V1.0. /// Instead, this type should be converted to/from an AriesMessage pub type Complete = MsgParts; @@ -27,25 +27,43 @@ pub struct CompleteDecorators { #[serde(rename = "~timing")] #[serde(skip_serializing_if = "Option::is_none")] pub timing: Option, - #[serde(skip, default = "DidExchangeTypeV1::new_v1_1")] - pub(crate) version: DidExchangeTypeV1, } -impl Complete { +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +#[serde(untagged)] +pub enum AnyComplete { + V1_0(Complete), + V1_1(Complete), +} + +impl AnyComplete { pub fn get_version(&self) -> DidExchangeTypeV1 { - self.decorators.version + match self { + AnyComplete::V1_0(_) => DidExchangeTypeV1::new_v1_0(), + AnyComplete::V1_1(_) => DidExchangeTypeV1::new_v1_1(), + } + } +} + +impl AnyComplete { + pub fn into_inner(self) -> Complete { + match self { + AnyComplete::V1_0(r) | AnyComplete::V1_1(r) => r, + } + } + + pub fn inner(&self) -> &Complete { + match self { + AnyComplete::V1_0(r) | AnyComplete::V1_1(r) => r, + } } } -impl From for AriesMessage { - fn from(value: Complete) -> Self { - match value.get_version() { - DidExchangeTypeV1::V1_0(_) => { - DidExchange::V1_0(DidExchangeV1_0::Complete(value)).into() - } - DidExchangeTypeV1::V1_1(_) => { - DidExchange::V1_1(DidExchangeV1_1::Complete(value)).into() - } +impl From for AriesMessage { + fn from(value: AnyComplete) -> Self { + match value { + AnyComplete::V1_0(inner) => DidExchange::V1_0(DidExchangeV1_0::Complete(inner)).into(), + AnyComplete::V1_1(inner) => DidExchange::V1_1(DidExchangeV1_1::Complete(inner)).into(), } } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/problem_report.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/problem_report.rs index b67e5dfa4c..1f78c6e021 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/problem_report.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/problem_report.rs @@ -24,8 +24,6 @@ pub struct ProblemReportContent { pub problem_code: Option, #[serde(skip_serializing_if = "Option::is_none")] pub explain: Option, - #[serde(skip, default = "DidExchangeTypeV1::new_v1_1")] - pub(crate) version: DidExchangeTypeV1, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -61,21 +59,41 @@ impl ProblemReportDecorators { } } -impl ProblemReport { +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +#[serde(untagged)] +pub enum AnyProblemReport { + V1_0(ProblemReport), + V1_1(ProblemReport), +} + +impl AnyProblemReport { pub fn get_version(&self) -> DidExchangeTypeV1 { - self.content.version + match self { + AnyProblemReport::V1_0(_) => DidExchangeTypeV1::new_v1_0(), + AnyProblemReport::V1_1(_) => DidExchangeTypeV1::new_v1_1(), + } + } +} + +impl AnyProblemReport { + pub fn into_inner(self) -> ProblemReport { + match self { + AnyProblemReport::V1_0(r) | AnyProblemReport::V1_1(r) => r, + } + } + + pub fn inner(&self) -> &ProblemReport { + match self { + AnyProblemReport::V1_0(r) | AnyProblemReport::V1_1(r) => r, + } } } -impl From for AriesMessage { - fn from(value: ProblemReport) -> Self { - match value.get_version() { - DidExchangeTypeV1::V1_0(_) => { - DidExchange::V1_0(DidExchangeV1_0::ProblemReport(value)).into() - } - DidExchangeTypeV1::V1_1(_) => { - DidExchange::V1_1(DidExchangeV1_1::ProblemReport(value)).into() - } +impl From for AriesMessage { + fn from(value: AnyProblemReport) -> Self { + match value { + AnyProblemReport::V1_0(inner) => DidExchange::V1_0(DidExchangeV1_0::ProblemReport(inner)).into(), + AnyProblemReport::V1_1(inner) => DidExchange::V1_1(DidExchangeV1_1::ProblemReport(inner)).into(), } } } diff --git a/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/request.rs b/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/request.rs index 4ac427c70e..1d2d8838bc 100644 --- a/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/request.rs +++ b/aries/messages/src/msg_fields/protocols/did_exchange/v1_x/request.rs @@ -18,7 +18,7 @@ use crate::{ /// Alias type for the shared DIDExchange v1.X request message type. /// Note the direct serialization of this message type is not recommended, -/// as version metadata will be lost. +/// as it will be indistinguisable between V1.1 & V1.0. /// Instead, this type should be converted to/from an AriesMessage pub type Request = MsgParts; @@ -32,8 +32,6 @@ pub struct RequestContent { pub did: String, // TODO: Use Did #[serde(rename = "did_doc~attach", skip_serializing_if = "Option::is_none")] pub did_doc: Option, - #[serde(skip, default = "DidExchangeTypeV1::new_v1_1")] - pub(crate) version: DidExchangeTypeV1, } #[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)] @@ -47,17 +45,41 @@ pub struct RequestDecorators { pub timing: Option, } -impl Request { +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +#[serde(untagged)] +pub enum AnyRequest { + V1_0(Request), + V1_1(Request), +} + +impl AnyRequest { pub fn get_version(&self) -> DidExchangeTypeV1 { - self.content.version + match self { + AnyRequest::V1_0(_) => DidExchangeTypeV1::new_v1_0(), + AnyRequest::V1_1(_) => DidExchangeTypeV1::new_v1_1(), + } + } +} + +impl AnyRequest { + pub fn into_inner(self) -> Request { + match self { + AnyRequest::V1_0(r) | AnyRequest::V1_1(r) => r, + } + } + + pub fn inner(&self) -> &Request { + match self { + AnyRequest::V1_0(r) | AnyRequest::V1_1(r) => r, + } } } -impl From for AriesMessage { - fn from(value: Request) -> Self { - match value.get_version() { - DidExchangeTypeV1::V1_0(_) => DidExchange::V1_0(DidExchangeV1_0::Request(value)).into(), - DidExchangeTypeV1::V1_1(_) => DidExchange::V1_1(DidExchangeV1_1::Request(value)).into(), +impl From for AriesMessage { + fn from(value: AnyRequest) -> Self { + match value { + AnyRequest::V1_0(inner) => DidExchange::V1_0(DidExchangeV1_0::Request(inner)).into(), + AnyRequest::V1_1(inner) => DidExchange::V1_1(DidExchangeV1_1::Request(inner)).into(), } } }