From 572001a0daaa74989a254bddbc8b0be7492d7c20 Mon Sep 17 00:00:00 2001 From: "artem.ivanov" Date: Mon, 13 Nov 2023 17:06:57 +0300 Subject: [PATCH] Added function to retrieve credential attribute Signed-off-by: artem.ivanov --- Cargo.toml | 2 +- src/ffi/credential.rs | 39 +++++++++++++++++++++++++++++++++++---- tests/utils/mock.rs | 8 ++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 52bb00be..cf7cb7a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ logger = ["dep:env_logger"] vendored = ["anoncreds-clsignatures/openssl_vendored"] [dependencies] -anoncreds-clsignatures = { git = "https://github.com/hyperledger/anoncreds-clsignatures-rs.git" } +anoncreds-clsignatures = "0.2.4" bs58 = "0.4.0" env_logger = { version = "0.9.3", optional = true } ffi-support = { version = "0.4.0", optional = true } diff --git a/src/ffi/credential.rs b/src/ffi/credential.rs index 7ec6b64c..d0542fa2 100644 --- a/src/ffi/credential.rs +++ b/src/ffi/credential.rs @@ -212,7 +212,7 @@ pub extern "C" fn anoncreds_credential_get_attribute( /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_create_credential( +pub extern "C" fn anoncreds_create_w3c_credential( cred_def: ObjectHandle, cred_def_private: ObjectHandle, cred_offer: ObjectHandle, @@ -263,7 +263,7 @@ pub extern "C" fn anoncreds_w3c_create_credential( /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_process_credential( +pub extern "C" fn anoncreds_process_w3c_credential( cred: ObjectHandle, cred_req_metadata: ObjectHandle, link_secret: FfiStr, @@ -393,7 +393,7 @@ pub extern "C" fn anoncreds_w3c_credential_add_non_anoncreds_integrity_proof( /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_credential_id( +pub extern "C" fn anoncreds_w3c_set_credential_id( cred: ObjectHandle, id: FfiStr, cred_p: *mut ObjectHandle, @@ -424,7 +424,7 @@ pub extern "C" fn anoncreds_w3c_credential_id( /// # Returns /// Error code #[no_mangle] -pub extern "C" fn anoncreds_w3c_credential_subject_id( +pub extern "C" fn anoncreds_credential_w3c_subject_id( cred: ObjectHandle, id: FfiStr, cred_p: *mut ObjectHandle, @@ -509,6 +509,37 @@ pub extern "C" fn anoncreds_w3c_credential_add_type( }) } +#[no_mangle] +pub extern "C" fn anoncreds_w3c_credential_get_attribute( + handle: ObjectHandle, + name: FfiStr, + result_p: *mut *const c_char, +) -> ErrorCode { + catch_error(|| { + check_useful_c_ptr!(result_p); + let cred = handle.load()?; + let cred = cred.cast_ref::()?; + let val = match name.as_opt_str().unwrap_or_default() { + "schema_id" => rust_string_to_c(cred.credential_schema.schema.clone()), + "cred_def_id" => rust_string_to_c(cred.credential_schema.definition.to_string()), + "rev_reg_id" => cred + .credential_schema + .revocation_registry + .as_ref() + .map_or(ptr::null_mut(), |s| rust_string_to_c(s.to_string())), + "rev_reg_index" => cred + .get_credential_signature_proof()? + .get_credential_signature()? + .signature + .extract_index() + .map_or(ptr::null_mut(), |s| rust_string_to_c(s.to_string())), + s => return Err(err_msg!("Unsupported attribute: {}", s)), + }; + unsafe { *result_p = val }; + Ok(()) + }) +} + fn _encoded_credential_values( attr_names: FfiStrList, attr_raw_values: FfiStrList, diff --git a/tests/utils/mock.rs b/tests/utils/mock.rs index aa0355fa..a7c2b03b 100644 --- a/tests/utils/mock.rs +++ b/tests/utils/mock.rs @@ -498,6 +498,11 @@ impl<'a> Mock<'a> { } }; + println!("--------"); + println!("Legacy Credential {}", json!(legacy_credential).to_string()); + println!("W3C Credential {}", json!(w3c_credential).to_string()); + println!("--------"); + // Update prover wallets and ledger with new revocation status list let pw = self.prover_wallets.get_mut(prover_id).unwrap(); pw.cred_reqs.push(cred_req_data); @@ -674,6 +679,9 @@ impl<'a> Mock<'a> { &cred_defs, ) .expect("Error creating presentation"); + println!("--------"); + println!("W3C Presentation {}", json!(presentation).to_string()); + println!("--------"); Presentations::W3C(presentation) } }