diff --git a/src/data_types/cred_def.rs b/src/data_types/cred_def.rs index 354d3f41..caf6fd30 100644 --- a/src/data_types/cred_def.rs +++ b/src/data_types/cred_def.rs @@ -13,19 +13,6 @@ pub const CL_SIGNATURE_TYPE: &str = "CL"; impl_anoncreds_object_identifier!(CredentialDefinitionId); -impl CredentialDefinitionId { - // FIXME: As AnonCreds-rs is DID method agnostic, so it does not analyze/handle the values of id fields. - // For conversion into W3C Credentials form we need to set issuer_id attribute but legacy credentials do not contain it explicitly. - // We only can parse issuer from the legacy form? - pub fn issuer_did(&self) -> IssuerId { - if let Some(caps) = LEGACY_CRED_DEF_IDENTIFIER.captures(&self.0) { - IssuerId(caps[0].to_string()) - } else { - IssuerId(self.0.to_owned()) - } - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum SignatureType { CL, diff --git a/src/data_types/w3c/credential.rs b/src/data_types/w3c/credential.rs index a4a777c1..f8d18009 100644 --- a/src/data_types/w3c/credential.rs +++ b/src/data_types/w3c/credential.rs @@ -1,14 +1,17 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; -use std::collections::HashSet; +use serde_json::{json, Value}; +use std::collections::{HashMap, HashSet}; use std::string::ToString; +use zeroize::Zeroize; +use crate::data_types::pres_request::{PredicateInfo, PredicateTypes}; use crate::data_types::w3c::constants::{ANONCREDS_CONTEXTS, ANONCREDS_TYPES}; use crate::data_types::w3c::credential_proof::{CredentialProof, CredentialSignatureProof}; use crate::data_types::w3c::presentation_proof::CredentialPresentationProof; use crate::data_types::{ cred_def::CredentialDefinitionId, - credential::{CredentialValuesEncoding, RawCredentialValues}, + credential::CredentialValuesEncoding, issuer_id::IssuerId, rev_reg_def::RevocationRegistryDefinitionId, schema::SchemaId, @@ -20,7 +23,9 @@ use crate::data_types::{ uri::URI, }, }; -use crate::utils::datetime; +use crate::error::ValidationError; +use crate::types::{CredentialValues, MakeCredentialValues}; +use crate::utils::validation::Validatable; use crate::Result; /// AnonCreds W3C Credential definition @@ -61,7 +66,138 @@ pub struct CredentialSubject { #[serde(skip_serializing_if = "Option::is_none")] pub id: Option, #[serde(flatten)] - pub attributes: RawCredentialValues, + pub attributes: CredentialAttributes, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)] +pub struct CredentialAttributes(pub HashMap); + +#[cfg(feature = "zeroize")] +impl Drop for CredentialAttributes { + fn drop(&mut self) { + self.zeroize(); + } +} + +#[cfg(feature = "zeroize")] +impl Zeroize for CredentialAttributes { + fn zeroize(&mut self) { + for attr in self.0.values_mut() { + if let Value::String(attr) = attr { + attr.zeroize() + } + } + } +} + +impl Validatable for CredentialAttributes { + fn validate(&self) -> std::result::Result<(), ValidationError> { + if self.0.is_empty() { + return Err( + "CredentialAttributes validation failed: empty list has been passed".into(), + ); + } + for (attribute, value) in self.0.iter() { + match value { + Value::String(_) | Value::Object(_) => {} + _ => { + return Err(format!( + "CredentialAttributes validation failed: {} value format is not supported", + attribute + ) + .into()) + } + } + } + + Ok(()) + } +} + +impl From<&CredentialValues> for CredentialAttributes { + fn from(values: &CredentialValues) -> Self { + CredentialAttributes( + values + .0 + .iter() + .map(|(attribute, values)| { + (attribute.to_owned(), Value::String(values.raw.to_owned())) + }) + .collect(), + ) + } +} + +impl CredentialAttributes { + pub fn add_attribute(&mut self, attribute: String, value: Value) { + self.0.insert(attribute, value); + } + + pub fn add_predicate(&mut self, attribute: String, value: PredicateAttribute) { + self.0.insert(attribute, json!(value)); + } + + pub fn get_attribute(&self, attribute: &str) -> Result<&Value> { + self.0 + .get(attribute) + .ok_or_else(|| err_msg!("Credential attribute {} not found", attribute)) + } + + pub fn encode(&self, encoding: &CredentialValuesEncoding) -> Result { + match encoding { + CredentialValuesEncoding::Auto => { + let mut cred_values = MakeCredentialValues::default(); + for (attribute, raw_value) in self.0.iter() { + match raw_value { + Value::String(raw_value) => { + cred_values.add_raw(attribute, &raw_value.to_string())? + } + value => { + return Err(err_msg!( + "Encoding is not supported for credential value {:?}", + value + )); + } + } + } + Ok(cred_values.into()) + } + encoding => Err(err_msg!( + "Credential values encoding {:?} is not supported", + encoding + )), + } + } +} + +#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] +pub struct PredicateAttribute { + #[serde(rename = "type")] + pub type_: PredicateAttributeType, + pub p_type: PredicateTypes, + pub p_value: i32, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum PredicateAttributeType { + #[serde(rename = "AnonCredsPredicate")] + AnonCredsPredicate, +} + +impl Default for PredicateAttributeType { + fn default() -> Self { + PredicateAttributeType::AnonCredsPredicate + } +} + +impl From for PredicateAttribute { + fn from(info: PredicateInfo) -> Self { + PredicateAttribute { + type_: PredicateAttributeType::AnonCredsPredicate, + p_type: info.p_type, + p_value: info.p_value, + } + } } #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] @@ -118,7 +254,7 @@ impl W3CCredential { W3CCredential { context: ANONCREDS_CONTEXTS.clone(), type_: ANONCREDS_TYPES.clone(), - issuance_date: datetime::today(), + issuance_date: Utc::now(), proof: OneOrMany::Many(Vec::new()), ..Default::default() } @@ -136,7 +272,7 @@ impl W3CCredential { self.credential_schema = credential_schema } - pub fn set_attributes(&mut self, attributes: RawCredentialValues) { + pub fn set_attributes(&mut self, attributes: CredentialAttributes) { self.credential_subject.attributes = attributes } diff --git a/src/ffi/credential.rs b/src/ffi/credential.rs index d0542fa2..f6ebf6e7 100644 --- a/src/ffi/credential.rs +++ b/src/ffi/credential.rs @@ -7,7 +7,8 @@ use super::error::{catch_error, ErrorCode}; use super::object::{AnoncredsObject, ObjectHandle}; use super::util::FfiStrList; use crate::conversion::{credential_from_w3c, credential_to_w3c}; -use crate::data_types::credential::{CredentialValuesEncoding, RawCredentialValues}; +use crate::data_types::credential::CredentialValuesEncoding; +use crate::data_types::w3c::credential::CredentialAttributes; use crate::data_types::w3c::credential_proof::{CredentialProof, NonAnonCredsDataIntegrityProof}; use crate::data_types::w3c::uri::URI; use crate::data_types::{link_secret::LinkSecret, w3c::credential::W3CCredential}; @@ -18,7 +19,7 @@ use crate::services::{ prover::{process_credential, process_w3c_credential}, types::{Credential, CredentialRevocationConfig, MakeCredentialValues}, }; -use crate::types::{CredentialValues, MakeRawCredentialValues}; +use crate::types::{CredentialValues, MakeCredentialAttributes}; use crate::Error; #[derive(Debug)] @@ -226,7 +227,7 @@ pub extern "C" fn anoncreds_create_w3c_credential( catch_error(|| { check_useful_c_ptr!(cred_p); - let cred_values = _raw_credential_values(attr_names, attr_raw_values)?; + let cred_values = _credential_attributes(attr_names, attr_raw_values)?; let revocation_config = _revocation_config(revocation)?; let encoding = encoding.as_opt_str().map(CredentialValuesEncoding::from); @@ -305,6 +306,7 @@ pub extern "C" fn anoncreds_process_w3c_credential( #[no_mangle] pub extern "C" fn anoncreds_credential_to_w3c( cred: ObjectHandle, + cred_def: ObjectHandle, cred_p: *mut ObjectHandle, ) -> ErrorCode { catch_error(|| { @@ -313,7 +315,7 @@ pub extern "C" fn anoncreds_credential_to_w3c( let credential = cred.load()?; let credential = credential.cast_ref::()?; - let w3c_credential = credential_to_w3c(credential)?; + let w3c_credential = credential_to_w3c(credential, cred_def.load()?.cast_ref()?)?; let w3c_cred = ObjectHandle::create(w3c_credential)?; unsafe { *cred_p = w3c_cred }; @@ -583,10 +585,10 @@ fn _encoded_credential_values( Ok(cred_values.into()) } -fn _raw_credential_values( +fn _credential_attributes( attr_names: FfiStrList, attr_raw_values: FfiStrList, -) -> Result { +) -> Result { if attr_names.is_empty() { return Err(err_msg!("Cannot create credential with no attribute")); } @@ -595,7 +597,7 @@ fn _raw_credential_values( "Mismatch between length of attribute names and raw values" )); } - let mut cred_values = MakeRawCredentialValues::default(); + let mut cred_values = MakeCredentialAttributes::default(); for (name, raw) in attr_names.as_slice().iter().zip(attr_raw_values.as_slice()) { let name = name .as_opt_str() diff --git a/src/services/conversion.rs b/src/services/conversion.rs index 614f7b6c..571d1369 100644 --- a/src/services/conversion.rs +++ b/src/services/conversion.rs @@ -1,5 +1,6 @@ -use crate::data_types::credential::{CredentialValuesEncoding, RawCredentialValues}; -use crate::data_types::w3c::credential::{CredentialSchema, W3CCredential}; +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_proof::{ CredentialProof, CredentialSignature, CredentialSignatureProof, }; @@ -7,15 +8,92 @@ use crate::types::Credential; use crate::utils::validation::Validatable; use crate::Error; -pub fn credential_to_w3c(credential: &Credential) -> Result { +/// Convert credential in legacy form into W3C AnonCreds credential form +/// +/// # Example +/// +/// ```rust +/// use anoncreds::conversion; +/// use anoncreds::issuer; +/// use anoncreds::prover; +/// use anoncreds::types::MakeCredentialValues; +/// +/// use anoncreds::types::CredentialDefinitionConfig; +/// use anoncreds::types::SignatureType; +/// use anoncreds::data_types::issuer_id::IssuerId; +/// use anoncreds::data_types::schema::SchemaId; +/// use anoncreds::data_types::cred_def::CredentialDefinitionId; +/// +/// let attribute_names: &[&str] = &["name", "age"]; +/// let issuer_id = IssuerId::new("did:web:xyz").expect("Invalid issuer ID"); +/// let schema_id = SchemaId::new("did:web:xyz/resource/schema").expect("Invalid schema ID"); +/// let cred_def_id = CredentialDefinitionId::new("did:web:xyz/resource/cred-def").expect("Invalid credential definition ID"); +/// +/// let schema = issuer::create_schema("schema name", +/// "1.0", +/// issuer_id.clone(), +/// attribute_names.into(), +/// ).expect("Unable to create schema"); +/// +/// let (cred_def, cred_def_priv, key_correctness_proof) = +/// issuer::create_credential_definition(schema_id.clone(), +/// &schema, +/// issuer_id, +/// "default-tag", +/// SignatureType::CL, +/// CredentialDefinitionConfig::default(), +/// ).expect("Unable to create Credential Definition"); +/// +/// let credential_offer = +/// issuer::create_credential_offer(schema_id, +/// cred_def_id, +/// &key_correctness_proof, +/// ).expect("Unable to create Credential Offer"); +/// +/// let link_secret = +/// prover::create_link_secret().expect("Unable to create link secret"); +/// +/// let (credential_request, credential_request_metadata) = +/// prover::create_credential_request(Some("entropy"), +/// None, +/// &cred_def, +/// &link_secret, +/// "my-secret-id", +/// &credential_offer, +/// ).expect("Unable to create credential request"); +/// +/// let mut credential_values = MakeCredentialValues::default(); +/// credential_values.add_raw("name", "john").expect("Unable to add credential value"); +/// credential_values.add_raw("age", "28").expect("Unable to add credential value"); +/// +/// let mut credential = +/// issuer::create_credential(&cred_def, +/// &cred_def_priv, +/// &credential_offer, +/// &credential_request, +/// credential_values.into(), +/// None, +/// ).expect("Unable to create credential"); +/// +/// prover::process_credential(&mut credential, +/// &credential_request_metadata, +/// &link_secret, +/// &cred_def, +/// None, +/// ).expect("Unable to process the credential"); +/// +/// let _w3c_credential = conversion::credential_to_w3c(&credential, &cred_def) +/// .expect("Unable to convert credential to w3c form"); +/// +/// ``` +pub fn credential_to_w3c( + credential: &Credential, + cred_def: &CredentialDefinition, +) -> Result { credential.validate()?; let credential = credential.try_clone()?; - - // FIXME: As AnonCreds-rs is DID method agnostic, so it does not analyze/handle the values of id fields. - // For conversion into W3C Credentials form we need to set issuer_id attribute but legacy credentials do not contain it explicitly. - // We only can parse issuer from the legacy form? - let issuer = credential.cred_def_id.issuer_did(); + let issuer = cred_def.issuer_id.clone(); let signature = CredentialSignature::new( credential.signature, credential.signature_correctness_proof, @@ -23,7 +101,7 @@ pub fn credential_to_w3c(credential: &Credential) -> Result Result Result { w3c_credential.validate()?; @@ -71,11 +228,14 @@ mod tests { use super::*; use crate::data_types::cred_def::CredentialDefinitionId; use crate::data_types::issuer_id::IssuerId; - use crate::data_types::schema::SchemaId; + use crate::data_types::schema::{Schema, SchemaId}; use crate::data_types::w3c::constants::{ANONCREDS_CONTEXTS, ANONCREDS_TYPES}; use crate::data_types::w3c::one_or_many::OneOrMany; - use crate::types::{CredentialValues, MakeCredentialValues}; - use crate::ErrorKind; + use crate::types::{ + AttributeNames, CredentialDefinitionConfig, CredentialValues, MakeCredentialValues, + SignatureType, + }; + use crate::{issuer, ErrorKind}; use anoncreds_clsignatures::{ CredentialSignature as CLCredentialSignature, SignatureCorrectnessProof as CLSignatureCorrectnessProof, @@ -97,6 +257,27 @@ mod tests { CredentialDefinitionId::new_unchecked(CRED_DEF_ID) } + fn _attributes() -> AttributeNames { + AttributeNames::from(vec!["name".to_owned(), "age".to_owned()]) + } + + fn _credential_definition() -> CredentialDefinition { + let schema = + issuer::create_schema("schema:name", "1.0", _issuer_id(), _attributes()).unwrap(); + let (cred_def, _, _) = issuer::create_credential_definition( + _schema_id(), + &schema, + _issuer_id(), + "default", + SignatureType::CL, + CredentialDefinitionConfig { + support_revocation: true, + }, + ) + .unwrap(); + cred_def + } + fn _cred_values() -> CredentialValues { let mut make = MakeCredentialValues::default(); make.add_raw("name", "Alice").unwrap(); @@ -154,7 +335,7 @@ mod tests { None, CredentialValuesEncoding::Auto, )); - credential.set_attributes(RawCredentialValues::from(&_cred_values())); + credential.set_attributes(CredentialAttributes::from(&_cred_values())); credential.add_proof(CredentialProof::AnonCredsSignatureProof( CredentialSignatureProof::new(_signature_data()), )); @@ -164,8 +345,9 @@ mod tests { #[test] fn test_convert_credential_to_and_from_w3c() { let original_legacy_credential = _legacy_credential(); - let w3c_credential = credential_to_w3c(&original_legacy_credential) - .expect("unable to convert credential to w3c form"); + let w3c_credential = + credential_to_w3c(&original_legacy_credential, &_credential_definition()) + .expect("unable to convert credential to w3c form"); let legacy_credential = credential_from_w3c(&w3c_credential) .expect("unable to convert credential to legacy form"); assert_eq!(json!(original_legacy_credential), json!(legacy_credential),) @@ -174,7 +356,7 @@ mod tests { #[test] fn test_credential_to_w3c_form() { let legacy_credential = _legacy_credential(); - let w3c_credential = credential_to_w3c(&legacy_credential) + let w3c_credential = credential_to_w3c(&legacy_credential, &_credential_definition()) .expect("unable to convert credential to w3c form"); assert_eq!(w3c_credential.context, ANONCREDS_CONTEXTS.clone()); assert_eq!(w3c_credential.type_, ANONCREDS_TYPES.clone()); @@ -196,7 +378,7 @@ mod tests { ); assert_eq!( w3c_credential.credential_subject.attributes, - RawCredentialValues::from(&legacy_credential.values) + CredentialAttributes::from(&legacy_credential.values) ); let proof = w3c_credential .get_credential_signature_proof() diff --git a/src/services/issuer.rs b/src/services/issuer.rs index 95b64367..7170f087 100644 --- a/src/services/issuer.rs +++ b/src/services/issuer.rs @@ -1,10 +1,10 @@ use crate::cl::{Issuer, RevocationRegistry as CryptoRevocationRegistry}; use crate::data_types::cred_def::CredentialDefinitionId; -use crate::data_types::credential::{CredentialValuesEncoding, RawCredentialValues}; +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::{CredentialSchema, W3CCredential}; +use crate::data_types::w3c::credential::{CredentialAttributes, CredentialSchema, W3CCredential}; use crate::data_types::w3c::credential_proof::{ CredentialProof, CredentialSignature, CredentialSignatureProof, }; @@ -757,7 +757,7 @@ pub fn create_credential( /// ```rust /// use anoncreds::issuer; /// use anoncreds::prover; -/// use anoncreds::types::MakeRawCredentialValues; +/// use anoncreds::types::MakeCredentialAttributes; /// /// use anoncreds::types::CredentialDefinitionConfig; /// use anoncreds::types::SignatureType; @@ -803,7 +803,7 @@ pub fn create_credential( /// &credential_offer, /// ).expect("Unable to create credential request"); /// -/// let mut credential_values = MakeRawCredentialValues::default(); +/// let mut credential_values = MakeCredentialAttributes::default(); /// credential_values.add("name", "john"); /// credential_values.add("age", "28"); /// @@ -823,7 +823,7 @@ pub fn create_w3c_credential( cred_def_private: &CredentialDefinitionPrivate, cred_offer: &CredentialOffer, cred_request: &CredentialRequest, - raw_credential_values: RawCredentialValues, + raw_credential_values: CredentialAttributes, revocation_config: Option, encoding: Option, ) -> Result { diff --git a/src/services/prover.rs b/src/services/prover.rs index 90885c41..1c31262d 100644 --- a/src/services/prover.rs +++ b/src/services/prover.rs @@ -17,7 +17,7 @@ use crate::data_types::presentation::RevealedAttributeInfo; use crate::data_types::presentation::SubProofReferent; use crate::data_types::rev_status_list::RevocationStatusList; use crate::data_types::schema::{Schema, SchemaId}; -use crate::data_types::w3c::credential::{CredentialSubject, W3CCredential}; +use crate::data_types::w3c::credential::{CredentialSubject, PredicateAttribute, W3CCredential}; use crate::data_types::w3c::one_or_many::OneOrMany; use crate::data_types::w3c::presentation::W3CPresentation; use crate::error::{Error, Result}; @@ -284,7 +284,7 @@ pub fn process_credential( /// ```rust /// use anoncreds::issuer; /// use anoncreds::prover; -/// use anoncreds::types::MakeRawCredentialValues; +/// use anoncreds::types::MakeCredentialAttributes; /// /// use anoncreds::types::CredentialDefinitionConfig; /// use anoncreds::types::SignatureType; @@ -330,7 +330,7 @@ pub fn process_credential( /// &credential_offer, /// ).expect("Unable to create credential request"); /// -/// let mut credential_values = MakeRawCredentialValues::default(); +/// let mut credential_values = MakeCredentialAttributes::default(); /// credential_values.add("name", "john"); /// credential_values.add("age", "28"); /// @@ -1146,15 +1146,12 @@ fn build_credential_subject<'p>( .cred .credential_subject .attributes - .0 - .get(name) - .ok_or_else(|| err_msg!("attribute {} not found credential", name))?; + .get_attribute(name)?; if *reveal { credential_subject .attributes - .0 - .insert(name.to_string(), value.to_string()); + .add_attribute(name.to_string(), value.to_owned()); } } if let Some(ref names) = requested_attribute.names { @@ -1163,13 +1160,10 @@ fn build_credential_subject<'p>( .cred .credential_subject .attributes - .0 - .get(name) - .ok_or_else(|| err_msg!("attribute {} not found credential", name))?; + .get_attribute(name)?; credential_subject .attributes - .0 - .insert(name.to_string(), value.to_string()); + .add_attribute(name.to_string(), value.to_owned()); } } } @@ -1180,10 +1174,9 @@ fn build_credential_subject<'p>( .get(referent) .unwrap() .clone(); - credential_subject.attributes.0.insert( - predicate_info.name.to_string(), - format!("{} {}", predicate_info.p_type, predicate_info.p_value), - ); + let name = predicate_info.name.to_owned(); + let attribute = PredicateAttribute::from(predicate_info); + credential_subject.attributes.add_predicate(name, attribute); } Ok(credential_subject) diff --git a/src/services/types.rs b/src/services/types.rs index 6f674c67..5ae226d1 100644 --- a/src/services/types.rs +++ b/src/services/types.rs @@ -1,5 +1,5 @@ use crate::cl::{RevocationRegistry as CryptoRevocationRegistry, Witness}; -use crate::data_types::credential::RawCredentialValues; +use crate::data_types::w3c::credential::CredentialAttributes; pub use crate::data_types::{ cred_def::{CredentialDefinitionPrivate, CredentialKeyCorrectnessProof, SignatureType}, cred_offer::CredentialOffer, @@ -21,6 +21,7 @@ use crate::{ invalid, utils::validation::Validatable, }; +use serde_json::Value; use std::collections::HashSet; #[derive(Debug, Clone, Serialize, Deserialize, Default)] @@ -77,16 +78,16 @@ impl From for CredentialValues { } #[derive(Debug, Default)] -pub struct MakeRawCredentialValues(pub(crate) RawCredentialValues); +pub struct MakeCredentialAttributes(pub(crate) CredentialAttributes); -impl MakeRawCredentialValues { +impl MakeCredentialAttributes { pub fn add(&mut self, name: impl Into, raw: impl Into) { - self.0 .0.insert(name.into(), raw.into()); + self.0 .0.insert(name.into(), Value::String(raw.into())); } } -impl From for RawCredentialValues { - fn from(m: MakeRawCredentialValues) -> Self { +impl From for CredentialAttributes { + fn from(m: MakeCredentialAttributes) -> Self { m.0 } } diff --git a/src/utils/datetime.rs b/src/utils/datetime.rs deleted file mode 100644 index 16aa9fcc..00000000 --- a/src/utils/datetime.rs +++ /dev/null @@ -1,10 +0,0 @@ -use chrono::{DateTime, Timelike, Utc}; - -pub fn today() -> DateTime { - Utc::now() - .with_hour(0) - .and_then(|issuance_date| issuance_date.with_minute(0)) - .and_then(|issuance_date| issuance_date.with_second(0)) - .and_then(|issuance_date| issuance_date.with_nanosecond(0)) - .unwrap_or_default() -} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e18e8204..a9bbe7af 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -9,7 +9,5 @@ pub mod hash; pub mod query; -pub mod datetime; - #[macro_use] pub mod macros; diff --git a/tests/anoncreds_demos.rs b/tests/anoncreds_demos.rs index 0923815c..32d35700 100644 --- a/tests/anoncreds_demos.rs +++ b/tests/anoncreds_demos.rs @@ -2884,8 +2884,8 @@ fn anoncreds_demo_works_for_issue_legacy_credential_convert_into_w3c_and_present .expect("Error processing credential"); // Convert legacy credential into W3C form - let w3c_cred = - credential_to_w3c(&legacy_cred).expect("Error converting legacy credential into W3C form"); + let w3c_cred = credential_to_w3c(&legacy_cred, &gvt_cred_def) + .expect("Error converting legacy credential into W3C form"); // Store both credential forms in wallet prover_wallet.credentials.push(legacy_cred); @@ -2947,12 +2947,13 @@ fn anoncreds_demo_works_for_issue_legacy_credential_convert_into_w3c_and_present ); assert_eq!( - "GE 18", + json!({ "type": "AnonCredsPredicate", "p_type": ">=", "p_value": 18 }), presentation.verifiable_credential[0] .credential_subject .attributes .0 .get("age") + .cloned() .unwrap() ); @@ -3086,12 +3087,13 @@ fn anoncreds_demo_works_for_issue_w3c_credential_and_present_w3c_presentation() ); assert_eq!( - "GE 18", + json!({ "type": "AnonCredsPredicate", "p_type": ">=", "p_value": 18 }), presentation.verifiable_credential[0] .credential_subject .attributes .0 .get("age") + .cloned() .unwrap() ); @@ -3313,7 +3315,7 @@ fn anoncreds_demo_works_for_issue_two_credentials_in_different_forms_and_present .expect("Error processing credential"); // Convert legacy GVT credential into W3C form - let w3c_gvt_cred = credential_to_w3c(&legacy_gvt_cred) + let w3c_gvt_cred = credential_to_w3c(&legacy_gvt_cred, &gvt_cred_def) .expect("Error converting w3c credential into legacy form"); // Store both credential forms in wallet @@ -3621,12 +3623,13 @@ fn anoncreds_demo_works_for_issue_w3c_credential_add_identity_proof_present_w3c_ ); assert_eq!( - "GE 18", + json!({ "type": "AnonCredsPredicate", "p_type": ">=", "p_value": 18 }), presentation.verifiable_credential[0] .credential_subject .attributes .0 .get("age") + .cloned() .unwrap() ); diff --git a/tests/utils/fixtures.rs b/tests/utils/fixtures.rs index da9dc229..fb2ad667 100644 --- a/tests/utils/fixtures.rs +++ b/tests/utils/fixtures.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use anoncreds::types::MakeRawCredentialValues; +use anoncreds::types::MakeCredentialAttributes; use anoncreds::{ data_types::{ cred_def::{CredentialDefinition, CredentialDefinitionId}, @@ -262,10 +262,10 @@ pub fn credential_values(name: &str) -> MakeCredentialValues { } } -pub fn raw_credential_values(name: &str) -> MakeRawCredentialValues { +pub fn raw_credential_values(name: &str) -> MakeCredentialAttributes { match name { "GVT" => { - let mut gvt_cred_values = MakeRawCredentialValues::default(); + let mut gvt_cred_values = MakeCredentialAttributes::default(); gvt_cred_values.add("sex", "male"); gvt_cred_values.add("name", "Alex"); gvt_cred_values.add("height", "175"); @@ -273,7 +273,7 @@ pub fn raw_credential_values(name: &str) -> MakeRawCredentialValues { gvt_cred_values } "EMP" => { - let mut emp_cred_values = MakeRawCredentialValues::default(); + let mut emp_cred_values = MakeCredentialAttributes::default(); emp_cred_values.add("name", "John"); emp_cred_values.add("role", "Developer"); emp_cred_values.add("department", "IT"); diff --git a/tests/utils/mock.rs b/tests/utils/mock.rs index a7c2b03b..1cdba746 100644 --- a/tests/utils/mock.rs +++ b/tests/utils/mock.rs @@ -9,7 +9,7 @@ use anoncreds::conversion::{credential_from_w3c, credential_to_w3c}; use anoncreds::data_types::w3c::credential::W3CCredential; use anoncreds::data_types::w3c::presentation::W3CPresentation; use anoncreds::types::{ - MakeRawCredentialValues, RevocationRegistryDefinition, RevocationStatusList, + MakeCredentialAttributes, RevocationRegistryDefinition, RevocationStatusList, }; use anoncreds::{ data_types::{ @@ -374,7 +374,7 @@ impl<'a> Mock<'a> { let cred_def = self.get_cred_def(&offer.cred_def_id); let rev_config = self.get_rev_config(issuer_wallet, rev_reg_id, rev_idx, prev_rev_reg_time); - let mut cred_values = MakeRawCredentialValues::default(); + let mut cred_values = MakeCredentialAttributes::default(); let names: Vec = schema.attr_names.clone().0.into_iter().collect(); for (i, v) in names.iter().enumerate() { if let Some(value) = values.get(&v.as_str()) { @@ -464,8 +464,8 @@ impl<'a> Mock<'a> { ) .expect("Error processing credential"); - let w3c_credential = - credential_to_w3c(&recv_cred).expect("Error converting credential to w3c"); + let w3c_credential = credential_to_w3c(&recv_cred, cred_def) + .expect("Error converting credential to w3c"); (recv_cred, w3c_credential) }