From f54861d01f1f0803743d2a631037a5db0e1ccc69 Mon Sep 17 00:00:00 2001 From: George Mulhearn <57472912+gmulhearn@users.noreply.github.com> Date: Thu, 8 Aug 2024 09:58:15 +1000 Subject: [PATCH] move some logic (#1280) Signed-off-by: George Mulhearn Co-authored-by: George Mulhearn --- .../src/controllers/did_exchange.rs | 47 +++++++++++++++++-- .../src/controllers/didcomm.rs | 23 ++++----- aries/agents/aath-backchannel/src/main.rs | 7 +-- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/aries/agents/aath-backchannel/src/controllers/did_exchange.rs b/aries/agents/aath-backchannel/src/controllers/did_exchange.rs index 218670a0f3..41337c19c8 100644 --- a/aries/agents/aath-backchannel/src/controllers/did_exchange.rs +++ b/aries/agents/aath-backchannel/src/controllers/did_exchange.rs @@ -60,8 +60,26 @@ impl HarnessAgent { .to_string()) } - pub fn queue_didexchange_request(&self, request: AnyRequest) -> HarnessResult<()> { - info!("queue_didexchange_request >> request: {:?}", request); + pub fn queue_didexchange_request( + &self, + request: AnyRequest, + recipient_verkey: String, + ) -> HarnessResult<()> { + info!( + "queue_didexchange_request >> request: {:?} for recipient {}", + request, recipient_verkey + ); + + let thid = request + .inner() + .decorators + .thread + .clone() + .ok_or(HarnessError::from_msg( + HarnessErrorType::InvalidState, + "DID Exchange request is missing a thread", + ))?; + let mut msg_buffer = self.didx_msg_buffer.write().map_err(|_| { HarnessError::from_msg( HarnessErrorType::InvalidState, @@ -70,6 +88,19 @@ impl HarnessAgent { })?; let m = AriesMessage::from(request); msg_buffer.push(m); + + let mut recipients = self + .didx_thid_to_request_recipient_verkey + .lock() + .map_err(|_| { + HarnessError::from_msg( + HarnessErrorType::InvalidState, + "Failed to lock DIDExchange ", + ) + })?; + + recipients.insert(thid.thid, recipient_verkey); + Ok(()) } @@ -178,9 +209,15 @@ impl HarnessAgent { let request_thread = &request.inner().decorators.thread; - let inviter_key = request_thread + let recipient_key = request_thread .as_ref() - .and_then(|th| self.inviter_keys.read().unwrap().get(&th.thid).cloned()) + .and_then(|th| { + self.didx_thid_to_request_recipient_verkey + .lock() + .unwrap() + .get(&th.thid) + .cloned() + }) .ok_or_else(|| { HarnessError::from_msg(HarnessErrorType::InvalidState, "Inviter key not found") })?; @@ -195,7 +232,7 @@ impl HarnessAgent { let (thid, pthid, my_did, their_did) = self .aries_agent .did_exchange() - .handle_msg_request(request, inviter_key, opt_invitation) + .handle_msg_request(request, recipient_key, opt_invitation) .await?; if let Some(pthid) = pthid { diff --git a/aries/agents/aath-backchannel/src/controllers/didcomm.rs b/aries/agents/aath-backchannel/src/controllers/didcomm.rs index fe6cac82c0..6ea4cd1856 100644 --- a/aries/agents/aath-backchannel/src/controllers/didcomm.rs +++ b/aries/agents/aath-backchannel/src/controllers/didcomm.rs @@ -166,13 +166,17 @@ impl HarnessAgent { Ok(()) } - async fn handle_did_exchange_msg(&self, msg: DidExchange) -> HarnessResult<()> { + async fn handle_did_exchange_msg( + &self, + msg: DidExchange, + recipient_verkey: String, + ) -> HarnessResult<()> { match msg { DidExchange::V1_0(DidExchangeV1_0::Request(request)) => { - self.queue_didexchange_request(AnyRequest::V1_0(request))?; + self.queue_didexchange_request(AnyRequest::V1_0(request), recipient_verkey)?; } DidExchange::V1_1(DidExchangeV1_1::Request(request)) => { - self.queue_didexchange_request(AnyRequest::V1_1(request))?; + self.queue_didexchange_request(AnyRequest::V1_1(request), recipient_verkey)?; } DidExchange::V1_0(DidExchangeV1_0::Response(response)) => { let res = self @@ -223,15 +227,6 @@ impl HarnessAgent { ) })?; - let connection_id = self - .aries_agent - .connections() - .get_by_sender_vk(sender_vk.clone())?; - self.inviter_keys - .write() - .unwrap() - .insert(connection_id.clone(), recipient_vk); - info!("Received message: {}", message); match message { AriesMessage::Notification(msg) => { @@ -261,7 +256,9 @@ impl HarnessAgent { let connection_id = self.aries_agent.connections().get_by_sender_vk(sender_vk)?; self.handle_issuance_msg(msg, &connection_id).await? } - AriesMessage::DidExchange(msg) => self.handle_did_exchange_msg(msg).await?, + AriesMessage::DidExchange(msg) => { + self.handle_did_exchange_msg(msg, recipient_vk).await? + } AriesMessage::PresentProof(msg) => { let connection_id = self.aries_agent.connections().get_by_sender_vk(sender_vk)?; self.handle_presentation_msg(msg, &connection_id).await? diff --git a/aries/agents/aath-backchannel/src/main.rs b/aries/agents/aath-backchannel/src/main.rs index 6bb4b52d57..d0fed19de4 100644 --- a/aries/agents/aath-backchannel/src/main.rs +++ b/aries/agents/aath-backchannel/src/main.rs @@ -75,7 +75,8 @@ pub struct HarnessAgent { // todo: extra didx specific AATH service didx_msg_buffer: RwLock>, didx_pthid_to_thid: Mutex>, - inviter_keys: RwLock>, + // A map of DIDExchange thread IDs to the intended recipient + didx_thid_to_request_recipient_verkey: Mutex>, } #[macro_export] @@ -121,8 +122,8 @@ async fn main() -> std::io::Result<()> { aries_agent: aries_agent.clone(), status: Status::Active, didx_msg_buffer: Default::default(), - didx_pthid_to_thid: Mutex::new(Default::default()), - inviter_keys: RwLock::new(HashMap::new()), + didx_pthid_to_thid: Default::default(), + didx_thid_to_request_recipient_verkey: Default::default(), }))) .app_data(web::Data::new(RwLock::new(Vec::::new()))) .service(