From d9952aab0b4d4e7bfecd7dd6c25342ac4042d4a9 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Fri, 18 Aug 2023 12:55:30 -0700 Subject: [PATCH 1/2] update to anoncreds-clsignatures 0.2.2; simplify LinkSecret handling Signed-off-by: Andrew Whitehead --- Cargo.toml | 2 +- src/data_types/link_secret.rs | 44 ++++++++++++----------------------- src/services/helpers.rs | 11 +++++---- src/services/prover.rs | 6 ++--- 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2f77c370..883b97fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ logger = ["env_logger"] vendored = ["anoncreds-clsignatures/openssl_vendored"] [dependencies] -anoncreds-clsignatures = "0.2.1" +anoncreds-clsignatures = "0.2.2" bs58 = "0.4.0" env_logger = { version = "0.9.3", optional = true } ffi-support = { version = "0.4.0", optional = true } diff --git a/src/data_types/link_secret.rs b/src/data_types/link_secret.rs index 9bd9422c..9d644962 100644 --- a/src/data_types/link_secret.rs +++ b/src/data_types/link_secret.rs @@ -1,16 +1,15 @@ +use std::fmt; + use crate::cl::{bn::BigNumber, LinkSecret as ClLinkSecret, Prover as CryptoProver}; use crate::error::ConversionError; -use std::fmt; -pub struct LinkSecret(pub BigNumber); +pub struct LinkSecret(pub(crate) BigNumber); impl LinkSecret { pub fn new() -> Result { let value = CryptoProver::new_link_secret() - .and_then(|v| v.value()) - .map_err(|err| { - ConversionError::from_msg(format!("Error creating link secret: {err}")) - })?; + .map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}")))? + .into(); Ok(Self(value)) } @@ -32,28 +31,15 @@ impl fmt::Debug for LinkSecret { } } -impl TryInto for LinkSecret { - type Error = ConversionError; - - fn try_into(self) -> Result { - let j = serde_json::json!({ - "ms": self.0 - }); - serde_json::from_value(j) - .map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}"))) +impl From for ClLinkSecret { + fn from(sec: LinkSecret) -> ClLinkSecret { + sec.0.into() } } -impl TryInto for &LinkSecret { - type Error = ConversionError; - - fn try_into(self) -> Result { - let j = serde_json::json!({ - "ms": self.0 - }); - - serde_json::from_value(j) - .map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}"))) +impl From for LinkSecret { + fn from(sec: ClLinkSecret) -> LinkSecret { + Self(sec.into()) } } @@ -61,9 +47,9 @@ impl TryInto for LinkSecret { type Error = ConversionError; fn try_into(self) -> Result { - self.0 - .to_dec() - .map_err(|err| ConversionError::from_msg(format!("Error creating link secret: {err}"))) + self.0.to_dec().map_err(|err| { + ConversionError::from_msg(format!("Error converting link secret: {err}")) + }) } } @@ -72,7 +58,7 @@ impl TryFrom<&str> for LinkSecret { fn try_from(value: &str) -> Result { Ok(Self(BigNumber::from_dec(value).map_err(|err| { - ConversionError::from_msg(format!("Error creating link secret: {err}")) + ConversionError::from_msg(format!("Error converting link secret: {err}")) })?)) } } diff --git a/src/services/helpers.rs b/src/services/helpers.rs index 31eb3634..fb76dc54 100644 --- a/src/services/helpers.rs +++ b/src/services/helpers.rs @@ -1,11 +1,12 @@ use std::collections::HashMap; use crate::cl::{ - bn::BigNumber, CredentialSchema, CredentialValues, Issuer, LinkSecret as ClLinkSecret, - NonCredentialSchema, SubProofRequest, Verifier, + bn::BigNumber, CredentialSchema, CredentialValues, Issuer, NonCredentialSchema, + SubProofRequest, Verifier, }; use crate::data_types::{ credential::AttributeValues, + link_secret::LinkSecret, nonce::Nonce, pres_request::{AttributeInfo, NonRevokedInterval, PredicateInfo, PresentationRequestPayload}, presentation::RequestedProof, @@ -46,7 +47,7 @@ pub fn build_non_credential_schema() -> Result { pub fn build_credential_values( credential_values: &HashMap, - link_secret: Option<&ClLinkSecret>, + link_secret: Option<&LinkSecret>, ) -> Result { trace!( "build_credential_values >>> credential_values: {:?}", @@ -57,9 +58,9 @@ pub fn build_credential_values( for (attr, values) in credential_values { credential_values_builder.add_dec_known(&attr_common_view(attr), &values.encoded)?; } - if let Some(ms) = link_secret { + if let Some(ls) = link_secret { // value is master_secret as that's what's historically been used in published credential definitions - credential_values_builder.add_value_hidden("master_secret", &ms.value()?)?; + credential_values_builder.add_value_hidden("master_secret", &ls.0)?; } let res = credential_values_builder.finalize()?; diff --git a/src/services/prover.rs b/src/services/prover.rs index f80849ad..9363d7ed 100644 --- a/src/services/prover.rs +++ b/src/services/prover.rs @@ -254,8 +254,7 @@ pub fn process_credential( &cred_def.value.primary, cred_def.value.revocation.as_ref(), )?; - let credential_values = - build_credential_values(&credential.values.0, Some(&link_secret.try_into()?))?; + let credential_values = build_credential_values(&credential.values.0, Some(&link_secret))?; let rev_pub_key = rev_reg_def.map(|d| &d.value.public_keys.accum_key); Prover::process_credential_signature( @@ -454,8 +453,7 @@ pub fn create_presentation( )?; let credential_schema = build_credential_schema(&schema.attr_names.0)?; - let credential_values = - build_credential_values(&credential.values.0, Some(&link_secret.try_into()?))?; + let credential_values = build_credential_values(&credential.values.0, Some(&link_secret))?; let (req_attrs, req_predicates) = prepare_credential_for_proving( present.requested_attributes, present.requested_predicates, From c5778e959191f8d1c4ed9ce3aa4b42afbd96a4fe Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Fri, 18 Aug 2023 12:58:33 -0700 Subject: [PATCH 2/2] remove unneeded ClLinkSecret conversion Signed-off-by: Andrew Whitehead --- src/data_types/link_secret.rs | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/src/data_types/link_secret.rs b/src/data_types/link_secret.rs index 9d644962..68f6e704 100644 --- a/src/data_types/link_secret.rs +++ b/src/data_types/link_secret.rs @@ -1,6 +1,6 @@ use std::fmt; -use crate::cl::{bn::BigNumber, LinkSecret as ClLinkSecret, Prover as CryptoProver}; +use crate::cl::{bn::BigNumber, Prover as CryptoProver}; use crate::error::ConversionError; pub struct LinkSecret(pub(crate) BigNumber); @@ -31,18 +31,6 @@ impl fmt::Debug for LinkSecret { } } -impl From for ClLinkSecret { - fn from(sec: LinkSecret) -> ClLinkSecret { - sec.0.into() - } -} - -impl From for LinkSecret { - fn from(sec: ClLinkSecret) -> LinkSecret { - Self(sec.into()) - } -} - impl TryInto for LinkSecret { type Error = ConversionError; @@ -81,23 +69,6 @@ mod link_secret_tests { assert_eq!(link_secret_str, ls); } - #[test] - fn should_convert_between_link_secret() { - let link_secret = LinkSecret::new().expect("Unable to create link secret"); - let cl_link_secret: ClLinkSecret = link_secret - .try_clone() - .expect("Error cloning link secret") - .try_into() - .expect("error converting to CL link secret"); - - assert_eq!( - link_secret.0, - cl_link_secret - .value() - .expect("Error getting value from CL link secret") - ); - } - #[test] fn should_clone_link_secret() { let link_secret = LinkSecret::new().expect("Unable to create link secret");