diff --git a/Cargo.lock b/Cargo.lock index a6bf01c2f0..3852c754a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5976,6 +5976,8 @@ dependencies = [ "napi", "napi-build", "napi-derive", + "serde", + "serde_json", "uuid 1.5.0", "wallet_migrator", ] diff --git a/aries/aries_vcx_core/src/wallet/askar/askar_utils.rs b/aries/aries_vcx_core/src/wallet/askar/askar_utils.rs index 50b2f2b109..80bdfbf966 100644 --- a/aries/aries_vcx_core/src/wallet/askar/askar_utils.rs +++ b/aries/aries_vcx_core/src/wallet/askar/askar_utils.rs @@ -3,7 +3,6 @@ use aries_askar::{ kms::{KeyAlg, LocalKey}, }; use public_key::{Key, KeyType}; - use serde::Deserialize; use crate::{ diff --git a/aries/aries_vcx_core/src/wallet/askar/askar_wallet_config.rs b/aries/aries_vcx_core/src/wallet/askar/askar_wallet_config.rs new file mode 100644 index 0000000000..e6094dda50 --- /dev/null +++ b/aries/aries_vcx_core/src/wallet/askar/askar_wallet_config.rs @@ -0,0 +1,61 @@ +use std::sync::Arc; + +use async_trait::async_trait; +use serde::Deserialize; + +use super::{key_method::KeyMethod, AskarWallet}; +use crate::{ + errors::error::VcxCoreResult, + wallet::base_wallet::{BaseWallet, ManageWallet}, +}; + +#[derive(Clone, Debug, Deserialize)] +pub struct AskarWalletConfig { + db_url: String, + key_method: KeyMethod, + pass_key: String, + profile: String, +} + +impl AskarWalletConfig { + pub fn new(db_url: &str, key_method: KeyMethod, pass_key: &str, profile: &str) -> Self { + Self { + db_url: db_url.into(), + key_method: key_method.into(), + pass_key: pass_key.into(), + profile: profile.into(), + } + } + + pub fn db_url(&self) -> &str { + &self.db_url + } + + pub fn key_method(&self) -> &KeyMethod { + &self.key_method + } + + pub fn pass_key(&self) -> &str { + &self.pass_key + } + + pub fn profile(&self) -> &str { + &self.profile + } +} + +#[async_trait] +impl ManageWallet for AskarWalletConfig { + async fn create_wallet(&self) -> VcxCoreResult> { + let askar_wallet = AskarWallet::create(&self, false).await?; + Ok(Arc::new(askar_wallet)) + } + + async fn open_wallet(&self) -> VcxCoreResult> { + Ok(Arc::new(AskarWallet::open(&self).await?)) + } + + async fn delete_wallet(&self) -> VcxCoreResult<()> { + todo!(); + } +} diff --git a/aries/aries_vcx_core/src/wallet/askar/key_method.rs b/aries/aries_vcx_core/src/wallet/askar/key_method.rs new file mode 100644 index 0000000000..bfefa44bde --- /dev/null +++ b/aries/aries_vcx_core/src/wallet/askar/key_method.rs @@ -0,0 +1,50 @@ +use aries_askar::{ + storage::{Argon2Level, KdfMethod}, + StoreKeyMethod, +}; +use serde::Deserialize; + +#[derive(Debug, Copy, Clone, Deserialize)] +pub enum KeyMethod { + DeriveKey(AskarKdfMethod), + RawKey, + Unprotected, +} + +impl From for StoreKeyMethod { + fn from(value: KeyMethod) -> Self { + match value { + KeyMethod::DeriveKey(payload) => StoreKeyMethod::DeriveKey(payload.into()), + KeyMethod::RawKey => StoreKeyMethod::RawKey, + KeyMethod::Unprotected => StoreKeyMethod::Unprotected, + } + } +} + +#[derive(Copy, Clone, Debug, Deserialize)] +pub enum AskarKdfMethod { + Argon2i(ArgonLevel), +} + +impl From for KdfMethod { + fn from(value: AskarKdfMethod) -> Self { + match value { + AskarKdfMethod::Argon2i(payload) => KdfMethod::Argon2i(payload.into()), + } + } +} + +#[derive(Copy, Clone, Debug, Deserialize)] +pub enum ArgonLevel { + Interactive, + Moderate, +} + +impl From for Argon2Level { + fn from(value: ArgonLevel) -> Self { + match value { + ArgonLevel::Interactive => Argon2Level::Interactive, + ArgonLevel::Moderate => Argon2Level::Moderate, + } + } +} diff --git a/aries/aries_vcx_core/src/wallet/askar/mod.rs b/aries/aries_vcx_core/src/wallet/askar/mod.rs index 71fc5fa559..b42e560a8b 100644 --- a/aries/aries_vcx_core/src/wallet/askar/mod.rs +++ b/aries/aries_vcx_core/src/wallet/askar/mod.rs @@ -1,22 +1,26 @@ use aries_askar::{ entry::EntryTag, kms::{KeyAlg, KeyEntry, LocalKey}, - PassKey, Session, Store, StoreKeyMethod, + Session, Store, }; use async_trait::async_trait; use public_key::Key; -use self::{askar_utils::local_key_to_bs58_name, rng_method::RngMethod}; +use self::{ + askar_utils::local_key_to_bs58_name, askar_wallet_config::AskarWalletConfig, + rng_method::RngMethod, +}; use super::base_wallet::{did_value::DidValue, record_category::RecordCategory, BaseWallet}; - use crate::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult}; mod all_askar_records; mod askar_did_wallet; mod askar_record_wallet; mod askar_utils; +pub mod askar_wallet_config; mod entry; mod entry_tags; +mod key_method; mod key_value; mod pack; mod packing_types; @@ -44,30 +48,34 @@ impl BaseWallet for AskarWallet { impl AskarWallet { pub async fn create( - db_url: &str, - key_method: StoreKeyMethod, - pass_key: PassKey<'_>, + wallet_config: &AskarWalletConfig, recreate: bool, - profile: &str, ) -> Result { - let backend = - Store::provision(db_url, key_method, pass_key, Some(profile.into()), recreate).await?; + let backend = Store::provision( + wallet_config.db_url(), + wallet_config.key_method().clone().into(), + wallet_config.pass_key().into(), + Some(wallet_config.profile().to_owned()), + recreate, + ) + .await?; Ok(Self { backend, - profile: profile.into(), + profile: wallet_config.profile().into(), }) } - pub async fn open( - db_url: &str, - key_method: Option, - pass_key: PassKey<'_>, - profile: &str, - ) -> Result { + pub async fn open(wallet_config: &AskarWalletConfig) -> Result { Ok(Self { - backend: Store::open(db_url, key_method, pass_key, Some(profile.into())).await?, - profile: profile.into(), + backend: Store::open( + wallet_config.db_url(), + Some(wallet_config.key_method().clone().into()), + wallet_config.pass_key().into(), + Some(wallet_config.profile().into()), + ) + .await?, + profile: wallet_config.profile().into(), }) } @@ -191,24 +199,23 @@ impl AskarWallet { #[cfg(test)] pub mod tests { - use crate::wallet::base_wallet::BaseWallet; + use std::sync::Arc; + + use crate::wallet::{ + askar::{askar_wallet_config::AskarWalletConfig, key_method::KeyMethod}, + base_wallet::{BaseWallet, ManageWallet}, + }; - pub async fn dev_setup_askar_wallet() -> Box { - use aries_askar::StoreKeyMethod; + pub async fn dev_setup_askar_wallet() -> Arc { use uuid::Uuid; - use crate::wallet::askar::AskarWallet; + let config = AskarWalletConfig::new( + "sqlite://:memory:", + KeyMethod::Unprotected, + "", + &Uuid::new_v4().to_string(), + ); - Box::new( - AskarWallet::create( - "sqlite://:memory:", - StoreKeyMethod::Unprotected, - None.into(), - true, - &Uuid::new_v4().to_string(), - ) - .await - .unwrap(), - ) + config.create_wallet().await.unwrap() } } diff --git a/aries/aries_vcx_core/src/wallet/askar/partial_record.rs b/aries/aries_vcx_core/src/wallet/askar/partial_record.rs index 764d5f46d4..5665de0485 100644 --- a/aries/aries_vcx_core/src/wallet/askar/partial_record.rs +++ b/aries/aries_vcx_core/src/wallet/askar/partial_record.rs @@ -1,3 +1,4 @@ +use super::{askar_utils::value_from_entry, key_value::KeyValue}; use crate::{ errors::error::VcxCoreResult, wallet::{ @@ -6,8 +7,6 @@ use crate::{ }, }; -use super::{askar_utils::value_from_entry, key_value::KeyValue}; - impl PartialRecord { pub fn from_askar_entry(entry: aries_askar::entry::Entry) -> VcxCoreResult { Ok(Self::builder() diff --git a/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs b/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs index 3ba3fffbcd..25c5cabaa4 100644 --- a/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs +++ b/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs @@ -22,13 +22,28 @@ pub trait ImportWallet { #[async_trait] pub trait ManageWallet { - async fn create_wallet(&self) -> VcxCoreResult<()>; + async fn create_wallet(&self) -> VcxCoreResult>; async fn open_wallet(&self) -> VcxCoreResult>; async fn delete_wallet(&self) -> VcxCoreResult<()>; } +#[async_trait] +impl ManageWallet for Box { + async fn create_wallet(&self) -> VcxCoreResult> { + self.as_ref().create_wallet().await + } + + async fn open_wallet(&self) -> VcxCoreResult> { + self.as_ref().open_wallet().await + } + + async fn delete_wallet(&self) -> VcxCoreResult<()> { + self.as_ref().delete_wallet().await + } +} + #[async_trait] pub trait BaseWallet: RecordWallet + DidWallet + Send + Sync + std::fmt::Debug { async fn export_wallet(&self, path: &str, backup_key: &str) -> VcxCoreResult<()>; @@ -63,7 +78,7 @@ impl BaseWallet for Arc { #[cfg(test)] mod tests { - use std::str::FromStr; + use std::{str::FromStr, sync::Arc}; use super::BaseWallet; use crate::{ @@ -76,7 +91,7 @@ mod tests { }; #[allow(unused_variables)] - async fn build_test_wallet() -> Box { + async fn build_test_wallet() -> Arc { #[cfg(feature = "vdrtools_wallet")] let wallet = { use crate::wallet::indy::tests::dev_setup_indy_wallet; @@ -489,14 +504,16 @@ mod tests { #[cfg(test)] #[cfg(all(feature = "vdrtools_wallet", feature = "askar_wallet"))] mod compat_tests { + use std::sync::Arc; + use crate::wallet::{ askar::tests::dev_setup_askar_wallet, base_wallet::BaseWallet, indy::tests::dev_setup_indy_wallet, }; async fn pack_and_unpack_anoncrypt( - sender: Box, - recipient: Box, + sender: Arc, + recipient: Arc, ) { let did_data = recipient.create_and_store_my_did(None, None).await.unwrap(); @@ -513,8 +530,8 @@ mod compat_tests { } async fn pack_and_unpack_authcrypt( - sender: Box, - recipient: Box, + sender: Arc, + recipient: Arc, ) { let sender_did_data = sender.create_and_store_my_did(None, None).await.unwrap(); let recipient_did_data = recipient.create_and_store_my_did(None, None).await.unwrap(); diff --git a/aries/aries_vcx_core/src/wallet/base_wallet/record.rs b/aries/aries_vcx_core/src/wallet/base_wallet/record.rs index 0c853bbb98..ab483825c7 100644 --- a/aries/aries_vcx_core/src/wallet/base_wallet/record.rs +++ b/aries/aries_vcx_core/src/wallet/base_wallet/record.rs @@ -1,9 +1,8 @@ use async_trait::async_trait; use typed_builder::TypedBuilder; -use crate::{errors::error::VcxCoreResult, wallet::record_tags::RecordTags}; - use super::record_category::RecordCategory; +use crate::{errors::error::VcxCoreResult, wallet::record_tags::RecordTags}; #[derive(Debug, Default, Clone, TypedBuilder)] pub struct Record { @@ -57,42 +56,6 @@ impl PartialRecord { pub fn tags(&self) -> &Option { &self.tags } - - // #[cfg(feature = "askar_wallet")] - // pub fn from_askar_entry(entry: aries_askar::entry::Entry) -> VcxCoreResult { - // use crate::wallet::askar::askar_utils::value_from_entry; - - // Ok(Self::builder() - // .name(entry.name.clone()) - // .category(Some(entry.category.clone())) - // .value(Some(value_from_entry(entry.clone())?)) - // .tags(Some(entry.tags.into())) - // .build()) - // } - - // #[cfg(feature = "askar_wallet")] - // pub fn from_askar_key_entry(key_entry: aries_askar::kms::KeyEntry) -> VcxCoreResult { - // use crate::wallet::askar::KeyValue; - - // let local_key = key_entry.load_local_key()?; - // let name = key_entry.name(); - // let tags = key_entry.tags_as_slice(); - - // // check for private key length!!!! - // let value = KeyValue::new( - // local_key_to_bs58_private_key(&local_key)?, - // local_key_to_bs58_public_key(&local_key)?, - // ); - - // let value = serde_json::to_string(&value)?; - - // Ok(Self::builder() - // .name(name.into()) - // .category(Some(INDY_KEY.into())) - // .value(Some(value)) - // .tags(Some(tags.to_vec().into())) - // .build()) - // } } #[async_trait] diff --git a/aries/aries_vcx_core/src/wallet/indy/all_indy_records.rs b/aries/aries_vcx_core/src/wallet/indy/all_indy_records.rs new file mode 100644 index 0000000000..2758d4fcd2 --- /dev/null +++ b/aries/aries_vcx_core/src/wallet/indy/all_indy_records.rs @@ -0,0 +1,30 @@ +use async_trait::async_trait; +use vdrtools::indy_wallet::iterator::WalletIterator; + +use crate::{ + errors::error::VcxCoreResult, + wallet::base_wallet::record::{AllRecords, PartialRecord}, +}; + +pub struct AllIndyRecords { + iterator: WalletIterator, +} + +impl AllIndyRecords { + pub fn new(iterator: WalletIterator) -> Self { + Self { iterator } + } +} + +#[async_trait] +impl AllRecords for AllIndyRecords { + fn total_count(&self) -> VcxCoreResult> { + Ok(self.iterator.get_total_count()?) + } + + async fn next(&mut self) -> VcxCoreResult> { + let item = self.iterator.next().await?; + + Ok(item.map(PartialRecord::from_wallet_record)) + } +} diff --git a/aries/aries_vcx_core/src/wallet/indy/indy_record_wallet.rs b/aries/aries_vcx_core/src/wallet/indy/indy_record_wallet.rs index d3ab11fa34..4de08f7c6a 100644 --- a/aries/aries_vcx_core/src/wallet/indy/indy_record_wallet.rs +++ b/aries/aries_vcx_core/src/wallet/indy/indy_record_wallet.rs @@ -1,13 +1,13 @@ use async_trait::async_trait; use indy_api_types::domain::wallet::IndyRecord; -use vdrtools::{indy_wallet::iterator::WalletIterator, Locator}; +use vdrtools::Locator; -use super::{indy_tags::IndyTags, WALLET_OPTIONS}; +use super::{all_indy_records::AllIndyRecords, indy_tags::IndyTags, WALLET_OPTIONS}; use crate::{ errors::error::VcxCoreResult, wallet::{ base_wallet::{ - record::{AllRecords, PartialRecord, Record}, + record::{AllRecords, Record}, record_category::RecordCategory, record_wallet::RecordWallet, search_filter::SearchFilter, @@ -17,29 +17,6 @@ use crate::{ }, }; -pub struct AllIndyRecords { - iterator: WalletIterator, -} - -impl AllIndyRecords { - pub fn new(iterator: WalletIterator) -> Self { - Self { iterator } - } -} - -#[async_trait] -impl AllRecords for AllIndyRecords { - fn total_count(&self) -> VcxCoreResult> { - Ok(self.iterator.get_total_count()?) - } - - async fn next(&mut self) -> VcxCoreResult> { - let item = self.iterator.next().await?; - - Ok(item.map(PartialRecord::from_wallet_record)) - } -} - #[async_trait] impl RecordWallet for IndySdkWallet { async fn all_records(&self) -> VcxCoreResult> { diff --git a/aries/aries_vcx_core/src/wallet/indy/indy_tags.rs b/aries/aries_vcx_core/src/wallet/indy/indy_tags.rs index c3ba3f45f9..71881b7866 100644 --- a/aries/aries_vcx_core/src/wallet/indy/indy_tags.rs +++ b/aries/aries_vcx_core/src/wallet/indy/indy_tags.rs @@ -26,7 +26,6 @@ impl IndyTags { .into_iter() .map(|(key, val)| RecordTag::new(&key, &val)) .collect(); - // let mut items: Vec = self.0.into_iter().map(RecordTag::from_pair).collect(); items.sort(); RecordTags::new(items) diff --git a/aries/aries_vcx_core/src/wallet/indy/mod.rs b/aries/aries_vcx_core/src/wallet/indy/mod.rs index cbecf4d0e8..8997f345d3 100644 --- a/aries/aries_vcx_core/src/wallet/indy/mod.rs +++ b/aries/aries_vcx_core/src/wallet/indy/mod.rs @@ -4,45 +4,27 @@ use async_trait::async_trait; use indy_api_types::domain::wallet::{default_key_derivation_method, IndyRecord}; use serde::Deserialize; use serde_json::Value; -use vdrtools::{indy_wallet::iterator::WalletIterator, Locator, WalletRecord}; +use vdrtools::Locator; use self::indy_tags::IndyTags; use super::base_wallet::{ - record::{AllRecords, PartialRecord, Record}, - record_category::RecordCategory, - search_filter::SearchFilter, - BaseWallet, + record::Record, record_category::RecordCategory, search_filter::SearchFilter, BaseWallet, }; use crate::{ errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult}, WalletHandle, }; +mod all_indy_records; mod indy_did_wallet; mod indy_record_wallet; mod indy_tags; mod indy_utils; pub mod indy_wallet_record; +mod partial_record; pub mod restore_wallet_configs; pub mod wallet_config; -impl PartialRecord { - pub fn from_wallet_record(wallet_record: WalletRecord) -> Self { - let name = wallet_record.get_id().into(); - let category = wallet_record.get_type(); - let value = wallet_record.get_value(); - - let found_tags = wallet_record.get_tags(); - - Self::builder() - .name(name) - .category(category.map(Into::into)) - .value(value.map(Into::into)) - .tags(found_tags.map(|tags| IndyTags::new(tags.clone()).into_record_tags())) - .build() - } -} - impl Record { pub fn try_from_indy_record(indy_record: IndyRecord) -> VcxCoreResult { Ok(Record::builder() @@ -166,29 +148,6 @@ impl BaseWallet for IndySdkWallet { } } -pub struct AllIndyRecords { - iterator: WalletIterator, -} - -impl AllIndyRecords { - pub fn new(iterator: WalletIterator) -> Self { - Self { iterator } - } -} - -#[async_trait] -impl AllRecords for AllIndyRecords { - fn total_count(&self) -> VcxCoreResult> { - Ok(self.iterator.get_total_count()?) - } - - async fn next(&mut self) -> VcxCoreResult> { - let item = self.iterator.next().await?; - - Ok(item.map(PartialRecord::from_wallet_record)) - } -} - #[cfg(test)] pub mod tests { use std::sync::Arc; diff --git a/aries/aries_vcx_core/src/wallet/indy/partial_record.rs b/aries/aries_vcx_core/src/wallet/indy/partial_record.rs new file mode 100644 index 0000000000..5c81dd4dda --- /dev/null +++ b/aries/aries_vcx_core/src/wallet/indy/partial_record.rs @@ -0,0 +1,21 @@ +use vdrtools::WalletRecord; + +use super::indy_tags::IndyTags; +use crate::wallet::base_wallet::record::PartialRecord; + +impl PartialRecord { + pub fn from_wallet_record(wallet_record: WalletRecord) -> Self { + let name = wallet_record.get_id().into(); + let category = wallet_record.get_type(); + let value = wallet_record.get_value(); + + let found_tags = wallet_record.get_tags(); + + Self::builder() + .name(name) + .category(category.map(Into::into)) + .value(value.map(Into::into)) + .tags(found_tags.map(|tags| IndyTags::new(tags.clone()).into_record_tags())) + .build() + } +} diff --git a/aries/aries_vcx_core/src/wallet/indy/wallet_config.rs b/aries/aries_vcx_core/src/wallet/indy/wallet_config.rs index 6f1b62af29..6b39809998 100644 --- a/aries/aries_vcx_core/src/wallet/indy/wallet_config.rs +++ b/aries/aries_vcx_core/src/wallet/indy/wallet_config.rs @@ -37,7 +37,7 @@ pub struct WalletConfig { #[async_trait] impl ManageWallet for WalletConfig { - async fn create_wallet(&self) -> VcxCoreResult<()> { + async fn create_wallet(&self) -> VcxCoreResult> { let credentials = vdrtools::types::domain::wallet::Credentials { key: self.wallet_key.clone(), key_derivation_method: parse_key_derivation_method(&self.wallet_key_derivation)?, @@ -70,14 +70,14 @@ impl ManageWallet for WalletConfig { .await; match res { - Ok(()) => Ok(()), + Ok(()) => self.open_wallet().await, Err(err) if err.kind() == IndyErrorKind::WalletAlreadyExists => { warn!( "wallet \"{}\" already exists. skipping creation", self.wallet_name ); - Ok(()) + self.open_wallet().await } Err(err) => Err(AriesVcxCoreError::from_msg( diff --git a/aries/misc/legacy/libvcx_core/Cargo.toml b/aries/misc/legacy/libvcx_core/Cargo.toml index f6f6e2b8d1..f4e7539d92 100644 --- a/aries/misc/legacy/libvcx_core/Cargo.toml +++ b/aries/misc/legacy/libvcx_core/Cargo.toml @@ -8,6 +8,9 @@ edition.workspace = true [features] fatal_warnings = [] +askar_wallet = ["aries_vcx/askar_wallet", "aries_vcx_core/askar_wallet"] +vdrtools_wallet = ["aries_vcx/vdrtools_wallet", "aries_vcx_core/vdrtools_wallet"] + [dependencies] num-traits = "0.2.0" once_cell = { version = "1.15" } @@ -23,7 +26,7 @@ serde = "1.0.97" serde_json = "1.0.40" serde_derive = "1.0.97" futures = { version = "0.3", default-features = false } -aries_vcx = { path = "../../../aries_vcx", features = ["credx", "vdrtools_wallet"] } +aries_vcx = { path = "../../../aries_vcx", features = ["credx"] } aries_vcx_core = { path = "../../../aries_vcx_core" } anoncreds_types = { path = "../../../../aries/misc/anoncreds_types" } diddoc_legacy = { path = "../diddoc_legacy" } diff --git a/aries/misc/legacy/libvcx_core/src/api_vcx/api_global/wallet.rs b/aries/misc/legacy/libvcx_core/src/api_vcx/api_global/wallet.rs index 5ecf3d1eac..d75df35653 100644 --- a/aries/misc/legacy/libvcx_core/src/api_vcx/api_global/wallet.rs +++ b/aries/misc/legacy/libvcx_core/src/api_vcx/api_global/wallet.rs @@ -58,8 +58,8 @@ fn setup_global_wallet(wallet: Arc) -> LibvcxResult<()> { Ok(()) } -pub async fn open_as_main_wallet( - wallet_config: &WalletConfig, +pub async fn open_as_main_wallet( + wallet_config: &T, ) -> LibvcxResult> { let wallet = wallet_config.open_wallet().await?; setup_global_wallet(wallet.clone())?; @@ -67,7 +67,7 @@ pub async fn open_as_main_wallet( } pub async fn create_and_open_as_main_wallet( - wallet_config: &WalletConfig, + wallet_config: &impl ManageWallet, ) -> LibvcxResult> { wallet_config.create_wallet().await?; let wallet = wallet_config.open_wallet().await?; @@ -91,7 +91,7 @@ pub async fn close_main_wallet() -> LibvcxResult<()> { Ok(()) } -pub async fn create_main_wallet(config: &WalletConfig) -> LibvcxResult<()> { +pub async fn create_main_wallet(config: &T) -> LibvcxResult<()> { let wallet_handle = create_and_open_as_main_wallet(config).await?; trace!("Created wallet with handle {:?}", wallet_handle); let wallet = get_main_wallet()?; @@ -426,7 +426,6 @@ mod tests { aries_vcx_core::wallet::indy::restore_wallet_configs::ImportWalletConfigs, global::settings::{DEFAULT_WALLET_BACKUP_KEY, DEFAULT_WALLET_KEY, WALLET_KDF_RAW}, }; - use aries_vcx_core::wallet::{ base_wallet::{record_category::RecordCategory, ManageWallet}, indy::{indy_wallet_record::IndyWalletRecord, wallet_config::WalletConfig}, diff --git a/aries/wrappers/vcx-napi-rs/Cargo.toml b/aries/wrappers/vcx-napi-rs/Cargo.toml index 7edeb4557d..4dbbadb99c 100644 --- a/aries/wrappers/vcx-napi-rs/Cargo.toml +++ b/aries/wrappers/vcx-napi-rs/Cargo.toml @@ -11,6 +11,11 @@ path = "src/lib.rs" crate-type = ["cdylib"] doctest = false +[features] + +askar_wallet = ["libvcx_core/askar_wallet"] +vdrtools_wallet = ["libvcx_core/vdrtools_wallet"] + [dependencies] libvcx_core = { path = "../../misc/legacy/libvcx_core" } wallet_migrator = { path = "../../misc/wallet_migrator" } @@ -20,6 +25,8 @@ napi-derive = { version = "2.10.1" } uuid = { version = "1.5.0", default-features = false, features = ["v4"] } chrono = "0.4.23" libvcx_logger = { path = "../../misc/legacy/libvcx_logger" } +serde = { version = "1.0.159", features = ["derive"] } +serde_json = "1.0.95" [build-dependencies] napi-build = "2.0.1" diff --git a/aries/wrappers/vcx-napi-rs/src/api/wallet.rs b/aries/wrappers/vcx-napi-rs/src/api/wallet.rs index 9665ae5f34..add1f4f625 100644 --- a/aries/wrappers/vcx-napi-rs/src/api/wallet.rs +++ b/aries/wrappers/vcx-napi-rs/src/api/wallet.rs @@ -3,6 +3,7 @@ use std::{ops::Deref, sync::Arc}; use libvcx_core::{ api_vcx::api_global::{ledger, wallet}, aries_vcx::aries_vcx_core::wallet::{ + askar::askar_wallet_config::AskarWalletConfig, base_wallet::{BaseWallet, ManageWallet}, indy::{restore_wallet_configs::ImportWalletConfigs, wallet_config::WalletConfig}, }, @@ -25,16 +26,36 @@ impl Deref for NapiWallet { } } +#[allow(unreachable_code)] +fn parse_wallet_config(config: &str) -> napi::Result> { + #[cfg(feature = "vdrtools_wallet")] + return Ok(Box::new( + serde_json::from_str::(&config) + .map_err(|err| { + LibvcxError::from_msg( + LibvcxErrorKind::InvalidConfiguration, + format!("Serialization error: {:?}", err), + ) + }) + .map_err(to_napi_err)?, + )); + + #[cfg(feature = "askar_wallet")] + return Ok(Box::new( + serde_json::from_str::(&config) + .map_err(|err| { + LibvcxError::from_msg( + LibvcxErrorKind::InvalidConfiguration, + format!("Serialization error: {:?}", err), + ) + }) + .map_err(to_napi_err)?, + )); +} + #[napi] pub async fn wallet_open_as_main(wallet_config: String) -> napi::Result { - let wallet_config = serde_json::from_str::(&wallet_config) - .map_err(|err| { - LibvcxError::from_msg( - LibvcxErrorKind::InvalidConfiguration, - format!("Serialization error: {:?}", err), - ) - }) - .map_err(to_napi_err)?; + let wallet_config = parse_wallet_config(&wallet_config)?; let wallet = wallet::open_as_main_wallet(&wallet_config) .await .map_err(to_napi_err)?; @@ -43,14 +64,7 @@ pub async fn wallet_open_as_main(wallet_config: String) -> napi::Result napi::Result<()> { - let wallet_config = serde_json::from_str::(&wallet_config) - .map_err(|err| { - LibvcxError::from_msg( - LibvcxErrorKind::InvalidConfiguration, - format!("Serialization error: {:?}", err), - ) - }) - .map_err(to_napi_err)?; + let wallet_config = parse_wallet_config(&wallet_config)?; wallet::create_main_wallet(&wallet_config) .await .map_err(to_napi_err)