From 348e509ddc15c5a91a19493de2bdea83d3659d0a Mon Sep 17 00:00:00 2001 From: Sosthene Date: Fri, 6 Dec 2024 15:26:07 +0100 Subject: [PATCH] decrypt_all check is the value is clear or a commitment --- src/pcd.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/pcd.rs b/src/pcd.rs index e0a158e..1fd7d26 100644 --- a/src/pcd.rs +++ b/src/pcd.rs @@ -236,16 +236,17 @@ pub trait Pcd<'a>: Serialize + Deserialize<'a> { .map_err(|_| Error::msg(format!("Failed to decrypt field {}", field)))?; let decrypted_value: String = String::from_utf8(plain)?; - fields2plain.insert(field.to_owned(), Value::String(decrypted_value)); + fields2plain.insert(field.to_owned(), serde_json::from_str(&decrypted_value)?); + } else if let Some(commitment) = fields2commit.get(field) { // We should always have a commitment + // We check if the hashed value is the commitment + let hashed_value = AnkPcdHash::from_value_with_outpoint(encrypted_value, &serialize(&commited_in)); + if commitment.as_str().unwrap() != &hashed_value.to_string() { + // The value is encrypted, and we don't have the key + // We put the commitment instead of the encrypted value + fields2plain.insert(field.to_owned(), commitment.clone()); + } // else it means the value is simply unencrypted, we leave it as it is } else { - // We put the commitment instead of the encrypted value - let commitment = fields2commit.get(field); - match commitment { - Some(hash) => { - fields2plain.insert(field.to_owned(), hash.clone()); - }, - None => return Err(Error::msg(format!("Missing commitment for field {}", field))) - } + return Err(Error::msg(format!("Missing commitment for field {}", field))); } }