Skip to content

Commit

Permalink
refactor: improve record category type, closes #1112
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <[email protected]>
  • Loading branch information
Ondrej Prazak committed Feb 14, 2024
1 parent 4425e0d commit 047bfe3
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 267 deletions.
3 changes: 0 additions & 3 deletions aries/agents/rust/mediator/src/didcomm_handlers/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
pub mod prelude {
pub use std::sync::Arc;

pub use aries_vcx::utils::encryption_envelope::EncryptionEnvelope;
pub use aries_vcx_core::wallet::base_wallet::BaseWallet;

pub use crate::{aries_agent::ArcAgent, persistence::MediatorPersistence, utils::prelude::*};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
98 changes: 44 additions & 54 deletions aries/aries_vcx_core/src/anoncreds/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,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,
Expand Down Expand Up @@ -177,7 +163,7 @@ impl Anoncreds {
async fn get_wallet_record_value<T>(
&self,
wallet: &impl BaseWallet,
category: &str,
category: RecordCategory,
id: &str,
) -> VcxCoreResult<T>
where
Expand All @@ -193,7 +179,7 @@ impl Anoncreds {
link_secret_id: &str,
) -> VcxCoreResult<LinkSecret> {
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())
Expand All @@ -205,7 +191,7 @@ impl Anoncreds {
credential_id: &str,
) -> VcxCoreResult<Credential> {
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())?;
Expand All @@ -219,7 +205,7 @@ impl Anoncreds {
) -> VcxCoreResult<Vec<(String, Credential)>> {
let records = wallet
.search_record(
CATEGORY_CREDENTIAL,
RecordCategory::Cred,
Some(SearchFilter::JsonFilter(wql.into())),
)
.await?;
Expand Down Expand Up @@ -431,7 +417,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)?;
Expand Down Expand Up @@ -469,7 +455,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?;
Expand All @@ -491,15 +477,15 @@ 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?;

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?;
Expand All @@ -508,7 +494,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?;
Expand Down Expand Up @@ -540,7 +526,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?
Expand Down Expand Up @@ -568,30 +554,30 @@ 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?;

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?;

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;
Expand All @@ -604,9 +590,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?;

Expand All @@ -621,15 +607,15 @@ 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?;

let schema_id_value = self
.get_wallet_record_value::<Value>(
wallet,
CATEGORY_CRED_MAP_SCHEMA_ID,
RecordCategory::CredMapSchemaId,
&cred_def_id.to_string(),
)
.await?;
Expand Down Expand Up @@ -659,29 +645,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 =
Expand Down Expand Up @@ -771,11 +757,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)
Expand Down Expand Up @@ -1269,7 +1255,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();
Expand All @@ -1284,7 +1270,7 @@ impl BaseAnonCreds for Anoncreds {
wallet: &impl BaseWallet,
cred_id: &str,
) -> VcxCoreResult<()> {
wallet.delete_record(CATEGORY_CREDENTIAL, cred_id).await
wallet.delete_record(RecordCategory::Cred, cred_id).await
}

async fn prover_create_link_secret(
Expand All @@ -1293,7 +1279,7 @@ impl BaseAnonCreds for Anoncreds {
link_secret_id: &str,
) -> 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

Expand All @@ -1317,7 +1303,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?;
Expand Down Expand Up @@ -1351,7 +1337,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?;
Expand All @@ -1374,13 +1360,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(
Expand All @@ -1404,15 +1394,15 @@ 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,
)
.await?;
} 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?;
Expand All @@ -1429,7 +1419,7 @@ impl BaseAnonCreds for Anoncreds {
let res_rev_reg_delta = self
.get_wallet_record_value::<RevocationRegistryDelta>(
wallet,
CATEGORY_REV_REG_DELTA,
RecordCategory::RevRegDelta,
&rev_reg_id.to_string(),
)
.await;
Expand All @@ -1452,7 +1442,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?;
}

Expand Down
Loading

0 comments on commit 047bfe3

Please sign in to comment.