Skip to content

Commit

Permalink
Make wallet_handle injectable when creating connection
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jul 26, 2022
1 parent 9a59824 commit 4bb754b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 52 deletions.
53 changes: 10 additions & 43 deletions aries_vcx/src/handlers/connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ pub enum Actor {
}

impl Connection {
pub async fn create(source_id: &str, autohop_enabled: bool, agency_client: &AgencyClient) -> VcxResult<Self> {
pub async fn create(source_id: &str, wallet_handle: WalletHandle, agency_client: &AgencyClient, autohop_enabled: bool) -> VcxResult<Self> {
trace!("Connection::create >>> source_id: {}", source_id);
// todo: Would be cleaner to pass wallet_handle as argument instead of reading off AgencyClient
let pairwise_info = PairwiseInfo::create(agency_client.get_wallet_handle()).await?;
let pairwise_info = PairwiseInfo::create(wallet_handle).await?;
let cloud_agent_info = CloudAgentInfo::create(agency_client, &pairwise_info).await?;
Ok(Self {
cloud_agent_info,
Expand All @@ -92,9 +91,9 @@ impl Connection {
})
}

pub async fn create_with_invite(source_id: &str, invitation: Invitation, autohop_enabled: bool, agency_client: &AgencyClient) -> VcxResult<Self> {
pub async fn create_with_invite(source_id: &str, wallet_handle: WalletHandle, agency_client: &AgencyClient, invitation: Invitation, autohop_enabled: bool) -> VcxResult<Self> {
trace!("Connection::create_with_invite >>> source_id: {}, invitation: {:?}", source_id, invitation);
let pairwise_info = PairwiseInfo::create(agency_client.get_wallet_handle()).await?;
let pairwise_info = PairwiseInfo::create(wallet_handle).await?;
let cloud_agent_info = CloudAgentInfo::create(agency_client, &pairwise_info).await?;
let mut connection = Self {
cloud_agent_info,
Expand Down Expand Up @@ -183,15 +182,6 @@ impl Connection {
self.cloud_agent_info.clone()
}

// pub fn bootstrap_agent_info(&self) -> Option<&PairwiseInfo> {
// match &self.connection_sm {
// SmConnection::Inviter(sm_inviter) => {
// sm_inviter.prev_agent_info()
// }
// SmConnection::Invitee(_sm_invitee) => None
// }
// }

pub fn remote_did(&self) -> VcxResult<String> {
match &self.connection_sm {
SmConnection::Inviter(sm_inviter) => {
Expand Down Expand Up @@ -355,19 +345,6 @@ impl Connection {
}
}

// fn _get_bootstrap_agent_messages(&self, remote_vk: VcxResult<String>, bootstrap_agent_info: Option<&PairwiseInfo>) -> VcxResult<Option<(HashMap<String, A2AMessage>, PairwiseInfo)>> {
// let expected_sender_vk = match remote_vk {
// Ok(vk) => vk,
// Err(_) => return Ok(None)
// };
// if let Some(bootstrap_agent_info) = bootstrap_agent_info {
// trace!("Connection::_get_bootstrap_agent_messages >>> Inviter found no message to handle on main connection agent. Will check bootstrap agent.");
// let messages = bootstrap_agent_info.get_messages(&expected_sender_vk)?;
// return Ok(Some((messages, bootstrap_agent_info.clone())));
// }
// Ok(None)
// }

fn _update_state(&mut self, wallet_handle: WalletHandle, message: Option<A2AMessage>, agency_client: AgencyClient) -> BoxFuture<'_, VcxResult<()>> {
Box::pin(async move {
let (new_connection_sm, can_autohop) = match &self.connection_sm {
Expand Down Expand Up @@ -404,18 +381,8 @@ impl Connection {
self.cloud_agent_info().clone().update_message_status(agency_client, self.pairwise_info(), uid).await?;
}
None => {
// Todo: Restore lookup into bootstrap cloud agent
// self.bootstrap_agent_info()
// if let Some((messages, bootstrap_agent_info)) = self._get_bootstrap_agent_messages(self.remote_vk(), )? {
// if let Some((uid, message)) = self.find_message_to_handle(messages) {
// trace!("Connection::update_state >>> handling message found on bootstrap agent uid: {:?}", uid);
// self._update_state(Some(message))?;
// bootstrap_agent_info.update_message_status(uid)?;
// }
// } else {
trace!("Connection::update_state >>> trying to update state without message");
self._update_state(wallet_handle, None, agency_client.clone()).await?;
// }
}
}
Ok(())
Expand Down Expand Up @@ -851,7 +818,7 @@ mod tests {
let _setup = SetupMocks::init();
let agency_client = AgencyClient::new();
enable_agency_mocks();
let connection = Connection::create_with_invite("abc", Invitation::Pairwise(_pairwise_invitation()), true, &agency_client).await.unwrap();
let connection = Connection::create_with_invite("abc", WalletHandle(0), &agency_client, Invitation::Pairwise(_pairwise_invitation()), true).await.unwrap();
assert_eq!(connection.get_state(), ConnectionState::Invitee(InviteeState::Invited));
}

Expand All @@ -860,7 +827,7 @@ mod tests {
let _setup = SetupMocks::init();
let agency_client = AgencyClient::new();
enable_agency_mocks();
let connection = Connection::create_with_invite("abc", Invitation::Public(_public_invitation()), true, &agency_client).await.unwrap();
let connection = Connection::create_with_invite("abc", WalletHandle(0), &agency_client, Invitation::Public(_public_invitation()), true).await.unwrap();
assert_eq!(connection.get_state(), ConnectionState::Invitee(InviteeState::Invited));
}

Expand All @@ -871,13 +838,13 @@ mod tests {
enable_agency_mocks();

let pub_inv = _public_invitation_random_id();
let mut connection = Connection::create_with_invite("abcd", Invitation::Public(pub_inv.clone()), true, &agency_client).await.unwrap();
let mut connection = Connection::create_with_invite("abcd", WalletHandle(0), &agency_client, Invitation::Public(pub_inv.clone()), true).await.unwrap();
connection.connect(WalletHandle(0), &agency_client).await.unwrap();
assert_eq!(connection.get_state(), ConnectionState::Invitee(InviteeState::Requested));
assert_ne!(connection.get_thread_id(), pub_inv.id.0);

let pw_inv = _pairwise_invitation_random_id();
let mut connection = Connection::create_with_invite("dcba", Invitation::Pairwise(pw_inv.clone()), true, &agency_client).await.unwrap();
let mut connection = Connection::create_with_invite("dcba", WalletHandle(0), &agency_client, Invitation::Pairwise(pw_inv.clone()), true).await.unwrap();
connection.connect(WalletHandle(0), &agency_client).await.unwrap();
assert_eq!(connection.get_state(), ConnectionState::Invitee(InviteeState::Requested));
assert_eq!(connection.get_thread_id(), pw_inv.id.0);
Expand Down Expand Up @@ -934,7 +901,7 @@ mod tests {
async fn test_serialize_deserialize() {
let _setup = SetupMocks::init();

let connection = Connection::create("test_serialize_deserialize", true, &_dummy_agency_client()).await.unwrap();
let connection = Connection::create("test_serialize_deserialize", WalletHandle(0), &_dummy_agency_client(), true).await.unwrap();
let first_string = connection.to_string().unwrap();

let connection2 = Connection::from_string(&first_string).unwrap();
Expand All @@ -947,7 +914,7 @@ mod tests {
async fn test_serialize_deserialize_serde() {
let _setup = SetupMocks::init();

let connection = Connection::create("test_serialize_deserialize", true, &_dummy_agency_client()).await.unwrap();
let connection = Connection::create("test_serialize_deserialize", WalletHandle(0), &_dummy_agency_client(), true).await.unwrap();
let first_string = serde_json::to_string(&connection).unwrap();

let connection: Connection = serde_json::from_str(&first_string).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/out_of_band/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl OutOfBandReceiver {

pub async fn build_connection(&self, agency_client: &AgencyClient, autohop_enabled: bool) -> VcxResult<Connection> {
trace!("OutOfBandReceiver::build_connection >>> autohop_enabled: {}", autohop_enabled);
Connection::create_with_invite(&self.oob.id.0, Invitation::OutOfBand(self.oob.clone()), autohop_enabled, agency_client).await
Connection::create_with_invite(&self.oob.id.0, agency_client.get_wallet_handle(), Invitation::OutOfBand(self.oob.clone()), autohop_enabled).await
}

pub fn to_a2a_message(&self) -> A2AMessage {
Expand Down
6 changes: 3 additions & 3 deletions aries_vcx/tests/utils/devsetup_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub mod test_utils {
init_issuer_config(&config_issuer).unwrap();
let mut agency_client = AgencyClient::new();
let config_agency = provision_cloud_agent(&mut agency_client, wallet_handle, &config_provision_agent).await.unwrap();
let connection = Connection::create("faber", true, &agency_client).await.unwrap();
let connection = Connection::create("faber", agency_client.get_wallet_handle(), &agency_client, true).await.unwrap();
let agent = PublicAgent::create(wallet_handle, &agency_client, "faber", &config_issuer.institution_did).await.unwrap();
let faber = Faber {
wallet_handle,
Expand Down Expand Up @@ -354,7 +354,7 @@ pub mod test_utils {
let wallet_handle = open_wallet(&config_wallet).await.unwrap();
let mut agency_client = AgencyClient::new();
let config_agency = provision_cloud_agent(&mut agency_client, wallet_handle, &config_provision_agent).await.unwrap();
let connection = Connection::create("tmp_empoty", true, &agency_client).await.unwrap();
let connection = Connection::create("tmp_empoty", agency_client.get_wallet_handle(), &agency_client, true).await.unwrap();
let alice = Alice {
wallet_handle,
agency_client,
Expand All @@ -370,7 +370,7 @@ pub mod test_utils {

pub async fn accept_invite(&mut self, invite: &str) {
self.activate().await.unwrap();
self.connection = Connection::create_with_invite("faber", serde_json::from_str(invite).unwrap(), true, &self.agency_client).await.unwrap();
self.connection = Connection::create_with_invite("faber", &self.wallet_handle, &self.agency_client, serde_json::from_str(invite).unwrap(), true).await.unwrap();
self.connection.connect(self.wallet_handle, &self.agency_client).await.unwrap();
self.connection.update_state(self.wallet_handle, &self.agency_client).await.unwrap();
assert_eq!(ConnectionState::Invitee(InviteeState::Requested), self.connection.get_state());
Expand Down
6 changes: 3 additions & 3 deletions aries_vcx/tests/utils/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ pub mod test_utils {
let public_invite: Invitation = serde_json::from_str(&public_invite_json).unwrap();

alice.activate().await.unwrap();
let mut consumer_to_institution = Connection::create_with_invite("institution", public_invite, true, &alice.agency_client).await.unwrap();
let mut consumer_to_institution = Connection::create_with_invite("institution", &alice.wallet_handle, &alice.agency_client, public_invite, true).await.unwrap();
consumer_to_institution.connect(alice.wallet_handle, &alice.agency_client).await.unwrap();
consumer_to_institution.update_state(alice.wallet_handle, &alice.agency_client).await.unwrap();

Expand All @@ -602,13 +602,13 @@ pub mod test_utils {
pub async fn create_connected_connections(alice: &mut Alice, faber: &mut Faber) -> (Connection, Connection) {
debug!("Institution is going to create connection.");
faber.activate().await.unwrap();
let mut institution_to_consumer = Connection::create("consumer", true, &faber.agency_client).await.unwrap();
let mut institution_to_consumer = Connection::create("consumer", faber.wallet_handle, &faber.agency_client, true).await.unwrap();
institution_to_consumer.connect(faber.wallet_handle, &faber.agency_client).await.unwrap();
let details = institution_to_consumer.get_invite_details().unwrap();

alice.activate().await.unwrap();
debug!("Consumer is going to accept connection invitation.");
let mut consumer_to_institution = Connection::create_with_invite("institution", details.clone(), true, &alice.agency_client).await.unwrap();
let mut consumer_to_institution = Connection::create_with_invite("institution", &alice.wallet_handle, &alice.agency_client, details.clone(), true).await.unwrap();

consumer_to_institution.connect(alice.wallet_handle, &alice.agency_client).await.unwrap();
consumer_to_institution.update_state(alice.wallet_handle, &alice.agency_client).await.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions libvcx/src/api_lib/api_handle/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ pub fn store_connection(connection: Connection) -> VcxResult<u32> {

pub async fn create_connection(source_id: &str) -> VcxResult<u32> {
trace!("create_connection >>> source_id: {}", source_id);
let connection = Connection::create(source_id, true, &get_main_agency_client().unwrap()).await?;
let connection = Connection::create(source_id, get_main_wallet_handle(), &get_main_agency_client().unwrap(), true).await?;
store_connection(connection)
}

pub async fn create_connection_with_invite(source_id: &str, details: &str) -> VcxResult<u32> {
debug!("create connection {} with invite {}", source_id, details);
if let Some(invitation) = serde_json::from_str::<InvitationV3>(details).ok() {
let connection = Connection::create_with_invite(source_id, invitation, true, &get_main_agency_client().unwrap()).await?;
let connection = Connection::create_with_invite(source_id, get_main_wallet_handle(), &get_main_agency_client().unwrap(), invitation, true).await?;
store_connection(connection)
} else {
Err(VcxError::from_msg(VcxErrorKind::InvalidJson, "Used invite has invalid structure")) // TODO: Specific error type
Expand Down

0 comments on commit 4bb754b

Please sign in to comment.