From 31e034600e638c3e04e07bfeaf6a629340de2824 Mon Sep 17 00:00:00 2001 From: Bhavy Airi Date: Sun, 30 Apr 2023 22:12:25 +0530 Subject: [PATCH] Refactor basewallet and corresponding consumers Signed-off-by: Bhavy Airi --- .../src/utils/mockdata/profile/mock_wallet.rs | 14 +++++++---- .../src/anoncreds/credx_anoncreds.rs | 4 ++-- .../src/wallet/agency_client_wallet.rs | 17 ++++++++----- aries_vcx_core/src/wallet/base_wallet.rs | 22 +++++++++++++---- aries_vcx_core/src/wallet/indy_wallet.rs | 24 +++++++++++++------ libvcx_core/src/api_vcx/api_global/wallet.rs | 11 ++++++--- 6 files changed, 65 insertions(+), 27 deletions(-) diff --git a/aries_vcx/src/utils/mockdata/profile/mock_wallet.rs b/aries_vcx/src/utils/mockdata/profile/mock_wallet.rs index b281542ae7..ff2d2caacf 100644 --- a/aries_vcx/src/utils/mockdata/profile/mock_wallet.rs +++ b/aries_vcx/src/utils/mockdata/profile/mock_wallet.rs @@ -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; @@ -38,12 +39,12 @@ impl BaseWallet for MockWallet { xtype: &str, id: &str, value: &str, - tags_json: Option<&str>, + tags: Option>, ) -> VcxCoreResult<()> { Ok(()) } - async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult { + async fn get_wallet_record(&self, xtype: &str, id: &str, options: &str) -> VcxCoreResult { Ok(r#"{"id":"123","type":"record type","value":"record value","tags":null}"#.to_string()) } @@ -55,11 +56,16 @@ impl BaseWallet for MockWallet { 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: HashMap) -> 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, + ) -> VcxCoreResult<()> { Ok(()) } diff --git a/aries_vcx_core/src/anoncreds/credx_anoncreds.rs b/aries_vcx_core/src/anoncreds/credx_anoncreds.rs index e59d279757..50c5a022a4 100644 --- a/aries_vcx_core/src/anoncreds/credx_anoncreds.rs +++ b/aries_vcx_core/src/anoncreds/credx_anoncreds.rs @@ -606,10 +606,10 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let credential_id = cred_id.map_or(Uuid::new_v4().to_string(), String::from); let record_value = serde_json::to_string(&credential)?; - let tags_json = serde_json::to_string(&tags)?; + let tags_json: HashMap = serde_json::from_value(tags)?; self.wallet - .add_wallet_record(CATEGORY_CREDENTIAL, &credential_id, &record_value, Some(&tags_json)) + .add_wallet_record(CATEGORY_CREDENTIAL, &credential_id, &record_value, Some(tags_json)) .await?; Ok(credential_id) diff --git a/aries_vcx_core/src/wallet/agency_client_wallet.rs b/aries_vcx_core/src/wallet/agency_client_wallet.rs index 4736e4f598..9cc12d430f 100644 --- a/aries_vcx_core/src/wallet/agency_client_wallet.rs +++ b/aries_vcx_core/src/wallet/agency_client_wallet.rs @@ -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}; @@ -43,12 +43,12 @@ impl BaseWallet for AgencyClientWallet { xtype: &str, id: &str, value: &str, - tags_json: Option<&str>, + tags: Option>, ) -> VcxCoreResult<()> { Err(unimplemented_agency_client_wallet_method("add_wallet_record")) } - async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult { + async fn get_wallet_record(&self, xtype: &str, id: &str, options: &str) -> VcxCoreResult { Err(unimplemented_agency_client_wallet_method("get_wallet_record")) } @@ -60,7 +60,7 @@ impl BaseWallet for AgencyClientWallet { 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: HashMap) -> VcxCoreResult<()> { Err(unimplemented_agency_client_wallet_method("add_wallet_record_tags")) } @@ -68,7 +68,12 @@ impl BaseWallet for AgencyClientWallet { 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, + ) -> VcxCoreResult<()> { Err(unimplemented_agency_client_wallet_method("update_wallet_record_tags")) } diff --git a/aries_vcx_core/src/wallet/base_wallet.rs b/aries_vcx_core/src/wallet/base_wallet.rs index f83ce1c2b5..430178b51f 100644 --- a/aries_vcx_core/src/wallet/base_wallet.rs +++ b/aries_vcx_core/src/wallet/base_wallet.rs @@ -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: @@ -26,18 +28,28 @@ 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>, + ) -> VcxCoreResult<()>; - async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult; + async fn get_wallet_record(&self, xtype: &str, id: &str, options: &str) -> VcxCoreResult; 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 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: HashMap) -> 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, + ) -> VcxCoreResult<()>; async fn delete_wallet_record_tags(&self, xtype: &str, id: &str, tag_names: &str) -> VcxCoreResult<()>; diff --git a/aries_vcx_core/src/wallet/indy_wallet.rs b/aries_vcx_core/src/wallet/indy_wallet.rs index 01cdd69ffb..efd0e0c5b1 100644 --- a/aries_vcx_core/src/wallet/indy_wallet.rs +++ b/aries_vcx_core/src/wallet/indy_wallet.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::thread; use async_trait::async_trait; @@ -52,13 +53,15 @@ impl BaseWallet for IndySdkWallet { xtype: &str, id: &str, value: &str, - tags_json: Option<&str>, + tags: Option>, ) -> VcxCoreResult<()> { + let res = tags.map(|x| serde_json::to_string(&x)).transpose()?.to_owned(); + let tags_json = res.as_deref(); indy::wallet::add_wallet_record(self.wallet_handle, xtype, id, value, tags_json).await } - async fn get_wallet_record(&self, xtype: &str, id: &str, options_json: &str) -> VcxCoreResult { - indy::wallet::get_wallet_record(self.wallet_handle, xtype, id, options_json).await + async fn get_wallet_record(&self, xtype: &str, id: &str, options: &str) -> VcxCoreResult { + indy::wallet::get_wallet_record(self.wallet_handle, xtype, id, options).await } async fn delete_wallet_record(&self, xtype: &str, id: &str) -> VcxCoreResult<()> { @@ -69,12 +72,19 @@ impl BaseWallet for IndySdkWallet { 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, + ) -> 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: HashMap) -> 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<()> { diff --git a/libvcx_core/src/api_vcx/api_global/wallet.rs b/libvcx_core/src/api_vcx/api_global/wallet.rs index 756ac54750..d92734de18 100644 --- a/libvcx_core/src/api_vcx/api_global/wallet.rs +++ b/libvcx_core/src/api_vcx/api_global/wallet.rs @@ -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 { @@ -113,7 +115,8 @@ pub async fn wallet_configure_issuer(enterprise_seed: &str) -> LibvcxResult) -> LibvcxResult<()> { let wallet = get_main_wallet(); - map_ariesvcx_core_result(wallet.add_wallet_record(type_, id, value, option).await) + let tags: Option> = 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<()> { @@ -123,12 +126,14 @@ pub async fn wallet_update_wallet_record_value(xtype: &str, id: &str, value: &st 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 = 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<()> { let wallet = get_main_wallet(); - map_ariesvcx_core_result(wallet.add_wallet_record_tags(xtype, id, tags_json).await) + let tags: HashMap = serde_json::from_str(tags_json)?; + 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<()> {