Skip to content

Commit

Permalink
move some logic (hyperledger#1280)
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
Co-authored-by: George Mulhearn <gmulhearn@anonyome.com>
  • Loading branch information
2 people authored and alberto-instnt committed Sep 10, 2024
1 parent a6e193e commit f54861d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
47 changes: 42 additions & 5 deletions aries/agents/aath-backchannel/src/controllers/did_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(())
}

Expand Down Expand Up @@ -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")
})?;
Expand All @@ -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 {
Expand Down
23 changes: 10 additions & 13 deletions aries/agents/aath-backchannel/src/controllers/didcomm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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?
Expand Down
7 changes: 4 additions & 3 deletions aries/agents/aath-backchannel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ pub struct HarnessAgent {
// todo: extra didx specific AATH service
didx_msg_buffer: RwLock<Vec<AriesMessage>>,
didx_pthid_to_thid: Mutex<HashMap<String, String>>,
inviter_keys: RwLock<HashMap<String, String>>,
// A map of DIDExchange thread IDs to the intended recipient
didx_thid_to_request_recipient_verkey: Mutex<HashMap<String, String>>,
}

#[macro_export]
Expand Down Expand Up @@ -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::<AriesMessage>::new())))
.service(
Expand Down

0 comments on commit f54861d

Please sign in to comment.