Skip to content

Commit

Permalink
Added function to retrieve credential attribute
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Nov 13, 2023
1 parent ea7188b commit 572001a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
39 changes: 35 additions & 4 deletions src/ffi/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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::<W3CCredential>()?;
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,
Expand Down
8 changes: 8 additions & 0 deletions tests/utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 572001a

Please sign in to comment.