Skip to content

Commit

Permalink
Refactor basewallet and corresponding consumers
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavy Airi <airiragahv@gmail.com>
  • Loading branch information
Bhavy Airi committed May 14, 2023
1 parent 036bbf9 commit 7b5b067
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 38 deletions.
23 changes: 17 additions & 6 deletions aries_vcx/src/utils/mockdata/profile/mock_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use aries_vcx_core::wallet::base_wallet::BaseWallet;
use async_trait::async_trait;

use crate::utils::{self};
use std::collections::HashMap;

#[derive(Debug)]
pub(crate) struct MockWallet;
Expand Down Expand Up @@ -38,32 +39,42 @@ impl BaseWallet for MockWallet {
xtype: &str,
id: &str,
value: &str,
tags_json: Option<&str>,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
Ok(())
}

async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult<String> {
async fn get_wallet_record(&self, xtype: &str, id: &str, options: String) -> VcxCoreResult<String> {
Ok(r#"{"id":"123","type":"record type","value":"record value","tags":null}"#.to_string())
}

async fn delete_wallet_record(&self, xtype: &str, id: &str) -> VcxCoreResult<()> {
Ok(())
}

async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: &str) -> VcxCoreResult<()> {
async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: String) -> VcxCoreResult<()> {
Ok(())
}

async fn add_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()> {
async fn add_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
Ok(())
}

async fn update_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()> {
async fn update_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: HashMap<String, String>,
) -> VcxCoreResult<()> {
Ok(())
}

async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: &str) -> VcxCoreResult<()> {
async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: String) -> VcxCoreResult<()> {
Ok(())
}

Expand Down
26 changes: 18 additions & 8 deletions aries_vcx_core/src/wallet/agency_client_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::utils::async_fn_iterator::AsyncFnIterator;
use std::collections::HashMap;
use std::sync::Arc;

use super::base_wallet::BaseWallet;
use crate::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult};
Expand Down Expand Up @@ -43,32 +43,42 @@ impl BaseWallet for AgencyClientWallet {
xtype: &str,
id: &str,
value: &str,
tags_json: Option<&str>,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
Err(unimplemented_agency_client_wallet_method("add_wallet_record"))
}

async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult<String> {
async fn get_wallet_record(&self, xtype: &str, id: &str, options: String) -> VcxCoreResult<String> {
Err(unimplemented_agency_client_wallet_method("get_wallet_record"))
}

async fn delete_wallet_record(&self, xtype: &str, id: &str) -> VcxCoreResult<()> {
Err(unimplemented_agency_client_wallet_method("delete_wallet_record"))
}

async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: &str) -> VcxCoreResult<()> {
async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: String) -> VcxCoreResult<()> {
Err(unimplemented_agency_client_wallet_method("update_wallet_record_value"))
}

async fn add_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()> {
async fn add_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
Err(unimplemented_agency_client_wallet_method("add_wallet_record_tags"))
}

async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: &str) -> VcxCoreResult<()> {
async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: String) -> VcxCoreResult<()> {
Err(unimplemented_agency_client_wallet_method("delete_wallet_record_tags"))
}

async fn update_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()> {
async fn update_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: HashMap<String, String>,
) -> VcxCoreResult<()> {
Err(unimplemented_agency_client_wallet_method("update_wallet_record_tags"))
}

Expand Down
31 changes: 24 additions & 7 deletions aries_vcx_core/src/wallet/base_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use async_trait::async_trait;
use crate::errors::error::VcxCoreResult;
use crate::utils::async_fn_iterator::AsyncFnIterator;

use std::collections::HashMap;

/// Trait defining standard 'wallet' related functionality. The APIs, including
/// input and output types are loosely based off the indy Wallet API:
/// see: <https://github.com/hyperledger/indy-sdk/blob/main/libindy/src/api/wallet.rs>
Expand All @@ -26,20 +28,35 @@ pub trait BaseWallet: std::fmt::Debug + Send + Sync {

// ---- records

async fn add_wallet_record(&self, xtype: &str, id: &str, value: &str, tags_json: Option<&str>)
-> VcxCoreResult<()>;
async fn add_wallet_record(
&self,
xtype: &str,
id: &str,
value: &str,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()>;

async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult<String>;
async fn get_wallet_record(&self, xtype: &str, id: &str, options: String) -> VcxCoreResult<String>;

async fn delete_wallet_record(&self, xtype: &str, id: &str) -> VcxCoreResult<()>;

async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: &str) -> VcxCoreResult<()>;
async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: String) -> VcxCoreResult<()>;

async fn add_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()>;
async fn add_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()>;

async fn update_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()>;
async fn update_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: HashMap<String, String>,
) -> VcxCoreResult<()>;

async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: &str) -> VcxCoreResult<()>;
async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: String) -> VcxCoreResult<()>;

