From 607cdf23d48c1bdcd7d2e506b885529acad6467b Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 13 Apr 2025 22:19:39 +0100 Subject: [PATCH 1/3] feat: Add robust mnemonic hashing and debug tests for Info struct --- crates/cdk-mintd/src/config.rs | 83 ++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/crates/cdk-mintd/src/config.rs b/crates/cdk-mintd/src/config.rs index 701ca52c..2aec65bf 100644 --- a/crates/cdk-mintd/src/config.rs +++ b/crates/cdk-mintd/src/config.rs @@ -26,14 +26,18 @@ pub struct Info { impl std::fmt::Debug for Info { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mnemonic_hash = sha256::Hash::from_slice(&self.mnemonic.clone().into_bytes()) - .map_err(|_| std::fmt::Error)?; + // Use a fallback approach that won't panic + let mnemonic_display = match sha256::Hash::hash(self.mnemonic.clone().into_bytes().as_ref()) + { + Ok(hash) => format!("", hash), + Err(_) => String::from(""), // Fallback if hashing fails + }; f.debug_struct("Info") .field("url", &self.url) .field("listen_host", &self.listen_host) .field("listen_port", &self.listen_port) - .field("mnemonic", &format!("", mnemonic_hash)) + .field("mnemonic", &mnemonic_display) .field("input_fee_ppk", &self.input_fee_ppk) .field("http_cache", &self.http_cache) .field("enable_swagger_ui", &self.enable_swagger_ui) @@ -361,3 +365,76 @@ impl Settings { Ok(settings) } } + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn test_info_debug_impl() { + // Create a sample Info struct with test data + let info = Info { + url: "http://example.com".to_string(), + listen_host: "127.0.0.1".to_string(), + listen_port: 8080, + mnemonic: "test secret mnemonic phrase".to_string(), + input_fee_ppk: Some(100), + ..Default::default() + }; + + // Convert the Info struct to a debug string + let debug_output = format!("{:?}", info); + + // Verify the debug output contains expected fields + assert!(debug_output.contains("url: \"http://example.com\"")); + assert!(debug_output.contains("listen_host: \"127.0.0.1\"")); + assert!(debug_output.contains("listen_port: 8080")); + + // The mnemonic should be hashed, not displayed in plaintext + assert!(!debug_output.contains("test secret mnemonic phrase")); + assert!(debug_output.contains(" Date: Sun, 13 Apr 2025 22:19:41 +0100 Subject: [PATCH 2/3] fix: Correct mnemonic hashing in Debug implementation --- crates/cdk-mintd/src/config.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/cdk-mintd/src/config.rs b/crates/cdk-mintd/src/config.rs index 2aec65bf..8d0fc299 100644 --- a/crates/cdk-mintd/src/config.rs +++ b/crates/cdk-mintd/src/config.rs @@ -27,10 +27,9 @@ pub struct Info { impl std::fmt::Debug for Info { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // Use a fallback approach that won't panic - let mnemonic_display = match sha256::Hash::hash(self.mnemonic.clone().into_bytes().as_ref()) - { - Ok(hash) => format!("", hash), - Err(_) => String::from(""), // Fallback if hashing fails + let mnemonic_display = { + let hash = sha256::Hash::hash(self.mnemonic.clone().into_bytes().as_ref()); + format!("", hash) }; f.debug_struct("Info") From 717742be05e301db1b2dd7196eb24342483b1141 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 13 Apr 2025 22:21:43 +0100 Subject: [PATCH 3/3] fix: debug print of info --- crates/cdk-mintd/src/config.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/cdk-mintd/src/config.rs b/crates/cdk-mintd/src/config.rs index 8d0fc299..5919b697 100644 --- a/crates/cdk-mintd/src/config.rs +++ b/crates/cdk-mintd/src/config.rs @@ -394,9 +394,7 @@ mod tests { assert!(!debug_output.contains("test secret mnemonic phrase")); assert!(debug_output.contains("