Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1861 from Artemkaaas/feature/annoncreds-doc
Browse files Browse the repository at this point in the history
Added test on different predicate types. Inproved anoncreds related documentation
  • Loading branch information
jovfer authored Aug 28, 2019
2 parents c9a16f8 + 7223e65 commit 641327b
Show file tree
Hide file tree
Showing 5 changed files with 843 additions and 428 deletions.
2 changes: 2 additions & 0 deletions libindy/src/api/anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,8 @@ pub extern fn indy_prover_get_credentials_for_proof_req(command_handle: CommandH
/// // (applies to every attribute and predicate but can be overridden on attribute level)
/// // (can be overridden on attribute level)
/// }
///
/// where
/// attr_info: Describes requested attribute
/// {
/// "name": string, // attribute name, (case insensitive and ignore spaces)
Expand Down
105 changes: 105 additions & 0 deletions libindy/tests/anoncreds_demos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2656,4 +2656,109 @@ mod demos {
wallet::close_and_delete_wallet(issuer_wallet_handle, &issuer_wallet_config).unwrap();
wallet::close_and_delete_wallet(prover_wallet_handle, &prover_wallet_config).unwrap();
}


#[test]
fn anoncreds_works_for_different_predicate_types() {
Setup::empty();

//1. Create Issuer wallet, gets wallet handle
let (issuer_wallet_handle, issuer_wallet_config) = wallet::create_and_open_default_wallet("anoncreds_works_for_single_issuer_single_prover").unwrap();

//2. Create Prover wallet, gets wallet handle
let (prover_wallet_handle, prover_wallet_config) = wallet::create_and_open_default_wallet("anoncreds_works_for_single_issuer_single_prover").unwrap();

let schema_attributes = r#"["age", "height", "weight", "salary"]"#;

//3. Issuer creates Schema and Credential Definition
let (schema_id, schema_json, cred_def_id, cred_def_json) = anoncreds::multi_steps_issuer_preparation(issuer_wallet_handle,
ISSUER_DID,
GVT_SCHEMA_NAME,
schema_attributes);

//4. Prover creates Master Secret
anoncreds::prover_create_master_secret(prover_wallet_handle, COMMON_MASTER_SECRET).unwrap();

let cred_values = json!({
"age": {"raw": "28", "encoded": "28"},
"height": {"raw": "175", "encoded": "175"},
"weight": {"raw": "78", "encoded": "78"},
"salary": {"raw": "2000", "encoded": "2000"}
}).to_string();

//5. Issuance credential for Prover
anoncreds::multi_steps_create_credential(COMMON_MASTER_SECRET,
prover_wallet_handle,
issuer_wallet_handle,
CREDENTIAL1_ID,
&cred_values,
&cred_def_id,
&cred_def_json);

//6. Proof request
let nonce = anoncreds::generate_nonce().unwrap();
let proof_req_json = json!({
"nonce": nonce,
"name":"proof_req_1",
"version":"0.1",
"requested_attributes":{},
"requested_predicates":{
"predicate1_referent":{
"name":"age","p_type":">=","p_value":18
},
"predicate2_referent":{
"name":"height","p_type":">","p_value":170
},
"predicate3_referent":{
"name":"weight","p_type":"<","p_value":90
},
"predicate4_referent":{
"name":"salary","p_type":"<=","p_value":2000
}
}
}).to_string();

//7. Prover gets Credentials for Proof Request
let credentials_json = anoncreds::prover_get_credentials_for_proof_req(prover_wallet_handle, &proof_req_json).unwrap();
let credential = anoncreds::get_credential_for_predicate_referent(&credentials_json, "predicate1_referent");

//8. Prover creates Proof
let requested_credentials_json =json!({
"self_attested_attributes": {},
"requested_attributes": {},
"requested_predicates": {
"predicate1_referent": {"cred_id": credential.referent},
"predicate2_referent": {"cred_id": credential.referent},
"predicate3_referent": {"cred_id": credential.referent},
"predicate4_referent": {"cred_id": credential.referent}
},
}).to_string();

let schemas_json = json!({schema_id: serde_json::from_str::<Schema>(&schema_json).unwrap()}).to_string();
let cred_defs_json = json!({cred_def_id: serde_json::from_str::<CredentialDefinition>(&cred_def_json).unwrap()}).to_string();
let rev_states_json = json!({}).to_string();

let proof_json = anoncreds::prover_create_proof(prover_wallet_handle,
&proof_req_json,
&requested_credentials_json,
COMMON_MASTER_SECRET,
&schemas_json,
&cred_defs_json,
&rev_states_json).unwrap();

//9. Verifier verifies proof
let rev_reg_defs_json = json!({}).to_string();
let rev_regs_json = json!({}).to_string();

let valid = anoncreds::verifier_verify_proof(&proof_req_json,
&proof_json,
&schemas_json,
&cred_defs_json,
&rev_reg_defs_json,
&rev_regs_json).unwrap();
assert!(valid);

wallet::close_and_delete_wallet(issuer_wallet_handle, &issuer_wallet_config).unwrap();
wallet::close_and_delete_wallet(prover_wallet_handle, &prover_wallet_config).unwrap();
}
}
Loading

0 comments on commit 641327b

Please sign in to comment.