Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Dec 21, 2023
1 parent fbde450 commit 2e76692
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/data_types/w3c/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Context {
pub struct Contexts(pub Vec<Context>);

impl Contexts {
pub fn get(version: VerifiableCredentialSpecVersion) -> Contexts {
pub fn get(version: &VerifiableCredentialSpecVersion) -> Contexts {
match version {
VerifiableCredentialSpecVersion::V1_1 => ANONCREDS_VC_1_1_CONTEXTS.clone(),
VerifiableCredentialSpecVersion::V2_0 => ANONCREDS_VC_2_0_CONTEXTS.clone(),
Expand Down
26 changes: 23 additions & 3 deletions src/data_types/w3c/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ impl W3CCredential {
issuer: IssuerId,
attributes: CredentialAttributes,
proof: DataIntegrityProof,
version: Option<VerifiableCredentialSpecVersion>,
version: Option<&VerifiableCredentialSpecVersion>,
) -> Self {
let time = Utc::now();
let version = version.unwrap_or_default();
let version = version.cloned().unwrap_or_default();
let (issuance_date, valid_from) = match version {
VerifiableCredentialSpecVersion::V1_1 => (Some(time), None),
VerifiableCredentialSpecVersion::V2_0 => (None, Some(time)),
};
Self {
context: Contexts::get(version),
context: Contexts::get(&version),
type_: ANONCREDS_CREDENTIAL_TYPES.clone(),
issuance_date,
valid_from,
Expand All @@ -93,6 +93,26 @@ impl W3CCredential {
}
}

pub(crate) fn derive(
attributes: CredentialAttributes,
proof: DataIntegrityProof,
credential: &W3CCredential,
) -> W3CCredential {
W3CCredential {
context: credential.context.clone(),
type_: credential.type_.clone(),
issuer: credential.issuer.clone(),
id: credential.id.clone(),
issuance_date: credential.issuance_date,
valid_from: credential.valid_from,
credential_subject: CredentialSubject {
id: credential.credential_subject.id.clone(),
attributes,
},
proof: OneOrMany::One(CredentialProof::DataIntegrityProof(proof)),
}
}

pub fn version(&self) -> Result<VerifiableCredentialSpecVersion> {
self.context.version()
}
Expand Down
6 changes: 3 additions & 3 deletions src/data_types/w3c/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl W3CPresentation {
pub fn new(
verifiable_credential: Vec<W3CCredential>,
proof: DataIntegrityProof,
version: Option<VerifiableCredentialSpecVersion>,
version: Option<&VerifiableCredentialSpecVersion>,
) -> Self {
let version = version.unwrap_or_default();
let version = version.cloned().unwrap_or_default();
Self {
context: Contexts::get(version),
context: Contexts::get(&version),
type_: ANONCREDS_PRESENTATION_TYPES.clone(),
verifiable_credential,
proof,
Expand Down
2 changes: 1 addition & 1 deletion src/services/w3c/credential_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn credential_to_w3c(
witness: credential.witness,
};
let proof = DataIntegrityProof::new_credential_proof(signature);
let w3c_credential = W3CCredential::new(issuer, attributes, proof, version);
let w3c_credential = W3CCredential::new(issuer, attributes, proof, version.as_ref());

trace!("credential_to_w3c <<< w3c_credential {:?}", w3c_credential);

Expand Down
2 changes: 1 addition & 1 deletion src/services/w3c/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn create_credential(
cred_def.issuer_id.to_owned(),
raw_credential_values,
proof,
version,
version.as_ref(),
);

trace!(
Expand Down
38 changes: 12 additions & 26 deletions src/services/w3c/prover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::data_types::cred_def::{CredentialDefinition, CredentialDefinitionId};
use crate::data_types::pres_request::PresentationRequestPayload;
use crate::data_types::schema::{Schema, SchemaId};
use crate::data_types::w3c::credential::{CredentialProof, CredentialSubject, W3CCredential};
use crate::data_types::w3c::credential::W3CCredential;
use crate::data_types::w3c::presentation::W3CPresentation;
use crate::error::Result;
use crate::types::{
Expand All @@ -10,7 +10,7 @@ use crate::types::{
};
use crate::utils::validation::Validatable;

use crate::data_types::w3c::one_or_many::OneOrMany;
use crate::data_types::w3c::credential_attributes::CredentialAttributes;
use crate::data_types::w3c::presentation::PredicateAttribute;
use crate::data_types::w3c::proof::{
CredentialPresentationProofValue, DataIntegrityProof, PresentationProofValue,
Expand Down Expand Up @@ -183,7 +183,7 @@ pub fn create_presentation(
// cl signatures generates sub proofs and aggregated at once at the end
// so we need to iterate over credentials again an put sub proofs into their proofs
for (present, sub_proof) in credentials.0.iter().zip(cl_proof.proofs) {
let credential_subject = build_credential_subject(presentation_request, present)?;
let credential_attributes = build_credential_attributes(presentation_request, present)?;
let credential_proof = present.cred.get_credential_signature_proof()?;
let proof = CredentialPresentationProofValue {
schema_id: credential_proof.schema_id.to_owned(),
Expand All @@ -193,12 +193,7 @@ pub fn create_presentation(
sub_proof,
};
let proof = DataIntegrityProof::new_credential_presentation_proof(proof);
let credential = W3CCredential {
credential_subject,
proof: OneOrMany::One(CredentialProof::DataIntegrityProof(proof)),
..present.cred.to_owned()
};

let credential = W3CCredential::derive(credential_attributes, proof, present.cred);
verifiable_credentials.push(credential);
}

Expand All @@ -209,7 +204,7 @@ pub fn create_presentation(
presentation_proof,
presentation_request.nonce.to_string(),
);
let presentation = W3CPresentation::new(verifiable_credentials, proof, version);
let presentation = W3CPresentation::new(verifiable_credentials, proof, version.as_ref());

trace!(
"create_w3c_presentation <<< presentation: {:?}",
Expand All @@ -219,14 +214,11 @@ pub fn create_presentation(
Ok(presentation)
}

fn build_credential_subject<'p>(
fn build_credential_attributes<'p>(
pres_req: &PresentationRequestPayload,
credentials: &PresentCredential<'p, W3CCredential>,
) -> Result<CredentialSubject> {
let mut credential_subject = CredentialSubject {
id: credentials.cred.credential_subject.id.clone(),
attributes: Default::default(),
};
) -> Result<CredentialAttributes> {
let mut attributes = CredentialAttributes::default();

for (referent, reveal) in credentials.requested_attributes.iter() {
let requested_attribute = pres_req
Expand All @@ -237,17 +229,13 @@ fn build_credential_subject<'p>(
if let Some(ref name) = requested_attribute.name {
let (attribute, value) = credentials.cred.get_case_insensitive_attribute(name)?;
if *reveal {
credential_subject
.attributes
.add_attribute(attribute, value);
attributes.add_attribute(attribute, value);
}
}
if let Some(ref names) = requested_attribute.names {
for name in names {
let (attribute, value) = credentials.cred.get_case_insensitive_attribute(name)?;
credential_subject
.attributes
.add_attribute(attribute, value);
attributes.add_attribute(attribute, value);
}
}
}
Expand All @@ -262,10 +250,8 @@ fn build_credential_subject<'p>(
.cred
.get_case_insensitive_attribute(&predicate_info.name)?;
let predicate = PredicateAttribute::from(predicate_info);
credential_subject
.attributes
.add_predicate(attribute, predicate)?;
attributes.add_predicate(attribute, predicate)?;
}

Ok(credential_subject)
Ok(attributes)
}

0 comments on commit 2e76692

Please sign in to comment.