Skip to content

Commit

Permalink
Merge pull request #228 from bobozaur/refactor/aries_improvements
Browse files Browse the repository at this point in the history
refactor: services function signatures improvements
  • Loading branch information
swcurran authored Aug 11, 2023
2 parents 1eb7aa2 + 764a3df commit 79d6097
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 278 deletions.
10 changes: 5 additions & 5 deletions src/data_types/cred_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod test_cred_def {
issuer::create_schema(
"name",
"1.0",
"did:example",
"did:example".try_into().unwrap(),
vec!["name".to_owned(), "age".to_owned()].into(),
)
.expect("Unable to create Schema")
Expand All @@ -110,9 +110,9 @@ mod test_cred_def {
) {
let schema = schema();
issuer::create_credential_definition(
"did:example/schema",
"did:example/schema".try_into().unwrap(),
&schema,
"did:exampple",
"did:exampple".try_into().unwrap(),
"default-tag",
SignatureType::CL,
CredentialDefinitionConfig::default(),
Expand All @@ -124,9 +124,9 @@ mod test_cred_def {
fn should_create_credential_definition() {
let schema = schema();
let result = issuer::create_credential_definition(
"did:example/schema",
"did:example/schema".try_into().unwrap(),
&schema,
"did:exampple",
"did:exampple".try_into().unwrap(),
"default-tag",
SignatureType::CL,
CredentialDefinitionConfig::default(),
Expand Down
20 changes: 13 additions & 7 deletions src/data_types/cred_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ mod cred_req_tests {
const LINK_SECRET_ID: &str = "link:secret:id";

fn cred_def() -> Result<(CredentialDefinition, CredentialKeyCorrectnessProof)> {
let credential_definition_issuer_id = "sample:id";

let issuer_id = "sample:uri".try_into()?;
let schema_id = "schema:id".try_into()?;
let credential_definition_issuer_id = "sample:id".try_into()?;
let attr_names = AttributeNames::from(vec!["name".to_owned(), "age".to_owned()]);
let schema = create_schema("schema:name", "1.0", "sample:uri", attr_names)?;

let schema = create_schema("schema:name", "1.0", issuer_id, attr_names)?;
let cred_def = create_credential_definition(
"schema:id",
schema_id,
&schema,
credential_definition_issuer_id,
"default",
Expand All @@ -150,12 +152,16 @@ mod cred_req_tests {
) -> Result<CredentialOffer> {
if is_legacy {
create_credential_offer(
LEGACY_SCHEMA_IDENTIFIER,
LEGACY_CRED_DEF_IDENTIFIER,
LEGACY_SCHEMA_IDENTIFIER.try_into()?,
LEGACY_CRED_DEF_IDENTIFIER.try_into()?,
&correctness_proof,
)
} else {
create_credential_offer(NEW_IDENTIFIER, NEW_IDENTIFIER, &correctness_proof)
create_credential_offer(
NEW_IDENTIFIER.try_into()?,
NEW_IDENTIFIER.try_into()?,
&correctness_proof,
)
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/data_types/rev_status_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ impl TryFrom<&RevocationStatusList> for Option<RevocationRegistry> {
type Error = Error;

fn try_from(value: &RevocationStatusList) -> std::result::Result<Self, Self::Error> {
let value = match value.registry {
Some(registry) => Some(RevocationRegistry {
value: registry.into(),
}),
None => None,
};
let value = value.registry.map(|registry| RevocationRegistry {
value: registry.into(),
});

Ok(value)
}
Expand Down Expand Up @@ -258,7 +255,7 @@ mod rev_reg_tests {

list.update(None, Some(BTreeSet::from([0u32])), None, Some(1245))
.unwrap();
assert_eq!(list.get(0usize).unwrap(), false);
assert!(!list.get(0usize).unwrap());
assert_eq!(list.timestamp().unwrap(), 1245);
}
}
7 changes: 2 additions & 5 deletions src/data_types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Schema {
pub issuer_id: IssuerId,
}

// QUESTION: If these must be unique, why not directly store them as a set?
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AttributeNames(pub Vec<String>);

Expand Down Expand Up @@ -55,11 +56,7 @@ impl Validatable for Schema {
impl Validatable for AttributeNames {
fn validate(&self) -> Result<(), ValidationError> {
let mut unique = HashSet::new();
let is_unique = self
.0
.clone()
.into_iter()
.all(move |name| unique.insert(name));
let is_unique = self.0.iter().all(move |name| unique.insert(name));

if !is_unique {
return Err("Attributes inside the schema must be unique".into());
Expand Down
7 changes: 5 additions & 2 deletions src/ffi/cred_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ pub extern "C" fn anoncreds_create_credential_definition(
let tag = tag.as_opt_str().ok_or_else(|| err_msg!("Missing tag"))?;
let schema_id = schema_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing schema id"))?;
.ok_or_else(|| err_msg!("Missing schema id"))?
.try_into()?;
let signature_type = {
let stype = signature_type
.as_opt_str()
Expand All @@ -41,7 +42,9 @@ pub extern "C" fn anoncreds_create_credential_definition(
};
let issuer_id = issuer_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing issuer id"))?;
.ok_or_else(|| err_msg!("Missing issuer id"))?
.try_into()?;

let (cred_def, cred_def_pvt, key_proof) = create_credential_definition(
schema_id,
schema.load()?.cast_ref()?,
Expand Down
6 changes: 4 additions & 2 deletions src/ffi/cred_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ pub extern "C" fn anoncreds_create_credential_offer(
check_useful_c_ptr!(cred_offer_p);
let schema_id = schema_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing schema ID"))?;
.ok_or_else(|| err_msg!("Missing schema ID"))?
.try_into()?;
let cred_def_id = cred_def_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing cred def ID"))?;
.ok_or_else(|| err_msg!("Missing cred def ID"))?
.try_into()?;
let cred_offer =
create_credential_offer(schema_id, cred_def_id, key_proof.load()?.cast_ref()?)?;
let cred_offer = ObjectHandle::create(cred_offer)?;
Expand Down
19 changes: 7 additions & 12 deletions src/ffi/revocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub extern "C" fn anoncreds_create_revocation_status_list(
rev_reg_def_id: FfiStr,
rev_reg_def: ObjectHandle,
reg_rev_priv: ObjectHandle,
issuer_id: FfiStr,
_issuer_id: FfiStr, // leaving it here not to break existing code
issuance_by_default: i8,
timestamp: i64,
rev_status_list_p: *mut ObjectHandle,
Expand All @@ -33,10 +33,9 @@ pub extern "C" fn anoncreds_create_revocation_status_list(
check_useful_c_ptr!(rev_status_list_p);
let rev_reg_def_id = rev_reg_def_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing rev_reg_def_id"))?;
let issuer_id = issuer_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing issuer_id"))?;
.ok_or_else(|| err_msg!("Missing rev_reg_def_id"))?
.try_into()?;

let timestamp = if timestamp <= 0 {
None
} else {
Expand All @@ -48,7 +47,6 @@ pub extern "C" fn anoncreds_create_revocation_status_list(
rev_reg_def_id,
rev_reg_def.load()?.cast_ref()?,
reg_rev_priv.load()?.cast_ref()?,
issuer_id,
issuance_by_default != 0,
timestamp,
)?;
Expand Down Expand Up @@ -134,7 +132,7 @@ pub extern "C" fn anoncreds_update_revocation_status_list_timestamp_only(
pub extern "C" fn anoncreds_create_revocation_registry_def(
cred_def: ObjectHandle,
cred_def_id: FfiStr,
issuer_id: FfiStr,
_issuer_id: FfiStr, // leaving it here not to break existing code
tag: FfiStr,
rev_reg_type: FfiStr,
max_cred_num: i64,
Expand All @@ -148,10 +146,8 @@ pub extern "C" fn anoncreds_create_revocation_registry_def(
let tag = tag.as_opt_str().ok_or_else(|| err_msg!("Missing tag"))?;
let cred_def_id = cred_def_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing cred def id"))?;
let issuer_id = issuer_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing issuer id"))?;
.ok_or_else(|| err_msg!("Missing cred def id"))?
.try_into()?;
let rev_reg_type = {
let rtype = rev_reg_type
.as_opt_str()
Expand All @@ -162,7 +158,6 @@ pub extern "C" fn anoncreds_create_revocation_registry_def(
let (reg_def, reg_def_private) = create_revocation_registry_def(
cred_def.load()?.cast_ref()?,
cred_def_id,
issuer_id,
tag,
rev_reg_type,
max_cred_num
Expand Down
3 changes: 2 additions & 1 deletion src/ffi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub extern "C" fn anoncreds_create_schema(
.ok_or_else(|| err_msg!("Missing schema version"))?;
let issuer_id = issuer_id
.as_opt_str()
.ok_or_else(|| err_msg!("Missing issuer_id"))?;
.ok_or_else(|| err_msg!("Missing issuer_id"))?
.try_into()?;
let schema = create_schema(
schema_name,
schema_version,
Expand Down
Loading

0 comments on commit 79d6097

Please sign in to comment.