diff --git a/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs b/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs index 6a618033fb..9ea2ea7432 100644 --- a/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs +++ b/aries/agents/rust/mediator/tests/common/agent_and_transport_utils.rs @@ -81,7 +81,7 @@ pub async fn send_message_and_pop_response_message( let EncryptionEnvelope(packed_message) = agent .pack_didcomm(message_bytes, our_verkey, their_diddoc) .await - .map_err(|e| GenericStringError { msg: e.to_string() })?; + .map_err(|e| GenericStringError { msg: e })?; let packed_json = serde_json::from_slice(&packed_message)?; // Send serialized envelope over transport let response_envelope = aries_transport @@ -103,7 +103,7 @@ pub async fn gen_and_register_recipient_key( ) -> Result<(VerKey, AriesDidDoc)> { let agent_invite: OOBInvitation = agent .get_oob_invite() - .map_err(|e| GenericStringError { msg: e.to_string() })?; + .map_err(|e| GenericStringError { msg: e })?; let mock_ledger = MockLedger {}; let agent_diddoc = oob_invitation_to_legacy_did_doc(&mock_ledger, &agent_invite) .await diff --git a/aries/aries_vcx_core/src/anoncreds/anoncreds/mod.rs b/aries/aries_vcx_core/src/anoncreds/anoncreds/mod.rs index c69f17a167..5078da84c3 100644 --- a/aries/aries_vcx_core/src/anoncreds/anoncreds/mod.rs +++ b/aries/aries_vcx_core/src/anoncreds/anoncreds/mod.rs @@ -77,25 +77,11 @@ use crate::{ constants::ATTRS, json::{AsTypeOrDeserializationError, TryGetIndex}, }, - wallet::base_wallet::{record::Record, search_filter::SearchFilter, BaseWallet}, + wallet::base_wallet::{ + record::Record, record_category::RecordCategory, search_filter::SearchFilter, BaseWallet, + }, }; -pub const CATEGORY_LINK_SECRET: &str = "VCX_LINK_SECRET"; - -pub const CATEGORY_CREDENTIAL: &str = "VCX_CREDENTIAL"; -pub const CATEGORY_CRED_DEF: &str = "VCX_CRED_DEF"; -pub const CATEGORY_CRED_KEY_CORRECTNESS_PROOF: &str = "VCX_CRED_KEY_CORRECTNESS_PROOF"; -pub const CATEGORY_CRED_DEF_PRIV: &str = "VCX_CRED_DEF_PRIV"; -pub const CATEGORY_CRED_SCHEMA: &str = "VCX_CRED_SCHEMA"; - -pub const CATEGORY_CRED_MAP_SCHEMA_ID: &str = "VCX_CRED_MAP_SCHEMA_ID"; - -pub const CATEGORY_REV_REG: &str = "VCX_REV_REG"; -pub const CATEGORY_REV_REG_DELTA: &str = "VCX_REV_REG_DELTA"; -pub const CATEGORY_REV_REG_INFO: &str = "VCX_REV_REG_INFO"; -pub const CATEGORY_REV_REG_DEF: &str = "VCX_REV_REG_DEF"; -pub const CATEGORY_REV_REG_DEF_PRIV: &str = "VCX_REV_REG_DEF_PRIV"; - fn from_revocation_registry_delta_to_revocation_status_list( delta: &RevocationRegistryDeltaValue, rev_reg_def: &AnoncredsRevocationRegistryDefinition, @@ -193,7 +179,7 @@ impl Anoncreds { async fn get_wallet_record_value( &self, wallet: &impl BaseWallet, - category: &str, + category: RecordCategory, id: &str, ) -> VcxCoreResult where @@ -209,7 +195,7 @@ impl Anoncreds { link_secret_id: &LinkSecretId, ) -> VcxCoreResult { let ms_decimal = wallet - .get_record(CATEGORY_LINK_SECRET, link_secret_id) + .get_record(RecordCategory::LinkSecret, link_secret_id) .await?; Ok(ms_decimal.value().try_into().unwrap()) @@ -221,7 +207,7 @@ impl Anoncreds { credential_id: &str, ) -> VcxCoreResult { let cred_record = wallet - .get_record(CATEGORY_CREDENTIAL, credential_id) + .get_record(RecordCategory::Cred, credential_id) .await?; let credential: Credential = serde_json::from_str(cred_record.value())?; @@ -235,7 +221,7 @@ impl Anoncreds { ) -> VcxCoreResult> { let records = wallet .search_record( - CATEGORY_CREDENTIAL, + RecordCategory::Cred, Some(SearchFilter::JsonFilter(wql.into())), ) .await?; @@ -351,7 +337,7 @@ impl BaseAnonCreds for Anoncreds { let mut tails_writer = TailsFileWriter::new(Some(tails_dir.to_str().unwrap().to_string())); let cred_def: AnoncredsCredentialDefinition = self - .get_wallet_record_value(wallet, CATEGORY_CRED_DEF, &cred_def_id.to_string()) + .get_wallet_record_value(wallet, RecordCategory::CredDef, &cred_def_id.to_string()) .await?; let rev_reg_id = make_revocation_registry_id(issuer_did, cred_def_id, tag, RegistryType::CL_ACCUM)?; @@ -389,7 +375,7 @@ impl BaseAnonCreds for Anoncreds { let str_rev_reg_info = serde_json::to_string(&rev_reg_info)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG_INFO.to_string()) + .category(RecordCategory::RevRegInfo) .value(str_rev_reg_info) .build(); wallet.add_record(record).await?; @@ -411,7 +397,7 @@ impl BaseAnonCreds for Anoncreds { let str_rev_reg_def = serde_json::to_string(&rev_reg_def_val)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG_DEF.to_string()) + .category(RecordCategory::RevRegDef) .value(str_rev_reg_def.clone()) .build(); wallet.add_record(record).await?; @@ -419,7 +405,7 @@ impl BaseAnonCreds for Anoncreds { let str_rev_reg_def_priv = serde_json::to_string(&rev_reg_def_priv)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG_DEF_PRIV.to_string()) + .category(RecordCategory::RevRegDefPriv) .value(str_rev_reg_def_priv) .build(); wallet.add_record(record).await?; @@ -428,7 +414,7 @@ impl BaseAnonCreds for Anoncreds { let str_rev_reg = serde_json::to_string(&rev_reg)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG.to_string()) + .category(RecordCategory::RevReg) .value(str_rev_reg.clone()) .build(); wallet.add_record(record).await?; @@ -464,7 +450,7 @@ impl BaseAnonCreds for Anoncreds { // If cred def already exists, return it if let Ok(cred_def) = self - .get_wallet_record_value(wallet, CATEGORY_CRED_DEF, &cred_def_id.0) + .get_wallet_record_value(wallet, RecordCategory::CredDef, &cred_def_id.0) .await { // TODO! Convert? @@ -492,7 +478,7 @@ impl BaseAnonCreds for Anoncreds { let str_cred_def = serde_json::to_string(&cred_def)?; let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_DEF.to_string()) + .category(RecordCategory::CredDef) .value(str_cred_def.clone()) .build(); wallet.add_record(record).await?; @@ -500,7 +486,7 @@ impl BaseAnonCreds for Anoncreds { let str_cred_def_priv = serde_json::to_string(&cred_def_priv)?; let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_DEF_PRIV.to_string()) + .category(RecordCategory::CredDefPriv) .value(str_cred_def_priv) .build(); wallet.add_record(record).await?; @@ -508,14 +494,14 @@ impl BaseAnonCreds for Anoncreds { let str_cred_key_proof = serde_json::to_string(&cred_key_correctness_proof)?; let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_KEY_CORRECTNESS_PROOF.to_string()) + .category(RecordCategory::CredKeyCorrectnessProof) .value(str_cred_key_proof) .build(); wallet.add_record(record).await?; let record = Record::builder() .name(schema_id.to_string()) - .category(CATEGORY_CRED_SCHEMA.to_string()) + .category(RecordCategory::CredSchema) .value(serde_json::to_string(&schema_json)?) .build(); let store_schema_res = wallet.add_record(record).await; @@ -528,9 +514,9 @@ impl BaseAnonCreds for Anoncreds { let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_MAP_SCHEMA_ID.to_string()) + .category(RecordCategory::CredMapSchemaId) // .value(schema_id.to_string()) - .value(json!({"schemaId": schema_id}).to_string()) + .value(json!({ "schemaId": schema_id }).to_string()) .build(); wallet.add_record(record).await?; @@ -545,7 +531,7 @@ impl BaseAnonCreds for Anoncreds { let correctness_proof = self .get_wallet_record_value( wallet, - CATEGORY_CRED_KEY_CORRECTNESS_PROOF, + RecordCategory::CredKeyCorrectnessProof, &cred_def_id.to_string(), ) .await?; @@ -553,7 +539,7 @@ impl BaseAnonCreds for Anoncreds { let schema_id_value = self .get_wallet_record_value::( wallet, - CATEGORY_CRED_MAP_SCHEMA_ID, + RecordCategory::CredMapSchemaId, &cred_def_id.to_string(), ) .await?; @@ -583,29 +569,29 @@ impl BaseAnonCreds for Anoncreds { let cred_def_id = &cred_offer.cred_def_id.0; let cred_def = self - .get_wallet_record_value(wallet, CATEGORY_CRED_DEF, cred_def_id) + .get_wallet_record_value(wallet, RecordCategory::CredDef, cred_def_id) .await?; let cred_def_private = self - .get_wallet_record_value(wallet, CATEGORY_CRED_DEF_PRIV, cred_def_id) + .get_wallet_record_value(wallet, RecordCategory::CredDefPriv, cred_def_id) .await?; let rev_reg_id = rev_reg_id.map(ToString::to_string); let mut revocation_config_parts = match (tails_dir, &rev_reg_id) { (Some(tails_dir), Some(rev_reg_def_id)) => { let rev_reg_def: AnoncredsRevocationRegistryDefinition = self - .get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF, rev_reg_def_id) + .get_wallet_record_value(wallet, RecordCategory::RevRegDef, rev_reg_def_id) .await?; let rev_reg_def_priv = self - .get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF_PRIV, rev_reg_def_id) + .get_wallet_record_value(wallet, RecordCategory::RevRegDefPriv, rev_reg_def_id) .await?; let rev_reg: AnoncredsRevocationRegistry = self - .get_wallet_record_value(wallet, CATEGORY_REV_REG, rev_reg_def_id) + .get_wallet_record_value(wallet, RecordCategory::RevReg, rev_reg_def_id) .await?; let rev_reg_info: RevocationRegistryInfo = self - .get_wallet_record_value(wallet, CATEGORY_REV_REG_INFO, rev_reg_def_id) + .get_wallet_record_value(wallet, RecordCategory::RevRegInfo, rev_reg_def_id) .await?; let rev_reg_def_id = @@ -695,11 +681,11 @@ impl BaseAnonCreds for Anoncreds { }; let str_rev_reg = serde_json::to_string(&rev_reg)?; wallet - .update_record_value(CATEGORY_REV_REG, &rev_reg_id, &str_rev_reg) + .update_record_value(RecordCategory::RevReg, &rev_reg_id, &str_rev_reg) .await?; wallet - .update_record_value(CATEGORY_REV_REG_INFO, &rev_reg_id, &str_rev_reg_info) + .update_record_value(RecordCategory::RevRegInfo, &rev_reg_id, &str_rev_reg_info) .await?; Some(cred_rev_id) @@ -1152,7 +1138,7 @@ impl BaseAnonCreds for Anoncreds { let record = Record::builder() .name(credential_id.clone()) - .category(CATEGORY_CREDENTIAL.into()) + .category(RecordCategory::Cred) .value(record_value) .tags(tags) .build(); @@ -1167,7 +1153,7 @@ impl BaseAnonCreds for Anoncreds { wallet: &impl BaseWallet, cred_id: &CredentialId, ) -> VcxCoreResult<()> { - wallet.delete_record(CATEGORY_CREDENTIAL, cred_id).await + wallet.delete_record(RecordCategory::Cred, cred_id).await } async fn prover_create_link_secret( @@ -1176,7 +1162,7 @@ impl BaseAnonCreds for Anoncreds { link_secret_id: &LinkSecretId, ) -> VcxCoreResult<()> { let existing_record = wallet - .get_record(CATEGORY_LINK_SECRET, link_secret_id) + .get_record(RecordCategory::LinkSecret, link_secret_id) .await .ok(); // ignore error, as we only care about whether it exists or not @@ -1200,7 +1186,7 @@ impl BaseAnonCreds for Anoncreds { let record = Record::builder() .name(link_secret_id.into()) - .category(CATEGORY_LINK_SECRET.into()) + .category(RecordCategory::LinkSecret) .value(ms_decimal) .build(); wallet.add_record(record).await?; @@ -1232,7 +1218,7 @@ impl BaseAnonCreds for Anoncreds { ledger_rev_reg_delta_json: RevocationRegistryDelta, ) -> VcxCoreResult<()> { let rev_reg_def: RevocationRegistryDefinition = self - .get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF, &rev_reg_id.to_string()) + .get_wallet_record_value(wallet, RecordCategory::RevRegDef, &rev_reg_id.to_string()) .await?; let last_rev_reg_delta_stored = self.get_rev_reg_delta(wallet, rev_reg_id).await?; @@ -1254,13 +1240,17 @@ impl BaseAnonCreds for Anoncreds { let cred_def = self .get_wallet_record_value( wallet, - CATEGORY_CRED_DEF, + RecordCategory::CredDef, &rev_reg_def.cred_def_id.to_string(), ) .await?; let rev_reg_def_priv = self - .get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF_PRIV, &rev_reg_id.to_string()) + .get_wallet_record_value( + wallet, + RecordCategory::RevRegDefPriv, + &rev_reg_id.to_string(), + ) .await?; let updated_rev_status_list = anoncreds::issuer::update_revocation_status_list( @@ -1284,7 +1274,7 @@ impl BaseAnonCreds for Anoncreds { if last_rev_reg_delta_stored.is_some() { wallet .update_record_value( - CATEGORY_REV_REG_DELTA, + RecordCategory::RevRegDelta, &rev_reg_id.to_string(), &updated_revocation_registry_delta_str, ) @@ -1292,7 +1282,7 @@ impl BaseAnonCreds for Anoncreds { } else { let record = Record::builder() .name(rev_reg_id.to_string()) - .category(CATEGORY_REV_REG_DELTA.into()) + .category(RecordCategory::RevRegDelta) .value(updated_revocation_registry_delta_str) .build(); wallet.add_record(record).await?; @@ -1309,7 +1299,7 @@ impl BaseAnonCreds for Anoncreds { let res_rev_reg_delta = self .get_wallet_record_value::( wallet, - CATEGORY_REV_REG_DELTA, + RecordCategory::RevRegDelta, &rev_reg_id.to_string(), ) .await; @@ -1332,7 +1322,7 @@ impl BaseAnonCreds for Anoncreds { ) -> VcxCoreResult<()> { if self.get_rev_reg_delta(wallet, rev_reg_id).await?.is_some() { wallet - .delete_record(CATEGORY_REV_REG_DELTA, &rev_reg_id.to_string()) + .delete_record(RecordCategory::RevRegDelta, &rev_reg_id.to_string()) .await?; } diff --git a/aries/aries_vcx_core/src/anoncreds/credx_anoncreds/mod.rs b/aries/aries_vcx_core/src/anoncreds/credx_anoncreds/mod.rs index 478dbea79d..036a6e4b2b 100644 --- a/aries/aries_vcx_core/src/anoncreds/credx_anoncreds/mod.rs +++ b/aries/aries_vcx_core/src/anoncreds/credx_anoncreds/mod.rs @@ -68,28 +68,14 @@ use crate::{ json::{AsTypeOrDeserializationError, TryGetIndex}, }, wallet::{ - base_wallet::{record::Record, search_filter::SearchFilter, BaseWallet, RecordWallet}, + base_wallet::{ + record::Record, record_category::RecordCategory, search_filter::SearchFilter, + BaseWallet, RecordWallet, + }, record_tags::RecordTags, }, }; -pub const CATEGORY_LINK_SECRET: &str = "VCX_LINK_SECRET"; - -pub const CATEGORY_CREDENTIAL: &str = "VCX_CREDENTIAL"; -pub const CATEGORY_CRED_DEF: &str = "VCX_CRED_DEF"; -pub const CATEGORY_CRED_KEY_CORRECTNESS_PROOF: &str = "VCX_CRED_KEY_CORRECTNESS_PROOF"; -pub const CATEGORY_CRED_DEF_PRIV: &str = "VCX_CRED_DEF_PRIV"; -pub const CATEGORY_CRED_SCHEMA: &str = "VCX_CRED_SCHEMA"; - -// Category used for mapping a cred_def_id to a schema_id -pub const CATEGORY_CRED_MAP_SCHEMA_ID: &str = "VCX_CRED_MAP_SCHEMA_ID"; - -pub const CATEGORY_REV_REG: &str = "VCX_REV_REG"; -pub const CATEGORY_REV_REG_DELTA: &str = "VCX_REV_REG_DELTA"; -pub const CATEGORY_REV_REG_INFO: &str = "VCX_REV_REG_INFO"; -pub const CATEGORY_REV_REG_DEF: &str = "VCX_REV_REG_DEF"; -pub const CATEGORY_REV_REG_DEF_PRIV: &str = "VCX_REV_REG_DEF_PRIV"; - #[derive(Debug, Deserialize, Serialize, Clone)] pub struct RevocationRegistryInfo { pub id: CredxRevocationRegistryId, @@ -108,13 +94,13 @@ impl RecordWallet for WalletAdapter { self.0.add_record(record).await } - async fn get_record(&self, category: &str, name: &str) -> VcxCoreResult { + async fn get_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult { self.0.get_record(category, name).await } async fn update_record_tags( &self, - category: &str, + category: RecordCategory, name: &str, new_tags: RecordTags, ) -> VcxCoreResult<()> { @@ -123,20 +109,20 @@ impl RecordWallet for WalletAdapter { async fn update_record_value( &self, - category: &str, + category: RecordCategory, name: &str, new_value: &str, ) -> VcxCoreResult<()> { self.0.update_record_value(category, name, new_value).await } - async fn delete_record(&self, category: &str, name: &str) -> VcxCoreResult<()> { + async fn delete_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult<()> { self.0.delete_record(category, name).await } async fn search_record( &self, - category: &str, + category: RecordCategory, search_filter: Option, ) -> VcxCoreResult> { self.0.search_record(category, search_filter).await @@ -149,7 +135,7 @@ pub struct IndyCredxAnonCreds; impl IndyCredxAnonCreds { async fn get_wallet_record_value( wallet: &impl BaseWallet, - category: &str, + category: RecordCategory, id: &str, ) -> VcxCoreResult where @@ -164,7 +150,7 @@ impl IndyCredxAnonCreds { link_secret_id: &LinkSecretId, ) -> VcxCoreResult { let record = wallet - .get_record(CATEGORY_LINK_SECRET, link_secret_id) + .get_record(RecordCategory::LinkSecret, link_secret_id) .await?; let ms_bn: BigNumber = BigNumber::from_dec(record.value()).map_err(|err| { @@ -186,7 +172,7 @@ impl IndyCredxAnonCreds { credential_id: &str, ) -> VcxCoreResult { let cred_record = wallet - .get_record(CATEGORY_CREDENTIAL, credential_id) + .get_record(RecordCategory::Cred, credential_id) .await?; let credential: CredxCredential = serde_json::from_str(cred_record.value())?; @@ -200,7 +186,7 @@ impl IndyCredxAnonCreds { ) -> VcxCoreResult> { let records = wallet .search_record( - CATEGORY_CREDENTIAL, + RecordCategory::Cred, Some(SearchFilter::JsonFilter(wql.into())), ) .await?; @@ -346,7 +332,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let mut tails_writer = TailsFileWriter::new(Some(tails_dir.to_str().unwrap().to_string())); let cred_def = - Self::get_wallet_record_value(wallet, CATEGORY_CRED_DEF, &cred_def_id.0).await?; + Self::get_wallet_record_value(wallet, RecordCategory::CredDef, &cred_def_id.0).await?; let rev_reg_id = credx::issuer::make_revocation_registry_id( &issuer_did, @@ -356,9 +342,9 @@ impl BaseAnonCreds for IndyCredxAnonCreds { )?; let res_rev_reg = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG, &rev_reg_id.0).await; + Self::get_wallet_record_value(wallet, RecordCategory::RevReg, &rev_reg_id.0).await; let res_rev_reg_def = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF, &rev_reg_id.0).await; + Self::get_wallet_record_value(wallet, RecordCategory::RevRegDef, &rev_reg_id.0).await; if let (Ok(rev_reg), Ok(rev_reg_def)) = (res_rev_reg, res_rev_reg_def) { return Ok((rev_reg_id.to_string().try_into()?, rev_reg, rev_reg_def)); @@ -385,7 +371,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_rev_reg_info = serde_json::to_string(&rev_reg_info)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG_INFO.to_string()) + .category(RecordCategory::RevRegInfo) .value(str_rev_reg_info) .build(); wallet.add_record(record).await?; @@ -393,7 +379,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_rev_reg_def = serde_json::to_string(&rev_reg_def)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG_DEF.to_string()) + .category(RecordCategory::RevRegDef) .value(str_rev_reg_def.clone()) .build(); wallet.add_record(record).await?; @@ -401,7 +387,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_rev_reg_def_priv = serde_json::to_string(&rev_reg_def_priv)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG_DEF_PRIV.to_string()) + .category(RecordCategory::RevRegDefPriv) .value(str_rev_reg_def_priv) .build(); wallet.add_record(record).await?; @@ -409,7 +395,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_rev_reg = serde_json::to_string(&rev_reg)?; let record = Record::builder() .name(rev_reg_id.0.clone()) - .category(CATEGORY_REV_REG.to_string()) + .category(RecordCategory::RevReg) .value(str_rev_reg.clone()) .build(); wallet.add_record(record).await?; @@ -450,7 +436,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { // If cred def already exists, return it if let Ok(cred_def) = - Self::get_wallet_record_value(wallet, CATEGORY_CRED_DEF, &cred_def_id.0).await + Self::get_wallet_record_value(wallet, RecordCategory::CredDef, &cred_def_id.0).await { // TODO: Perform conversion return Ok(cred_def); @@ -469,7 +455,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_cred_def = serde_json::to_string(&cred_def)?; let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_DEF.to_string()) + .category(RecordCategory::CredDef) .value(str_cred_def.clone()) .build(); wallet.add_record(record).await?; @@ -477,7 +463,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_cred_def_priv = serde_json::to_string(&cred_def_priv)?; let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_DEF_PRIV.to_string()) + .category(RecordCategory::CredDefPriv) .value(str_cred_def_priv) .build(); wallet.add_record(record).await?; @@ -485,14 +471,14 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_cred_key_proof = serde_json::to_string(&cred_key_correctness_proof)?; let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_KEY_CORRECTNESS_PROOF.to_string()) + .category(RecordCategory::CredKeyCorrectnessProof) .value(str_cred_key_proof) .build(); wallet.add_record(record).await?; let record = Record::builder() .name(schema.id().to_string()) - .category(CATEGORY_CRED_SCHEMA.to_string()) + .category(RecordCategory::CredSchema) .value(serde_json::to_string(&schema_json)?) .build(); let store_schema_res = wallet.add_record(record).await; @@ -505,7 +491,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let record = Record::builder() .name(cred_def_id.0.clone()) - .category(CATEGORY_CRED_MAP_SCHEMA_ID.to_string()) + .category(RecordCategory::CredMapSchemaId) .value(schema.id().0.clone()) .build(); wallet.add_record(record).await?; @@ -519,17 +505,17 @@ impl BaseAnonCreds for IndyCredxAnonCreds { cred_def_id: &CredentialDefinitionId, ) -> VcxCoreResult { let cred_def = - Self::get_wallet_record_value(wallet, CATEGORY_CRED_DEF, &cred_def_id.0).await?; + Self::get_wallet_record_value(wallet, RecordCategory::CredDef, &cred_def_id.0).await?; let correctness_proof = Self::get_wallet_record_value( wallet, - CATEGORY_CRED_KEY_CORRECTNESS_PROOF, + RecordCategory::CredKeyCorrectnessProof, &cred_def_id.0, ) .await?; let schema = wallet - .get_record(CATEGORY_CRED_MAP_SCHEMA_ID, &cred_def_id.0) + .get_record(RecordCategory::CredMapSchemaId, &cred_def_id.0) .await?; let schema_id = CredxSchemaId(schema.value().into()); @@ -560,24 +546,29 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let cred_def_id = &cred_offer.cred_def_id.0; let cred_def = - Self::get_wallet_record_value(wallet, CATEGORY_CRED_DEF, cred_def_id).await?; + Self::get_wallet_record_value(wallet, RecordCategory::CredDef, cred_def_id).await?; let cred_def_private = - Self::get_wallet_record_value(wallet, CATEGORY_CRED_DEF_PRIV, cred_def_id).await?; + Self::get_wallet_record_value(wallet, RecordCategory::CredDefPriv, cred_def_id).await?; let mut revocation_config_parts = match &rev_reg_id { Some(rev_reg_id) => { let rev_reg_def = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF, rev_reg_id).await?; - - let rev_reg_def_priv = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF_PRIV, rev_reg_id) + Self::get_wallet_record_value(wallet, RecordCategory::RevRegDef, rev_reg_id) .await?; + let rev_reg_def_priv = Self::get_wallet_record_value( + wallet, + RecordCategory::RevRegDefPriv, + rev_reg_id, + ) + .await?; + let rev_reg = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG, rev_reg_id).await?; + Self::get_wallet_record_value(wallet, RecordCategory::RevReg, rev_reg_id) + .await?; let rev_reg_info: RevocationRegistryInfo = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_INFO, rev_reg_id) + Self::get_wallet_record_value(wallet, RecordCategory::RevRegInfo, rev_reg_id) .await?; Some((rev_reg_def, rev_reg_def_priv, rev_reg, rev_reg_info)) @@ -642,11 +633,11 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_rev_reg_info = serde_json::to_string(&rev_reg_info)?; wallet - .update_record_value(CATEGORY_REV_REG, &rev_reg_id, str_rev_reg) + .update_record_value(RecordCategory::RevReg, &rev_reg_id, str_rev_reg) .await?; wallet - .update_record_value(CATEGORY_REV_REG_INFO, &rev_reg_id, &str_rev_reg_info) + .update_record_value(RecordCategory::RevRegInfo, &rev_reg_id, &str_rev_reg_info) .await?; Some(cred_rev_id) @@ -1068,7 +1059,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let record = Record::builder() .name(credential_id.clone()) - .category(CATEGORY_CREDENTIAL.into()) + .category(RecordCategory::Cred) .value(record_value) .tags(tags) .build(); @@ -1084,7 +1075,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { link_secret_id: &LinkSecretId, ) -> VcxCoreResult<()> { let existing_record = wallet - .get_record(CATEGORY_LINK_SECRET, link_secret_id) + .get_record(RecordCategory::LinkSecret, link_secret_id) .await .ok(); // ignore error, as we only care about whether it exists or not @@ -1124,7 +1115,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let record = Record::builder() .name(link_secret_id.into()) - .category(CATEGORY_LINK_SECRET.into()) + .category(RecordCategory::LinkSecret) .value(ms_decimal) .build(); @@ -1137,7 +1128,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { wallet: &impl BaseWallet, cred_id: &CredentialId, ) -> VcxCoreResult<()> { - wallet.delete_record(CATEGORY_CREDENTIAL, cred_id).await + wallet.delete_record(RecordCategory::Cred, cred_id).await } async fn issuer_create_schema( @@ -1165,18 +1156,21 @@ impl BaseAnonCreds for IndyCredxAnonCreds { _rev_reg_delta_json: RevocationRegistryDelta, ) -> VcxCoreResult<()> { let rev_reg_id_str = &rev_reg_id.to_string(); + let rev_reg = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG, rev_reg_id_str).await?; + Self::get_wallet_record_value(wallet, RecordCategory::RevReg, rev_reg_id_str).await?; let rev_reg_def = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF, rev_reg_id_str).await?; + Self::get_wallet_record_value(wallet, RecordCategory::RevRegDef, rev_reg_id_str) + .await?; let rev_reg_priv = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_DEF_PRIV, rev_reg_id_str) + Self::get_wallet_record_value(wallet, RecordCategory::RevRegDefPriv, rev_reg_id_str) .await?; let mut rev_reg_info: RevocationRegistryInfo = - Self::get_wallet_record_value(wallet, CATEGORY_REV_REG_INFO, rev_reg_id_str).await?; + Self::get_wallet_record_value(wallet, RecordCategory::RevRegInfo, rev_reg_id_str) + .await?; let (issuance_type, cred_def_id) = match &rev_reg_def { CredxRevocationRegistryDefinition::RevocationRegistryDefinitionV1(r) => { @@ -1185,7 +1179,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { }; let cred_def = - Self::get_wallet_record_value(wallet, CATEGORY_CRED_DEF, cred_def_id).await?; + Self::get_wallet_record_value(wallet, RecordCategory::CredDef, cred_def_id).await?; match issuance_type { IssuanceType::ISSUANCE_ON_DEMAND => { @@ -1238,23 +1232,31 @@ impl BaseAnonCreds for IndyCredxAnonCreds { let str_rev_reg_delta = serde_json::to_string(&rev_reg_delta)?; wallet - .update_record_value(CATEGORY_REV_REG, rev_reg_id_str, &str_rev_reg) + .update_record_value(RecordCategory::RevReg, rev_reg_id_str, &str_rev_reg) .await?; wallet - .update_record_value(CATEGORY_REV_REG_INFO, rev_reg_id_str, &str_rev_reg_info) + .update_record_value( + RecordCategory::RevRegInfo, + rev_reg_id_str, + &str_rev_reg_info, + ) .await?; match old_str_rev_reg_delta { Some(_) => { wallet - .update_record_value(CATEGORY_REV_REG_DELTA, rev_reg_id_str, &str_rev_reg_delta) + .update_record_value( + RecordCategory::RevRegDelta, + rev_reg_id_str, + &str_rev_reg_delta, + ) .await? } None => { let record = Record::builder() .name(rev_reg_id_str.into()) - .category(CATEGORY_REV_REG_DELTA.into()) + .category(RecordCategory::RevRegDelta) .value(str_rev_reg_delta) .build(); wallet.add_record(record).await? @@ -1271,7 +1273,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { ) -> VcxCoreResult> { let res_rev_reg_delta = Self::get_wallet_record_value::( wallet, - CATEGORY_REV_REG_DELTA, + RecordCategory::RevRegDelta, &rev_reg_id.to_string(), ) .await; @@ -1294,7 +1296,7 @@ impl BaseAnonCreds for IndyCredxAnonCreds { ) -> VcxCoreResult<()> { if self.get_rev_reg_delta(wallet, rev_reg_id).await?.is_some() { wallet - .delete_record(CATEGORY_REV_REG_DELTA, &rev_reg_id.to_string()) + .delete_record(RecordCategory::RevRegDelta, &rev_reg_id.to_string()) .await?; } diff --git a/aries/aries_vcx_core/src/wallet/agency_client_wallet.rs b/aries/aries_vcx_core/src/wallet/agency_client_wallet.rs index fabe056893..1b6ad88cdf 100644 --- a/aries/aries_vcx_core/src/wallet/agency_client_wallet.rs +++ b/aries/aries_vcx_core/src/wallet/agency_client_wallet.rs @@ -9,8 +9,8 @@ use public_key::{Key, KeyType}; use super::{ base_wallet::{ - did_data::DidData, record::Record, search_filter::SearchFilter, BaseWallet, DidWallet, - RecordWallet, + did_data::DidData, record::Record, record_category::RecordCategory, + search_filter::SearchFilter, BaseWallet, DidWallet, RecordWallet, }, record_tags::RecordTags, structs_io::UnpackMessageOutput, @@ -31,13 +31,13 @@ impl RecordWallet for AgencyClientWallet { Err(unimplemented_agency_client_wallet_method("add_record")) } - async fn get_record(&self, category: &str, name: &str) -> VcxCoreResult { + async fn get_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult { Err(unimplemented_agency_client_wallet_method("get_record")) } async fn update_record_tags( &self, - category: &str, + category: RecordCategory, name: &str, new_tags: RecordTags, ) -> VcxCoreResult<()> { @@ -48,7 +48,7 @@ impl RecordWallet for AgencyClientWallet { async fn update_record_value( &self, - category: &str, + category: RecordCategory, name: &str, new_value: &str, ) -> VcxCoreResult<()> { @@ -57,13 +57,13 @@ impl RecordWallet for AgencyClientWallet { )) } - async fn delete_record(&self, category: &str, name: &str) -> VcxCoreResult<()> { + async fn delete_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult<()> { Err(unimplemented_agency_client_wallet_method("delete_record")) } async fn search_record( &self, - category: &str, + category: RecordCategory, search_filter: Option, ) -> VcxCoreResult> { Err(unimplemented_agency_client_wallet_method("search_record")) 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 b29e7a4f23..93f5bb350a 100644 --- a/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs +++ b/aries/aries_vcx_core/src/wallet/base_wallet/mod.rs @@ -5,13 +5,17 @@ use super::record_tags::RecordTags; use crate::{ errors::error::VcxCoreResult, wallet::{ - base_wallet::{did_data::DidData, record::Record, search_filter::SearchFilter}, + base_wallet::{ + did_data::DidData, record::Record, record_category::RecordCategory, + search_filter::SearchFilter, + }, structs_io::UnpackMessageOutput, }, }; pub mod did_data; pub mod record; +pub mod record_category; pub mod search_filter; pub trait BaseWallet: RecordWallet + DidWallet + Send + Sync + std::fmt::Debug {} @@ -48,27 +52,27 @@ pub trait DidWallet { pub trait RecordWallet { async fn add_record(&self, record: Record) -> VcxCoreResult<()>; - async fn get_record(&self, category: &str, name: &str) -> VcxCoreResult; + async fn get_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult; async fn update_record_tags( &self, - category: &str, + category: RecordCategory, name: &str, new_tags: RecordTags, ) -> VcxCoreResult<()>; async fn update_record_value( &self, - category: &str, + category: RecordCategory, name: &str, new_value: &str, ) -> VcxCoreResult<()>; - async fn delete_record(&self, category: &str, name: &str) -> VcxCoreResult<()>; + async fn delete_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult<()>; async fn search_record( &self, - category: &str, + category: RecordCategory, search_filter: Option, ) -> VcxCoreResult>; } @@ -81,7 +85,7 @@ mod tests { use crate::{ errors::error::AriesVcxCoreErrorKind, wallet::{ - base_wallet::{DidWallet, Record, RecordWallet}, + base_wallet::{record_category::RecordCategory, DidWallet, Record, RecordWallet}, record_tags::RecordTags, }, }; @@ -171,17 +175,17 @@ mod tests { let wallet = build_test_wallet().await; let name = "foo"; - let category = "my"; + let category = RecordCategory::default(); let value = "bar"; let record1 = Record::builder() .name(name.into()) - .category(category.into()) + .category(category) .value(value.into()) .build(); let record2 = Record::builder() .name("baz".into()) - .category(category.into()) + .category(category) .value("box".into()) .build(); @@ -198,12 +202,12 @@ mod tests { let wallet = build_test_wallet().await; let name = "foo"; - let category = "my"; + let category = RecordCategory::default(); let value = "bar"; let record = Record::builder() .name(name.into()) - .category(category.into()) + .category(category) .value(value.into()) .build(); @@ -226,27 +230,27 @@ mod tests { let name1 = "foo"; let name2 = "foa"; let name3 = "fob"; - let category1 = "my"; - let category2 = "your"; + let category1 = RecordCategory::Cred; + let category2 = RecordCategory::CredDef; let value = "xxx"; let record1 = Record::builder() .name(name1.into()) - .category(category1.into()) + .category(category1) .value(value.into()) .build(); wallet.add_record(record1).await.unwrap(); let record2 = Record::builder() .name(name2.into()) - .category(category1.into()) + .category(category1) .value(value.into()) .build(); wallet.add_record(record2).await.unwrap(); let record3 = Record::builder() .name(name3.into()) - .category(category2.into()) + .category(category2) .value(value.into()) .build(); wallet.add_record(record3).await.unwrap(); @@ -261,7 +265,7 @@ mod tests { let wallet = build_test_wallet().await; let name = "foo"; - let category = "my"; + let category = RecordCategory::default(); let value1 = "xxx"; let value2 = "yyy"; let tags1: RecordTags = vec![("a".into(), "b".into())].into(); @@ -269,7 +273,7 @@ mod tests { let record = Record::builder() .name(name.into()) - .category(category.into()) + .category(category) .tags(tags1.clone()) .value(value1.into()) .build(); @@ -294,14 +298,14 @@ mod tests { let wallet = build_test_wallet().await; let name = "foo"; - let category = "my"; + let category = RecordCategory::default(); let value1 = "xxx"; let value2 = "yyy"; let tags: RecordTags = vec![("a".into(), "b".into())].into(); let record = Record::builder() .name(name.into()) - .category(category.into()) + .category(category) .tags(tags.clone()) .value(value1.into()) .build(); @@ -322,14 +326,14 @@ mod tests { let wallet = build_test_wallet().await; let name = "foo"; - let category = "my"; + let category = RecordCategory::default(); let value = "xxx"; let tags1: RecordTags = vec![("a".into(), "b".into())].into(); let tags2: RecordTags = vec![("c".into(), "d".into())].into(); let record = Record::builder() .name(name.into()) - .category(category.into()) + .category(category) .tags(tags1.clone()) .value(value.into()) .build(); 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 36b8e3b70b..fe16fd588e 100644 --- a/aries/aries_vcx_core/src/wallet/base_wallet/record.rs +++ b/aries/aries_vcx_core/src/wallet/base_wallet/record.rs @@ -1,10 +1,10 @@ use typed_builder::TypedBuilder; -use crate::wallet::record_tags::RecordTags; +use crate::wallet::{base_wallet::record_category::RecordCategory, record_tags::RecordTags}; #[derive(Debug, Default, Clone, TypedBuilder)] pub struct Record { - category: String, + category: RecordCategory, name: String, value: String, #[builder(default)] @@ -20,7 +20,7 @@ impl Record { &self.name } - pub fn category(&self) -> &str { + pub fn category(&self) -> &RecordCategory { &self.category } diff --git a/aries/aries_vcx_core/src/wallet/base_wallet/record_category.rs b/aries/aries_vcx_core/src/wallet/base_wallet/record_category.rs new file mode 100644 index 0000000000..5ef557a99c --- /dev/null +++ b/aries/aries_vcx_core/src/wallet/base_wallet/record_category.rs @@ -0,0 +1,82 @@ +use std::{ + fmt::{self, Display, Formatter}, + str::FromStr, +}; + +use crate::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind}; + +const LINK_SECRET: &str = "VCX_LINK_SECRET"; +const CRED: &str = "VCX_CREDENTIAL"; +const CRED_DEF: &str = "VCX_CRED_DEF"; +const CRED_KEY_CORRECTNESS_PROOF: &str = "VCX_CRED_KEY_CORRECTNESS_PROOF"; +const CRED_DEF_PRIV: &str = "VCX_CRED_DEF_PRIV"; +const CRED_SCHEMA: &str = "VCX_CRED_SCHEMA"; +const CRED_MAP_SCHEMA_ID: &str = "VCX_CRED_MAP_SCHEMA_ID"; +const REV_REG: &str = "VCX_REV_REG"; +const REV_REG_DELTA: &str = "VCX_REV_REG_DELTA"; +const REV_REG_INFO: &str = "VCX_REV_REG_INFO"; +const REV_REG_DEF: &str = "VCX_REV_REG_DEF"; +const REV_REG_DEF_PRIV: &str = "VCX_REV_REG_DEF_PRIV"; + +#[derive(Clone, Copy, Debug, Default)] +pub enum RecordCategory { + #[default] + LinkSecret, + Cred, + CredDef, + CredKeyCorrectnessProof, + CredDefPriv, + CredSchema, + CredMapSchemaId, + RevReg, + RevRegDelta, + RevRegInfo, + RevRegDef, + RevRegDefPriv, +} + +impl FromStr for RecordCategory { + type Err = AriesVcxCoreError; + + fn from_str(s: &str) -> Result { + match s { + LINK_SECRET => Ok(RecordCategory::LinkSecret), + CRED => Ok(RecordCategory::Cred), + CRED_DEF => Ok(RecordCategory::CredDef), + CRED_KEY_CORRECTNESS_PROOF => Ok(RecordCategory::CredKeyCorrectnessProof), + CRED_DEF_PRIV => Ok(RecordCategory::CredDefPriv), + CRED_SCHEMA => Ok(RecordCategory::CredSchema), + CRED_MAP_SCHEMA_ID => Ok(RecordCategory::CredMapSchemaId), + REV_REG => Ok(RecordCategory::RevReg), + REV_REG_DELTA => Ok(RecordCategory::RevRegDelta), + REV_REG_INFO => Ok(RecordCategory::RevRegInfo), + REV_REG_DEF => Ok(RecordCategory::RevRegDef), + REV_REG_DEF_PRIV => Ok(RecordCategory::RevRegDefPriv), + _ => Err(Self::Err::from_msg( + AriesVcxCoreErrorKind::InvalidInput, + format!("unknown category: {}", s), + )), + } + } +} + +impl Display for RecordCategory { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let value = match self { + RecordCategory::LinkSecret => LINK_SECRET, + RecordCategory::Cred => CRED, + RecordCategory::CredDef => CRED_DEF, + RecordCategory::CredKeyCorrectnessProof => CRED_KEY_CORRECTNESS_PROOF, + RecordCategory::CredDefPriv => CRED_DEF_PRIV, + RecordCategory::CredSchema => CRED_SCHEMA, + RecordCategory::CredMapSchemaId => CRED_MAP_SCHEMA_ID, + RecordCategory::RevReg => REV_REG, + RecordCategory::RevRegDelta => REV_REG_DELTA, + RecordCategory::RevRegInfo => REV_REG_INFO, + RecordCategory::RevRegDef => REV_REG_DEF, + RecordCategory::RevRegDefPriv => REV_REG_DEF_PRIV, + }; + + write!(f, "{}", value) + } +} 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 36b25d1487..81567d3f6b 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 @@ -8,7 +8,10 @@ use super::{indy_tags::IndyTags, SEARCH_OPTIONS, WALLET_OPTIONS}; use crate::{ errors::error::{AriesVcxCoreError, VcxCoreResult}, wallet::{ - base_wallet::{record::Record, search_filter::SearchFilter, RecordWallet}, + base_wallet::{ + record::Record, record_category::RecordCategory, search_filter::SearchFilter, + RecordWallet, + }, indy::IndySdkWallet, record_tags::RecordTags, }, @@ -27,7 +30,7 @@ impl RecordWallet for IndySdkWallet { .non_secret_controller .add_record( self.wallet_handle, - record.category().into(), + record.category().to_string(), record.name().into(), record.value().into(), tags_map, @@ -35,12 +38,12 @@ impl RecordWallet for IndySdkWallet { .await?) } - async fn get_record(&self, category: &str, name: &str) -> VcxCoreResult { + async fn get_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult { let res = Locator::instance() .non_secret_controller .get_record( self.wallet_handle, - category.into(), + category.to_string(), name.into(), WALLET_OPTIONS.into(), ) @@ -48,12 +51,12 @@ impl RecordWallet for IndySdkWallet { let indy_record: IndyRecord = serde_json::from_str(&res)?; - Ok(indy_record.into()) + Ok(Record::try_from_indy_record(indy_record)?) } async fn update_record_tags( &self, - category: &str, + category: RecordCategory, name: &str, new_tags: RecordTags, ) -> VcxCoreResult<()> { @@ -61,7 +64,7 @@ impl RecordWallet for IndySdkWallet { .non_secret_controller .update_record_tags( self.wallet_handle, - category.into(), + category.to_string(), name.into(), IndyTags::from_entry_tags(new_tags).into_inner(), ) @@ -70,7 +73,7 @@ impl RecordWallet for IndySdkWallet { async fn update_record_value( &self, - category: &str, + category: RecordCategory, name: &str, new_value: &str, ) -> VcxCoreResult<()> { @@ -78,23 +81,23 @@ impl RecordWallet for IndySdkWallet { .non_secret_controller .update_record_value( self.wallet_handle, - category.into(), + category.to_string(), name.into(), new_value.into(), ) .await?) } - async fn delete_record(&self, category: &str, name: &str) -> VcxCoreResult<()> { + async fn delete_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult<()> { Ok(Locator::instance() .non_secret_controller - .delete_record(self.wallet_handle, category.into(), name.into()) + .delete_record(self.wallet_handle, category.to_string(), name.into()) .await?) } async fn search_record( &self, - category: &str, + category: RecordCategory, search_filter: Option, ) -> VcxCoreResult> { let json_filter = search_filter @@ -109,7 +112,7 @@ impl RecordWallet for IndySdkWallet { .non_secret_controller .open_search( self.wallet_handle, - category.into(), + category.to_string(), query_json, SEARCH_OPTIONS.into(), ) @@ -133,7 +136,7 @@ impl RecordWallet for IndySdkWallet { let mut records = Vec::new(); while let Some(record) = next().await? { - records.push(record.into()); + records.push(Record::try_from_indy_record(record)?); } Ok(records) diff --git a/aries/aries_vcx_core/src/wallet/indy/mod.rs b/aries/aries_vcx_core/src/wallet/indy/mod.rs index 12e861ffb4..0ab22d3b29 100644 --- a/aries/aries_vcx_core/src/wallet/indy/mod.rs +++ b/aries/aries_vcx_core/src/wallet/indy/mod.rs @@ -1,9 +1,11 @@ +use std::str::FromStr; + use indy_api_types::domain::wallet::IndyRecord; use serde::{Deserialize, Serialize}; use typed_builder::TypedBuilder; use self::indy_tags::IndyTags; -use super::base_wallet::{record::Record, BaseWallet}; +use super::base_wallet::{record::Record, record_category::RecordCategory, BaseWallet}; use crate::{errors::error::VcxCoreResult, WalletHandle}; mod indy_did_wallet; @@ -87,32 +89,21 @@ impl IndyWalletRecord { Ok(Self { id: Some(record.name().into()), - record_type: Some(record.category().into()), + record_type: Some(record.category().to_string()), value: Some(record.value().into()), tags, }) } } -impl From for Record { - fn from(ir: IndyRecord) -> Self { - Self::builder() - .name(ir.id) - .category(ir.type_) - .value(ir.value) - .tags(IndyTags::new(ir.tags).into_entry_tags()) - .build() - } -} - -impl From for IndyRecord { - fn from(record: Record) -> Self { - Self { - id: record.name().into(), - type_: record.category().into(), - value: record.value().into(), - tags: IndyTags::from_entry_tags(record.tags().to_owned()).into_inner(), - } +impl Record { + pub fn try_from_indy_record(indy_record: IndyRecord) -> VcxCoreResult { + Ok(Record::builder() + .name(indy_record.id) + .category(RecordCategory::from_str(&indy_record.type_)?) + .value(indy_record.value) + .tags(IndyTags::new(indy_record.tags).into_entry_tags()) + .build()) } } 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 e596f0ec67..2e5ad2de2d 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 @@ -1,4 +1,7 @@ -use std::sync::{Arc, RwLock}; +use std::{ + str::FromStr, + sync::{Arc, RwLock}, +}; use aries_vcx::{ aries_vcx_core::{ @@ -18,7 +21,7 @@ use aries_vcx::{ protocols::mediated_connection::pairwise_info::PairwiseInfo, }; use aries_vcx_core::wallet::{ - base_wallet::{record::Record, DidWallet, RecordWallet}, + base_wallet::{record::Record, record_category::RecordCategory, DidWallet, RecordWallet}, indy::IndyWalletRecord, record_tags::RecordTags, }; @@ -191,14 +194,14 @@ pub async fn wallet_add_wallet_record( let record = if let Some(record_tags) = tags { Record::builder() .name(id.into()) - .category(type_.into()) + .category(RecordCategory::from_str(type_)?) .value(value.into()) .tags(record_tags) .build() } else { Record::builder() .name(id.into()) - .category(type_.into()) + .category(RecordCategory::from_str(type_)?) .value(value.into()) .build() }; @@ -212,7 +215,11 @@ pub async fn wallet_update_wallet_record_value( value: &str, ) -> LibvcxResult<()> { let wallet = get_main_wallet()?; - map_ariesvcx_core_result(wallet.update_record_value(xtype, id, value).await) + map_ariesvcx_core_result( + wallet + .update_record_value(RecordCategory::from_str(xtype)?, id, value) + .await, + ) } pub async fn wallet_update_wallet_record_tags( @@ -221,8 +228,12 @@ pub async fn wallet_update_wallet_record_tags( tags_json: &str, ) -> LibvcxResult<()> { let wallet = get_main_wallet()?; - let tags = serde_json::from_str(tags_json)?; - map_ariesvcx_core_result(wallet.update_record_tags(xtype, id, tags).await) + let tags: RecordTags = serde_json::from_str(tags_json)?; + map_ariesvcx_core_result( + wallet + .update_record_tags(RecordCategory::from_str(xtype)?, id, tags) + .await, + ) } pub async fn wallet_add_wallet_record_tags( @@ -231,7 +242,9 @@ pub async fn wallet_add_wallet_record_tags( tags_json: &str, ) -> LibvcxResult<()> { let wallet = get_main_wallet()?; - let record = wallet.get_record(xtype, id).await?; + let record = wallet + .get_record(RecordCategory::from_str(xtype)?, id) + .await?; let tags = { let mut tags: RecordTags = serde_json::from_str(tags_json)?; @@ -239,7 +252,11 @@ pub async fn wallet_add_wallet_record_tags( tags }; - map_ariesvcx_core_result(wallet.update_record_tags(xtype, id, tags).await) + map_ariesvcx_core_result( + wallet + .update_record_tags(RecordCategory::from_str(xtype)?, id, tags) + .await, + ) } pub async fn wallet_delete_wallet_record_tags( @@ -250,14 +267,20 @@ pub async fn wallet_delete_wallet_record_tags( let wallet = get_main_wallet()?; let tags: RecordTags = serde_json::from_str(tags_json)?; - let record = wallet.get_record(xtype, id).await?; + let record = wallet + .get_record(RecordCategory::from_str(xtype)?, id) + .await?; let mut found_tags = record.tags().clone(); for key in tags { found_tags.remove(key); } - map_ariesvcx_core_result(wallet.update_record_tags(xtype, id, found_tags).await) + map_ariesvcx_core_result( + wallet + .update_record_tags(RecordCategory::from_str(xtype)?, id, found_tags) + .await, + ) } pub async fn wallet_get_wallet_record( @@ -269,7 +292,7 @@ pub async fn wallet_get_wallet_record( map_ariesvcx_result( wallet - .get_record(xtype, id) + .get_record(RecordCategory::from_str(xtype)?, id) .map(|res| { let wallet_record = IndyWalletRecord::from_record(res?)?; @@ -281,7 +304,11 @@ pub async fn wallet_get_wallet_record( pub async fn wallet_delete_wallet_record(xtype: &str, id: &str) -> LibvcxResult<()> { let wallet = get_main_wallet()?; - map_ariesvcx_core_result(wallet.delete_record(xtype, id).await) + map_ariesvcx_core_result( + wallet + .delete_record(RecordCategory::from_str(xtype)?, id) + .await, + ) } pub async fn wallet_open_search_wallet( @@ -344,7 +371,9 @@ pub mod test_utils { aries_vcx_core::wallet::indy::WalletConfig, global::settings::{DEFAULT_WALLET_BACKUP_KEY, DEFAULT_WALLET_KEY, WALLET_KDF_RAW}, }; - use aries_vcx_core::wallet::base_wallet::{record::Record, DidWallet, RecordWallet}; + use aries_vcx_core::wallet::base_wallet::{ + record::Record, record_category::RecordCategory, DidWallet, RecordWallet, + }; use crate::{ api_vcx::api_global::{ @@ -357,10 +386,6 @@ pub mod test_utils { errors::error::LibvcxResult, }; - fn _record() -> (&'static str, &'static str, &'static str) { - ("type1", "id1", "value1") - } - pub async fn _create_main_wallet_and_its_backup() -> (TempFile, String, WalletConfig) { let wallet_name = &format!("export_test_wallet_{}", uuid::Uuid::new_v4()); @@ -381,12 +406,11 @@ pub mod test_utils { .unwrap(); let wallet = get_main_wallet().unwrap(); wallet.create_and_store_my_did(None, None).await.unwrap(); - let (type_, id, value) = _record(); let new_record = Record::builder() - .name(id.into()) - .category(type_.into()) - .value(value.into()) + .name("id1".to_owned()) + .category(RecordCategory::default()) + .value("value1".to_owned()) .build(); wallet.add_record(new_record).await.unwrap(); @@ -426,6 +450,7 @@ mod tests { }, global::settings::{DEFAULT_WALLET_BACKUP_KEY, DEFAULT_WALLET_KEY, WALLET_KDF_RAW}, }; + use aries_vcx_core::wallet::base_wallet::record_category::RecordCategory; use test_utils::devsetup::{SetupMocks, TempFile}; use crate::{ @@ -481,7 +506,7 @@ mod tests { async fn test_wallet_record_add_with_tag() { _create_and_open_wallet().await.unwrap(); - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let value = "Record Value".to_string(); let tags = r#"{"tagName1":"tag1","tagName2":"tag2"}"#.to_string(); @@ -496,7 +521,7 @@ mod tests { async fn test_wallet_record_add_with_no_tag() { _create_and_open_wallet().await.unwrap(); - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let value = "Record Value".to_string(); @@ -510,7 +535,7 @@ mod tests { async fn test_wallet_record_add_fails_with_duplication_error() { _create_and_open_wallet().await.unwrap(); - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let value = "Record Value".to_string(); @@ -528,7 +553,7 @@ mod tests { async fn test_wallet_record_get_fails_if_record_does_not_exist() { _create_and_open_wallet().await.unwrap(); - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let options = json!({ "retrieveType": true, @@ -544,7 +569,7 @@ mod tests { } async fn _add_and_get_wallet_record() -> LibvcxResult<()> { - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let value = "Record Value".to_string(); let tags = r#"{"tagName1":"tag1","tagName2":"tag2"}"#.to_string(); @@ -568,7 +593,7 @@ mod tests { async fn test_wallet_record_delete() { _create_and_open_wallet().await.unwrap(); - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let value = "Record Value".to_string(); @@ -718,7 +743,7 @@ mod tests { async fn test_wallet_record_update() { _create_and_open_wallet().await.unwrap(); - let xtype = "record_type".to_string(); + let xtype = RecordCategory::default().to_string(); let id = "123".to_string(); let value = "Record Value".to_string(); let new_value = "New Record Value".to_string(); diff --git a/aries/misc/test_utils/src/lib.rs b/aries/misc/test_utils/src/lib.rs index c131eeb11e..85c68c796e 100644 --- a/aries/misc/test_utils/src/lib.rs +++ b/aries/misc/test_utils/src/lib.rs @@ -3,5 +3,4 @@ pub mod mockdata; pub mod random; #[rustfmt::skip] pub mod constants; - pub mod mock_wallet; diff --git a/aries/misc/test_utils/src/mock_wallet.rs b/aries/misc/test_utils/src/mock_wallet.rs index ac0d1351e7..64c5389df0 100644 --- a/aries/misc/test_utils/src/mock_wallet.rs +++ b/aries/misc/test_utils/src/mock_wallet.rs @@ -2,8 +2,8 @@ use aries_vcx_core::{ errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult}, wallet::{ base_wallet::{ - did_data::DidData, record::Record, search_filter::SearchFilter, BaseWallet, DidWallet, - RecordWallet, + did_data::DidData, record::Record, record_category::RecordCategory, + search_filter::SearchFilter, BaseWallet, DidWallet, RecordWallet, }, record_tags::RecordTags, structs_io::UnpackMessageOutput, @@ -27,17 +27,17 @@ impl RecordWallet for MockWallet { Ok(()) } - async fn get_record(&self, category: &str, name: &str) -> VcxCoreResult { + async fn get_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult { Ok(Record::builder() .name("123".into()) - .category("record type".into()) + .category(RecordCategory::default()) .value("record value".into()) .build()) } async fn update_record_value( &self, - category: &str, + category: RecordCategory, name: &str, new_value: &str, ) -> VcxCoreResult<()> { @@ -46,20 +46,20 @@ impl RecordWallet for MockWallet { async fn update_record_tags( &self, - category: &str, + category: RecordCategory, name: &str, new_tags: RecordTags, ) -> VcxCoreResult<()> { Ok(()) } - async fn delete_record(&self, category: &str, name: &str) -> VcxCoreResult<()> { + async fn delete_record(&self, category: RecordCategory, name: &str) -> VcxCoreResult<()> { Ok(()) } async fn search_record( &self, - category: &str, + category: RecordCategory, search_filter: Option, ) -> VcxCoreResult> { Err(AriesVcxCoreError::from_msg( diff --git a/aries/misc/wallet_migrator/src/vdrtools2credx/conv.rs b/aries/misc/wallet_migrator/src/vdrtools2credx/conv.rs index c8256f3661..987c74d9b4 100644 --- a/aries/misc/wallet_migrator/src/vdrtools2credx/conv.rs +++ b/aries/misc/wallet_migrator/src/vdrtools2credx/conv.rs @@ -1,8 +1,6 @@ -use aries_vcx_core::anoncreds::credx_anoncreds::{ - RevocationRegistryInfo, CATEGORY_CREDENTIAL, CATEGORY_CRED_DEF, CATEGORY_CRED_DEF_PRIV, - CATEGORY_CRED_KEY_CORRECTNESS_PROOF, CATEGORY_CRED_MAP_SCHEMA_ID, CATEGORY_CRED_SCHEMA, - CATEGORY_LINK_SECRET, CATEGORY_REV_REG, CATEGORY_REV_REG_DEF, CATEGORY_REV_REG_DEF_PRIV, - CATEGORY_REV_REG_DELTA, CATEGORY_REV_REG_INFO, +use aries_vcx_core::{ + anoncreds::credx_anoncreds::RevocationRegistryInfo, + wallet::base_wallet::record_category::RecordCategory, }; use vdrtools::{types::domain::wallet::IndyRecord, IndyError}; @@ -21,31 +19,31 @@ pub fn convert_master_secret(mut record: IndyRecord) -> MigrationResult MigrationResult { - record.type_ = CATEGORY_CREDENTIAL.to_owned(); + record.type_ = RecordCategory::Cred.to_string(); let _: credx::types::Credential = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_cred_def(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_CRED_DEF.to_owned(); + record.type_ = RecordCategory::CredDef.to_string(); let _: credx::types::CredentialDefinition = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_cred_def_priv_key(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_CRED_DEF_PRIV.to_owned(); + record.type_ = RecordCategory::CredDefPriv.to_string(); let _: credx::types::CredentialDefinitionPrivate = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_cred_def_correctness_proof(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_CRED_KEY_CORRECTNESS_PROOF.to_owned(); + record.type_ = RecordCategory::CredKeyCorrectnessProof.to_string(); let old: vdrtools::CredentialDefinitionCorrectnessProof = serde_json::from_str(&record.value)?; let old_value = serde_json::to_string(&old.value)?; let new_value = serde_json::from_str(&old_value)?; @@ -55,13 +53,13 @@ pub fn convert_cred_def_correctness_proof(mut record: IndyRecord) -> MigrationRe } pub fn convert_schema(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_CRED_SCHEMA.to_owned(); + record.type_ = RecordCategory::CredSchema.to_string(); let _: credx::types::Schema = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_schema_id(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_CRED_MAP_SCHEMA_ID.to_owned(); + record.type_ = RecordCategory::CredMapSchemaId.to_string(); // The plain ID is stored as a String, // so not that much to check. let _ = credx::types::SchemaId(record.value.clone()); @@ -69,13 +67,13 @@ pub fn convert_schema_id(mut record: IndyRecord) -> MigrationResult } pub fn convert_rev_reg(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_REV_REG.to_owned(); + record.type_ = RecordCategory::RevReg.to_string(); let _: credx::types::RevocationRegistry = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_rev_reg_delta(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_REV_REG_DELTA.to_owned(); + record.type_ = RecordCategory::RevRegDelta.to_string(); // Shave off the useless prefix, if found. record.id = record @@ -91,19 +89,19 @@ pub fn convert_rev_reg_delta(mut record: IndyRecord) -> MigrationResult MigrationResult { - record.type_ = CATEGORY_REV_REG_INFO.to_owned(); + record.type_ = RecordCategory::RevRegInfo.to_string(); let _: RevocationRegistryInfo = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_rev_reg_def(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_REV_REG_DEF.to_owned(); + record.type_ = RecordCategory::RevRegDef.to_string(); let _: credx::types::RevocationRegistryDefinition = serde_json::from_str(&record.value)?; Ok(record) } pub fn convert_rev_reg_def_priv(mut record: IndyRecord) -> MigrationResult { - record.type_ = CATEGORY_REV_REG_DEF_PRIV.to_owned(); + record.type_ = RecordCategory::RevRegDefPriv.to_string(); let _: credx::types::RevocationRegistryDefinitionPrivate = serde_json::from_str(&record.value)?; Ok(record) } diff --git a/aries/misc/wallet_migrator/src/vdrtools2credx/mod.rs b/aries/misc/wallet_migrator/src/vdrtools2credx/mod.rs index f6fb140f7b..4cbbd1a405 100644 --- a/aries/misc/wallet_migrator/src/vdrtools2credx/mod.rs +++ b/aries/misc/wallet_migrator/src/vdrtools2credx/mod.rs @@ -56,11 +56,9 @@ pub fn migrate_any_record(record: IndyRecord) -> MigrationResult