diff --git a/aries/aries_vcx_core/src/anoncreds/credx_anoncreds.rs b/aries/aries_vcx_core/src/anoncreds/credx_anoncreds.rs index dab818a539..40887dda0a 100644 --- a/aries/aries_vcx_core/src/anoncreds/credx_anoncreds.rs +++ b/aries/aries_vcx_core/src/anoncreds/credx_anoncreds.rs @@ -32,7 +32,6 @@ use crate::{ wallet::{ base_wallet::{record::Record, search_filter::SearchFilter, BaseWallet, RecordWallet}, entry_tag::EntryTags, - indy::indy_tag::IndyTags, }, }; @@ -1029,13 +1028,13 @@ 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_map: HashMap = serde_json::from_value(tags.clone())?; + let tags = serde_json::from_value(tags.clone())?; let record = Record::builder() .name(credential_id.clone()) .category(CATEGORY_CREDENTIAL.into()) .value(record_value) - .tags(IndyTags::new(tags_map).into_entry_tags()) + .tags(tags) .build(); wallet.add_record(record).await?; 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 8a41a1c3f0..68a55413cd 100644 --- a/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs +++ b/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs @@ -83,7 +83,6 @@ mod tests { wallet::{ base_wallet::{DidWallet, Record, RecordWallet}, entry_tag::{EntryTag, EntryTags}, - indy::IndySdkWallet, }, }; @@ -97,26 +96,10 @@ mod tests { async fn build_test_wallet() -> impl BaseWallet { #[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) + { + use crate::wallet::indy::test::dev_setup_indy_wallet; + dev_setup_indy_wallet().await + } } #[tokio::test] diff --git a/aries/aries_vcx_core/src/wallet/entry_tag.rs b/aries/aries_vcx_core/src/wallet/entry_tag.rs index a21e6ed1d1..304fd4ca4c 100644 --- a/aries/aries_vcx_core/src/wallet/entry_tag.rs +++ b/aries/aries_vcx_core/src/wallet/entry_tag.rs @@ -1,4 +1,6 @@ -use serde::{ser::SerializeMap, Deserialize, Serialize}; +use std::fmt; + +use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub enum EntryTag { @@ -19,15 +21,46 @@ impl Serialize for EntryTags { let mut map = serializer.serialize_map(Some(self.inner.len()))?; for tag in self.inner.iter() { match tag { - EntryTag::Encrypted(key, val) | EntryTag::Plaintext(key, val) => { - map.serialize_entry(&key, &val)? - } + EntryTag::Encrypted(key, val) => map.serialize_entry(&key, &val)?, + EntryTag::Plaintext(key, val) => map.serialize_entry(&format!("~{}", key), &val)?, } } map.end() } } +struct EntryTagsVisitor; + +impl<'de> Visitor<'de> for EntryTagsVisitor { + type Value = EntryTags; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a map representing tags") + } + + fn visit_map(self, mut map: A) -> Result + where + A: serde::de::MapAccess<'de>, + { + let mut tags = EntryTags::new(vec![]); + + while let Some(pair) = map.next_entry()? { + tags.add(pair_into_entry_tag(pair)); + } + + Ok(tags) + } +} + +impl<'de> Deserialize<'de> for EntryTags { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_map(EntryTagsVisitor) + } +} + impl EntryTags { pub fn new(inner: Vec) -> Self { Self { inner } @@ -77,3 +110,42 @@ impl From for Vec { value.inner } } + +pub fn pair_into_entry_tag(pair: (String, String)) -> EntryTag { + if pair.0.starts_with('~') { + EntryTag::Plaintext(pair.0.trim_start_matches('~').into(), pair.1) + } else { + EntryTag::Encrypted(pair.0, pair.1) + } +} + +#[cfg(test)] +mod tests { + use crate::wallet::entry_tag::{EntryTag, EntryTags}; + + #[test] + fn should_serialize_entry_tags() { + let tags = EntryTags::new(vec![ + EntryTag::Plaintext("a".into(), "b".into()), + EntryTag::Encrypted("c".into(), "d".into()), + ]); + + let res = serde_json::to_string(&tags).unwrap(); + + assert_eq!("{\"~a\":\"b\",\"c\":\"d\"}", res); + } + + #[test] + fn shoud_deserialize_entry_tags() { + let json = "{\"a\":\"b\",\"~c\":\"d\"}"; + + let tags = EntryTags::new(vec![ + EntryTag::Encrypted("a".into(), "b".into()), + EntryTag::Plaintext("c".into(), "d".into()), + ]); + + let res = serde_json::from_str(json).unwrap(); + + assert_eq!(tags, res); + } +} diff --git a/aries/aries_vcx_core/src/wallet/indy/indy_tag.rs b/aries/aries_vcx_core/src/wallet/indy/indy_tag.rs index fe32c1b18e..bfa010bd61 100644 --- a/aries/aries_vcx_core/src/wallet/indy/indy_tag.rs +++ b/aries/aries_vcx_core/src/wallet/indy/indy_tag.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::wallet::entry_tag::{EntryTag, EntryTags}; +use crate::wallet::entry_tag::{pair_into_entry_tag, EntryTag, EntryTags}; pub struct IndyTag((String, String)); @@ -16,11 +16,7 @@ impl IndyTag { pub fn into_entry_tag(self) -> EntryTag { let inner = self.into_inner(); - if inner.0.starts_with('~') { - EntryTag::Plaintext(inner.0.trim_start_matches('~').into(), inner.1) - } else { - EntryTag::Encrypted(inner.0, inner.1) - } + pair_into_entry_tag(inner) } pub fn from_entry_tag(tag: EntryTag) -> Self { diff --git a/aries/aries_vcx_core/src/wallet/indy/mod.rs b/aries/aries_vcx_core/src/wallet/indy/mod.rs index fd0585f6f2..0a49bab753 100644 --- a/aries/aries_vcx_core/src/wallet/indy/mod.rs +++ b/aries/aries_vcx_core/src/wallet/indy/mod.rs @@ -8,9 +8,8 @@ use crate::{errors::error::VcxCoreResult, WalletHandle}; mod indy_did_wallet; mod indy_record_wallet; -pub mod indy_tag; +mod indy_tag; pub mod internal; -pub mod signing; pub mod wallet; pub mod wallet_non_secrets; @@ -134,3 +133,26 @@ const WALLET_OPTIONS: &str = const SEARCH_OPTIONS: &str = r#"{"retrieveType": true, "retrieveValue": true, "retrieveTags": true, "retrieveRecords": true}"#; impl BaseWallet for IndySdkWallet {} + +#[cfg(test)] +pub mod test { + use super::IndySdkWallet; + + pub 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) + } +} diff --git a/aries/aries_vcx_core/src/wallet/indy/signing.rs b/aries/aries_vcx_core/src/wallet/indy/signing.rs deleted file mode 100644 index d50c75ea55..0000000000 --- a/aries/aries_vcx_core/src/wallet/indy/signing.rs +++ /dev/null @@ -1,96 +0,0 @@ -use vdrtools::Locator; - -use crate::{ - errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult}, - WalletHandle, -}; - -pub async fn vdrtools_sign( - wallet_handle: WalletHandle, - my_vk: &str, - msg: &[u8], -) -> VcxCoreResult> { - let res = Locator::instance() - .crypto_controller - .crypto_sign(wallet_handle, my_vk, msg) - .await?; - - Ok(res) -} - -pub async fn vdrtools_verify(vk: &str, msg: &[u8], signature: &[u8]) -> VcxCoreResult { - let res = Locator::instance() - .crypto_controller - .crypto_verify(vk, msg, signature) - .await?; - - Ok(res) -} - -pub async fn vdrtools_pack_message( - wallet_handle: WalletHandle, - sender_vk: Option<&str>, - receiver_keys: &str, - msg: &[u8], -) -> VcxCoreResult> { - // parse json array of keys - let receiver_list = serde_json::from_str::>(receiver_keys) - .map_err(|_| { - AriesVcxCoreError::from_msg( - AriesVcxCoreErrorKind::InvalidJson, - "Invalid RecipientKeys has been passed", - ) - }) - .and_then(|list| { - // break early and error out if no receivers keys are provided - if list.is_empty() { - Err(AriesVcxCoreError::from_msg( - AriesVcxCoreErrorKind::InvalidLibindyParam, - "Empty RecipientKeys has been passed", - )) - } else { - Ok(list) - } - })?; - - let res = Locator::instance() - .crypto_controller - .pack_msg( - msg.into(), - receiver_list, - sender_vk.map(ToOwned::to_owned), - wallet_handle, - ) - .await?; - - Ok(res) -} - -pub async fn vdrtools_unpack_message( - wallet_handle: WalletHandle, - msg: &[u8], -) -> VcxCoreResult> { - let res = Locator::instance() - .crypto_controller - .unpack_msg(serde_json::from_slice(msg)?, wallet_handle) - .await?; - - Ok(res) -} - -pub async fn create_key(wallet_handle: WalletHandle, seed: Option<&str>) -> VcxCoreResult { - use vdrtools::KeyInfo; - - let res = Locator::instance() - .crypto_controller - .create_key( - wallet_handle, - &KeyInfo { - seed: seed.map(ToOwned::to_owned), - crypto_type: None, - }, - ) - .await?; - - Ok(res) -}