diff --git a/aries_vcx/src/handlers/mod.rs b/aries_vcx/src/handlers/mod.rs index 2441c49b5a..853074bca3 100644 --- a/aries_vcx/src/handlers/mod.rs +++ b/aries_vcx/src/handlers/mod.rs @@ -1,4 +1,4 @@ -use crate::handlers::connection::mediated_connection::ConnectionState; +use crate::handlers::connection::mediated_connection::ConnectionState as MediatedConnectionState; use crate::protocols::connection::invitee::state_machine::InviteeState; use crate::protocols::connection::inviter::state_machine::InviterState; use crate::protocols::issuance::holder::state_machine::HolderState; @@ -6,6 +6,8 @@ use crate::protocols::issuance::issuer::state_machine::IssuerState; use crate::protocols::proof_presentation::prover::state_machine::ProverState; use crate::protocols::proof_presentation::verifier::state_machine::VerifierState; +use self::connection::connection::ConnectionState; + pub mod connection; pub mod discovery; pub mod issuance; @@ -15,6 +17,27 @@ pub mod revocation_notification; pub mod trust_ping; pub mod util; +impl From for u32 { + fn from(state: MediatedConnectionState) -> u32 { + match state { + MediatedConnectionState::Inviter(inviter_state) => match inviter_state { + InviterState::Initial => 0, + InviterState::Invited => 1, + InviterState::Requested => 2, + InviterState::Responded => 3, + InviterState::Completed => 4, + }, + MediatedConnectionState::Invitee(invitee_state) => match invitee_state { + InviteeState::Initial => 0, + InviteeState::Invited => 1, + InviteeState::Requested => 2, + InviteeState::Responded => 3, + InviteeState::Completed => 4, + }, + } + } +} + impl From for u32 { fn from(state: ConnectionState) -> u32 { match state { diff --git a/libvcx/src/api_vcx/api_handle/connection.rs b/libvcx/src/api_vcx/api_handle/connection.rs index 612952e1b8..bf4c47be45 100644 --- a/libvcx/src/api_vcx/api_handle/connection.rs +++ b/libvcx/src/api_vcx/api_handle/connection.rs @@ -16,6 +16,18 @@ fn store_connection(connection: Connection) -> LibvcxResult { .map_err(|e| LibvcxError::from_msg(LibvcxErrorKind::IOError, e.to_string())) } +fn serialize(data: &T) -> LibvcxResult +where + T: serde::ser::Serialize, +{ + serde_json::to_string(data).map_err(|err| { + LibvcxError::from_msg( + LibvcxErrorKind::SerializationError, + format!("Serialization failed: {}", err), + ) + }) +} + fn deserialize(data: &str) -> LibvcxResult where T: serde::de::DeserializeOwned, @@ -24,6 +36,7 @@ where .map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Deserialization failed: {}", err))) } +// ----------------------------- CONSTRUCTORS ------------------------------------ pub async fn create_inviter() -> LibvcxResult { trace!("create_inviter >>>"); store_connection(Connection::create_inviter(&get_main_profile()?).await?) @@ -37,6 +50,43 @@ pub async fn create_invitee(invitation: &str) -> LibvcxResult { ) } +// ----------------------------- GETTERS ------------------------------------ +pub fn get_thread_id(handle: u32) -> LibvcxResult { + trace!("get_thread_id >>> handle: {}", handle); + CONNECTION_MAP.get(handle, |connection| Ok(connection.get_thread_id())) +} + +pub fn get_pairwise_info(handle: u32) -> LibvcxResult { + trace!("get_pairwise_info >>> handle: {}", handle); + CONNECTION_MAP.get(handle, |connection| serialize(connection.pairwise_info())) +} + +pub fn get_remote_did(handle: u32) -> LibvcxResult { + trace!("get_remote_did >>> handle: {}", handle); + CONNECTION_MAP.get(handle, |connection| connection.remote_did().map_err(|e| e.into())) +} + +pub fn get_remote_vk(handle: u32) -> LibvcxResult { + trace!("get_remote_vk >>> handle: {}", handle); + CONNECTION_MAP.get(handle, |connection| connection.remote_vk().map_err(|e| e.into())) +} + +pub fn get_state(handle: u32) -> LibvcxResult { + trace!("get_state >>> handle: {}", handle); + CONNECTION_MAP.get(handle, |connection| Ok(connection.get_state().into())) +} + +pub fn get_invitation(handle: u32) -> LibvcxResult { + trace!("get_invitation >>> handle: {}", handle); + CONNECTION_MAP.get(handle, |connection| { + serialize(connection.get_invite_details().ok_or(LibvcxError::from_msg( + LibvcxErrorKind::ActionNotSupported, + "Invitation is not available for the connection.", + ))?) + }) +} + +// ----------------------------- MSG PROCESSING ------------------------------------ pub fn process_invite(handle: u32, invitation: &str) -> LibvcxResult { trace!("process_invite >>>"); store_connection( @@ -127,16 +177,18 @@ pub async fn create_invite(handle: u32, service_endpoint: String, routing_keys: ) } -pub fn from_string(connection_data: &str) -> LibvcxResult { - trace!("from_string >>>"); - store_connection(Connection::from_string(connection_data)?) -} - +// ------------------------- (DE)SERIALIZATION ---------------------------------- pub fn to_string(handle: u32) -> LibvcxResult { trace!("to_string >>>"); CONNECTION_MAP.get(handle, |connection| connection.to_string().map_err(|err| err.into())) } +pub fn from_string(connection_data: &str) -> LibvcxResult { + trace!("from_string >>>"); + store_connection(Connection::from_string(connection_data)?) +} + +// ------------------------------ CLEANUP --------------------------------------- pub fn release(handle: u32) -> LibvcxResult<()> { trace!("release >>>"); CONNECTION_MAP.release(handle)