Skip to content

Commit

Permalink
Use credential status property for exposing revocation registry id
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Nov 21, 2023
1 parent e4bc7ad commit 142af17
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 86 deletions.
55 changes: 49 additions & 6 deletions src/data_types/w3c/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,31 @@ impl CredentialAttributes {

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CredentialStatus {
pub id: URI,
#[serde(rename = "type")]
pub type_: String,
pub type_: CredentialStatusType,
pub id: RevocationRegistryDefinitionId,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CredentialStatusType {
#[serde(rename = "AnonCredsCredentialStatusList2023")]
AnonCredsCredentialStatusList2023,
Other(String),
}

impl Default for CredentialStatusType {
fn default() -> Self {
CredentialStatusType::AnonCredsCredentialStatusList2023
}
}

impl CredentialStatus {
pub fn new(id: RevocationRegistryDefinitionId) -> CredentialStatus {
CredentialStatus {
type_: CredentialStatusType::AnonCredsCredentialStatusList2023,
id,
}
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -197,8 +219,6 @@ pub struct CredentialSchema {
pub type_: CredentialSchemaType,
pub definition: CredentialDefinitionId,
pub schema: SchemaId,
#[serde(skip_serializing_if = "Option::is_none")]
pub revocation_registry: Option<RevocationRegistryDefinitionId>,
#[serde(default)]
pub encoding: CredentialValuesEncoding,
}
Expand All @@ -207,14 +227,12 @@ impl CredentialSchema {
pub fn new(
schema: SchemaId,
definition: CredentialDefinitionId,
revocation_registry: Option<RevocationRegistryDefinitionId>,
encoding: CredentialValuesEncoding,
) -> CredentialSchema {
CredentialSchema {
type_: CredentialSchemaType::AnonCredsDefinition,
definition,
schema,
revocation_registry,
encoding,
}
}
Expand Down Expand Up @@ -255,6 +273,10 @@ impl W3CCredential {
self.credential_schema = credential_schema
}

pub fn set_credential_status(&mut self, credential_status: CredentialStatus) {
self.credential_status = Some(credential_status)
}

pub fn set_attributes(&mut self, attributes: CredentialAttributes) {
self.credential_subject.attributes = attributes
}
Expand Down Expand Up @@ -321,4 +343,25 @@ impl W3CCredential {
}
Ok(())
}

pub fn schema_id(&self) -> &SchemaId {
&self.credential_schema.schema
}

pub fn cred_def_id(&self) -> &CredentialDefinitionId {
&self.credential_schema.definition
}

pub fn get_rev_reg_id(&self) -> Option<&RevocationRegistryDefinitionId> {
if let Some(credential_status) = self.credential_status.as_ref() {
match credential_status.type_ {
CredentialStatusType::AnonCredsCredentialStatusList2023 => {
Some(&credential_status.id)
}
CredentialStatusType::Other(_) => None,
}
} else {
None
}
}
}
23 changes: 9 additions & 14 deletions src/data_types/w3c/credential_proof.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::data_types::w3c::presentation_proof::CredentialPresentationProof;
use crate::utils::base64;
use crate::utils::encoded_object::EncodedObject;
use anoncreds_clsignatures::{
CredentialSignature as CLCredentialSignature, RevocationRegistry, SignatureCorrectnessProof,
Witness,
};
use serde_json::Value;

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CredentialProof {
Expand All @@ -24,14 +25,14 @@ pub struct CredentialSignatureProof {
impl CredentialSignatureProof {
pub fn new(signature: CredentialSignature) -> Self {
CredentialSignatureProof {
type_: CredentialSignatureType::CLSignature2023,
type_: CredentialSignatureType::AnonCredsProof2023,
signature: signature.encode(),
}
}

pub fn get_credential_signature(&self) -> crate::Result<CredentialSignature> {
match self.type_ {
CredentialSignatureType::CLSignature2023 => {
CredentialSignatureType::AnonCredsProof2023 => {
CredentialSignature::decode(&self.signature)
}
}
Expand All @@ -42,13 +43,13 @@ pub type NonAnonCredsDataIntegrityProof = Value;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CredentialSignatureType {
#[serde(rename = "CLSignature2023")]
CLSignature2023,
#[serde(rename = "AnonCredsProof2023")]
AnonCredsProof2023,
}

impl Default for CredentialSignatureType {
fn default() -> Self {
CredentialSignatureType::CLSignature2023
CredentialSignatureType::AnonCredsProof2023
}
}

Expand Down Expand Up @@ -107,12 +108,6 @@ impl CredentialSignature {
witness,
}
}

pub fn encode(&self) -> String {
base64::encode_json(&self)
}

pub fn decode(string: &str) -> crate::Result<CredentialSignature> {
base64::decode_json(string)
}
}

impl EncodedObject for CredentialSignature {}
30 changes: 9 additions & 21 deletions src/data_types/w3c/presentation_proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::data_types::pres_request::{PredicateInfo, PredicateTypes};
use crate::utils::base64;
use crate::utils::encoded_object::EncodedObject;
use anoncreds_clsignatures::{AggregatedProof, SubProof};
use std::collections::HashSet;

Expand Down Expand Up @@ -28,16 +28,10 @@ impl CredentialPresentationProofValue {
pub fn new(sub_proof: SubProof) -> CredentialPresentationProofValue {
CredentialPresentationProofValue { sub_proof }
}

pub fn encode(&self) -> String {
base64::encode_json(&self)
}

pub fn decode(string: &str) -> crate::Result<CredentialPresentationProofValue> {
base64::decode_json(string)
}
}

impl EncodedObject for CredentialPresentationProofValue {}

impl CredentialPresentationProof {
pub fn new(
proof_value: CredentialPresentationProofValue,
Expand Down Expand Up @@ -99,16 +93,10 @@ impl PresentationProofValue {
aggregated: aggregated_proof,
}
}

pub fn encode(&self) -> String {
base64::encode_json(&self)
}

pub fn decode(string: &str) -> crate::Result<PresentationProofValue> {
base64::decode_json(string)
}
}

impl EncodedObject for PresentationProofValue {}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PresentationProofType {
#[serde(rename = "AnonCredsPresentationProof2023")]
Expand Down Expand Up @@ -137,8 +125,8 @@ pub struct CredentialAttributesMapping {
pub struct PredicateAttribute {
#[serde(rename = "type")]
pub type_: PredicateAttributeType,
pub p_type: PredicateTypes,
pub p_value: i32,
pub predicate: PredicateTypes,
pub value: i32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -157,8 +145,8 @@ impl From<PredicateInfo> for PredicateAttribute {
fn from(info: PredicateInfo) -> Self {
PredicateAttribute {
type_: PredicateAttributeType::AnonCredsPredicate,
p_type: info.p_type,
p_value: info.p_value,
predicate: info.p_type,
value: info.p_value,
}
}
}
8 changes: 3 additions & 5 deletions src/ffi/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,10 @@ pub extern "C" fn anoncreds_w3c_credential_get_attribute(
let cred = handle.load()?;
let cred = cred.cast_ref::<W3CCredential>()?;
let val = match name.as_opt_str().unwrap_or_default() {
"schema_id" => rust_string_to_c(cred.credential_schema.schema.clone()),
"cred_def_id" => rust_string_to_c(cred.credential_schema.definition.to_string()),
"schema_id" => rust_string_to_c(cred.schema_id().clone()),
"cred_def_id" => rust_string_to_c(cred.cred_def_id().to_string()),
"rev_reg_id" => cred
.credential_schema
.revocation_registry
.as_ref()
.get_rev_reg_id()
.map_or(ptr::null_mut(), |s| rust_string_to_c(s.to_string())),
"rev_reg_index" => cred
.get_credential_signature_proof()?
Expand Down
26 changes: 18 additions & 8 deletions src/services/credential_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::data_types::cred_def::CredentialDefinition;
use crate::data_types::credential::CredentialValuesEncoding;
use crate::data_types::w3c::credential::{CredentialAttributes, CredentialSchema, W3CCredential};
use crate::data_types::w3c::credential::{
CredentialAttributes, CredentialSchema, CredentialStatus, W3CCredential,
};
use crate::data_types::w3c::credential_proof::{
CredentialProof, CredentialSignature, CredentialSignatureProof,
};
Expand Down Expand Up @@ -114,9 +116,11 @@ pub fn credential_to_w3c(
w3c_credential.set_credential_schema(CredentialSchema::new(
credential.schema_id,
credential.cred_def_id,
credential.rev_reg_id,
CredentialValuesEncoding::Auto,
));
if let Some(rev_reg_id) = credential.rev_reg_id {
w3c_credential.set_credential_status(CredentialStatus::new(rev_reg_id))
}
w3c_credential.set_attributes(attributes);
w3c_credential.add_proof(CredentialProof::AnonCredsSignatureProof(proof));

Expand Down Expand Up @@ -215,9 +219,9 @@ pub fn credential_from_w3c(w3c_credential: &W3CCredential) -> Result<Credential,

w3c_credential.validate()?;

let schema_id = w3c_credential.credential_schema.schema.clone();
let cred_def_id = w3c_credential.credential_schema.definition.clone();
let rev_reg_id = w3c_credential.credential_schema.revocation_registry.clone();
let schema_id = w3c_credential.schema_id().clone();
let cred_def_id = w3c_credential.cred_def_id().clone();
let rev_reg_id = w3c_credential.get_rev_reg_id().cloned();
let proof = w3c_credential.get_credential_signature_proof()?;
let credential_signature = proof.get_credential_signature()?;
let values = w3c_credential
Expand Down Expand Up @@ -253,6 +257,7 @@ mod tests {
AttributeNames, CredentialDefinitionConfig, CredentialValues, MakeCredentialValues,
SignatureType,
};
use crate::utils::encoded_object::EncodedObject;
use crate::{issuer, ErrorKind};
use anoncreds_clsignatures::{
CredentialSignature as CLCredentialSignature,
Expand Down Expand Up @@ -350,7 +355,6 @@ mod tests {
credential.set_credential_schema(CredentialSchema::new(
_schema_id(),
_cred_def_id(),
None,
CredentialValuesEncoding::Auto,
));
credential.set_attributes(CredentialAttributes::from(&_cred_values()));
Expand Down Expand Up @@ -387,7 +391,10 @@ mod tests {
legacy_credential.cred_def_id
);
assert_eq!(
w3c_credential.credential_schema.revocation_registry,
w3c_credential
.credential_status
.clone()
.map(|status| status.id),
legacy_credential.rev_reg_id
);
assert_eq!(
Expand Down Expand Up @@ -419,7 +426,10 @@ mod tests {
);
assert_eq!(
legacy_credential.rev_reg_id,
w3c_credential.credential_schema.revocation_registry
w3c_credential
.credential_status
.clone()
.map(|status| status.id)
);
assert_eq!(legacy_credential.values, _cred_values());
assert_eq!(legacy_credential.signature, _signature_data().signature);
Expand Down
8 changes: 6 additions & 2 deletions src/services/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::data_types::credential::CredentialValuesEncoding;
use crate::data_types::issuer_id::IssuerId;
use crate::data_types::rev_reg_def::RevocationRegistryDefinitionId;
use crate::data_types::schema::SchemaId;
use crate::data_types::w3c::credential::{CredentialAttributes, CredentialSchema, W3CCredential};
use crate::data_types::w3c::credential::{
CredentialAttributes, CredentialSchema, CredentialStatus, W3CCredential,
};
use crate::data_types::w3c::credential_proof::{
CredentialProof, CredentialSignature, CredentialSignatureProof,
};
Expand Down Expand Up @@ -858,9 +860,11 @@ pub fn create_w3c_credential(
credential.set_credential_schema(CredentialSchema::new(
cred_offer.schema_id.clone(),
cred_offer.cred_def_id.clone(),
rev_reg_id,
encoding,
));
if let Some(rev_reg_id) = rev_reg_id {
credential.set_credential_status(CredentialStatus::new(rev_reg_id));
}
credential.set_attributes(raw_credential_values);
credential.add_proof(CredentialProof::AnonCredsSignatureProof(proof));

Expand Down
7 changes: 4 additions & 3 deletions src/services/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::data_types::w3c::presentation_proof::{
CredentialAttributesMapping, CredentialPresentationProof, CredentialPresentationProofValue,
PredicateAttribute, PresentationProof, PresentationProofValue,
};
use crate::utils::encoded_object::EncodedObject;
use anoncreds_clsignatures::{
CredentialSignature as CLCredentialSignature, NonCredentialSchema, Proof, ProofBuilder,
SignatureCorrectnessProof,
Expand Down Expand Up @@ -668,9 +669,9 @@ pub fn create_w3c_presentation(
.encode(&credential.credential_schema.encoding)?;
let proof = credential.get_credential_signature_proof()?;
let signature = proof.get_credential_signature()?;
let schema_id = &credential.credential_schema.schema;
let cred_def_id = &credential.credential_schema.definition;
let rev_reg_id = credential.credential_schema.revocation_registry.as_ref();
let schema_id = credential.schema_id();
let cred_def_id = credential.cred_def_id();
let rev_reg_id = credential.get_rev_reg_id();

proof_builder.add_sub_proof(
&credential_values,
Expand Down
20 changes: 8 additions & 12 deletions src/services/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,9 @@ pub fn verify_w3c_presentation(
for verifiable_credential in presentation.verifiable_credential.iter() {
let credential_proof = verifiable_credential.get_presentation_proof()?;
let proof_data = credential_proof.get_proof_value()?;
let schema_id = &verifiable_credential.credential_schema.schema;
let cred_def_id = &verifiable_credential.credential_schema.definition;
let rev_reg_id = verifiable_credential
.credential_schema
.revocation_registry
.as_ref();
let schema_id = &verifiable_credential.schema_id();
let cred_def_id = &verifiable_credential.cred_def_id();
let rev_reg_id = verifiable_credential.get_rev_reg_id();

let mut revealed_attribute: HashSet<String> =
credential_proof.mapping.revealed_attributes.clone();
Expand Down Expand Up @@ -825,13 +822,12 @@ fn collect_received_attrs_and_predicates_from_w3c_presentation(

for verifiable_credential in proof.verifiable_credential.iter() {
let presentation_proof = verifiable_credential.get_presentation_proof()?;
let rev_reg_id = verifiable_credential.get_rev_reg_id().cloned();

let identifier: Identifier = Identifier {
schema_id: verifiable_credential.credential_schema.schema.clone(),
cred_def_id: verifiable_credential.credential_schema.definition.clone(),
rev_reg_id: verifiable_credential
.credential_schema
.revocation_registry
.clone(),
schema_id: verifiable_credential.schema_id().clone(),
cred_def_id: verifiable_credential.cred_def_id().clone(),
rev_reg_id,
timestamp: None,
};
for revealed_attribute in &presentation_proof.mapping.revealed_attributes {
Expand Down
Loading

0 comments on commit 142af17

Please sign in to comment.