Skip to content

Commit

Permalink
Add getters
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Jan 4, 2023
1 parent 017aeae commit aea2905
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
25 changes: 24 additions & 1 deletion aries_vcx/src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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;
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;
Expand All @@ -15,6 +17,27 @@ pub mod revocation_notification;
pub mod trust_ping;
pub mod util;

impl From<MediatedConnectionState> 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<ConnectionState> for u32 {
fn from(state: ConnectionState) -> u32 {
match state {
Expand Down
62 changes: 57 additions & 5 deletions libvcx/src/api_vcx/api_handle/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ fn store_connection(connection: Connection) -> LibvcxResult<u32> {
.map_err(|e| LibvcxError::from_msg(LibvcxErrorKind::IOError, e.to_string()))
}

fn serialize<T>(data: &T) -> LibvcxResult<String>
where
T: serde::ser::Serialize,
{
serde_json::to_string(data).map_err(|err| {
LibvcxError::from_msg(
LibvcxErrorKind::SerializationError,
format!("Serialization failed: {}", err),
)
})
}

fn deserialize<T>(data: &str) -> LibvcxResult<T>
where
T: serde::de::DeserializeOwned,
Expand All @@ -24,6 +36,7 @@ where
.map_err(|err| LibvcxError::from_msg(LibvcxErrorKind::InvalidJson, format!("Deserialization failed: {}", err)))
}

// ----------------------------- CONSTRUCTORS ------------------------------------
pub async fn create_inviter() -> LibvcxResult<u32> {
trace!("create_inviter >>>");
store_connection(Connection::create_inviter(&get_main_profile()?).await?)
Expand All @@ -37,6 +50,43 @@ pub async fn create_invitee(invitation: &str) -> LibvcxResult<u32> {
)
}

// ----------------------------- GETTERS ------------------------------------
pub fn get_thread_id(handle: u32) -> LibvcxResult<String> {
trace!("get_thread_id >>> handle: {}", handle);
CONNECTION_MAP.get(handle, |connection| Ok(connection.get_thread_id()))
}

pub fn get_pairwise_info(handle: u32) -> LibvcxResult<String> {
trace!("get_pairwise_info >>> handle: {}", handle);
CONNECTION_MAP.get(handle, |connection| serialize(connection.pairwise_info()))
}

pub fn get_remote_did(handle: u32) -> LibvcxResult<String> {
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<String> {
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<u32> {
trace!("get_state >>> handle: {}", handle);
CONNECTION_MAP.get(handle, |connection| Ok(connection.get_state().into()))
}

pub fn get_invitation(handle: u32) -> LibvcxResult<String> {
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<u32> {
trace!("process_invite >>>");
store_connection(
Expand Down Expand Up @@ -127,16 +177,18 @@ pub async fn create_invite(handle: u32, service_endpoint: String, routing_keys:
)
}

pub fn from_string(connection_data: &str) -> LibvcxResult<u32> {
trace!("from_string >>>");
store_connection(Connection::from_string(connection_data)?)
}

// ------------------------- (DE)SERIALIZATION ----------------------------------
pub fn to_string(handle: u32) -> LibvcxResult<String> {
trace!("to_string >>>");
CONNECTION_MAP.get(handle, |connection| connection.to_string().map_err(|err| err.into()))
}

pub fn from_string(connection_data: &str) -> LibvcxResult<u32> {
trace!("from_string >>>");
store_connection(Connection::from_string(connection_data)?)
}

// ------------------------------ CLEANUP ---------------------------------------
pub fn release(handle: u32) -> LibvcxResult<()> {
trace!("release >>>");
CONNECTION_MAP.release(handle)
Expand Down

0 comments on commit aea2905

Please sign in to comment.