Skip to content

Commit

Permalink
feat(mediator): simplify result passing
Browse files Browse the repository at this point in the history
  • Loading branch information
nain-F49FF806 committed Sep 21, 2023
1 parent 05b4342 commit 241be31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
32 changes: 21 additions & 11 deletions mediator/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::utils::prelude::*;
use crate::utils::structs::{UnpackMessage, VeriKey};
use aries_vcx::handlers::out_of_band::sender::OutOfBandSender;
use aries_vcx::messages::msg_fields::protocols::out_of_band::invitation::OobService;
use aries_vcx::utils::encryption_envelope::EncryptionEnvelope;
use aries_vcx_core::errors::error::AriesVcxCoreError;
use aries_vcx_core::wallet::base_wallet::BaseWallet;
use aries_vcx_core::wallet::indy::wallet::create_and_open_wallet;
Expand All @@ -14,7 +15,10 @@ use diddoc_legacy::aries::diddoc::AriesDidDoc;
use diddoc_legacy::aries::service::AriesService;
use messages::msg_fields::protocols::connection::request::Request;
use messages::msg_fields::protocols::connection::response::Response;
use messages::msg_fields::protocols::connection::Connection;

use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation;
use messages::AriesMessage;

use serde_json::Value;

Expand Down Expand Up @@ -113,37 +117,43 @@ where
// pub async fn pack_message(&self, message: AriesMessage, recipient_vk: VeriKey, sender_vk: VeriKey) -> Value {
// todo!()
// }
pub async fn response_for_connection_req(&self, request: Request) -> Result<(Response, AriesDidDoc), String> {
pub async fn handle_connection_req(&self, request: Request) -> Result<EncryptionEnvelope, String> {
if let Err(err) = request.content.connection.did_doc.validate() {
return Err(format!("Request DidDoc validation failed! {:?}", err));
}

let thread_id = request.decorators.thread.map(|t| t.thid).unwrap_or(request.id);
let did_doc = request.content.connection.did_doc;
let (did, vk) = self
.wallet
.create_and_store_my_did(None, None)
.await
.map_err(|e| e.to_string())?;
let old_vk = self
.service
.as_ref()
.unwrap()
.recipient_keys
.first()
.unwrap()
.to_owned();

let response: Response = utils::build_response_content(
&self.wallet_ref,
thread_id,
self.service
.as_ref()
.unwrap()
.recipient_keys
.first()
.unwrap()
.to_owned(),
old_vk.clone(),
did,
vk,
self.service.as_ref().unwrap().service_endpoint.clone(),
self.service.as_ref().unwrap().routing_keys.clone(),
)
.await
.map_err(|e| e.to_string())?;
Ok((response, did_doc))
// Ok(response)
let aries_response = AriesMessage::Connection(Connection::Response(response));
let their_diddoc = request.content.connection.did_doc;
let packed_response_envelope =
EncryptionEnvelope::create(&self.get_wallet_ref(), &aries_response, Some(&old_vk), &their_diddoc)
.await
.map_err(|e| e.to_string())?;
Ok(packed_response_envelope)
}
}
23 changes: 7 additions & 16 deletions mediator/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ pub fn unhandled_aries(message: impl Debug) -> String {
pub async fn handle_aries_connection<T: BaseWallet>(
agent: ArcAgent<T>,
connection: Connection,
) -> Result<(Connection, AriesDidDoc), String> {
) -> Result<EncryptionEnvelope, String> {
match connection {
Connection::Invitation(_invite) => return Err("Mediator does not handle random invites. Sorry.".to_owned()),
Connection::Request(register_request) => {
let (register_response, their_diddoc) = agent.response_for_connection_req(register_request).await?;
Ok((Connection::Response(register_response), their_diddoc))
}
_ => return Err(unhandled_aries(connection)),
Connection::Invitation(_invite) => Err("Mediator does not handle random invites. Sorry.".to_owned()),
Connection::Request(register_request) => agent.handle_connection_req(register_request).await,
_ => Err(unhandled_aries(connection)),
}
}
pub async fn handle_didcomm(
Expand All @@ -47,17 +44,11 @@ pub async fn handle_didcomm(
let aries_message: AriesMessage =
serde_json::from_str(&unpacked.message).expect("Decoding unpacked message as AriesMessage");

let (aries_response, their_diddoc) = match aries_message {
AriesMessage::Connection(conn) => {
let (conn_response, their_diddoc) = handle_aries_connection(agent.clone(), conn).await?;
(AriesMessage::Connection(conn_response), their_diddoc)
}
let packed_response = match aries_message {
AriesMessage::Connection(conn) => handle_aries_connection(agent.clone(), conn).await?,
_ => return Err(unhandled_aries(aries_message)),
};
let EncryptionEnvelope(packed_message_bytes) =
EncryptionEnvelope::create(&agent.get_wallet_ref(), &aries_response, Some(&my_key), &their_diddoc)
.await
.map_err(|e| e.to_string())?;
let EncryptionEnvelope(packed_message_bytes) = packed_response;
let packed_json = serde_json::from_slice(&packed_message_bytes[..]).unwrap();
Ok(Json(packed_json))
}
Expand Down

0 comments on commit 241be31

Please sign in to comment.