Skip to content

Commit

Permalink
Move w3c modules under the feature
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Nov 22, 2023
1 parent cdebb35 commit 410ef34
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 53 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 2 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ mod pres_req;
mod presentation;
mod revocation;
mod schema;

#[cfg(feature = "w3c")]
mod w3c;

#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/w3c/cred_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/w3c/cred_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/ffi/w3c/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 0 additions & 19 deletions src/services/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
))
}
}
2 changes: 2 additions & 0 deletions src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ pub mod prover;
pub mod tails;
pub mod types;
pub mod verifier;

#[cfg(feature = "w3c")]
pub mod w3c;
18 changes: 0 additions & 18 deletions src/services/types.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -76,23 +75,6 @@ impl From<MakeCredentialValues> for CredentialValues {
}
}

#[derive(Debug, Default)]
pub struct MakeCredentialAttributes(pub(crate) CredentialAttributes);

impl MakeCredentialAttributes {
pub fn add(&mut self, name: impl Into<String>, raw: impl Into<String>) {
self.0
.0
.insert(name.into(), CredentialAttributeValue::Attribute(raw.into()));
}
}

impl From<MakeCredentialAttributes> for CredentialAttributes {
fn from(m: MakeCredentialAttributes) -> Self {
m.0
}
}

#[derive(Debug)]
pub struct PresentCredentials<'p, T>(pub(crate) Vec<PresentCredential<'p, T>>);

Expand Down
3 changes: 2 additions & 1 deletion src/services/w3c/credential_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/services/w3c/helpers.rs
Original file line number Diff line number Diff line change
@@ -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
))
}
}
2 changes: 1 addition & 1 deletion src/services/w3c/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/services/w3c/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod credential_conversion;
pub mod helpers;
pub mod issuer;
pub mod prover;
pub mod types;
pub mod verifier;
2 changes: 1 addition & 1 deletion src/services/w3c/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/services/w3c/types.rs
Original file line number Diff line number Diff line change
@@ -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<String>, raw: impl Into<String>) {
self.0
.0
.insert(name.into(), CredentialAttributeValue::Attribute(raw.into()));
}
}

impl From<MakeCredentialAttributes> for CredentialAttributes {
fn from(m: MakeCredentialAttributes) -> Self {
m.0
}
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/anoncreds_demos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/fixtures.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
5 changes: 2 additions & 3 deletions tests/utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 410ef34

Please sign in to comment.