Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <[email protected]>
  • Loading branch information
Ondrej Prazak committed Jan 26, 2024
1 parent 6c138c9 commit 2fd55fc
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 132 deletions.
5 changes: 2 additions & 3 deletions aries/aries_vcx_core/src/anoncreds/credx_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::{
wallet::{
base_wallet::{record::Record, search_filter::SearchFilter, BaseWallet, RecordWallet},
entry_tag::EntryTags,
indy::indy_tag::IndyTags,
},
};

Expand Down Expand Up @@ -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<String, String> = 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?;
Expand Down
25 changes: 4 additions & 21 deletions aries/aries_vcx_core/src/wallet/base_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ mod tests {
wallet::{
base_wallet::{DidWallet, Record, RecordWallet},
entry_tag::{EntryTag, EntryTags},
indy::IndySdkWallet,
},
};

Expand All @@ -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]
Expand Down
80 changes: 76 additions & 4 deletions aries/aries_vcx_core/src/wallet/entry_tag.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<A>(self, mut map: A) -> Result<Self::Value, A::Error>
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<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_map(EntryTagsVisitor)
}
}

impl EntryTags {
pub fn new(inner: Vec<EntryTag>) -> Self {
Self { inner }
Expand Down Expand Up @@ -77,3 +110,42 @@ impl From<EntryTags> for Vec<EntryTag> {
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);
}
}
8 changes: 2 additions & 6 deletions aries/aries_vcx_core/src/wallet/indy/indy_tag.rs
Original file line number Diff line number Diff line change
@@ -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));

Expand All @@ -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 {
Expand Down
26 changes: 24 additions & 2 deletions aries/aries_vcx_core/src/wallet/indy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
}
}
96 changes: 0 additions & 96 deletions aries/aries_vcx_core/src/wallet/indy/signing.rs

This file was deleted.

0 comments on commit 2fd55fc

Please sign in to comment.