From bc3a5c68414f7a2d919fe94b3f1d8192a352c5cc Mon Sep 17 00:00:00 2001 From: Ondrej Prazak Date: Thu, 4 Jan 2024 14:10:59 +0100 Subject: [PATCH] split update method Signed-off-by: Ondrej Prazak --- Cargo.lock | 2 - aries/aries_vcx_core/Cargo.toml | 4 +- aries/aries_vcx_core/src/wallet2/entry_tag.rs | 4 + .../wallet2/indy_wallet/indy_did_wallet.rs | 107 +----- .../wallet2/indy_wallet/indy_record_wallet.rs | 264 ++------------ aries/aries_vcx_core/src/wallet2/mod.rs | 336 ++++++++++++++++-- aries/aries_vcx_core/src/wallet2/utils.rs | 9 + aries/misc/test_utils/src/devsetup.rs | 34 -- 8 files changed, 349 insertions(+), 411 deletions(-) create mode 100644 aries/aries_vcx_core/src/wallet2/utils.rs diff --git a/Cargo.lock b/Cargo.lock index af643cf0cc..c87730dd1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -395,7 +395,6 @@ version = "0.1.0" dependencies = [ "agency_client", "async-trait", - "bs58 0.5.0", "derive_builder", "futures", "indy-api-types", @@ -410,7 +409,6 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "test_utils", "thiserror", "time 0.3.20", "tokio", diff --git a/aries/aries_vcx_core/Cargo.toml b/aries/aries_vcx_core/Cargo.toml index 5a516615f9..2130f3e441 100644 --- a/aries/aries_vcx_core/Cargo.toml +++ b/aries/aries_vcx_core/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [features] ########################## DEP FLAGS ################################ -vdrtools_wallet = ["dep:libvdrtools", "dep:indy-api-types", "test_utils/vdrtools_wallet"] +vdrtools_wallet = ["dep:libvdrtools", "dep:indy-api-types"] # Feature flag to include the 'modular library' dependencies (vdrtools alternatives; indy-vdr, indy-credx) credx = ["dep:indy-credx"] vdr_proxy_ledger = ["credx", "dep:indy-vdr-proxy-client"] @@ -14,7 +14,6 @@ legacy_proof = [] [dependencies] agency_client = { path = "../misc/legacy/agency_client" } -bs58 = { version = "0.5" } indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", default-features = false, features = ["log"] } indy-credx = { git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.1.0", optional = true } libvdrtools = { path = "../misc/legacy/libvdrtools", optional = true } @@ -36,5 +35,4 @@ indy-ledger-response-parser = { path = "../misc/indy_ledger_response_parser" } lru = { version = "0.12.0" } [dev-dependencies] -test_utils = { path = "../misc/test_utils" } tokio = { version = "1.20", features = ["rt", "macros", "rt-multi-thread"] } diff --git a/aries/aries_vcx_core/src/wallet2/entry_tag.rs b/aries/aries_vcx_core/src/wallet2/entry_tag.rs index 65b966a5be..93496f7089 100644 --- a/aries/aries_vcx_core/src/wallet2/entry_tag.rs +++ b/aries/aries_vcx_core/src/wallet2/entry_tag.rs @@ -8,6 +8,7 @@ pub enum EntryTag { Plaintext(String, String), } +#[cfg(feature = "vdrtools_wallet")] impl From for (String, String) { fn from(value: EntryTag) -> Self { match value { @@ -17,6 +18,7 @@ impl From for (String, String) { } } +#[cfg(feature = "vdrtools_wallet")] impl From<(String, String)> for EntryTag { fn from(value: (String, String)) -> Self { if value.0.starts_with('~') { @@ -100,6 +102,7 @@ impl From for Vec { } } +#[cfg(feature = "vdrtools_wallet")] impl From for HashMap { fn from(value: EntryTags) -> Self { let tags: Vec = value.into(); @@ -112,6 +115,7 @@ impl From for HashMap { } } +#[cfg(feature = "vdrtools_wallet")] impl From> for EntryTags { fn from(value: HashMap) -> Self { Self { diff --git a/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_did_wallet.rs b/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_did_wallet.rs index 30ef83aee1..0e0589386d 100644 --- a/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_did_wallet.rs +++ b/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_did_wallet.rs @@ -4,7 +4,7 @@ use vdrtools::{DidMethod, DidValue, KeyInfo, Locator, MyDidInfo}; use crate::{ errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult}, wallet::{indy::IndySdkWallet, structs_io::UnpackMessageOutput}, - wallet2::{DidData, DidWallet, Key, UnpackedMessage}, + wallet2::{DidData, DidWallet, Key}, }; #[async_trait] @@ -94,7 +94,7 @@ impl DidWallet for IndySdkWallet { .await?) } - async fn unpack_message(&self, msg: &[u8]) -> VcxCoreResult { + async fn unpack_message(&self, msg: &[u8]) -> VcxCoreResult { let unpacked_bytes = Locator::instance() .crypto_controller .unpack_msg(serde_json::from_slice(msg)?, self.wallet_handle) @@ -105,107 +105,6 @@ impl DidWallet for IndySdkWallet { AriesVcxCoreError::from_msg(AriesVcxCoreErrorKind::ParsingError, err.to_string()) })?; - Ok(res.into()) - } -} - -#[cfg(test)] -mod tests { - use rand::{distributions::Alphanumeric, Rng}; - use test_utils::devsetup::create_indy_test_wallet_handle; - - use crate::{ - wallet::indy::IndySdkWallet, - wallet2::{DidWallet, Key}, - }; - - #[tokio::test] - #[ignore] - async fn test_indy_should_sign_and_verify() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let seed: String = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(32) - .map(char::from) - .collect(); - - let did_data = DidWallet::create_and_store_my_did(&wallet, Some(&seed), None) - .await - .unwrap(); - - let msg = "sign this".as_bytes(); - let sig = DidWallet::sign(&wallet, &did_data.verkey, msg) - .await - .unwrap(); - - let res = DidWallet::verify(&wallet, &did_data.verkey, msg, &sig) - .await - .unwrap(); - assert!(res); - } - - #[tokio::test] - #[ignore] - async fn test_indy_should_rotate_keys() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let seed: String = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(32) - .map(char::from) - .collect(); - - let did_data = DidWallet::create_and_store_my_did(&wallet, Some(&seed), None) - .await - .unwrap(); - - let key = wallet.did_key(&did_data.did).await.unwrap(); - - assert_eq!(did_data.verkey, key); - - let new_seed: String = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(32) - .map(char::from) - .collect(); - - let res = wallet - .replace_did_key_start(&did_data.did, Some(&new_seed)) - .await - .unwrap(); - - wallet.replace_did_key_apply(&did_data.did).await.unwrap(); - - let new_key = wallet.did_key(&did_data.did).await.unwrap(); - assert_eq!(res, new_key); - } - - #[tokio::test] - #[ignore] - async fn test_indy_should_pack_and_unpack() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let sender_data = DidWallet::create_and_store_my_did(&wallet, None, None) - .await - .unwrap(); - - let receiver_data = DidWallet::create_and_store_my_did(&wallet, None, None) - .await - .unwrap(); - - let receiver_key = Key { - pubkey_bs58: receiver_data.verkey, - }; - let msg = "pack me"; - - let packed = wallet - .pack_message(Some(sender_data.verkey), vec![receiver_key], msg.as_bytes()) - .await - .unwrap(); - - let unpacked = wallet.unpack_message(&packed).await.unwrap(); - - assert_eq!(msg, unpacked.message); + Ok(res) } } diff --git a/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_record_wallet.rs b/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_record_wallet.rs index 8145bfd56c..ec106abc9f 100644 --- a/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_record_wallet.rs +++ b/aries/aries_vcx_core/src/wallet2/indy_wallet/indy_record_wallet.rs @@ -8,7 +8,7 @@ use super::{SEARCH_OPTIONS, WALLET_OPTIONS}; use crate::{ errors::error::{AriesVcxCoreError, VcxCoreResult}, wallet::indy::IndySdkWallet, - wallet2::{Record, RecordUpdate, RecordWallet, SearchFilter}, + wallet2::{entry_tag::EntryTags, Record, RecordWallet, SearchFilter}, }; #[async_trait] @@ -48,27 +48,38 @@ impl RecordWallet for IndySdkWallet { Ok(indy_record.into()) } - async fn update_record(&self, record: RecordUpdate) -> VcxCoreResult<()> { - if let Some(tags) = record.tags { - Locator::instance() - .non_secret_controller - .update_record_tags( - self.wallet_handle, - record.category.clone(), - record.name.clone(), - tags.into(), - ) - .await?; - } - - if let Some(value) = record.value { - Locator::instance() - .non_secret_controller - .update_record_value(self.wallet_handle, record.category, record.name, value) - .await?; - } + async fn update_record_tags( + &self, + name: &str, + category: &str, + new_tags: EntryTags, + ) -> VcxCoreResult<()> { + Ok(Locator::instance() + .non_secret_controller + .update_record_tags( + self.wallet_handle, + category.into(), + name.into(), + new_tags.into(), + ) + .await?) + } - Ok(()) + async fn update_record_value( + &self, + name: &str, + category: &str, + new_value: &str, + ) -> VcxCoreResult<()> { + Ok(Locator::instance() + .non_secret_controller + .update_record_value( + self.wallet_handle, + category.into(), + name.into(), + new_value.into(), + ) + .await?) } async fn delete_record(&self, name: &str, category: &str) -> VcxCoreResult<()> { @@ -125,214 +136,3 @@ impl RecordWallet for IndySdkWallet { Ok(records) } } - -#[cfg(test)] -mod tests { - use test_utils::devsetup::create_indy_test_wallet_handle; - - use crate::{ - errors::error::AriesVcxCoreErrorKind, - wallet::indy::IndySdkWallet, - wallet2::{ - entry_tag::EntryTags, EntryTag, RecordBuilder, RecordUpdateBuilder, RecordWallet, - }, - }; - - #[tokio::test] - #[ignore] - async fn indy_wallet_should_create_record() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let name = "foo"; - let category = "my"; - let value = "bar"; - - let record1 = RecordBuilder::default() - .name(name.into()) - .category(category.into()) - .value(value.into()) - .build() - .unwrap(); - let record2 = RecordBuilder::default() - .name("baz".into()) - .category(category.into()) - .value("box".into()) - .build() - .unwrap(); - - wallet.add_record(record1).await.unwrap(); - wallet.add_record(record2).await.unwrap(); - - let res = wallet.get_record(name, category).await.unwrap(); - - assert_eq!(value, res.value); - } - - #[tokio::test] - #[ignore] - async fn indy_wallet_should_delete_record() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let name = "foo"; - let category = "my"; - let value = "bar"; - - let record = RecordBuilder::default() - .name(name.into()) - .category(category.into()) - .value(value.into()) - .build() - .unwrap(); - - wallet.add_record(record).await.unwrap(); - - let res = wallet.get_record(name, category).await.unwrap(); - - assert_eq!(value, res.value); - - wallet.delete_record(name, category).await.unwrap(); - - let err = wallet.get_record(name, category).await.unwrap_err(); - assert_eq!(AriesVcxCoreErrorKind::WalletRecordNotFound, err.kind()); - } - - #[tokio::test] - #[ignore] - async fn indy_wallet_should_search_for_records() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let name1 = "foo"; - let name2 = "foa"; - let name3 = "fob"; - let category1 = "my"; - let category2 = "your"; - let value = "xxx"; - - let mut record_builder = RecordBuilder::default(); - record_builder - .name(name1.into()) - .category(category1.into()) - .value(value.into()); - - let record1 = record_builder.build().unwrap(); - wallet.add_record(record1).await.unwrap(); - - let record2 = record_builder.name(name2.into()).build().unwrap(); - wallet.add_record(record2).await.unwrap(); - - let record3 = record_builder - .name(name3.into()) - .category(category2.into()) - .build() - .unwrap(); - wallet.add_record(record3).await.unwrap(); - - let res = wallet.search_record(category1, None).await.unwrap(); - - assert_eq!(2, res.len()); - } - - #[tokio::test] - #[ignore] - async fn indy_wallet_should_update_record() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let name = "foo"; - let category = "my"; - let value1 = "xxx"; - let value2 = "yyy"; - let tags1 = EntryTags::from_vec(vec![EntryTag::Plaintext("a".into(), "b".into())]); - let tags2 = EntryTags::default(); - - let record = RecordBuilder::default() - .name(name.into()) - .category(category.into()) - .tags(tags1.clone()) - .value(value1.into()) - .build() - .unwrap(); - wallet.add_record(record.clone()).await.unwrap(); - - let record_update = RecordUpdateBuilder::default() - .name(record.name) - .value(value2.into()) - .category(record.category) - .tags(tags2.clone()) - .build() - .unwrap(); - - wallet.update_record(record_update).await.unwrap(); - - let res = wallet.get_record(name, category).await.unwrap(); - assert_eq!(value2, res.value); - assert_eq!(tags2, res.tags); - } - - #[tokio::test] - #[ignore] - async fn indy_wallet_should_update_only_value() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let name = "foo"; - let category = "my"; - let value1 = "xxx"; - let value2 = "yyy"; - let tags = EntryTags::from_vec(vec![EntryTag::Plaintext("a".into(), "b".into())]); - - let record = RecordBuilder::default() - .name(name.into()) - .category(category.into()) - .tags(tags.clone()) - .value(value1.into()) - .build() - .unwrap(); - wallet.add_record(record.clone()).await.unwrap(); - - let record_update = RecordUpdateBuilder::default() - .name(record.name) - .value(value2.into()) - .category(record.category) - .build() - .unwrap(); - - wallet.update_record(record_update.clone()).await.unwrap(); - - let res = wallet.get_record(name, category).await.unwrap(); - assert_eq!(value2, res.value); - assert_eq!(tags, res.tags); - } - - #[tokio::test] - #[ignore] - async fn indy_wallet_should_update_only_tags() { - let wallet = IndySdkWallet::new(create_indy_test_wallet_handle().await); - - let name = "foo"; - let category = "my"; - let value = "xxx"; - let tags1 = EntryTags::from_vec(vec![EntryTag::Plaintext("a".into(), "b".into())]); - let tags2 = EntryTags::from_vec(vec![EntryTag::Plaintext("c".into(), "d".into())]); - - let record = RecordBuilder::default() - .name(name.into()) - .category(category.into()) - .tags(tags1.clone()) - .value(value.into()) - .build() - .unwrap(); - wallet.add_record(record.clone()).await.unwrap(); - - let record_update = RecordUpdateBuilder::default() - .name(record.name) - .category(record.category) - .tags(tags2.clone()) - .build() - .unwrap(); - - wallet.update_record(record_update.clone()).await.unwrap(); - - let res = wallet.get_record(name, category).await.unwrap(); - assert_eq!(value, res.value); - assert_eq!(tags2, res.tags); - } -} diff --git a/aries/aries_vcx_core/src/wallet2/mod.rs b/aries/aries_vcx_core/src/wallet2/mod.rs index 43f4b27168..067cded4f1 100644 --- a/aries/aries_vcx_core/src/wallet2/mod.rs +++ b/aries/aries_vcx_core/src/wallet2/mod.rs @@ -13,6 +13,7 @@ use crate::{errors::error::VcxCoreResult, wallet::structs_io::UnpackMessageOutpu pub mod indy_wallet; pub mod entry_tag; +pub mod utils; pub struct Key { pub pubkey_bs58: String, @@ -27,29 +28,14 @@ pub struct Record { pub tags: EntryTags, } -#[derive(Debug, Default, Clone, Builder)] -pub struct RecordUpdate { - pub category: String, - pub name: String, - #[builder(setter(strip_option), default = "None")] - pub value: Option, - #[builder(setter(strip_option), default = "None")] - pub tags: Option, -} - #[cfg(feature = "vdrtools_wallet")] impl From for Record { fn from(ir: IndyRecord) -> Self { - let tags = ir - .tags - .into_iter() - .map(|(key, value)| EntryTag::Plaintext(key, value)) - .collect(); Self { name: ir.id, category: ir.type_, value: ir.value, - tags, + tags: ir.tags.into(), } } } @@ -76,24 +62,6 @@ impl From for IndyRecord { } } -#[derive(Clone)] -pub struct UnpackedMessage { - pub message: String, - pub recipient_verkey: String, - pub sender_verkey: Option, -} - -#[cfg(feature = "vdrtools_wallet")] -impl From for UnpackedMessage { - fn from(value: UnpackMessageOutput) -> Self { - Self { - message: value.message, - recipient_verkey: value.recipient_verkey, - sender_verkey: value.sender_verkey, - } - } -} - #[derive(Debug, Deserialize, Serialize)] pub struct DidData { did: String, @@ -132,7 +100,7 @@ pub trait DidWallet { msg: &[u8], ) -> VcxCoreResult>; - async fn unpack_message(&self, msg: &[u8]) -> VcxCoreResult; + async fn unpack_message(&self, msg: &[u8]) -> VcxCoreResult; } #[async_trait] @@ -141,7 +109,19 @@ pub trait RecordWallet { async fn get_record(&self, name: &str, category: &str) -> VcxCoreResult; - async fn update_record(&self, record: RecordUpdate) -> VcxCoreResult<()>; + async fn update_record_tags( + &self, + name: &str, + category: &str, + new_tags: EntryTags, + ) -> VcxCoreResult<()>; + + async fn update_record_value( + &self, + name: &str, + category: &str, + new_value: &str, + ) -> VcxCoreResult<()>; async fn delete_record(&self, name: &str, category: &str) -> VcxCoreResult<()>; @@ -151,3 +131,287 @@ pub trait RecordWallet { search_filter: Option, ) -> VcxCoreResult>; } + +#[cfg(test)] +mod tests { + use super::BaseWallet2; + use crate::{ + errors::error::AriesVcxCoreErrorKind, + wallet::indy::IndySdkWallet, + wallet2::{ + entry_tag::{EntryTag, EntryTags}, + utils::random_seed, + DidWallet, Key, RecordBuilder, RecordWallet, + }, + }; + + async fn build_test_wallet() -> impl BaseWallet2 { + #[cfg(feature = "vdrtools_wallet")] + return dev_setup_indy_wallet().await; + } + + #[cfg(feature = "vdrtools_wallet")] + async fn dev_setup_indy_wallet() -> IndySdkWallet { + use crate::wallet::indy::{wallet::create_and_open_wallet, WalletConfig}; + + let config_wallet = WalletConfig { + wallet_name: format!("wallet_{}", uuid::Uuid::new_v4()), + wallet_key: "8dvfYSt5d1taSd6yJdpjq4emkwsPDDLYxkNFysFD2cZY".into(), + wallet_key_derivation: "RAW".into(), + wallet_type: None, + storage_config: None, + storage_credentials: None, + rekey: None, + rekey_derivation_method: None, + }; + let wallet_handle = create_and_open_wallet(&config_wallet).await.unwrap(); + + IndySdkWallet::new(wallet_handle) + } + + #[tokio::test] + async fn did_wallet_should_sign_and_verify() { + let wallet = build_test_wallet().await; + + let did_data = wallet + .create_and_store_my_did(Some(&random_seed()), None) + .await + .unwrap(); + + let msg = "sign this".as_bytes(); + let sig = wallet.sign(&did_data.verkey, msg).await.unwrap(); + + let res = wallet.verify(&did_data.verkey, msg, &sig).await.unwrap(); + assert!(res); + } + + #[tokio::test] + async fn did_wallet_should_rotate_keys() { + let wallet = build_test_wallet().await; + + let did_data = wallet + .create_and_store_my_did(Some(&random_seed()), None) + .await + .unwrap(); + + let key = wallet.did_key(&did_data.did).await.unwrap(); + + assert_eq!(did_data.verkey, key); + + let res = wallet + .replace_did_key_start(&did_data.did, Some(&random_seed())) + .await + .unwrap(); + + wallet.replace_did_key_apply(&did_data.did).await.unwrap(); + + let new_key = wallet.did_key(&did_data.did).await.unwrap(); + assert_eq!(res, new_key); + } + + #[tokio::test] + async fn did_wallet_should_pack_and_unpack() { + let wallet = build_test_wallet().await; + + let sender_data = wallet.create_and_store_my_did(None, None).await.unwrap(); + + let receiver_data = wallet.create_and_store_my_did(None, None).await.unwrap(); + + let receiver_key = Key { + pubkey_bs58: receiver_data.verkey, + }; + let msg = "pack me"; + + let packed = wallet + .pack_message(Some(sender_data.verkey), vec![receiver_key], msg.as_bytes()) + .await + .unwrap(); + + let unpacked = wallet.unpack_message(&packed).await.unwrap(); + + assert_eq!(msg, unpacked.message); + } + + #[tokio::test] + async fn record_wallet_should_create_record() { + let wallet = build_test_wallet().await; + + let name = "foo"; + let category = "my"; + let value = "bar"; + + let record1 = RecordBuilder::default() + .name(name.into()) + .category(category.into()) + .value(value.into()) + .build() + .unwrap(); + let record2 = RecordBuilder::default() + .name("baz".into()) + .category(category.into()) + .value("box".into()) + .build() + .unwrap(); + + wallet.add_record(record1).await.unwrap(); + wallet.add_record(record2).await.unwrap(); + + let res = wallet.get_record(name, category).await.unwrap(); + + assert_eq!(value, res.value); + } + + #[tokio::test] + async fn record_wallet_should_delete_record() { + let wallet = build_test_wallet().await; + + let name = "foo"; + let category = "my"; + let value = "bar"; + + let record = RecordBuilder::default() + .name(name.into()) + .category(category.into()) + .value(value.into()) + .build() + .unwrap(); + + wallet.add_record(record).await.unwrap(); + + let res = wallet.get_record(name, category).await.unwrap(); + + assert_eq!(value, res.value); + + wallet.delete_record(name, category).await.unwrap(); + + let err = wallet.get_record(name, category).await.unwrap_err(); + assert_eq!(AriesVcxCoreErrorKind::WalletRecordNotFound, err.kind()); + } + + #[tokio::test] + async fn record_wallet_should_search_for_records() { + let wallet = build_test_wallet().await; + + let name1 = "foo"; + let name2 = "foa"; + let name3 = "fob"; + let category1 = "my"; + let category2 = "your"; + let value = "xxx"; + + let mut record_builder = RecordBuilder::default(); + record_builder + .name(name1.into()) + .category(category1.into()) + .value(value.into()); + + let record1 = record_builder.build().unwrap(); + wallet.add_record(record1).await.unwrap(); + + let record2 = record_builder.name(name2.into()).build().unwrap(); + wallet.add_record(record2).await.unwrap(); + + let record3 = record_builder + .name(name3.into()) + .category(category2.into()) + .build() + .unwrap(); + wallet.add_record(record3).await.unwrap(); + + let res = wallet.search_record(category1, None).await.unwrap(); + + assert_eq!(2, res.len()); + } + + #[tokio::test] + async fn record_wallet_should_update_record() { + let wallet = build_test_wallet().await; + + let name = "foo"; + let category = "my"; + let value1 = "xxx"; + let value2 = "yyy"; + let tags1 = EntryTags::from_vec(vec![EntryTag::Plaintext("a".into(), "b".into())]); + let tags2 = EntryTags::default(); + + let record = RecordBuilder::default() + .name(name.into()) + .category(category.into()) + .tags(tags1.clone()) + .value(value1.into()) + .build() + .unwrap(); + wallet.add_record(record.clone()).await.unwrap(); + + wallet + .update_record_value(name, category, value2) + .await + .unwrap(); + wallet + .update_record_tags(name, category, tags2.clone()) + .await + .unwrap(); + + let res = wallet.get_record(name, category).await.unwrap(); + assert_eq!(value2, res.value); + assert_eq!(tags2, res.tags); + } + + #[tokio::test] + async fn record_wallet_should_update_only_value() { + let wallet = build_test_wallet().await; + + let name = "foo"; + let category = "my"; + let value1 = "xxx"; + let value2 = "yyy"; + let tags = EntryTags::from_vec(vec![EntryTag::Plaintext("a".into(), "b".into())]); + + let record = RecordBuilder::default() + .name(name.into()) + .category(category.into()) + .tags(tags.clone()) + .value(value1.into()) + .build() + .unwrap(); + wallet.add_record(record.clone()).await.unwrap(); + + wallet + .update_record_value(name, category, value2) + .await + .unwrap(); + + let res = wallet.get_record(name, category).await.unwrap(); + assert_eq!(value2, res.value); + assert_eq!(tags, res.tags); + } + + #[tokio::test] + async fn record_wallet_should_update_only_tags() { + let wallet = build_test_wallet().await; + + let name = "foo"; + let category = "my"; + let value = "xxx"; + let tags1 = EntryTags::from_vec(vec![EntryTag::Plaintext("a".into(), "b".into())]); + let tags2 = EntryTags::from_vec(vec![EntryTag::Plaintext("c".into(), "d".into())]); + + let record = RecordBuilder::default() + .name(name.into()) + .category(category.into()) + .tags(tags1.clone()) + .value(value.into()) + .build() + .unwrap(); + wallet.add_record(record.clone()).await.unwrap(); + + wallet + .update_record_tags(name, category, tags2.clone()) + .await + .unwrap(); + + let res = wallet.get_record(name, category).await.unwrap(); + assert_eq!(value, res.value); + assert_eq!(tags2, res.tags); + } +} diff --git a/aries/aries_vcx_core/src/wallet2/utils.rs b/aries/aries_vcx_core/src/wallet2/utils.rs new file mode 100644 index 0000000000..c39da4485f --- /dev/null +++ b/aries/aries_vcx_core/src/wallet2/utils.rs @@ -0,0 +1,9 @@ +use rand::{distributions::Alphanumeric, Rng}; + +pub fn random_seed() -> String { + rand::thread_rng() + .sample_iter(Alphanumeric) + .take(32) + .map(char::from) + .collect() +} diff --git a/aries/misc/test_utils/src/devsetup.rs b/aries/misc/test_utils/src/devsetup.rs index d780b639f3..03a40b5278 100644 --- a/aries/misc/test_utils/src/devsetup.rs +++ b/aries/misc/test_utils/src/devsetup.rs @@ -276,40 +276,6 @@ pub async fn dev_build_indy_wallet(key_seed: &str) -> (String, impl BaseWallet) (public_did, IndySdkWallet::new(wallet_handle)) } -#[cfg(feature = "vdrtools_wallet")] -pub async fn create_indy_test_wallet_handle() -> WalletHandle { - use aries_vcx_core::{ - wallet::indy::{wallet::create_and_open_wallet, IndySdkWallet}, - wallet2::{BaseWallet2, DidWallet, RecordWallet}, - }; - - let db_name = format!("mysqltest_{}", uuid::Uuid::new_v4()).replace('-', "_"); - let storage_config = json!({ - "read_host": "localhost", - "write_host": "localhost", - "port": 3306, - "db_name": db_name, - "default_connection_limit": 50 - }) - .to_string(); - let storage_credentials = json!({ - "user": "root", - "pass": "mysecretpassword" - }) - .to_string(); - let config_wallet = WalletConfigBuilder::default() - .wallet_name(format!("faber_wallet_{}", uuid::Uuid::new_v4())) - .wallet_key(DEFAULT_WALLET_KEY) - .wallet_key_derivation(WALLET_KDF_RAW) - .wallet_type("mysql") - .storage_config(storage_config) - .storage_credentials(storage_credentials) - .build() - .unwrap(); - - create_and_open_wallet(&config_wallet).await.unwrap() -} - #[allow(unreachable_code)] #[allow(unused_variables)] pub async fn dev_build_featured_anoncreds() -> impl BaseAnonCreds {