Skip to content

Commit

Permalink
Reorganize files, remove access to global data from aries-vcx src (hy…
Browse files Browse the repository at this point in the history
…perledger#517)

* Refactor tests

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Reorganize files to separate global data based APIs

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Pull get_main_wallet_handle() calls up in call stack

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Pull more get_main_wallet_handle() calls up in call stack

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Pull get_main_agency_client() up the call stack

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* AgencyClient: Use WalletHandle type, remove Default trait

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove access to global wallet or agency_client from aries-vcx src/

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Optimize imports, reformat

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas authored Jul 21, 2022
1 parent 24701ca commit 2948c2a
Show file tree
Hide file tree
Showing 98 changed files with 2,164 additions and 2,091 deletions.
30 changes: 20 additions & 10 deletions agency_client/src/agency_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use indy::WalletHandle;
use url::Url;

use crate::{AgencyClientError, AgencyClientErrorKind, validation};
Expand All @@ -7,9 +8,9 @@ use crate::testing::mocking;
use crate::utils::error_utils;

// todo: remove Default
#[derive(Default, Deserialize, Clone)]
#[derive(Clone)]
pub struct AgencyClient {
wallet_handle: i32,
wallet_handle: WalletHandle,
pub agency_url: String,
pub agency_did: String,
pub agency_pwdid: String,
Expand Down Expand Up @@ -40,7 +41,7 @@ pub fn validate_mandotory_config_val<F, S, E>(val: &str, err: AgencyClientErrorK


impl AgencyClient {
pub fn get_wallet_handle(&self) -> i32 { self.wallet_handle }
pub fn get_wallet_handle(&self) -> WalletHandle { self.wallet_handle }
pub fn get_agency_url_config(&self) -> String { self.agency_url.clone() }
pub fn get_agency_url_full(&self) -> String {
format!("{}/agency/msg", self.agency_url.clone())
Expand All @@ -56,12 +57,12 @@ impl AgencyClient {
pub fn get_my_pwdid(&self) -> String { self.my_pwdid.clone() }
pub fn get_my_vk(&self) -> String { self.my_vk.clone() }

pub fn set_wallet_handle(&mut self, wh: i32) {
self.wallet_handle = wh;
pub fn set_wallet_handle(&mut self, wallet_handle: WalletHandle) {
self.wallet_handle = wallet_handle;
}

pub fn reset_wallet_handle(&mut self) {
self.wallet_handle = indy::INVALID_WALLET_HANDLE.0;
self.wallet_handle = indy::INVALID_WALLET_HANDLE;
}
pub fn set_agency_url(&mut self, url: &str) {
self.agency_url = url.to_string();
Expand Down Expand Up @@ -126,13 +127,22 @@ impl AgencyClient {
self.set_my_vk(default_verkey);
}

pub fn new() -> AgencyClientResult<Self> {
let agency_client = Self::default();
Ok(agency_client)
pub fn new() -> Self {
AgencyClient {
wallet_handle: WalletHandle(0),
agency_url: "".to_string(),
agency_did: "".to_string(),
agency_pwdid: "".to_string(),
agency_vk: "".to_string(),
agent_pwdid: "".to_string(),
agent_vk: "".to_string(),
my_pwdid: "".to_string(),
my_vk: "".to_string()
}
}

// todo: use this in favor of `fn new()`
// pub fn new(config: &str, wallet_handle: i32, validate: bool) -> AgencyClientResult<Self> {
// pub fn new(config: &str, wallet_handle: WalletHandle, validate: bool) -> AgencyClientResult<Self> {
// let mut agency_client = Self::default();
// agency_client.process_config_string(config, wallet_handle, validate)?;
// Ok(agency_client)
Expand Down
9 changes: 5 additions & 4 deletions agency_client/src/api/downloaded_message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use indy::WalletHandle;
use crate::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::MessageStatusCode;
use crate::utils::encryption_envelope::EncryptionEnvelope;
Expand Down Expand Up @@ -38,7 +39,7 @@ impl DownloadedMessageEncrypted {
}
}

pub async fn decrypt_noauth(self, wallet_handle: i32) -> AgencyClientResult<DownloadedMessage> {
pub async fn decrypt_noauth(self, wallet_handle: WalletHandle) -> AgencyClientResult<DownloadedMessage> {
let decrypted_payload = self._noauth_decrypt_v3_message(wallet_handle).await?;
Ok(DownloadedMessage {
status_code: self.status_code.clone(),
Expand All @@ -47,7 +48,7 @@ impl DownloadedMessageEncrypted {
})
}

pub async fn decrypt_auth(self, wallet_handle: i32, expected_sender_vk: &str) -> AgencyClientResult<DownloadedMessage> {
pub async fn decrypt_auth(self, wallet_handle: WalletHandle, expected_sender_vk: &str) -> AgencyClientResult<DownloadedMessage> {
let decrypted_payload = self._auth_decrypt_v3_message(wallet_handle, expected_sender_vk).await?;
Ok(DownloadedMessage {
status_code: self.status_code.clone(),
Expand All @@ -56,11 +57,11 @@ impl DownloadedMessageEncrypted {
})
}

async fn _noauth_decrypt_v3_message(&self, wallet_handle: i32,) -> AgencyClientResult<String> {
async fn _noauth_decrypt_v3_message(&self, wallet_handle: WalletHandle,) -> AgencyClientResult<String> {
EncryptionEnvelope::anon_unpack(wallet_handle, self.payload()?).await
}

async fn _auth_decrypt_v3_message(&self, wallet_handle: i32, expected_sender_vk: &str) -> AgencyClientResult<String> {
async fn _auth_decrypt_v3_message(&self, wallet_handle: WalletHandle, expected_sender_vk: &str) -> AgencyClientResult<String> {
EncryptionEnvelope::auth_unpack(wallet_handle, self.payload()?, &expected_sender_vk).await
}
}
4 changes: 3 additions & 1 deletion agency_client/src/api/onboarding.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use indy::WalletHandle;
use crate::agency_client::AgencyClient;
use crate::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::messages::a2a_message::Client2AgencyMessage;
Expand Down Expand Up @@ -56,8 +57,9 @@ impl AgencyClient {
Ok(response)
}

pub async fn provision_cloud_agent(&mut self, my_did: &str, my_vk: &str, agency_did: &str, agency_vk: &str, agency_url: &str) -> AgencyClientResult<()> {
pub async fn provision_cloud_agent(&mut self, wallet_handle: WalletHandle, my_did: &str, my_vk: &str, agency_did: &str, agency_vk: &str, agency_url: &str) -> AgencyClientResult<()> {
info!("provision_cloud_agent >>> my_did: {}, my_vk: {}, agency_did: {}, agency_vk: {}, agency_url: {}", my_did, my_vk, agency_did, agency_vk, agency_url);
self.set_wallet_handle(wallet_handle);
self.set_agency_url(agency_url);
self.set_agency_vk(agency_vk);
self.set_agency_did(agency_did);
Expand Down
2 changes: 1 addition & 1 deletion agency_client/src/messages/create_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod tests {

let for_did = "11235yBzrpJQmNyZzgoTqB";
let for_verkey = "EkVTa7SCJ5SntpYyX7CSb2pcBhiVGT9kWSagA8a9T69A";
let client = AgencyClient::new().unwrap();
let client = AgencyClient::new();
let (res_did, res_vk) = client.create_connection_agent(for_did, for_verkey).await.unwrap();

assert_eq!(res_did, "MNepeSWtGfhnv8jLB1sFZC");
Expand Down
6 changes: 3 additions & 3 deletions agency_client/src/utils/encryption_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::utils::libindy::crypto;
pub struct EncryptionEnvelope(pub Vec<u8>);

impl EncryptionEnvelope {
async fn _unpack_a2a_message(wallet_handle: i32, payload: Vec<u8>) -> AgencyClientResult<(String, Option<String>)> {
async fn _unpack_a2a_message(wallet_handle: WalletHandle, payload: Vec<u8>) -> AgencyClientResult<(String, Option<String>)> {
trace!("EncryptionEnvelope::_unpack_a2a_message >>> processing payload of {} bytes", payload.len());

let unpacked_msg = crypto::unpack_message(wallet_handle, &payload).await?;
Expand All @@ -27,7 +27,7 @@ impl EncryptionEnvelope {
Ok((msg_string, sender_vk))
}

pub async fn anon_unpack(wallet_handle: i32, payload: Vec<u8>) -> AgencyClientResult<String> {
pub async fn anon_unpack(wallet_handle: WalletHandle, payload: Vec<u8>) -> AgencyClientResult<String> {
trace!("EncryptionEnvelope::anon_unpack >>> processing payload of {} bytes", payload.len());
if AgencyMockDecrypted::has_decrypted_mock_messages() {
trace!("EncryptionEnvelope::anon_unpack >>> returning decrypted mock message");
Expand All @@ -39,7 +39,7 @@ impl EncryptionEnvelope {
}
}

pub async fn auth_unpack(wallet_handle: i32, payload: Vec<u8>, expected_vk: &str) -> AgencyClientResult<String> {
pub async fn auth_unpack(wallet_handle: WalletHandle, payload: Vec<u8>, expected_vk: &str) -> AgencyClientResult<String> {
trace!("EncryptionEnvelope::auth_unpack >>> processing payload of {} bytes, expected_vk={}", payload.len(), expected_vk);

if AgencyMockDecrypted::has_decrypted_mock_messages() {
Expand Down
8 changes: 4 additions & 4 deletions agency_client/src/utils/libindy/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ use indy::{crypto, WalletHandle};
use crate::error::AgencyClientResult;
use crate::testing::mocking::agency_mocks_enabled;

pub async fn pack_message(wallet_handle: i32, sender_vk: Option<&str>, receiver_keys: &str, msg: &[u8]) -> AgencyClientResult<Vec<u8>> {
pub async fn pack_message(wallet_handle: WalletHandle, sender_vk: Option<&str>, receiver_keys: &str, msg: &[u8]) -> AgencyClientResult<Vec<u8>> {
trace!("pack_message >>> sender_vk: {:?}, receiver_keys: {}, msg: ...", sender_vk, receiver_keys);
if agency_mocks_enabled() {
trace!("pack_message >>> mocks enabled, returning message");
return Ok(msg.to_vec());
}

crypto::pack_message(WalletHandle(wallet_handle), msg, receiver_keys, sender_vk)
crypto::pack_message(wallet_handle, msg, receiver_keys, sender_vk)
.await
.map_err(|err| err.into())
}

pub async fn unpack_message(wallet_handle: i32, msg: &[u8]) -> AgencyClientResult<Vec<u8>> {
pub async fn unpack_message(wallet_handle: WalletHandle, msg: &[u8]) -> AgencyClientResult<Vec<u8>> {
if agency_mocks_enabled() {
trace!("unpack_message >>> mocks enabled, returning message");
return Ok(msg.to_vec());
}

crypto::unpack_message(WalletHandle(wallet_handle), msg)
crypto::unpack_message(wallet_handle, msg)
.await
.map_err(|err| err.into())
}
26 changes: 26 additions & 0 deletions aries_vcx/src/actors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use strum::IntoEnumIterator;

use crate::error::{VcxError, VcxErrorKind};
use crate::global::settings;
use crate::global::settings::CONFIG_ACTORS;

pub fn get_actors() -> Vec<Actors> {
settings::get_config_value(CONFIG_ACTORS)
.and_then(|actors|
serde_json::from_str(&actors)
.map_err(|_| VcxError::from(VcxErrorKind::InvalidOption))
).unwrap_or_else(|_| Actors::iter().collect())
}

#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq, EnumIter)]
#[serde(rename_all = "lowercase")]
pub enum Actors {
Inviter,
Invitee,
Issuer,
Holder,
Prover,
Verifier,
Sender,
Receiver,
}
39 changes: 39 additions & 0 deletions aries_vcx/src/global/agency_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::ops::Deref;
use std::sync::{RwLock, RwLockWriteGuard};

use agency_client::agency_client::AgencyClient;
use agency_client::configuration::AgencyClientConfig;

use crate::error::VcxResult;

lazy_static! {
pub static ref AGENCY_CLIENT: RwLock<AgencyClient> = RwLock::new(AgencyClient::new());
}

pub fn get_main_agency_client_mut() -> VcxResult<RwLockWriteGuard<'static, AgencyClient>> {
let agency_client = AGENCY_CLIENT.write()?;
Ok(agency_client)
}

pub fn get_main_agency_client() -> VcxResult<AgencyClient> {
let agency_client = AGENCY_CLIENT.read()?.deref().clone();
Ok(agency_client)
}

pub fn create_agency_client_for_main_wallet(config: &AgencyClientConfig) -> VcxResult<()> {
get_main_agency_client_mut()?
.configure(config)?;
Ok(())
}

pub fn enable_main_agency_client_mocks() -> VcxResult<()> {
info!("enable_agency_mocks >>>");
get_main_agency_client_mut()?.enable_test_mode();
Ok(())
}

pub fn reset_main_agency_client() {
trace!("reset_agency_client >>>");
let mut agency_client = AGENCY_CLIENT.write().unwrap();
*agency_client = AgencyClient::new();
}
4 changes: 4 additions & 0 deletions aries_vcx/src/global/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod agency_client;
pub mod settings;
pub mod wallet;
pub mod pool;
52 changes: 52 additions & 0 deletions aries_vcx/src/global/pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::sync::RwLock;

use indy::{ErrorCode, pool};

use crate::error::{VcxError, VcxErrorKind, VcxResult};
use crate::error::prelude::*;
use crate::global;
use crate::global::settings;
use crate::libindy::utils::pool::PoolConfig;
use crate::libindy::utils::pool::{create_pool_ledger_config, open_pool_ledger};

lazy_static! {
static ref POOL_HANDLE: RwLock<Option<i32>> = RwLock::new(None);
}

pub fn set_main_pool_handle(handle: Option<i32>) {
let mut h = POOL_HANDLE.write().unwrap();
*h = handle;
}

pub fn get_main_pool_handle() -> VcxResult<i32> {
POOL_HANDLE.read()
.or(Err(VcxError::from_msg(VcxErrorKind::NoPoolOpen, "There is no pool opened")))?
.ok_or(VcxError::from_msg(VcxErrorKind::NoPoolOpen, "There is no pool opened"))
}

pub fn is_main_pool_open() -> bool {
get_main_pool_handle().is_ok()
}

pub fn reset_main_pool_handle() { set_main_pool_handle(None); }

pub async fn open_main_pool(config: &PoolConfig) -> VcxResult<()> {
let pool_name = config.pool_name.clone().unwrap_or(settings::DEFAULT_POOL_NAME.to_string());
trace!("open_pool >>> pool_name: {}, path: {}, pool_config: {:?}", pool_name, config.genesis_path, config.pool_config);

create_pool_ledger_config(&pool_name, &config.genesis_path)
.await
.map_err(|err| err.extend("Can not create Pool Ledger Config"))?;

debug!("open_pool ::: Pool Config Created Successfully");

let handle = open_pool_ledger(&pool_name, config.pool_config.as_deref())
.await
.map_err(|err| err.extend("Can not open Pool Ledger"))?;

set_main_pool_handle(Some(handle));

info!("open_pool ::: Pool Opened Successfully");

Ok(())
}
Loading

0 comments on commit 2948c2a

Please sign in to comment.