From 5b3aa2c22e942a62b3ffecd2fee3780e04b58215 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Tue, 24 Oct 2023 11:04:38 -0700 Subject: [PATCH] avoid stripping leading zero from non-integer attribute Signed-off-by: Andrew Whitehead --- indy-credx/src/services/verifier.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/indy-credx/src/services/verifier.rs b/indy-credx/src/services/verifier.rs index 17bc76f..4d61216 100644 --- a/indy-credx/src/services/verifier.rs +++ b/indy-credx/src/services/verifier.rs @@ -26,6 +26,7 @@ pub struct Filter { static INTERNAL_TAG_MATCHER: Lazy = Lazy::new(|| Regex::new("^attr::([^:]+)::(value|marker)$").unwrap()); +static ENCODED_ATTR_MATCHER: Lazy = Lazy::new(|| Regex::new("^0+([0-9]+)$").unwrap()); pub fn verify_presentation( presentation: &Presentation, @@ -533,12 +534,19 @@ fn verify_revealed_attribute_values( Ok(()) } +fn normalize_encoded_attr(attr: &str) -> &str { + match ENCODED_ATTR_MATCHER.captures(attr) { + Some(m) => m.get(1).unwrap().as_str(), + None => attr, + } +} + fn verify_revealed_attribute_value( attr_name: &str, proof: &Presentation, attr_info: &RevealedAttributeInfo, ) -> Result<()> { - let reveal_attr_encoded = attr_info.encoded.trim_start_matches('0'); + let reveal_attr_encoded = normalize_encoded_attr(&attr_info.encoded); let sub_proof_index = attr_info.sub_proof_index as usize; let crypto_proof_encoded = proof @@ -1238,4 +1246,14 @@ mod tests { validate_timestamp(&_received(), "referent_2", &None, &Some(_interval())).unwrap_err(); validate_timestamp(&_received(), "referent_3", &None, &Some(_interval())).unwrap_err(); } + + #[test] + fn format_attribute() { + assert_eq!(normalize_encoded_attr(""), ""); + assert_eq!(normalize_encoded_attr("abc"), "abc"); + assert_eq!(normalize_encoded_attr("0"), "0"); + assert_eq!(normalize_encoded_attr("01"), "1"); + assert_eq!(normalize_encoded_attr("01.0"), "01.0"); + assert_eq!(normalize_encoded_attr("0abc"), "0abc"); + } }