From f2867d9a82709a53d037b598961d2a57e78c03d0 Mon Sep 17 00:00:00 2001 From: nain-F49FF806 <126972030+nain-F49FF806@users.noreply.github.com> Date: Wed, 15 Nov 2023 13:44:58 +0100 Subject: [PATCH] Refactor/mediator/mediation (use integrated aries-vcx structs) (#1056) * Use coordinate_mediation messages from aries-vcx Signed-off-by: Naian <126972030+nain-F49FF806@users.noreply.github.com> --- Cargo.lock | 8 +- .../mediator/mediation/src/didcomm_types.rs | 117 ----------------- .../agents/rust/mediator/mediation/src/lib.rs | 1 - .../rust/mediator/mediation/src/router.rs | 9 +- .../mediation/src/routes/coordination.rs | 97 +++++++++----- .../mediator/mediation/src/routes/forward.rs | 44 ++++++- .../mediator/src/didcomm_handlers/forward.rs | 12 +- .../src/didcomm_handlers/mediator_coord.rs | 21 ++- .../rust/mediator/src/didcomm_handlers/mod.rs | 35 +++-- .../tests/common/agent_and_transport_utils.rs | 73 +++++++++-- .../mediator/tests/mediator-coord-protocol.rs | 124 ++++++++++++------ .../tests/mediator-protocol-pickup.rs | 57 +------- .../tests/mediator-routing-forward.rs | 43 +----- .../protocols/coordinate_mediation/keylist.rs | 2 +- 14 files changed, 300 insertions(+), 343 deletions(-) delete mode 100644 aries/agents/rust/mediator/mediation/src/didcomm_types.rs diff --git a/Cargo.lock b/Cargo.lock index 811857deca..65bf5bc492 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4690,18 +4690,18 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" [[package]] name = "thiserror" -version = "1.0.49" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.49" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", diff --git a/aries/agents/rust/mediator/mediation/src/didcomm_types.rs b/aries/agents/rust/mediator/mediation/src/didcomm_types.rs deleted file mode 100644 index 627bfb38d9..0000000000 --- a/aries/agents/rust/mediator/mediation/src/didcomm_types.rs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2023 Naian G. -// SPDX-License-Identifier: Apache-2.0 - -use serde::{Deserialize, Serialize}; -pub mod type_uri { - pub const FORWARD: &str = "https://didcomm.org/routing/1.0/forward"; -} - -#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] -pub struct ForwardMsg { - #[serde(rename = "@type")] - _type: String, - #[serde(rename = "to")] - pub recipient_key: String, - #[serde(rename = "msg")] - pub message_data: String, -} - -impl ForwardMsg { - pub fn new(recipient_key: &str, message: &str) -> ForwardMsg { - ForwardMsg { - _type: type_uri::FORWARD.to_string(), - recipient_key: recipient_key.to_string(), - message_data: message.to_string(), - } - } -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct LiveDeliveryChangeMsg { - pub live_delivery: bool, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct ProblemReportMsg { - pub description: String, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct MessageReceivedMsg { - pub message_id_list: Vec, -} - -pub mod mediator_coord_structs { - use serde::{Deserialize, Serialize}; - #[derive(Serialize, Deserialize, Debug)] - #[serde(tag = "@type")] - pub enum MediatorCoordMsgEnum { - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/mediate-request")] - MediateRequest, - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/mediate-deny")] - MediateDeny(MediateDenyData), - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/mediate-grant")] - MediateGrant(MediateGrantData), - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/keylist-update")] - KeylistUpdateRequest(KeylistUpdateRequestData), - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/keylist-update-response")] - KeylistUpdateResponse(KeylistUpdateResponseData), - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/keylist-query")] - KeylistQuery(KeylistQueryData), - #[serde(rename = "https://didcomm.org/coordinate-mediation/1.0/keylist")] - Keylist(KeylistData), - XumErrorMsg { - error: String, - }, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct MediateDenyData { - pub reason: String, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct MediateGrantData { - pub endpoint: String, - pub routing_keys: Vec, - } - - #[derive(Serialize, Deserialize, Debug)] - pub struct KeylistUpdateRequestData { - pub updates: Vec, - } - #[derive(Serialize, Deserialize, Debug)] - pub struct KeylistUpdateResponseData { - pub updated: Vec, - } - #[derive(Serialize, Deserialize, Debug)] - pub struct KeylistUpdateItem { - pub recipient_key: String, - pub action: KeylistUpdateItemAction, - pub result: Option, - } - #[derive(Serialize, Deserialize, Debug)] - pub enum KeylistUpdateItemAction { - #[serde(rename = "add")] - Add, - #[serde(rename = "remove")] - Remove, - } - #[derive(Serialize, Deserialize, Debug)] - pub enum KeylistUpdateItemResult { - ClientError, - ServerError, - NoChange, - Success, - } - #[derive(Serialize, Deserialize, Debug)] - pub struct KeylistQueryData {} - #[derive(Serialize, Deserialize, Debug)] - pub struct KeylistData { - pub keys: Vec, - } - #[derive(Serialize, Deserialize, Debug)] - pub struct KeylistItem { - pub recipient_key: String, - } -} diff --git a/aries/agents/rust/mediator/mediation/src/lib.rs b/aries/agents/rust/mediator/mediation/src/lib.rs index c3773a0ccc..26292da0ad 100644 --- a/aries/agents/rust/mediator/mediation/src/lib.rs +++ b/aries/agents/rust/mediator/mediation/src/lib.rs @@ -1,7 +1,6 @@ // Copyright 2023 Naian G. // SPDX-License-Identifier: Apache-2.0 -pub mod didcomm_types; pub mod logging; pub mod router; pub mod routes; diff --git a/aries/agents/rust/mediator/mediation/src/router.rs b/aries/agents/rust/mediator/mediation/src/router.rs index 1144a39040..5fdf58b322 100644 --- a/aries/agents/rust/mediator/mediation/src/router.rs +++ b/aries/agents/rust/mediator/mediation/src/router.rs @@ -3,13 +3,10 @@ // use crate::routes::coordination::handle_coord; use std::sync::Arc; -use axum::{ - routing::{get, post}, - Router, -}; +use axum::{routing::get, Router}; use crate::{ - routes::{forward::handle_forward, hello_world, json, json::respond_message_json}, + routes::{hello_world, json, json::respond_message_json}, storage, }; @@ -26,7 +23,7 @@ pub async fn create_router() -> Router { "/json", get(json::echo_message_json).post(respond_message_json), ) - .route("/forward", post(handle_forward)) + // .route("/forward", post(handle_forward)) // .route("/pickup", post(handle_pickup)) // .route("/coord", post(handle_coord)) .with_state(Arc::new(storage)) diff --git a/aries/agents/rust/mediator/mediation/src/routes/coordination.rs b/aries/agents/rust/mediator/mediation/src/routes/coordination.rs index 652d52fdad..0088211200 100644 --- a/aries/agents/rust/mediator/mediation/src/routes/coordination.rs +++ b/aries/agents/rust/mediator/mediation/src/routes/coordination.rs @@ -4,19 +4,26 @@ use std::sync::Arc; use axum::{extract::State, Json}; - -use crate::{ - didcomm_types::mediator_coord_structs::{MediatorCoordMsgEnum::*, *}, - storage::MediatorPersistence, +use messages::msg_fields::protocols::coordinate_mediation::{ + keylist::KeylistItem, + keylist_update::{KeylistUpdateItem, KeylistUpdateItemAction}, + keylist_update_response::{KeylistUpdateItemResult, KeylistUpdateResponseItem}, + CoordinateMediation, Keylist, KeylistContent, KeylistDecorators, KeylistQueryContent, + KeylistUpdateContent, KeylistUpdateResponse, KeylistUpdateResponseContent, + KeylistUpdateResponseDecorators, MediateDeny, MediateDenyContent, MediateDenyDecorators, + MediateGrant, MediateGrantContent, MediateGrantDecorators, }; +use uuid::Uuid; + +use crate::storage::MediatorPersistence; pub async fn handle_coord_authenticated( State(storage): State>, - Json(message): Json, + Json(message): Json, auth_pubkey: &str, -) -> Json { +) -> Json { match message { - MediateRequest => { + CoordinateMediation::MediateRequest(_mediate_request) => { panic!( "Use handle_mediate_request directly. This handler is for preregistered clients." ); @@ -32,20 +39,18 @@ pub async fn handle_coord_authenticated( // ) // .await } - KeylistUpdateRequest(keylist_update_data) => { - handle_keylist_update(storage, keylist_update_data, auth_pubkey).await + CoordinateMediation::KeylistUpdate(keylist_update) => { + handle_keylist_update(storage, keylist_update.content, auth_pubkey).await } - KeylistQuery(keylist_query_data) => { - handle_keylist_query(storage, keylist_query_data, auth_pubkey).await + CoordinateMediation::KeylistQuery(keylist_query) => { + handle_keylist_query(storage, keylist_query.content, auth_pubkey).await } _ => handle_unimplemented().await, } } -pub async fn handle_unimplemented() -> Json { - Json(MediatorCoordMsgEnum::XumErrorMsg { - error: "Unimplemented".to_owned(), - }) +pub async fn handle_unimplemented() -> Json { + todo!("This error should ideally be handled on outer layer. Panicking for now.") } pub async fn handle_mediate_request( @@ -53,42 +58,65 @@ pub async fn handle_mediate_request( auth_pubkey: &str, did_doc: &str, our_signing_key: &str, - grant_data: MediateGrantData, -) -> Json { + grant_content: MediateGrantContent, +) -> Json { match storage .create_account(auth_pubkey, our_signing_key, did_doc) .await { - Ok(()) => Json(MediateGrant(grant_data)), - Err(msg) => Json(MediateDeny(MediateDenyData { reason: msg })), + Ok(()) => { + let mediate_grant_msg = MediateGrant::builder() + .content(grant_content) + .decorators(MediateGrantDecorators::default()) + .id(Uuid::new_v4().to_string()) + .build(); + Json(CoordinateMediation::MediateGrant(mediate_grant_msg)) + } + Err(_msg) => { + let mediate_deny_msg = MediateDeny::builder() + .content(MediateDenyContent::default()) + .decorators(MediateDenyDecorators::default()) + .id(Uuid::new_v4().to_string()) + .build(); + Json(CoordinateMediation::MediateDeny(mediate_deny_msg)) + } } } pub async fn handle_keylist_query( storage: Arc, //todo: use the limits mentioned in the KeylistQueryData to modify response - _keylist_query_data: KeylistQueryData, + _keylist_query_data: KeylistQueryContent, auth_pubkey: &str, -) -> Json { +) -> Json { let keylist_items: Vec = match storage.list_recipient_keys(auth_pubkey).await { Ok(recipient_keys) => recipient_keys .into_iter() .map(|recipient_key| KeylistItem { recipient_key }) .collect(), - Err(err) => return Json(MediatorCoordMsgEnum::XumErrorMsg { error: err }), + Err(err) => todo!( + "This error should ideally be handled on outer layer. Panicking for now{}", + err + ), }; - Json(MediatorCoordMsgEnum::Keylist(KeylistData { - keys: keylist_items, - })) + let keylist = Keylist::builder() + .content(KeylistContent { + keys: keylist_items, + pagination: None, + }) + .decorators(KeylistDecorators::default()) + .id(Uuid::new_v4().to_string()) + .build(); + Json(CoordinateMediation::Keylist(keylist)) } pub async fn handle_keylist_update( storage: Arc, - keylist_update_data: KeylistUpdateRequestData, + keylist_update_data: KeylistUpdateContent, auth_pubkey: &str, -) -> Json { +) -> Json { let updates: Vec = keylist_update_data.updates; - let mut updated: Vec = Vec::new(); + let mut updated: Vec = Vec::new(); for update_item in updates.into_iter() { let result = match &update_item.action { KeylistUpdateItemAction::Add => { @@ -106,13 +134,18 @@ pub async fn handle_keylist_update( Ok(()) => KeylistUpdateItemResult::Success, Err(_msg) => KeylistUpdateItemResult::ServerError, }; - updated.push(KeylistUpdateItem { + updated.push(KeylistUpdateResponseItem { recipient_key: update_item.recipient_key, action: update_item.action, - result: Some(update_item_result), + result: update_item_result, }); } - Json(MediatorCoordMsgEnum::KeylistUpdateResponse( - KeylistUpdateResponseData { updated }, + let keylist_update_response = KeylistUpdateResponse::builder() + .content(KeylistUpdateResponseContent { updated }) + .decorators(KeylistUpdateResponseDecorators::default()) + .id(Uuid::new_v4().to_string()) + .build(); + Json(CoordinateMediation::KeylistUpdateResponse( + keylist_update_response, )) } diff --git a/aries/agents/rust/mediator/mediation/src/routes/forward.rs b/aries/agents/rust/mediator/mediation/src/routes/forward.rs index 98269a44f9..03c7b7a034 100644 --- a/aries/agents/rust/mediator/mediation/src/routes/forward.rs +++ b/aries/agents/rust/mediator/mediation/src/routes/forward.rs @@ -5,20 +5,50 @@ use std::sync::Arc; use axum::{extract::State, Json}; use log::{debug, info}; +use messages::{ + decorators::thread::Thread, + msg_fields::protocols::{ + notification::ack::{Ack, AckContent, AckDecorators, AckStatus}, + routing::Forward, + }, +}; +use uuid::Uuid; -use crate::{didcomm_types::ForwardMsg, storage::MediatorPersistence}; +use crate::storage::MediatorPersistence; pub async fn handle_forward( State(storage): State>, - Json(forward_msg): Json, -) -> Json + Json(forward_msg): Json, +) -> Json where T: MediatorPersistence, { info!("Persisting forward message"); debug!("{forward_msg:#?}"); - let _ = storage - .persist_forward_message(&forward_msg.recipient_key, &forward_msg.message_data) - .await; - Json(forward_msg) + let _ack_status = match storage + .persist_forward_message( + &forward_msg.content.to, + &serde_json::to_string(&forward_msg.content.msg).unwrap(), + ) + .await + { + Ok(_) => { + info!("Persisted forward"); + AckStatus::Ok + } + Err(e) => { + info!("Error when persisting forward: {}", e); + AckStatus::Pending + } + }; + let ack_content = AckContent::builder().status(AckStatus::Ok).build(); + let ack_deco = AckDecorators::builder() + .thread(Thread::builder().thid(forward_msg.id).build()) + .build(); + let ack = Ack::builder() + .content(ack_content) + .decorators(ack_deco) + .id(Uuid::new_v4().to_string()) + .build(); + Json(ack) } diff --git a/aries/agents/rust/mediator/src/didcomm_handlers/forward.rs b/aries/agents/rust/mediator/src/didcomm_handlers/forward.rs index c49a9b2553..42d7d50176 100644 --- a/aries/agents/rust/mediator/src/didcomm_handlers/forward.rs +++ b/aries/agents/rust/mediator/src/didcomm_handlers/forward.rs @@ -1,17 +1,15 @@ use axum::{extract::State, Json}; -use mediation::{didcomm_types::ForwardMsg, routes::forward::handle_forward}; -use messages::msg_fields::protocols::routing::Forward; +use mediation::routes::forward::handle_forward; +use messages::msg_fields::protocols::{notification::ack::Ack, routing::Forward}; use super::{utils::prelude::*, ArcAgent}; pub async fn handle_routing_forward( agent: ArcAgent, forward: Forward, -) -> Result<(), String> { +) -> Result { info!("{:?}", forward); - let forward_msg_content_str = serde_json::to_string(&forward.content.msg).unwrap(); - let forward_msg: ForwardMsg = ForwardMsg::new(&forward.content.to, &forward_msg_content_str); + let Json(ack) = handle_forward(State(agent.get_persistence_ref()), Json(forward)).await; - let _ = handle_forward(State(agent.get_persistence_ref()), Json(forward_msg)).await; - Ok(()) + Ok(ack) } diff --git a/aries/agents/rust/mediator/src/didcomm_handlers/mediator_coord.rs b/aries/agents/rust/mediator/src/didcomm_handlers/mediator_coord.rs index 0e7a929fea..ed7a722a9f 100644 --- a/aries/agents/rust/mediator/src/didcomm_handlers/mediator_coord.rs +++ b/aries/agents/rust/mediator/src/didcomm_handlers/mediator_coord.rs @@ -1,14 +1,17 @@ use axum::{extract::State, Json}; -use mediation::didcomm_types::mediator_coord_structs::{MediateGrantData, MediatorCoordMsgEnum}; +use messages::msg_fields::protocols::coordinate_mediation::{ + CoordinateMediation, MediateGrant, MediateGrantContent, MediateGrantDecorators, +}; +use uuid::Uuid; use super::utils::prelude::*; pub async fn handle_mediation_coord( agent: &ArcAgent, - coord_msg: MediatorCoordMsgEnum, + coord_msg: CoordinateMediation, auth_pubkey: &str, -) -> Result { - if let MediatorCoordMsgEnum::MediateRequest = coord_msg { +) -> Result { + if let CoordinateMediation::MediateRequest(_mediate_request) = coord_msg { let service = agent .get_service_ref() .ok_or("Mediation agent must have service defined.")?; @@ -21,10 +24,16 @@ pub async fn handle_mediation_coord( .expect("Service must have recipient key") .to_owned(), ); - let coord_response = MediatorCoordMsgEnum::MediateGrant(MediateGrantData { + let mediate_grant_content = MediateGrantContent { endpoint: service.service_endpoint.to_string(), routing_keys, - }); + }; + let mediate_grant = MediateGrant::builder() + .content(mediate_grant_content) + .decorators(MediateGrantDecorators::default()) + .id(Uuid::new_v4().to_string()) + .build(); + let coord_response = CoordinateMediation::MediateGrant(mediate_grant); return Ok(coord_response); }; let Json(coord_response) = mediation::routes::coordination::handle_coord_authenticated( diff --git a/aries/agents/rust/mediator/src/didcomm_handlers/mod.rs b/aries/agents/rust/mediator/src/didcomm_handlers/mod.rs index f0e60f0296..6e6092e7ce 100644 --- a/aries/agents/rust/mediator/src/didcomm_handlers/mod.rs +++ b/aries/agents/rust/mediator/src/didcomm_handlers/mod.rs @@ -21,7 +21,6 @@ use pickup::handle_pickup_protocol; #[serde(untagged)] enum GeneralAriesMessage { AriesVCXSupported(AriesMessage), - XumCoord(mediation::didcomm_types::mediator_coord_structs::MediatorCoordMsgEnum), } pub fn unhandled_aries_message(message: impl Debug) -> String { format!("Don't know how to handle this message type {:#?}", message) @@ -46,34 +45,32 @@ pub async fn handle_aries( handle_routing_forward(agent.clone(), forward).await?; return Ok(Json(json!({}))); } else { - // Auth known VerKey then process account related messages + // Authenticated flow: Auth known VerKey then process account related messages let (account_name, auth_pubkey, our_signing_key, their_diddoc) = agent.auth_and_get_details(&unpacked.sender_verkey).await?; log::info!("Processing message for {:?}", account_name); - match aries_message { + let aries_response = match aries_message { GeneralAriesMessage::AriesVCXSupported(AriesMessage::Pickup(pickup_message)) => { let pickup_response = handle_pickup_protocol(&agent, pickup_message, &auth_pubkey).await?; - let aries_response = AriesMessage::Pickup(pickup_response); - let aries_response_bytes = - serde_json::to_vec(&aries_response).map_err(string_from_std_error)?; - agent - .pack_didcomm(&aries_response_bytes, &our_signing_key, &their_diddoc) - .await? + AriesMessage::Pickup(pickup_response) } - GeneralAriesMessage::AriesVCXSupported(aries_message) => { - Err(unhandled_aries_message(aries_message))? - } - GeneralAriesMessage::XumCoord(coord_message) => { + GeneralAriesMessage::AriesVCXSupported(AriesMessage::CoordinateMediation( + coord_message, + )) => { let coord_response = handle_mediation_coord(&agent, coord_message, &auth_pubkey).await?; - let aries_response = - serde_json::to_vec(&coord_response).map_err(string_from_std_error)?; - agent - .pack_didcomm(&aries_response, &our_signing_key, &their_diddoc) - .await? + AriesMessage::CoordinateMediation(coord_response) + } + GeneralAriesMessage::AriesVCXSupported(aries_message) => { + Err(unhandled_aries_message(aries_message))? } - } + }; + let aries_response_bytes = + serde_json::to_vec(&aries_response).map_err(string_from_std_error)?; + agent + .pack_didcomm(&aries_response_bytes, &our_signing_key, &their_diddoc) + .await? }; let EncryptionEnvelope(packed_message_bytes) = packed_response; let packed_json = serde_json::from_slice(&packed_message_bytes[..]).unwrap(); diff --git a/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs b/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs index affdae211a..db163117de 100644 --- a/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs +++ b/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs @@ -6,18 +6,26 @@ use aries_vcx::{ }; use aries_vcx_core::wallet::base_wallet::BaseWallet; use diddoc_legacy::aries::diddoc::AriesDidDoc; -use mediation::{ - didcomm_types::mediator_coord_structs::{MediateGrantData, MediatorCoordMsgEnum}, - storage::MediatorPersistence, -}; +use mediation::storage::MediatorPersistence; use mediator::{ aries_agent::{ client::transports::{AriesReqwest, AriesTransport}, + utils::oob2did, Agent, }, utils::{structs::VerKey, GenericStringError}, }; -use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation; +use messages::{ + msg_fields::protocols::{ + coordinate_mediation::{ + keylist_update::{KeylistUpdateItem, KeylistUpdateItemAction}, + CoordinateMediation, KeylistUpdate, KeylistUpdateContent, MediateGrantContent, + MediateRequest, MediateRequestContent, + }, + out_of_band::invitation::Invitation as OOBInvitation, + }, + AriesMessage, +}; use reqwest::header::ACCEPT; use super::prelude::*; @@ -91,15 +99,62 @@ pub async fn send_message_and_pop_response_message( .unwrap(); Ok(unpacked_response.message) } +/// Register recipient keys with mediator +pub async fn gen_and_register_recipient_key( + agent: &mut Agent, + agent_aries_transport: &mut impl AriesTransport, + agent_verkey: &VerKey, + mediator_diddoc: &AriesDidDoc, +) -> Result<(VerKey, AriesDidDoc)> { + let agent_invite: OOBInvitation = agent + .get_oob_invite() + .map_err(|e| GenericStringError { msg: e.to_string() })?; + let agent_diddoc = oob2did(agent_invite); + let agent_recipient_key = agent_diddoc + .recipient_keys() + .unwrap() + .first() + .unwrap() + .clone(); + // register recipient key with mediator + let key_update = KeylistUpdate::builder() + .content( + KeylistUpdateContent::builder() + .updates(vec![KeylistUpdateItem { + recipient_key: agent_recipient_key.clone(), + action: KeylistUpdateItemAction::Add, + }]) + .build(), + ) + .id("register-key-with-mediator".to_owned()) + .build(); + let message = AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdate(key_update)); + info!("Sending {:?}", serde_json::to_string(&message).unwrap()); + let message_bytes = serde_json::to_vec(&message)?; + let _response_message = send_message_and_pop_response_message( + &message_bytes, + agent, + agent_aries_transport, + agent_verkey, + mediator_diddoc, + ) + .await?; + Ok((agent_recipient_key, agent_diddoc)) +} pub async fn get_mediator_grant_data( agent: &Agent, agent_aries_transport: &mut impl AriesTransport, agent_verkey: &VerKey, mediator_diddoc: &AriesDidDoc, -) -> MediateGrantData { +) -> MediateGrantContent { // prepare request message - let message = MediatorCoordMsgEnum::MediateRequest; + let message = AriesMessage::CoordinateMediation(CoordinateMediation::MediateRequest( + MediateRequest::builder() + .content(MediateRequestContent::default()) + .id("mediate-requets".to_owned()) + .build(), + )); let message_bytes = serde_json::to_vec(&message).unwrap(); // send message and get response let response_message = send_message_and_pop_response_message( @@ -112,11 +167,11 @@ pub async fn get_mediator_grant_data( .await .unwrap(); // extract routing parameters - if let MediatorCoordMsgEnum::MediateGrant(grant_data) = + if let AriesMessage::CoordinateMediation(CoordinateMediation::MediateGrant(grant_data)) = serde_json::from_str(&response_message).unwrap() { info!("Grant Data {:?}", grant_data); - grant_data + grant_data.content } else { panic!( "Should get response that is of type Mediator Grant. Found {:?}", diff --git a/aries/agents/rust/mediator/tests/mediator-coord-protocol.rs b/aries/agents/rust/mediator/tests/mediator-coord-protocol.rs index 868aad9941..062b36df37 100644 --- a/aries/agents/rust/mediator/tests/mediator-coord-protocol.rs +++ b/aries/agents/rust/mediator/tests/mediator-coord-protocol.rs @@ -1,9 +1,13 @@ mod common; use aries_vcx_core::wallet::base_wallet::BaseWallet; -use mediation::didcomm_types::mediator_coord_structs::{ - KeylistData, KeylistQueryData, KeylistUpdateItem, KeylistUpdateItemAction, - KeylistUpdateRequestData, MediatorCoordMsgEnum, +use messages::{ + msg_fields::protocols::coordinate_mediation::{ + keylist_update::{KeylistUpdateItem, KeylistUpdateItemAction}, + CoordinateMediation, KeylistQuery, KeylistQueryContent, KeylistUpdate, + KeylistUpdateContent, MediateRequest, MediateRequestContent, + }, + AriesMessage, }; use crate::common::{ @@ -23,8 +27,13 @@ async fn test_mediate_grant() -> Result<()> { let (agent, mut aries_transport, our_verkey, their_diddoc) = gen_mediator_connected_agent().await?; // prepare request message - let message = MediatorCoordMsgEnum::MediateRequest; - let message_bytes = serde_json::to_vec(&message)?; + let mediate_request = CoordinateMediation::MediateRequest( + MediateRequest::builder() + .content(MediateRequestContent::default()) + .id("mediate-request-test".to_owned()) + .build(), + ); + let message_bytes = serde_json::to_vec(&AriesMessage::CoordinateMediation(mediate_request))?; // send message and get response let response_message = send_message_and_pop_response_message( &message_bytes, @@ -35,11 +44,11 @@ async fn test_mediate_grant() -> Result<()> { ) .await?; // verify response - if let MediatorCoordMsgEnum::MediateGrant(grant_data) = + if let AriesMessage::CoordinateMediation(CoordinateMediation::MediateGrant(grant_data)) = serde_json::from_str(&response_message).unwrap() { info!("Grant Data {:?}", grant_data); - } else if let MediatorCoordMsgEnum::MediateDeny(deny_data) = + } else if let AriesMessage::CoordinateMediation(CoordinateMediation::MediateDeny(deny_data)) = serde_json::from_str(&response_message).unwrap() { info!("Deny Data {:?}", deny_data); @@ -64,13 +73,19 @@ async fn test_mediate_keylist_update_add() -> Result<()> { .get_wallet_ref() .create_and_store_my_did(None, None) .await?; - let message = MediatorCoordMsgEnum::KeylistUpdateRequest(KeylistUpdateRequestData { - updates: vec![KeylistUpdateItem { - recipient_key: new_vk, - action: KeylistUpdateItemAction::Add, - result: None, - }], - }); + let keylist_update_request = KeylistUpdate::builder() + .content(KeylistUpdateContent { + updates: vec![KeylistUpdateItem { + recipient_key: new_vk, + action: KeylistUpdateItemAction::Add, + }], + }) + .id("key-add".to_owned()) + .build(); + + let message = AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdate( + keylist_update_request, + )); info!("Sending {:?}", serde_json::to_string(&message).unwrap()); let message_bytes = serde_json::to_vec(&message)?; // send message and get response @@ -83,8 +98,9 @@ async fn test_mediate_keylist_update_add() -> Result<()> { ) .await?; // verify response - if let MediatorCoordMsgEnum::KeylistUpdateResponse(update_response_data) = - serde_json::from_str(&response_message)? + if let AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdateResponse( + update_response_data, + )) = serde_json::from_str(&response_message)? { info!("Received update response {:?}", update_response_data); } else { @@ -108,13 +124,19 @@ async fn test_mediate_keylist_query() -> Result<()> { .get_wallet_ref() .create_and_store_my_did(None, None) .await?; - let message = MediatorCoordMsgEnum::KeylistUpdateRequest(KeylistUpdateRequestData { - updates: vec![KeylistUpdateItem { - recipient_key: new_vk, - action: KeylistUpdateItemAction::Add, - result: None, - }], - }); + let keylist_update_request = KeylistUpdate::builder() + .content(KeylistUpdateContent { + updates: vec![KeylistUpdateItem { + recipient_key: new_vk, + action: KeylistUpdateItemAction::Add, + }], + }) + .id("key-add".to_owned()) + .build(); + + let message = AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdate( + keylist_update_request, + )); let message_bytes = serde_json::to_vec(&message)?; // send message and get response let _ = send_message_and_pop_response_message( @@ -127,7 +149,12 @@ async fn test_mediate_keylist_query() -> Result<()> { .await?; info!("Proceeding to keylist query"); //prepare request message: list keys - let message = MediatorCoordMsgEnum::KeylistQuery(KeylistQueryData {}); + let keylist_query = KeylistQuery::builder() + .content(KeylistQueryContent::default()) + .id("keylist-query".to_owned()) + .build(); + let message = + AriesMessage::CoordinateMediation(CoordinateMediation::KeylistQuery(keylist_query)); info!("Sending {:?}", serde_json::to_string(&message).unwrap()); let message_bytes = serde_json::to_vec(&message)?; // send message and get response @@ -140,10 +167,10 @@ async fn test_mediate_keylist_query() -> Result<()> { ) .await?; // verify - if let MediatorCoordMsgEnum::Keylist(KeylistData { keys }) = + if let AriesMessage::CoordinateMediation(CoordinateMediation::Keylist(keylist)) = serde_json::from_str(&response_message)? { - info!("Keylist mediator sent {:?}", keys) + info!("Keylist mediator sent {:?}", keylist.content) } else { panic!( "Expected message of type Keylist. Found {:?}", @@ -165,13 +192,19 @@ async fn test_mediate_keylist_update_remove() -> Result<()> { .get_wallet_ref() .create_and_store_my_did(None, None) .await?; - let message = MediatorCoordMsgEnum::KeylistUpdateRequest(KeylistUpdateRequestData { - updates: vec![KeylistUpdateItem { - recipient_key: new_vk.clone(), - action: KeylistUpdateItemAction::Add, - result: None, - }], - }); + let keylist_update_request = KeylistUpdate::builder() + .content(KeylistUpdateContent { + updates: vec![KeylistUpdateItem { + recipient_key: new_vk.clone(), + action: KeylistUpdateItemAction::Add, + }], + }) + .id("key-add".to_owned()) + .build(); + + let message = AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdate( + keylist_update_request, + )); let message_bytes = serde_json::to_vec(&message)?; // send message and get response let _ = send_message_and_pop_response_message( @@ -184,13 +217,19 @@ async fn test_mediate_keylist_update_remove() -> Result<()> { .await?; info!("Proceeding to delete"); // prepare request message: delete key - let message = MediatorCoordMsgEnum::KeylistUpdateRequest(KeylistUpdateRequestData { - updates: vec![KeylistUpdateItem { - recipient_key: new_vk, - action: KeylistUpdateItemAction::Remove, - result: None, - }], - }); + let keylist_update_request = KeylistUpdate::builder() + .content(KeylistUpdateContent { + updates: vec![KeylistUpdateItem { + recipient_key: new_vk, + action: KeylistUpdateItemAction::Remove, + }], + }) + .id("key-remove".to_owned()) + .build(); + + let message = AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdate( + keylist_update_request, + )); info!("Sending {:?}", serde_json::to_string(&message).unwrap()); let message_bytes = serde_json::to_vec(&message)?; // send message and get response @@ -202,8 +241,9 @@ async fn test_mediate_keylist_update_remove() -> Result<()> { &their_diddoc, ) .await?; - if let MediatorCoordMsgEnum::KeylistUpdateResponse(update_response_data) = - serde_json::from_str(&response_message)? + if let AriesMessage::CoordinateMediation(CoordinateMediation::KeylistUpdateResponse( + update_response_data, + )) = serde_json::from_str(&response_message)? { info!("Received update response {:?}", update_response_data); } else { diff --git a/aries/agents/rust/mediator/tests/mediator-protocol-pickup.rs b/aries/agents/rust/mediator/tests/mediator-protocol-pickup.rs index 13aaa176bf..2e44127a36 100644 --- a/aries/agents/rust/mediator/tests/mediator-protocol-pickup.rs +++ b/aries/agents/rust/mediator/tests/mediator-protocol-pickup.rs @@ -2,27 +2,12 @@ mod common; use std::collections::VecDeque; use aries_vcx::utils::encryption_envelope::EncryptionEnvelope; -use aries_vcx_core::wallet::base_wallet::BaseWallet; use diddoc_legacy::aries::diddoc::AriesDidDoc; -use mediation::{ - didcomm_types::mediator_coord_structs::{ - KeylistUpdateItem, KeylistUpdateItemAction, KeylistUpdateRequestData, MediatorCoordMsgEnum, - }, - storage::MediatorPersistence, -}; -use mediator::{ - aries_agent::{ - client::transports::{AriesReqwest, AriesTransport}, - utils::oob2did, - Agent, - }, - utils::{structs::VerKey, GenericStringError}, -}; +use mediator::aries_agent::client::transports::{AriesReqwest, AriesTransport}; use messages::{ decorators::attachment::AttachmentType, msg_fields::protocols::{ basic_message::{BasicMessage, BasicMessageContent, BasicMessageDecorators}, - out_of_band::invitation::Invitation as OOBInvitation, pickup::{ DeliveryRequest, DeliveryRequestContent, DeliveryRequestDecorators, Pickup, StatusRequest, StatusRequestContent, StatusRequestDecorators, @@ -33,7 +18,7 @@ use messages::{ use crate::common::{ agent_and_transport_utils::{ - gen_mediator_connected_agent, get_mediator_grant_data, + gen_and_register_recipient_key, gen_mediator_connected_agent, get_mediator_grant_data, send_message_and_pop_response_message, }, prelude::*, @@ -42,44 +27,6 @@ use crate::common::{ static LOGGING_INIT: std::sync::Once = std::sync::Once::new(); -/// Register recipient keys with mediator -async fn gen_and_register_recipient_key( - agent: &mut Agent, - agent_aries_transport: &mut impl AriesTransport, - agent_verkey: &VerKey, - mediator_diddoc: &AriesDidDoc, -) -> Result<(VerKey, AriesDidDoc)> { - let agent_invite: OOBInvitation = agent - .get_oob_invite() - .map_err(|e| GenericStringError { msg: e.to_string() })?; - let agent_diddoc = oob2did(agent_invite); - let agent_recipient_key = agent_diddoc - .recipient_keys() - .unwrap() - .first() - .unwrap() - .clone(); - // register recipient key with mediator - let message = MediatorCoordMsgEnum::KeylistUpdateRequest(KeylistUpdateRequestData { - updates: vec![KeylistUpdateItem { - recipient_key: agent_recipient_key.clone(), - action: KeylistUpdateItemAction::Add, - result: None, - }], - }); - info!("Sending {:?}", serde_json::to_string(&message).unwrap()); - let message_bytes = serde_json::to_vec(&message)?; - let _response_message = send_message_and_pop_response_message( - &message_bytes, - agent, - agent_aries_transport, - agent_verkey, - mediator_diddoc, - ) - .await?; - Ok((agent_recipient_key, agent_diddoc)) -} - async fn forward_basic_anoncrypt_message( agent_diddoc: &AriesDidDoc, message_text: &str, diff --git a/aries/agents/rust/mediator/tests/mediator-routing-forward.rs b/aries/agents/rust/mediator/tests/mediator-routing-forward.rs index e09d2ca858..f18cfeaa5f 100644 --- a/aries/agents/rust/mediator/tests/mediator-routing-forward.rs +++ b/aries/agents/rust/mediator/tests/mediator-routing-forward.rs @@ -2,25 +2,14 @@ mod common; use std::collections::VecDeque; use aries_vcx::utils::encryption_envelope::EncryptionEnvelope; -use mediation::didcomm_types::mediator_coord_structs::{ - KeylistUpdateItem, KeylistUpdateItemAction, KeylistUpdateRequestData, MediatorCoordMsgEnum, -}; -use mediator::{ - aries_agent::{ - client::transports::{AriesReqwest, AriesTransport}, - utils::oob2did, - }, - utils::GenericStringError, -}; -use messages::msg_fields::protocols::{ - basic_message::{BasicMessage, BasicMessageContent, BasicMessageDecorators}, - out_of_band::invitation::Invitation as OOBInvitation, +use mediator::aries_agent::client::transports::{AriesReqwest, AriesTransport}; +use messages::msg_fields::protocols::basic_message::{ + BasicMessage, BasicMessageContent, BasicMessageDecorators, }; use crate::common::{ agent_and_transport_utils::{ - gen_mediator_connected_agent, get_mediator_grant_data, - send_message_and_pop_response_message, + gen_and_register_recipient_key, gen_mediator_connected_agent, get_mediator_grant_data, }, prelude::*, test_setup::setup_env_logging, @@ -45,29 +34,9 @@ async fn test_forward_flow() -> Result<()> { agent .init_service(grant_data.routing_keys, grant_data.endpoint.parse()?) .await?; - let agent_invite: OOBInvitation = agent - .get_oob_invite() - .map_err(|e| GenericStringError { msg: e.to_string() })?; - let agent_diddoc = oob2did(agent_invite); - let agent_recipient_key = agent_diddoc - .recipient_keys() - .unwrap() - .first() - .unwrap() - .clone(); // register recipient key with mediator - let message = MediatorCoordMsgEnum::KeylistUpdateRequest(KeylistUpdateRequestData { - updates: vec![KeylistUpdateItem { - recipient_key: agent_recipient_key, - action: KeylistUpdateItemAction::Add, - result: None, - }], - }); - info!("Sending {:?}", serde_json::to_string(&message).unwrap()); - let message_bytes = serde_json::to_vec(&message)?; - let _response_message = send_message_and_pop_response_message( - &message_bytes, - &agent, + let (_agent_recipient_key, agent_diddoc) = gen_and_register_recipient_key( + &mut agent, &mut agent_aries_transport, &agent_verkey, &mediator_diddoc, diff --git a/messages/src/msg_fields/protocols/coordinate_mediation/keylist.rs b/messages/src/msg_fields/protocols/coordinate_mediation/keylist.rs index b3974327ff..3a6eb2f347 100644 --- a/messages/src/msg_fields/protocols/coordinate_mediation/keylist.rs +++ b/messages/src/msg_fields/protocols/coordinate_mediation/keylist.rs @@ -11,7 +11,7 @@ pub struct KeylistContent { pub keys: Vec, #[builder(default, setter(strip_option))] #[serde(skip_serializing_if = "Option::is_none")] - pagination: Option, + pub pagination: Option, } #[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq, TypedBuilder)]