async fn iterate_wallet_records(
&self,
Expand Down
41 changes: 29 additions & 12 deletions aries_vcx_core/src/wallet/indy_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::thread;

use async_trait::async_trait;
Expand Down Expand Up @@ -52,33 +53,49 @@ impl BaseWallet for IndySdkWallet {
xtype: &str,
id: &str,
value: &str,
tags_json: Option<&str>,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
indy::wallet::add_wallet_record(self.wallet_handle, xtype, id, value, tags_json).await
let tags_json = serde_json::to_string(&tags)?;
indy::wallet::add_wallet_record(self.wallet_handle, xtype, id, value, Some(&tags_json)).await
}

async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult<String> {
indy::wallet::get_wallet_record(self.wallet_handle, xtype, id, options_json).await
async fn get_wallet_record(&self, xtype: &str, id: &str, options: String) -> VcxCoreResult<String> {
let options_json = serde_json::to_string(&options)?;
indy::wallet::get_wallet_record(self.wallet_handle, xtype, id, &options_json).await
}

async fn delete_wallet_record(&self, xtype: &str, id: &str) -> VcxCoreResult<()> {
indy::wallet::delete_wallet_record(self.wallet_handle, xtype, id).await
}

async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: &str) -> VcxCoreResult<()> {
indy::wallet::update_wallet_record_value(self.wallet_handle, xtype, id, value).await
async fn update_wallet_record_value(&self, xtype: &str, id: &str, value: String) -> VcxCoreResult<()> {
let value = serde_json::to_string(&value)?;
indy::wallet::update_wallet_record_value(self.wallet_handle, xtype, id, &value).await
}

async fn update_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()> {
indy::wallet::update_wallet_record_tags(self.wallet_handle, xtype, id, tags_json).await
async fn update_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: HashMap<String, String>,
) -> VcxCoreResult<()> {
let tags_json = serde_json::to_string(&tags)?;
indy::wallet::update_wallet_record_tags(self.wallet_handle, xtype, id, &tags_json).await
}

async fn add_wallet_record_tags(&self, xtype: &str, id: &str, tags_json: &str) -> VcxCoreResult<()> {
indy::wallet::add_wallet_record_tags(self.wallet_handle, xtype, id, tags_json).await
async fn add_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
let tags_json = serde_json::to_string(&tags)?;
indy::wallet::add_wallet_record_tags(self.wallet_handle, xtype, id, &tags_json).await
}

async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: &str) -> VcxCoreResult<()> {
indy::wallet::delete_wallet_record_tags(self.wallet_handle, xtype, id, tag_names).await
async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: String) -> VcxCoreResult<()> {
let tag_names = serde_json::to_string(&tag_names)?;
indy::wallet::delete_wallet_record_tags(self.wallet_handle, xtype, id, &tag_names).await
}

async fn iterate_wallet_records(
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/api_c/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ pub extern "C" fn vcx_wallet_add_record_tags(
);

execute_async::<BoxFuture<'static, Result<(), ()>>>(Box::pin(async move {
match wallet_add_wallet_record_tags(&type_, &id, &tags_json).await {
match wallet_add_wallet_record_tags(&type_, &id, Some(&tags_json)).await {
Ok(()) => {
trace!(
"vcx_wallet_add_record_tags(command_handle: {}, rc: {})",
Expand Down
16 changes: 12 additions & 4 deletions libvcx_core/src/api_vcx/api_global/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::errors::error::LibvcxResult;
use crate::errors::mapping_from_ariesvcx::map_ariesvcx_result;
use crate::errors::mapping_from_ariesvcxcore::map_ariesvcx_core_result;

use std::collections::HashMap;

pub static mut WALLET_HANDLE: WalletHandle = INVALID_WALLET_HANDLE;

pub fn set_main_wallet_handle(handle: WalletHandle) -> WalletHandle {
Expand Down Expand Up @@ -113,31 +115,37 @@ pub async fn wallet_configure_issuer(enterprise_seed: &str) -> LibvcxResult<Issu

pub async fn wallet_add_wallet_record(type_: &str, id: &str, value: &str, option: Option<&str>) -> LibvcxResult<()> {
let wallet = get_main_wallet();
map_ariesvcx_core_result(wallet.add_wallet_record(type_, id, value, option).await)
let tags: Option<HashMap<String, String>> = option.map(|tags| serde_json::from_str(tags)).transpose()?;
map_ariesvcx_core_result(wallet.add_wallet_record(type_, id, value, tags).await)
}

pub async fn wallet_update_wallet_record_value(xtype: &str, id: &str, value: &str) -> LibvcxResult<()> {
let wallet = get_main_wallet();
let value = value.to_string();
map_ariesvcx_core_result(wallet.update_wallet_record_value(xtype, id, value).await)
}

pub async fn wallet_update_wallet_record_tags(xtype: &str, id: &str, tags_json: &str) -> LibvcxResult<()> {
let wallet = get_main_wallet();
map_ariesvcx_core_result(wallet.update_wallet_record_tags(xtype, id, tags_json).await)
let tags: HashMap<String, String> = serde_json::from_str(tags_json)?;
map_ariesvcx_core_result(wallet.update_wallet_record_tags(xtype, id, tags).await)
}

pub async fn wallet_add_wallet_record_tags(xtype: &str, id: &str, tags_json: &str) -> LibvcxResult<()> {
pub async fn wallet_add_wallet_record_tags(xtype: &str, id: &str, option: Option<&str>) -> LibvcxResult<()> {
let wallet = get_main_wallet();
map_ariesvcx_core_result(wallet.add_wallet_record_tags(xtype, id, tags_json).await)
let tags: Option<HashMap<String, String>> = option.map(|tags| serde_json::from_str(tags)).transpose()?;
map_ariesvcx_core_result(wallet.add_wallet_record_tags(xtype, id, tags).await)
}

pub async fn wallet_delete_wallet_record_tags(xtype: &str, id: &str, tags_json: &str) -> LibvcxResult<()> {
let wallet = get_main_wallet();
let tags_json = serde_json::from_str(tags_json)?;
map_ariesvcx_core_result(wallet.delete_wallet_record_tags(xtype, id, tags_json).await)
}

pub async fn wallet_get_wallet_record(xtype: &str, id: &str, options: &str) -> LibvcxResult<String> {
let wallet = get_main_wallet();
let options = options.to_string();
map_ariesvcx_core_result(wallet.get_wallet_record(xtype, id, options).await)
}

Expand Down

0 comments on commit 7b5b067

Please sign in to comment.