Skip to content

Commit

Permalink
any-wrapper approach
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
  • Loading branch information
gmulhearn-anonyome committed Jun 26, 2024
1 parent 5f5133d commit 6a054c6
Show file tree
Hide file tree
Showing 21 changed files with 320 additions and 231 deletions.
14 changes: 8 additions & 6 deletions aries/agents/aath-backchannel/src/controllers/did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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) => {
Expand Down
13 changes: 9 additions & 4 deletions aries/agents/aath-backchannel/src/controllers/didcomm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down Expand Up @@ -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
Expand Down
39 changes: 12 additions & 27 deletions aries/agents/aries-vcx-agent/src/handlers/did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -87,31 +87,16 @@ impl<T: BaseWallet> DidcommHandlerDidExchange<T> {
// /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();
Expand Down Expand Up @@ -139,13 +124,13 @@ impl<T: BaseWallet> DidcommHandlerDidExchange<T> {
// rather than being supplied by upper layers
pub async fn handle_msg_request(
&self,
request: Request,
request: AnyRequest,
invitation: Option<OobInvitation>,
) -> AgentResult<(String, Option<String>, 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(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -94,7 +96,7 @@ impl GenericDidExchange {
our_peer_did: &PeerDid<Numalgo4>,
our_label: String,
version: DidExchangeTypeV1,
) -> Result<(Self, Request), AriesVcxError> {
) -> Result<(Self, AnyRequest), AriesVcxError> {
let TransitionResult { state, output } =
DidExchangeRequester::<RequestSent>::construct_request(
resolver_registry,
Expand All @@ -114,7 +116,7 @@ impl GenericDidExchange {
pub async fn handle_request(
wallet: &impl BaseWallet,
resolver_registry: Arc<ResolverRegistry>,
request: Request,
request: AnyRequest,
our_peer_did: &PeerDid<Numalgo4>,
invitation_key: Option<Key>,
) -> Result<(Self, AnyResponse), AriesVcxError> {
Expand All @@ -137,7 +139,7 @@ impl GenericDidExchange {
self,
response: AnyResponse,
resolver_registry: Arc<ResolverRegistry>,
) -> Result<(Self, Complete), (Self, AriesVcxError)> {
) -> Result<(Self, AnyComplete), (Self, AriesVcxError)> {
match self {
GenericDidExchange::Requester(requester_state) => match requester_state {
RequesterState::RequestSent(request_sent_state) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,11 +38,10 @@ impl<I, S: ThreadId> DidExchange<I, S> {
self,
reason: String,
problem_code: Option<ProblemCode>,
) -> TransitionResult<DidExchange<I, Abandoned>, ProblemReport> {
) -> TransitionResult<DidExchange<I, Abandoned>, AnyProblemReport> {
let content = ProblemReportContent::builder()
.problem_code(problem_code)
.explain(Some(reason.clone()))
.version(DidExchangeTypeV1::new_v1_1())
.build();
let decorators = ProblemReportDecorators::builder()
.thread(
Expand All @@ -69,7 +67,7 @@ impl<I, S: ThreadId> DidExchange<I, S> {
our_did_document: self.our_did_document,
their_did_document: self.their_did_document,
},
output: problem_report,
output: AnyProblemReport::V1_1(problem_report),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand All @@ -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 {
Expand All @@ -44,21 +44,25 @@ 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(
// pthid inclusion is overkill in practice, but needed. see: https://github.com/hyperledger/aries-rfcs/issues/817
invitation_id: Option<String>,
request_id: String,
version: DidExchangeTypeV1,
) -> Complete {
) -> AnyComplete {
let thread = match invitation_id {
Some(invitation_id) => Thread::builder()
.thid(request_id)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -33,7 +32,7 @@ impl DidExchangeRequester<RequestSent> {
our_peer_did: &PeerDid<Numalgo4>,
our_label: String,
version: DidExchangeTypeV1,
) -> Result<TransitionResult<Self, Request>, AriesVcxError> {
) -> Result<TransitionResult<Self, AnyRequest>, AriesVcxError> {
debug!(
"DidExchangeRequester<RequestSent>::construct_request >> their_did: {}, our_peer_did: \
{}",
Expand All @@ -52,13 +51,13 @@ impl DidExchangeRequester<RequestSent> {
);

debug!(
"DidExchangeRequester<RequestSent>::construct_request << prepared request: {}",
"DidExchangeRequester<RequestSent>::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,
Expand All @@ -72,7 +71,7 @@ impl DidExchangeRequester<RequestSent> {
self,
response: AnyResponse,
resolver_registry: Arc<ResolverRegistry>,
) -> Result<TransitionResult<DidExchangeRequester<Completed>, Complete>, TransitionError<Self>>
) -> Result<TransitionResult<DidExchangeRequester<Completed>, AnyComplete>, TransitionError<Self>>
{
debug!(
"DidExchangeRequester<RequestSent>::receive_response >> response: {:?}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -29,7 +31,7 @@ impl DidExchangeResponder<ResponseSent> {
pub async fn receive_request(
wallet: &impl BaseWallet,
resolver_registry: Arc<ResolverRegistry>,
request: Request,
request: AnyRequest,
our_peer_did: &PeerDid<Numalgo4>,
invitation_key: Option<Key>,
) -> Result<TransitionResult<DidExchangeResponder<ResponseSent>, AnyResponse>, AriesVcxError>
Expand All @@ -40,6 +42,7 @@ impl DidExchangeResponder<ResponseSent> {
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()?;
Expand Down
4 changes: 2 additions & 2 deletions aries/aries_vcx/tests/test_did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
.await
.unwrap();
info!(
"Invitee processes invitation, builds up request {}",
"Invitee processes invitation, builds up request {:?}",
&request
);

Expand Down Expand Up @@ -168,7 +168,7 @@ async fn did_exchange_test() -> Result<(), Box<dyn Error>> {
.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(
Expand Down
Loading

0 comments on commit 6a054c6

Please sign in to comment.