Skip to content

Commit

Permalink
move delta -> status list conversion as a ledger responsibility
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <[email protected]>
  • Loading branch information
gmulhearn-anonyome committed Dec 4, 2024
1 parent ab7becb commit 6c82328
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 129 deletions.
16 changes: 8 additions & 8 deletions aries/aries_vcx/src/common/proofs/prover/prover_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use aries_vcx_anoncreds::anoncreds::base_anoncreds::{
BaseAnonCreds, CredentialDefinitionsMap, RevocationStatesMap, SchemasMap,
};
use aries_vcx_ledger::ledger::base_ledger::AnoncredsLedgerRead;
use chrono::Utc;

use crate::errors::error::prelude::*;

Expand Down Expand Up @@ -160,26 +161,25 @@ pub async fn build_rev_states_json(
if !rtn.contains_key(rev_reg_id) {
// Does this make sense in case cred_info's for same rev_reg_ids have different
// revocation intervals
let (from, to) = if let Some(ref interval) = cred_info.revocation_interval {
let (_from, to) = if let Some(ref interval) = cred_info.revocation_interval {
(interval.from, interval.to)
} else {
(None, None)
};

let rev_reg_def_json = ledger_read
.get_rev_reg_def_json(&rev_reg_id.to_owned().try_into()?)
.await?;
let parsed_id = &rev_reg_id.to_owned().try_into()?;
let rev_reg_def_json = ledger_read.get_rev_reg_def_json(parsed_id).await?;

let (rev_reg_delta_json, timestamp) = ledger_read
.get_rev_reg_delta_json(&rev_reg_id.to_owned().try_into()?, from, to)
let on_or_before = to.unwrap_or(Utc::now().timestamp() as u64);
let (rev_status_list, timestamp) = ledger_read
.get_rev_status_list(parsed_id, on_or_before, Some(&rev_reg_def_json))
.await?;

let rev_state_json = anoncreds
.create_revocation_state(
Path::new(tails_dir),
rev_reg_def_json,
rev_reg_delta_json,
timestamp,
rev_status_list,
*cred_rev_id,
)
.await?;
Expand Down
138 changes: 50 additions & 88 deletions aries/aries_vcx_anoncreds/src/anoncreds/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,41 @@ use anoncreds::{
RevocationRegistryDefinition as AnoncredsRevocationRegistryDefinition,
},
};
use anoncreds_types::data_types::{
identifiers::{
cred_def_id::CredentialDefinitionId, rev_reg_def_id::RevocationRegistryDefinitionId,
schema_id::SchemaId,
},
ledger::{
cred_def::{CredentialDefinition, SignatureType},
rev_reg::RevocationRegistry,
rev_reg_def::RevocationRegistryDefinition,
rev_reg_delta::{RevocationRegistryDelta, RevocationRegistryDeltaValue},
rev_status_list::RevocationStatusList,
schema::{AttributeNames, Schema},
},
messages::{
cred_definition_config::CredentialDefinitionConfig,
cred_offer::CredentialOffer,
cred_request::{CredentialRequest, CredentialRequestMetadata},
cred_selection::{
RetrievedCredentialForReferent, RetrievedCredentialInfo, RetrievedCredentials,
use anoncreds_types::{
data_types::{
identifiers::{
cred_def_id::CredentialDefinitionId, rev_reg_def_id::RevocationRegistryDefinitionId,
schema_id::SchemaId,
},
ledger::{
cred_def::{CredentialDefinition, SignatureType},
rev_reg::RevocationRegistry,
rev_reg_def::RevocationRegistryDefinition,
rev_reg_delta::{RevocationRegistryDelta, RevocationRegistryDeltaValue},
rev_status_list::RevocationStatusList,
schema::{AttributeNames, Schema},
},
messages::{
cred_definition_config::CredentialDefinitionConfig,
cred_offer::CredentialOffer,
cred_request::{CredentialRequest, CredentialRequestMetadata},
cred_selection::{
RetrievedCredentialForReferent, RetrievedCredentialInfo, RetrievedCredentials,
},
credential::{Credential, CredentialValues},
nonce::Nonce,
pres_request::PresentationRequest,
presentation::{Presentation, RequestedCredentials},
revocation_state::CredentialRevocationState,
},
credential::{Credential, CredentialValues},
nonce::Nonce,
pres_request::PresentationRequest,
presentation::{Presentation, RequestedCredentials},
revocation_state::CredentialRevocationState,
},
utils::conversions::from_revocation_registry_delta_to_revocation_status_list,
};
use aries_vcx_wallet::wallet::{
base_wallet::{record::Record, record_category::RecordCategory, BaseWallet},
record_tags::{RecordTag, RecordTags},
};
use async_trait::async_trait;
use bitvec::bitvec;
use did_parser_nom::Did;
use log::warn;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
Expand All @@ -82,36 +84,6 @@ use crate::{
errors::error::{VcxAnoncredsError, VcxAnoncredsResult},
};

fn from_revocation_registry_delta_to_revocation_status_list(
delta: &RevocationRegistryDeltaValue,
rev_reg_def: &AnoncredsRevocationRegistryDefinition,
rev_reg_def_id: &RevocationRegistryDefinitionId,
timestamp: Option<u64>,
issuance_by_default: bool,
) -> VcxAnoncredsResult<RevocationStatusList> {
let default_state = if issuance_by_default { 0 } else { 1 };
let mut revocation_list = bitvec![default_state; rev_reg_def.value.max_cred_num as usize];

for issued in &delta.issued {
revocation_list.insert(*issued as usize, false);
}

for revoked in &delta.revoked {
revocation_list.insert(*revoked as usize, true);
}

let accum = delta.accum.into();

RevocationStatusList::new(
Some(&rev_reg_def_id.to_string()),
rev_reg_def.issuer_id.clone().convert(())?,
revocation_list,
Some(accum),
timestamp,
)
.map_err(Into::into)
}

fn from_revocation_status_list_to_revocation_registry_delta(
rev_status_list: &RevocationStatusList,
prev_accum: Option<Accumulator>,
Expand Down Expand Up @@ -288,17 +260,37 @@ impl BaseAnonCreds for Anoncreds {
let cred_defs: HashMap<AnoncredsCredentialDefinitionId, AnoncredsCredentialDefinition> =
credential_defs_json.convert(())?;

// tack on issuerId for ease of processing status lists
let rev_regs_map_with_issuer_ids: Option<HashMap<_, _>> =
match (rev_regs_json, &rev_reg_defs_json) {
(Some(regs), Some(defs)) => Some(
regs.into_iter()
.filter_map(|(k, v)| {
let Some(def) = defs.get(&k) else {
return None;
};
Some((k, (v, def.issuer_id.clone())))
})
.collect(),
),
_ => None,
};

let rev_reg_defs: Option<
HashMap<AnoncredsRevocationRegistryDefinitionId, AnoncredsRevocationRegistryDefinition>,
> = rev_reg_defs_json.map(|v| v.convert(())).transpose()?;

let rev_status_lists = rev_regs_map_with_issuer_ids
.map(|r| r.convert(()))
.transpose()?;

Ok(anoncreds::verifier::verify_presentation(
&presentation,
&pres_req,
&schemas,
&cred_defs,
rev_reg_defs.as_ref(),
rev_regs_json.map(|r| r.convert(())).transpose()?,
rev_status_lists,
None, // no idea what this is
)?)
}
Expand Down Expand Up @@ -947,18 +939,9 @@ impl BaseAnonCreds for Anoncreds {
&self,
tails_dir: &Path,
rev_reg_def_json: RevocationRegistryDefinition,
rev_reg_delta_json: RevocationRegistryDelta,
timestamp: u64,
rev_status_list: RevocationStatusList,
cred_rev_id: u32,
) -> VcxAnoncredsResult<CredentialRevocationState> {
let cred_def_id = rev_reg_def_json.cred_def_id.to_string();
let max_cred_num = rev_reg_def_json.value.max_cred_num;
let rev_reg_def_id = rev_reg_def_json.id.to_string();
let (_cred_def_method, issuer_did, _signature_type, _schema_num, _tag) =
cred_def_parts(&cred_def_id).ok_or(VcxAnoncredsError::InvalidSchema(format!(
"Could not process cred_def_id {cred_def_id} as parts."
)))?;

let revoc_reg_def: AnoncredsRevocationRegistryDefinition = rev_reg_def_json.convert(())?;
let tails_file_hash = revoc_reg_def.value.tails_hash.as_str();

Expand All @@ -970,25 +953,6 @@ impl BaseAnonCreds for Anoncreds {
VcxAnoncredsError::InvalidOption("tails file is not an unicode string".into())
})?;

let RevocationRegistryDeltaValue { accum, revoked, .. } = rev_reg_delta_json.value;

let issuer_id = IssuerId::new(issuer_did.did()).unwrap();
let mut revocation_list = bitvec!(0; max_cred_num as usize);
revoked.into_iter().for_each(|id| {
revocation_list
.get_mut(id as usize)
.map(|mut b| *b = true)
.unwrap_or_default()
});
let registry = CryptoRevocationRegistry { accum };

let rev_status_list = RevocationStatusList::new(
Some(&rev_reg_def_id),
issuer_id.convert(())?,
revocation_list,
Some(registry),
Some(timestamp),
)?;
let rev_state = anoncreds::prover::create_or_update_revocation_state(
tails_path,
&revoc_reg_def,
Expand Down Expand Up @@ -1164,10 +1128,8 @@ impl BaseAnonCreds for Anoncreds {
let current_time = OffsetDateTime::now_utc().unix_timestamp() as u64;
let rev_status_list = from_revocation_registry_delta_to_revocation_status_list(
&last_rev_reg_delta.value,
&rev_reg_def.clone().convert(())?,
rev_reg_id,
&rev_reg_def,
Some(current_time),
true,
)?;

let cred_def = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,32 +483,26 @@ impl Convert for HashMap<OurRevocationRegistryDefinitionId, OurRevocationRegistr
}
}

impl Convert for HashMap<OurRevocationRegistryDefinitionId, HashMap<u64, OurRevocationRegistry>> {
impl Convert
for HashMap<
OurRevocationRegistryDefinitionId,
(HashMap<u64, OurRevocationRegistry>, OurIssuerId),
>
{
type Args = ();
type Target = Vec<AnoncredsRevocationStatusList>;
type Error = Box<dyn std::error::Error>;

fn convert(self, _args: Self::Args) -> Result<Self::Target, Self::Error> {
let mut lists = Vec::new();
for (rev_reg_def_id, timestamp_map) in self.into_iter() {
for (rev_reg_def_id, (timestamp_map, issuer_id)) in self.into_iter() {
for (timestamp, entry) in timestamp_map {
// TODO - bad splitting
let issuer_id = AnoncredsIssuerId::new(
rev_reg_def_id
.to_string()
.split(':')
.next()
.unwrap()
.to_string(),
)
.unwrap();
let OurRevocationRegistry { value } = entry;
let registry = CryptoRevocationRegistry { accum: value.accum };

let rev_status_list = OurRevocationStatusList::new(
Some(&rev_reg_def_id.to_string()),
issuer_id.convert(())?,
// TODO - this seems incorrect.
issuer_id.clone(),
Default::default(),
Some(registry),
Some(timestamp),
Expand Down
5 changes: 3 additions & 2 deletions aries/aries_vcx_anoncreds/src/anoncreds/base_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anoncreds_types::data_types::{
rev_reg::RevocationRegistry,
rev_reg_def::RevocationRegistryDefinition,
rev_reg_delta::RevocationRegistryDelta,
rev_status_list::RevocationStatusList,
schema::{AttributeNames, Schema},
},
messages::{
Expand Down Expand Up @@ -138,8 +139,7 @@ pub trait BaseAnonCreds: std::fmt::Debug + Send + Sync {
&self,
tails_dir: &Path,
rev_reg_def_json: RevocationRegistryDefinition,
rev_reg_delta_json: RevocationRegistryDelta,
timestamp: u64,
rev_status_list: RevocationStatusList,
cred_rev_id: u32,
) -> VcxAnoncredsResult<CredentialRevocationState>;

Expand Down Expand Up @@ -174,6 +174,7 @@ pub trait BaseAnonCreds: std::fmt::Debug + Send + Sync {

// TODO - FUTURE - think about moving this to somewhere else, as it aggregates other calls (not
// PURE Anoncreds)
// TODO - review functionality below and convert to using statuslists
// ^ YES
async fn revoke_credential_local(
&self,
Expand Down
9 changes: 8 additions & 1 deletion aries/aries_vcx_ledger/src/ledger/base_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anoncreds_types::data_types::{
ledger::{
cred_def::CredentialDefinition, rev_reg::RevocationRegistry,
rev_reg_def::RevocationRegistryDefinition, rev_reg_delta::RevocationRegistryDelta,
schema::Schema,
rev_status_list::RevocationStatusList, schema::Schema,
},
};
use aries_vcx_wallet::wallet::base_wallet::BaseWallet;
Expand Down Expand Up @@ -95,6 +95,13 @@ pub trait AnoncredsLedgerRead: Debug + Send + Sync {
from: Option<u64>,
to: Option<u64>,
) -> VcxLedgerResult<(RevocationRegistryDelta, u64)>;
/// TODO - describe
async fn get_rev_status_list(
&self,
rev_reg_id: &RevocationRegistryDefinitionId,
timestamp: u64,
pre_fetched_rev_reg_def: Option<&RevocationRegistryDefinition>,
) -> VcxLedgerResult<(RevocationStatusList, u64)>;
async fn get_rev_reg(
&self,
rev_reg_id: &RevocationRegistryDefinitionId,
Expand Down
9 changes: 9 additions & 0 deletions aries/aries_vcx_ledger/src/ledger/cheqd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ impl AnoncredsLedgerRead for CheqdAnoncredsLedgerRead {
todo!()
}

async fn get_rev_status_list(
&self,
_rev_reg_id: &RevocationRegistryDefinitionId,
_timestamp: u64,
_: Option<&RevocationRegistryDefinition>,
) -> VcxLedgerResult<(RevocationStatusList, u64)> {
todo!()
}

async fn get_rev_reg(
&self,
_rev_reg_id: &RevocationRegistryDefinitionId,
Expand Down
Loading

0 comments on commit 6c82328

Please sign in to comment.