From a37c8bc4f5df23e57d79d63c3b19a92746d77238 Mon Sep 17 00:00:00 2001 From: vineet <10172895+vineetpant@users.noreply.github.com> Date: Wed, 13 Sep 2023 15:38:56 +0530 Subject: [PATCH 1/5] add convert to nquads helper --- src/api/vade_evan_api.rs | 78 +++++++++++++++++++++++++++++++++++++++ src/helpers/credential.rs | 47 ++++++++++++++++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/src/api/vade_evan_api.rs b/src/api/vade_evan_api.rs index fcf4322..d5c0380 100644 --- a/src/api/vade_evan_api.rs +++ b/src/api/vade_evan_api.rs @@ -496,6 +496,84 @@ impl VadeEvan { .map_err(|err| err.into()) } + /// Converts a given credential to nquads Vector + /// + /// # Arguments + /// + /// * `credential` - credential to be converted to nquads + /// + /// # Example + /// + /// ``` + /// cfg_if::cfg_if! { + /// if #[cfg(not(all(feature = "c-lib", feature = "target-c-sdk")))] { + /// use anyhow::Result; + /// use vade_evan::{VadeEvan, VadeEvanConfig, DEFAULT_TARGET, DEFAULT_SIGNER}; + /// + /// async fn example() -> Result<()> { + /// let mut vade_evan = VadeEvan::new(VadeEvanConfig { target: DEFAULT_TARGET, signer: DEFAULT_SIGNER })?; + /// let credential = r###"{ + /// "id": "uuid:70b7ec4e-f035-493e-93d3-2cf5be4c7f88", + /// "type": [ + /// "VerifiableCredential" + /// ], + /// "proof": { + /// "type": "BbsBlsSignature2020", + /// "created": "2023-02-01T14:08:17.000Z", + /// "signature": "kvSyi40dnZ5S3/mSxbSUQGKLpyMXDQNLCPtwDGM9GsnNNKF7MtaFHXIbvXaVXku0EY/n2uNMQ2bmK2P0KEmzgbjRHtzUOWVdfAnXnVRy8/UHHIyJR471X6benfZk8KG0qVqy+w67z9g628xRkFGA5Q==", + /// "proofPurpose": "assertionMethod", + /// "verificationMethod": "did:evan:EiAee4ixDnSP0eWyp0YFV7Wt9yrZ3w841FNuv9NSLFSCVA#bbs-key-1", + /// "credentialMessageCount": 13, + /// "requiredRevealStatements": [] + /// }, + /// "issuer": "did:evan:EiAee4ixDnSP0eWyp0YFV7Wt9yrZ3w841FNuv9NSLFSCVA", + /// "@context": [ + /// "https://www.w3.org/2018/credentials/v1", + /// "https://schema.org/", + /// "https://w3id.org/vc-revocation-list-2020/v1" + /// ], + /// "issuanceDate": "2023-02-01T14:08:09.849Z", + /// "credentialSchema": { + /// "id": "did:evan:EiCimsy3uWJ7PivWK0QUYSCkImQnjrx6fGr6nK8XIg26Kg", + /// "type": "EvanVCSchema" + /// }, + /// "credentialStatus": { + /// "id": "did:evan:EiA0Ns-jiPwu2Pl4GQZpkTKBjvFeRXxwGgXRTfG1Lyi8aA#4", + /// "type": "RevocationList2020Status", + /// "revocationListIndex": "4", + /// "revocationListCredential": "did:evan:EiA0Ns-jiPwu2Pl4GQZpkTKBjvFeRXxwGgXRTfG1Lyi8aA" + /// }, + /// "credentialSubject": { + /// "id": "did:evan:EiAee4ixDnSP0eWyp0YFV7Wt9yrZ3w841FNuv9NSLFSCVA", + /// "data": { + /// "bio": "biography" + /// } + /// } + /// }"###; + /// + /// // convert the credential to nquads + /// vade_evan + /// .helper_convert_credential_to_nquads(credential) + /// .await?; + /// + /// Ok(()) + /// } + /// } else { + /// // currently no example for target-c-sdk and c-lib/target-java-lib + /// } + /// } + #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + pub async fn helper_convert_credential_to_nquads( + &mut self, + credential: &str, + ) -> Result { + let credential_helper = Credential::new(self)?; + credential_helper + .convert_credential_to_nquads(credential) + .await + .map_err(|err| err.into()) + } + /// Proposes to share a proof for a credential. /// The proof proposal consists of the fields the prover wants to reveal per schema. /// diff --git a/src/helpers/credential.rs b/src/helpers/credential.rs index 570157c..694d950 100644 --- a/src/helpers/credential.rs +++ b/src/helpers/credential.rs @@ -451,13 +451,35 @@ impl<'a> Credential<'a> { )), } } + + /// Converts a Credential to nquads + /// + /// # Arguments + /// * `credential_str` - Credential to be converted + /// + /// # Returns + /// * `nquads` - Vec of nquads + pub async fn convert_credential_to_nquads( + &self, + credential_str: &str, + ) -> Result { + // get nquads + let mut parsed_credential: Map = serde_json::from_str(credential_str)?; + // remove proof if exists + if parsed_credential.contains_key("proof") { + parsed_credential.remove("proof"); + } + let credential_without_proof = serde_json::to_string(&parsed_credential)?; + let did_doc_nquads = convert_to_nquads(&credential_without_proof).await?; + Ok(serde_json::to_string(&did_doc_nquads)?) + } } #[cfg(test)] #[cfg(not(all(feature = "c-lib", feature = "target-c-sdk")))] mod tests { use crate::helpers::credential::is_revoked; - + const ADDITIONAL_HIDDEN_MESSAGES_COUNT: usize = 1; cfg_if::cfg_if! { if #[cfg(feature = "did-sidetree")] { use anyhow::Result; @@ -913,4 +935,27 @@ mod tests { Ok(()) } + + #[tokio::test] + #[cfg(feature = "did-sidetree")] + async fn helper_can_convert_to_nquads() -> Result<()> { + let mut vade_evan = VadeEvan::new(crate::VadeEvanConfig { + target: DEFAULT_TARGET, + signer: DEFAULT_SIGNER, + })?; + + let credential_helper = Credential::new(&mut vade_evan)?; + let credential: BbsCredential = serde_json::from_str(CREDENTIAL_ACTIVE)?; + // verify the credential issuer + let nquads_result = credential_helper + .convert_credential_to_nquads(CREDENTIAL_ACTIVE) + .await; + assert!(nquads_result.is_ok()); + let nquads: Vec = serde_json::from_str(&nquads_result?)?; + assert_eq!( + nquads.len() + ADDITIONAL_HIDDEN_MESSAGES_COUNT, + credential.proof.credential_message_count + ); + Ok(()) + } } From 655fadd5f9cc4fc9eac22070a7495d137e8c779f Mon Sep 17 00:00:00 2001 From: vineet <10172895+vineetpant@users.noreply.github.com> Date: Wed, 13 Sep 2023 15:58:37 +0530 Subject: [PATCH 2/5] add convert_to_nquads helper to bindings --- src/c_lib.rs | 20 ++++++++++++++++++++ src/main.rs | 21 +++++++++++++++++++++ src/wasm_lib.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/c_lib.rs b/src/c_lib.rs index 7b97dbf..b1a9e5e 100644 --- a/src/c_lib.rs +++ b/src/c_lib.rs @@ -770,6 +770,26 @@ pub extern "C" fn execute_vade( .map_err(stringify_vade_evan_error) } }), + #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + "helper_convert_credential_to_nquads" => runtime.block_on({ + async { + let mut vade_evan = get_vade_evan( + Some(&str_config), + #[cfg(all(feature = "c-lib", feature = "target-c-sdk"))] + ptr_request_list, + #[cfg(all(feature = "c-lib", feature = "target-c-sdk"))] + request_function_callback, + ) + .map_err(stringify_generic_error)?; + vade_evan + .helper_convert_credential_to_nquads( + arguments_vec.get(0).unwrap_or_else(|| &no_args), + ) + .await + .map_err(stringify_vade_evan_error)?; + Ok("".to_string()) + } + }), #[cfg(any(feature = "vc-zkp-bbs"))] "run_custom_function" => runtime.block_on({ execute_vade_function!( diff --git a/src/main.rs b/src/main.rs index 165dd9b..a917d89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -253,6 +253,17 @@ async fn main() -> Result<()> { .await? } #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + ("convert_credential_to_nquads", Some(sub_m)) => { + get_vade_evan(sub_m)? + .helper_convert_credential_to_nquads(get_argument_value( + sub_m, + "credential", + None, + )) + .await?; + "".to_string() + } + #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] ("create_self_issued_credential", Some(sub_m)) => { get_vade_evan(sub_m)? .helper_create_self_issued_credential( @@ -421,6 +432,16 @@ fn add_subcommand_helper<'a>(app: App<'a, 'a>) -> Result> { } else {} } + cfg_if::cfg_if! { + if #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] { + subcommand = subcommand.subcommand( + SubCommand::with_name("convert_credential_to_nquads") + .about("Converts a given credential to nquads vector.") + .arg(get_clap_argument("credential")?) + ); + } else {} + } + cfg_if::cfg_if! { if #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] { subcommand = subcommand.subcommand( diff --git a/src/wasm_lib.rs b/src/wasm_lib.rs index 848f4f8..a79274f 100644 --- a/src/wasm_lib.rs +++ b/src/wasm_lib.rs @@ -189,6 +189,12 @@ struct HelperVerifyPresentationPayload { pub presentation_str: String, pub proof_request_str: String, } +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct HelperConvertCredentialToNquads { + pub credential_str: String, +} + #[wasm_bindgen] pub fn set_panic_hook() { @@ -438,6 +444,20 @@ cfg_if::cfg_if! { .map_err(jsify_vade_evan_error)?) } + #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + #[wasm_bindgen] + pub async fn helper_convert_credential_to_nquads( + credential: String, + ) -> Result { + let mut vade_evan = get_vade_evan(None).map_err(jsify_generic_error)?; + vade_evan + .helper_convert_credential_to_nquads( + &credential, + ).await + .map_err(jsify_vade_evan_error)?; + Ok("".to_string()) + } + #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] #[wasm_bindgen] pub async fn helper_create_proof_proposal( @@ -787,6 +807,16 @@ pub async fn execute_vade( } } #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + "helper_convert_credential_to_nquads" => { + let payload_result = parse::(&payload); + match payload_result { + Ok(payload) => { + helper_convert_credential_to_nquads(payload.credential).await + } + Err(error) => Err(get_parsing_error_message(&error, &payload)), + } + } + #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] "helper_create_proof_proposal" => { let payload_result = parse::(&payload); match payload_result { From 70810e54016462a96d0a100d51d14f36ee460ce7 Mon Sep 17 00:00:00 2001 From: vineet <10172895+vineetpant@users.noreply.github.com> Date: Wed, 13 Sep 2023 15:59:42 +0530 Subject: [PATCH 3/5] update versions.md --- VERSIONS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/VERSIONS.md b/VERSIONS.md index ed3b7b7..8303772 100644 --- a/VERSIONS.md +++ b/VERSIONS.md @@ -5,6 +5,7 @@ ### Features - add support for `vc_zkp_propose_proof` function in `vade-evan-bbs` plugin +- add `helper_convert_credential_to_nquads` helper function ### Fixes From dcf2964eec07065b5a866ce9daa6db736c4f51bf Mon Sep 17 00:00:00 2001 From: vineet <10172895+vineetpant@users.noreply.github.com> Date: Wed, 13 Sep 2023 16:13:18 +0530 Subject: [PATCH 4/5] fix wasm error --- src/wasm_lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm_lib.rs b/src/wasm_lib.rs index 1928d24..9e498d1 100644 --- a/src/wasm_lib.rs +++ b/src/wasm_lib.rs @@ -815,7 +815,7 @@ pub async fn execute_vade( let payload_result = parse::(&payload); match payload_result { Ok(payload) => { - helper_convert_credential_to_nquads(payload.credential).await + helper_convert_credential_to_nquads(payload.credential_str).await } Err(error) => Err(get_parsing_error_message(&error, &payload)), } From 0a6a90b174387e1e0d880cdc3278a40b4ef66562 Mon Sep 17 00:00:00 2001 From: vineet <10172895+vineetpant@users.noreply.github.com> Date: Thu, 14 Sep 2023 14:16:02 +0530 Subject: [PATCH 5/5] update comments and feature checks --- src/api/vade_evan_api.rs | 4 ++-- src/c_lib.rs | 2 +- src/helpers/credential.rs | 4 ++-- src/main.rs | 4 ++-- src/wasm_lib.rs | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/api/vade_evan_api.rs b/src/api/vade_evan_api.rs index 9cb06aa..8ec6290 100644 --- a/src/api/vade_evan_api.rs +++ b/src/api/vade_evan_api.rs @@ -496,7 +496,7 @@ impl VadeEvan { .map_err(|err| err.into()) } - /// Converts a given credential to nquads Vector + /// Converts a Credential to canonized nquads /// /// # Arguments /// @@ -562,7 +562,7 @@ impl VadeEvan { /// // currently no example for target-c-sdk and c-lib/target-java-lib /// } /// } - #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + #[cfg(all(feature = "vc-zkp-bbs"))] pub async fn helper_convert_credential_to_nquads( &mut self, credential: &str, diff --git a/src/c_lib.rs b/src/c_lib.rs index bbe7234..b6ecded 100644 --- a/src/c_lib.rs +++ b/src/c_lib.rs @@ -792,7 +792,7 @@ pub extern "C" fn execute_vade( .map_err(stringify_vade_evan_error) } }), - #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + #[cfg(all(feature = "vc-zkp-bbs"))] "helper_convert_credential_to_nquads" => runtime.block_on({ async { let mut vade_evan = get_vade_evan( diff --git a/src/helpers/credential.rs b/src/helpers/credential.rs index 33bc8c2..9481c93 100644 --- a/src/helpers/credential.rs +++ b/src/helpers/credential.rs @@ -488,13 +488,13 @@ impl<'a> Credential<'a> { } } - /// Converts a Credential to nquads + /// Converts a Credential to canonized nquads /// /// # Arguments /// * `credential_str` - Credential to be converted /// /// # Returns - /// * `nquads` - Vec of nquads + /// * `nquads` - Vec of canonized nquads pub async fn convert_credential_to_nquads( &self, credential_str: &str, diff --git a/src/main.rs b/src/main.rs index ee4f3e3..14c50ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -253,7 +253,7 @@ async fn main() -> Result<()> { ) .await? } - #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + #[cfg(all(feature = "vc-zkp-bbs"))] ("convert_credential_to_nquads", Some(sub_m)) => { get_vade_evan(sub_m)? .helper_convert_credential_to_nquads(get_argument_value( @@ -434,7 +434,7 @@ fn add_subcommand_helper<'a>(app: App<'a, 'a>) -> Result> { } cfg_if::cfg_if! { - if #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] { + if #[cfg(all(feature = "vc-zkp-bbs"))] { subcommand = subcommand.subcommand( SubCommand::with_name("convert_credential_to_nquads") .about("Converts a given credential to nquads vector.") diff --git a/src/wasm_lib.rs b/src/wasm_lib.rs index 9e498d1..a928757 100644 --- a/src/wasm_lib.rs +++ b/src/wasm_lib.rs @@ -447,7 +447,7 @@ cfg_if::cfg_if! { .map_err(jsify_vade_evan_error)?) } - #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + #[cfg(all(feature = "vc-zkp-bbs"))] #[wasm_bindgen] pub async fn helper_convert_credential_to_nquads( credential: String, @@ -810,7 +810,7 @@ pub async fn execute_vade( Err(error) => Err(get_parsing_error_message(&error, &payload)), } } - #[cfg(all(feature = "vc-zkp-bbs", feature = "did-sidetree"))] + #[cfg(all(feature = "vc-zkp-bbs"))] "helper_convert_credential_to_nquads" => { let payload_result = parse::(&payload); match payload_result {