From 410ef34ab13ee20898d8ea6010aed7530c6369df Mon Sep 17 00:00:00 2001 From: "artem.ivanov" Date: Wed, 22 Nov 2023 10:24:05 +0300 Subject: [PATCH] Move w3c modules under the feature Signed-off-by: artem.ivanov --- Cargo.toml | 7 ++++--- src/data_types/mod.rs | 1 + src/ffi/mod.rs | 2 ++ src/ffi/w3c/cred_offer.rs | 2 +- src/ffi/w3c/cred_req.rs | 2 +- src/ffi/w3c/credential.rs | 7 ++++--- src/services/helpers.rs | 19 ------------------- src/services/mod.rs | 2 ++ src/services/types.rs | 18 ------------------ src/services/w3c/credential_conversion.rs | 3 ++- src/services/w3c/helpers.rs | 21 +++++++++++++++++++++ src/services/w3c/issuer.rs | 2 +- src/services/w3c/mod.rs | 2 ++ src/services/w3c/prover.rs | 2 +- src/services/w3c/types.rs | 18 ++++++++++++++++++ src/utils/mod.rs | 2 ++ tests/anoncreds_demos.rs | 3 ++- tests/utils/fixtures.rs | 2 +- tests/utils/mock.rs | 5 ++--- 19 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 src/services/w3c/helpers.rs create mode 100644 src/services/w3c/types.rs diff --git a/Cargo.toml b/Cargo.toml index cf7cb7a0..1332e8ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,11 +19,12 @@ path = "src/lib.rs" crate-type = ["staticlib", "rlib", "cdylib"] [features] -default = ["ffi", "logger", "zeroize"] +default = ["ffi", "logger", "zeroize", "w3c"] ffi = ["dep:ffi-support"] zeroize = ["dep:zeroize"] logger = ["dep:env_logger"] vendored = ["anoncreds-clsignatures/openssl_vendored"] +w3c = ["base64", "chrono"] [dependencies] anoncreds-clsignatures = "0.2.4" @@ -40,8 +41,8 @@ serde_json = { version = "1.0.94", features = ["raw_value"] } sha2 = "0.10.6" thiserror = "1.0.39" zeroize = { version = "1.5.7", optional = true, features = ["zeroize_derive"] } -base64 = "0.21.5" -chrono = { version = "0.4.31", features = ["serde"] } +base64 = { version = "0.21.5", optional = true } +chrono = { version = "0.4.31", optional = true, features = ["serde"] } [profile.release] lto = true diff --git a/src/data_types/mod.rs b/src/data_types/mod.rs index c5379f03..7a62e52c 100644 --- a/src/data_types/mod.rs +++ b/src/data_types/mod.rs @@ -40,5 +40,6 @@ pub mod macros; /// Identifier wrapper for the issuer pub mod issuer_id; +#[cfg(feature = "w3c")] /// W3C Credential standard definitions pub mod w3c; diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index de41c007..2f400375 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -34,6 +34,8 @@ mod pres_req; mod presentation; mod revocation; mod schema; + +#[cfg(feature = "w3c")] mod w3c; #[no_mangle] diff --git a/src/ffi/w3c/cred_offer.rs b/src/ffi/w3c/cred_offer.rs index e632320a..568fbd3c 100644 --- a/src/ffi/w3c/cred_offer.rs +++ b/src/ffi/w3c/cred_offer.rs @@ -15,7 +15,7 @@ use ffi_support::FfiStr; /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_create_credential_offer( +pub extern "C" fn anoncreds_create_w3c_credential_offer( schema_id: FfiStr, cred_def_id: FfiStr, key_proof: ObjectHandle, diff --git a/src/ffi/w3c/cred_req.rs b/src/ffi/w3c/cred_req.rs index 33385249..ba958e33 100644 --- a/src/ffi/w3c/cred_req.rs +++ b/src/ffi/w3c/cred_req.rs @@ -20,7 +20,7 @@ use crate::ffi::object::ObjectHandle; /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_create_credential_request( +pub extern "C" fn anoncreds_create_w3c_credential_request( entropy: FfiStr, prover_did: FfiStr, cred_def: ObjectHandle, diff --git a/src/ffi/w3c/credential.rs b/src/ffi/w3c/credential.rs index f236428f..76b994f0 100644 --- a/src/ffi/w3c/credential.rs +++ b/src/ffi/w3c/credential.rs @@ -13,10 +13,11 @@ use crate::ffi::credential::{FfiCredRevInfo, _link_secret, _revocation_config}; use crate::ffi::error::{catch_error, ErrorCode}; use crate::ffi::object::{AnoncredsObject, ObjectHandle}; use crate::ffi::util::FfiStrList; -use crate::types::{Credential, MakeCredentialAttributes}; +use crate::types::Credential; use crate::w3c::credential_conversion::{credential_from_w3c, credential_to_w3c}; use crate::w3c::issuer::create_credential; use crate::w3c::prover::process_credential; +use crate::w3c::types::MakeCredentialAttributes; impl_anoncreds_object!(W3CCredential, "W3CCredential"); impl_anoncreds_object_from_json!(W3CCredential, anoncreds_w3c_credential_from_json); @@ -220,7 +221,7 @@ pub extern "C" fn anoncreds_w3c_credential_add_non_anoncreds_integrity_proof( /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_set_credential_id( +pub extern "C" fn anoncreds_w3c_credential_set_id( cred: ObjectHandle, id: FfiStr, cred_p: *mut ObjectHandle, @@ -251,7 +252,7 @@ pub extern "C" fn anoncreds_w3c_set_credential_id( /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_credential_w3c_subject_id( +pub extern "C" fn anoncreds_w3c_credential_subject_id( cred: ObjectHandle, id: FfiStr, cred_p: *mut ObjectHandle, diff --git a/src/services/helpers.rs b/src/services/helpers.rs index 7a171fad..244a729c 100644 --- a/src/services/helpers.rs +++ b/src/services/helpers.rs @@ -5,7 +5,6 @@ use crate::cl::{ use crate::data_types::presentation::RequestedProof; use crate::data_types::rev_reg_def::RevocationRegistryDefinitionId; use crate::data_types::schema::Schema; -use crate::data_types::w3c::credential::{CredentialAttributeValue, W3CCredential}; use crate::data_types::{ credential::CredentialValues, link_secret::LinkSecret, @@ -278,21 +277,3 @@ impl RequestedProof { referents } } - -impl W3CCredential { - pub(crate) fn get_attribute( - &self, - requested_attribute: &str, - ) -> Result<(String, CredentialAttributeValue)> { - let requested_attribute = attr_common_view(requested_attribute); - for (attribute, value) in self.credential_subject.attributes.0.iter() { - if attr_common_view(attribute) == requested_attribute { - return Ok((attribute.to_owned(), value.to_owned())); - } - } - Err(err_msg!( - "Credential attribute {} not found", - requested_attribute - )) - } -} diff --git a/src/services/mod.rs b/src/services/mod.rs index 683c98c8..36a8d079 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -4,4 +4,6 @@ pub mod prover; pub mod tails; pub mod types; pub mod verifier; + +#[cfg(feature = "w3c")] pub mod w3c; diff --git a/src/services/types.rs b/src/services/types.rs index b844e497..53148c26 100644 --- a/src/services/types.rs +++ b/src/services/types.rs @@ -1,5 +1,4 @@ use crate::cl::{RevocationRegistry as CryptoRevocationRegistry, Witness}; -use crate::data_types::w3c::credential::{CredentialAttributeValue, CredentialAttributes}; pub use crate::data_types::{ cred_def::{CredentialDefinitionPrivate, CredentialKeyCorrectnessProof, SignatureType}, cred_offer::CredentialOffer, @@ -76,23 +75,6 @@ impl From for CredentialValues { } } -#[derive(Debug, Default)] -pub struct MakeCredentialAttributes(pub(crate) CredentialAttributes); - -impl MakeCredentialAttributes { - pub fn add(&mut self, name: impl Into, raw: impl Into) { - self.0 - .0 - .insert(name.into(), CredentialAttributeValue::Attribute(raw.into())); - } -} - -impl From for CredentialAttributes { - fn from(m: MakeCredentialAttributes) -> Self { - m.0 - } -} - #[derive(Debug)] pub struct PresentCredentials<'p, T>(pub(crate) Vec>); diff --git a/src/services/w3c/credential_conversion.rs b/src/services/w3c/credential_conversion.rs index 2805d47a..d47319ca 100644 --- a/src/services/w3c/credential_conversion.rs +++ b/src/services/w3c/credential_conversion.rs @@ -140,7 +140,8 @@ pub fn credential_to_w3c( /// use anoncreds::w3c; /// use anoncreds::issuer; /// use anoncreds::prover; -/// use anoncreds::types::{MakeCredentialAttributes, MakeCredentialValues}; +/// use anoncreds::w3c::types::MakeCredentialAttributes; +/// use anoncreds::types::MakeCredentialValues; /// /// use anoncreds::types::CredentialDefinitionConfig; /// use anoncreds::types::SignatureType; diff --git a/src/services/w3c/helpers.rs b/src/services/w3c/helpers.rs new file mode 100644 index 00000000..8983f22b --- /dev/null +++ b/src/services/w3c/helpers.rs @@ -0,0 +1,21 @@ +use crate::data_types::w3c::credential::{CredentialAttributeValue, W3CCredential}; +use crate::error::Result; +use crate::helpers::attr_common_view; + +impl W3CCredential { + pub(crate) fn get_attribute( + &self, + requested_attribute: &str, + ) -> Result<(String, CredentialAttributeValue)> { + let requested_attribute = attr_common_view(requested_attribute); + for (attribute, value) in self.credential_subject.attributes.0.iter() { + if attr_common_view(attribute) == requested_attribute { + return Ok((attribute.to_owned(), value.to_owned())); + } + } + Err(err_msg!( + "Credential attribute {} not found", + requested_attribute + )) + } +} diff --git a/src/services/w3c/issuer.rs b/src/services/w3c/issuer.rs index e00a644c..8d657b79 100644 --- a/src/services/w3c/issuer.rs +++ b/src/services/w3c/issuer.rs @@ -23,7 +23,7 @@ use crate::types::{ /// ```rust /// use anoncreds::{issuer, w3c}; /// use anoncreds::prover; -/// use anoncreds::types::MakeCredentialAttributes; +/// use anoncreds::w3c::types::MakeCredentialAttributes; /// /// use anoncreds::types::CredentialDefinitionConfig; /// use anoncreds::types::SignatureType; diff --git a/src/services/w3c/mod.rs b/src/services/w3c/mod.rs index 0d270088..17dac619 100644 --- a/src/services/w3c/mod.rs +++ b/src/services/w3c/mod.rs @@ -1,4 +1,6 @@ pub mod credential_conversion; +pub mod helpers; pub mod issuer; pub mod prover; +pub mod types; pub mod verifier; diff --git a/src/services/w3c/prover.rs b/src/services/w3c/prover.rs index 4624d76c..e2aea601 100644 --- a/src/services/w3c/prover.rs +++ b/src/services/w3c/prover.rs @@ -27,7 +27,7 @@ use std::collections::HashMap; /// ```rust /// use anoncreds::{issuer, w3c}; /// use anoncreds::prover; -/// use anoncreds::types::MakeCredentialAttributes; +/// use anoncreds::w3c::types::MakeCredentialAttributes; /// /// use anoncreds::types::CredentialDefinitionConfig; /// use anoncreds::types::SignatureType; diff --git a/src/services/w3c/types.rs b/src/services/w3c/types.rs new file mode 100644 index 00000000..365b106b --- /dev/null +++ b/src/services/w3c/types.rs @@ -0,0 +1,18 @@ +use crate::data_types::w3c::credential::{CredentialAttributeValue, CredentialAttributes}; + +#[derive(Debug, Default)] +pub struct MakeCredentialAttributes(pub(crate) CredentialAttributes); + +impl MakeCredentialAttributes { + pub fn add(&mut self, name: impl Into, raw: impl Into) { + self.0 + .0 + .insert(name.into(), CredentialAttributeValue::Attribute(raw.into())); + } +} + +impl From for CredentialAttributes { + fn from(m: MakeCredentialAttributes) -> Self { + m.0 + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d9be1bda..f64bcf45 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,8 +3,10 @@ pub mod validation; pub mod base58; +#[cfg(feature = "w3c")] pub mod base64; +#[cfg(feature = "w3c")] pub mod encoded_object; pub mod hash; diff --git a/tests/anoncreds_demos.rs b/tests/anoncreds_demos.rs index 279adcce..4eca69c6 100644 --- a/tests/anoncreds_demos.rs +++ b/tests/anoncreds_demos.rs @@ -8,9 +8,10 @@ use anoncreds::data_types::w3c::presentation_proof::{PredicateAttribute, Predica use anoncreds::data_types::w3c::uri::URI; use anoncreds::prover; use anoncreds::tails::TailsFileWriter; -use anoncreds::types::{CredentialRevocationConfig, MakeCredentialAttributes, PresentCredentials}; +use anoncreds::types::{CredentialRevocationConfig, PresentCredentials}; use anoncreds::verifier; use anoncreds::w3c::credential_conversion::{credential_from_w3c, credential_to_w3c}; +use anoncreds::w3c::types::MakeCredentialAttributes; use anoncreds::{issuer, w3c}; use serde_json::json; use std::collections::{BTreeSet, HashMap}; diff --git a/tests/utils/fixtures.rs b/tests/utils/fixtures.rs index fb2ad667..cdd55514 100644 --- a/tests/utils/fixtures.rs +++ b/tests/utils/fixtures.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use anoncreds::types::MakeCredentialAttributes; +use anoncreds::w3c::types::MakeCredentialAttributes; use anoncreds::{ data_types::{ cred_def::{CredentialDefinition, CredentialDefinitionId}, diff --git a/tests/utils/mock.rs b/tests/utils/mock.rs index d419d896..481d92e2 100644 --- a/tests/utils/mock.rs +++ b/tests/utils/mock.rs @@ -7,10 +7,9 @@ use std::{ use anoncreds::data_types::w3c::credential::W3CCredential; use anoncreds::data_types::w3c::presentation::W3CPresentation; -use anoncreds::types::{ - MakeCredentialAttributes, RevocationRegistryDefinition, RevocationStatusList, -}; +use anoncreds::types::{RevocationRegistryDefinition, RevocationStatusList}; use anoncreds::w3c::credential_conversion::{credential_from_w3c, credential_to_w3c}; +use anoncreds::w3c::types::MakeCredentialAttributes; use anoncreds::{ data_types::{ cred_def::{CredentialDefinition, CredentialDefinitionId},