Skip to content

Commit

Permalink
Merge pull request #50 from blu3beri/verifier-get-metadata
Browse files Browse the repository at this point in the history
Added `issuerId` to the `schema` and `cred_def` anoncreds objects
  • Loading branch information
berendsliedrecht authored Jan 11, 2023
2 parents b99a7ee + 629f59e commit f7ce032
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 78 deletions.
4 changes: 3 additions & 1 deletion anoncreds/src/data_types/anoncreds/cred_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;

use crate::{data_types::ConversionError, impl_anoncreds_object_identifier};

use super::schema::SchemaId;
use super::{issuer_id::IssuerId, schema::SchemaId};

pub const CL_SIGNATURE_TYPE: &str = "CL";

Expand Down Expand Up @@ -33,12 +33,14 @@ pub struct CredentialDefinitionData {
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CredentialDefinition {
pub schema_id: SchemaId,
#[serde(rename = "type")]
pub signature_type: SignatureType,
pub tag: String,
pub value: CredentialDefinitionData,
pub issuer_id: IssuerId,
}

impl CredentialDefinition {
Expand Down
47 changes: 47 additions & 0 deletions anoncreds/src/data_types/anoncreds/issuer_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::impl_anoncreds_object_identifier;

impl_anoncreds_object_identifier!(IssuerId);

#[test]
fn should_validate_new_and_legacy_identifiers() {
let valid_uri_identifier_1 = "did:uri:new";
let valid_uri_identifier_2 = "did:indy:idunion:test:2MZYuPv2Km7Q1eD4GCsSb6";
let valid_uri_identifier_3 = "did:indy:sovrin:staging:6cgbu8ZPoWTnR5Rv5JcSMB";
let valid_uri_identifier_4 = "did:indy:sovrin:7Tqg6BwSSWapxgUDm9KKgg";
let valid_uri_identifier_5 = "did:web:example.com#controller";
let valid_uri_identifier_6 = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";

let invalid_uri_identifier = "::::";

let valid_legacy_identifier_1 = "NcYxiDXkpYi6ov5FcYDi1e";
let valid_legacy_identifier_2 = "VsKV7grR1BUE29mG2Fm2kX";

let too_short_legacy_identifier = "abc";
let too_long_legacy_identifier = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let illegal_base58_legacy_identifier_zero = "0000000000000000000000";
let illegal_base58_legacy_identifier_captial_o = "OOOOOOOOOOOOOOOOOOOOOO";
let illegal_base58_legacy_identifier_captial_i = "IIIIIIIIIIIIIIIIIIIIII";
let illegal_base58_legacy_identifier_lower_l = "llllllllllllllllllllll";

// Instantiating a new IssuerId validates it
assert!(IssuerId::new(valid_uri_identifier_1).is_ok());
assert!(IssuerId::new(valid_uri_identifier_2).is_ok());
assert!(IssuerId::new(valid_uri_identifier_3).is_ok());
assert!(IssuerId::new(valid_uri_identifier_4).is_ok());
assert!(IssuerId::new(valid_uri_identifier_5).is_ok());
assert!(IssuerId::new(valid_uri_identifier_6).is_ok());

assert!(IssuerId::new(invalid_uri_identifier).is_err());

assert!(IssuerId::new(valid_legacy_identifier_1).is_ok());
assert!(IssuerId::new(valid_legacy_identifier_2).is_ok());

assert!(IssuerId::new(too_short_legacy_identifier).is_err());
assert!(IssuerId::new(too_long_legacy_identifier).is_err());
assert!(IssuerId::new(illegal_base58_legacy_identifier_zero).is_err());
assert!(IssuerId::new(illegal_base58_legacy_identifier_captial_o).is_err());
assert!(IssuerId::new(illegal_base58_legacy_identifier_captial_i).is_err());
assert!(IssuerId::new(illegal_base58_legacy_identifier_lower_l).is_err());

assert!(true);
}
33 changes: 19 additions & 14 deletions anoncreds/src/data_types/anoncreds/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@ macro_rules! impl_anoncreds_object_identifier {

pub fn new(s: impl Into<String>) -> Result<Self, $crate::data_types::ValidationError> {
let s = Self(s.into());
s.validate()?;
$crate::data_types::Validatable::validate(&s)?;
Ok(s)
}
}

impl $crate::data_types::Validatable for $i {
fn validate(&self) -> Result<(), $crate::data_types::ValidationError> {
// TODO: stricten the URI regex.
// Right now everything after the first colon is allowed, we might want to restrict
// this
let uri_regex = regex::Regex::new(r"^[a-zA-Z0-9\+\-\.]+:.+$").unwrap();
uri_regex
if $crate::utils::validation::URI_IDENTIFIER
.captures(&self.0)
.ok_or_else(|| {
indy_utils::invalid!(
"type: {}, identifier: {} is invalid. It MUST be a URI.",
stringify!($i),
self.0
)
})
.map(|_| ())
.is_some()
{
return Ok(());
}

if $crate::utils::validation::LEGACY_IDENTIFIER
.captures(&self.0)
.is_some()
{
return Ok(());
}

Err(indy_utils::invalid!(
"type: {}, identifier: {} is invalid. It MUST be a URI or legacy identifier.",
stringify!($i),
self.0
))
}
}

Expand Down
3 changes: 3 additions & 0 deletions anoncreds/src/data_types/anoncreds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ pub mod schema;

/// Macros for the data types
pub mod macros;

/// Identifier wrapper for the issuer
pub mod issuer_id;
1 change: 0 additions & 1 deletion anoncreds/src/data_types/anoncreds/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ mod tests {
"1a",
];
for v in invalid.iter() {
println!("try {}", v);
assert!(Nonce::try_from(*v).is_err())
}
}
Expand Down
21 changes: 6 additions & 15 deletions anoncreds/src/data_types/anoncreds/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::impl_anoncreds_object_identifier;
use std::collections::HashSet;
use std::iter::FromIterator;

use super::issuer_id::IssuerId;

pub const MAX_ATTRIBUTES_COUNT: usize = 125;

impl_anoncreds_object_identifier!(SchemaId);
Expand All @@ -13,8 +15,8 @@ impl_anoncreds_object_identifier!(SchemaId);
pub struct Schema {
pub name: String,
pub version: String,
#[serde(rename = "attrNames")]
pub attr_names: AttributeNames,
pub issuer_id: IssuerId,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -83,6 +85,7 @@ mod test_schema_validation {
"name": "gvt",
"version": "1.0",
"attrNames": ["aaa", "bbb", "ccc"],
"issuerId": "bob"
})
.to_string();

Expand All @@ -92,26 +95,14 @@ mod test_schema_validation {
}

#[test]
fn test_invalid_name_schema() {
fn test_invalid_schema() {
let schema_json = json!({
"name": "gvt1",
"version": "1.0",
"attrNames": ["aaa", "bbb", "ccc"],
})
.to_string();

serde_json::from_str::<Schema>(&schema_json).unwrap();
}

#[test]
fn test_invalid_version_schema() {
let schema_json = json!({
"name": "gvt",
"version": "1.1",
"attrNames": ["aaa", "bbb", "ccc"],
})
.to_string();

serde_json::from_str::<Schema>(&schema_json).unwrap();
assert!(serde_json::from_str::<Schema>(&schema_json).is_err());
}
}
5 changes: 5 additions & 0 deletions anoncreds/src/ffi/cred_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub extern "C" fn anoncreds_create_credential_definition(
schema_id: FfiStr,
schema: ObjectHandle,
tag: FfiStr,
issuer_id: FfiStr,
signature_type: FfiStr,
support_revocation: i8,
cred_def_p: *mut ObjectHandle,
Expand All @@ -38,9 +39,13 @@ pub extern "C" fn anoncreds_create_credential_definition(
.ok_or_else(|| err_msg!("Missing signature type"))?;
SignatureType::from_str(stype).map_err(err_map!(Input))?
};
let issuer_id = issuer_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing issuer id"))?;
let (cred_def, cred_def_pvt, key_proof) = create_credential_definition(
schema_id,
schema.load()?.cast_ref()?,
issuer_id,
tag,
signature_type,
CredentialDefinitionConfig {
Expand Down
5 changes: 5 additions & 0 deletions anoncreds/src/ffi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::services::issuer::create_schema;
pub extern "C" fn anoncreds_create_schema(
schema_name: FfiStr,
schema_version: FfiStr,
issuer_id: FfiStr,
attr_names: FfiStrList,
result_p: *mut ObjectHandle,
) -> ErrorCode {
Expand All @@ -21,9 +22,13 @@ pub extern "C" fn anoncreds_create_schema(
let schema_version = schema_version
.as_opt_str()
.ok_or_else(|| err_msg!("Missing schema version"))?;
let issuer_id = issuer_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing issuer_id version"))?;
let schema = create_schema(
schema_name,
schema_version,
issuer_id,
attr_names.to_string_vec()?.into(),
)?;
let handle = ObjectHandle::create(schema)?;
Expand Down
2 changes: 2 additions & 0 deletions anoncreds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub use self::error::{Error, ErrorKind};
mod services;
pub use services::*;

mod utils;

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

Expand Down
22 changes: 17 additions & 5 deletions anoncreds/src/services/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use indy_utils::ValidationError;

use super::types::*;
use crate::data_types::anoncreds::cred_def::CredentialDefinitionId;
use crate::data_types::anoncreds::issuer_id::IssuerId;
use crate::data_types::anoncreds::rev_reg::RevocationRegistryId;
use crate::data_types::anoncreds::schema::SchemaId;
use crate::data_types::anoncreds::{
Expand All @@ -26,29 +27,37 @@ use crate::ursa::cl::{

use super::tails::{TailsFileReader, TailsReader, TailsWriter};

pub fn create_schema(
pub fn create_schema<II>(
schema_name: &str,
schema_version: &str,
issuer_id: II,
attr_names: AttributeNames,
) -> Result<Schema> {
) -> Result<Schema>
where
II: TryInto<IssuerId, Error = ValidationError>,
{
trace!(
"create_schema >>> schema_name: {:?}, schema_version: {:?}, attr_names: {:?}",
"create_schema >>> schema_name: {}, schema_version: {}, attr_names: {:?}",
schema_name,
schema_version,
attr_names
attr_names,
);

let issuer_id = issuer_id.try_into()?;

let schema = Schema {
name: schema_name.to_string(),
version: schema_version.to_string(),
issuer_id,
attr_names,
};
Ok(schema)
}

pub fn create_credential_definition<SI>(
pub fn create_credential_definition<SI, II>(
schema_id: SI,
schema: &Schema,
issuer_id: II,
tag: &str,
signature_type: SignatureType,
config: CredentialDefinitionConfig,
Expand All @@ -59,12 +68,14 @@ pub fn create_credential_definition<SI>(
)>
where
SI: TryInto<SchemaId, Error = ValidationError>,
II: TryInto<IssuerId, Error = ValidationError>,
{
trace!(
"create_credential_definition >>> schema: {:?}, config: {:?}",
schema,
config
);
let issuer_id = issuer_id.try_into()?;
let schema_id = schema_id.try_into()?;

let credential_schema = build_credential_schema(&schema.attr_names.0)?;
Expand All @@ -80,6 +91,7 @@ where
let cred_def = CredentialDefinition {
schema_id,
signature_type,
issuer_id,
tag: tag.to_owned(),
value: CredentialDefinitionData {
primary: credential_public_key.get_primary_key()?.try_clone()?,
Expand Down
Loading

0 comments on commit f7ce032

Please sign in to comment.