Skip to content

Commit

Permalink
Refactor basewallet and corresponding consumers (#843)
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavy Airi <airiragahv@gmail.com>
  • Loading branch information
tech-bash authored Jun 9, 2023
1 parent dd8e039 commit 65cc7a4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
14 changes: 10 additions & 4 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,12 +39,12 @@ 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: &str) -> VcxCoreResult<String> {
Ok(r#"{"id":"123","type":"record type","value":"record value","tags":null}"#.to_string())
}

Expand All @@ -59,11 +60,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<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(())
}

Expand Down
4 changes: 2 additions & 2 deletions aries_vcx_core/src/anoncreds/credx_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,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<String, String> = 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)
Expand Down
17 changes: 11 additions & 6 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,12 +43,12 @@ 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: &str) -> VcxCoreResult<String> {
Err(unimplemented_agency_client_wallet_method("get_wallet_record"))
}

Expand All @@ -64,15 +64,20 @@ 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<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<()> {
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
22 changes: 17 additions & 5 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,30 @@ 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: &str) -> VcxCoreResult<String>;

async fn get_wallet_record_value(&self, xtype: &str, id: &str) -> 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 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<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<()>;

Expand Down
24 changes: 17 additions & 7 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 @@ -53,13 +54,15 @@ impl BaseWallet for IndySdkWallet {
xtype: &str,
id: &str,
value: &str,
tags_json: Option<&str>,
tags: Option<HashMap<String, String>>,
) -> 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<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: &str) -> VcxCoreResult<String> {
indy::wallet::get_wallet_record(self.wallet_handle, xtype, id, options).await
}

async fn get_wallet_record_value(&self, xtype: &str, id: &str) -> VcxCoreResult<String> {
Expand Down Expand Up @@ -87,12 +90,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<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: 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<()> {
Expand Down
11 changes: 8 additions & 3 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,7 +115,8 @@ 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<()> {
Expand All @@ -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<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<()> {
let wallet = get_main_wallet();
map_ariesvcx_core_result(wallet.add_wallet_record_tags(xtype, id, tags_json).await)
let tags: HashMap<String, String> = 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<()> {
Expand Down

0 comments on commit 65cc7a4

Please sign in to comment.