From 9dd5bda32d9efc1b1eaa92f17a596879a9388bb1 Mon Sep 17 00:00:00 2001 From: nullcoset Date: Fri, 27 Jun 2025 16:58:05 +0200 Subject: [PATCH 1/2] unit test ECC - add trivial 2 other invalid addr --- tests/compatibility/ecc_compatibility.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/compatibility/ecc_compatibility.rs b/tests/compatibility/ecc_compatibility.rs index 386095f..dd41c23 100644 --- a/tests/compatibility/ecc_compatibility.rs +++ b/tests/compatibility/ecc_compatibility.rs @@ -284,6 +284,8 @@ fn test_address_validation_compatibility() { "RSQLoVGDgRs9hTfTNJNuXKSpywcbdvy1agVX", // Wrong checksum "BTSLoVGDgRs9hTfTNJNuXKSpywcbdvy1agVK", // Wrong prefix "RSQLoVGDgRs9hTfTNJNuXKSpywcbdvy1agV", // Too short + "RSQLoVGDgRs9hTfTNJNuXKSpywcbdvy1agVKa", // extra char at end + "", // empty ]; for address_str in &invalid_addresses { From ed1b3c24da6dc9860b642e8f99ac89991c1993d6 Mon Sep 17 00:00:00 2001 From: nullcoset Date: Mon, 30 Jun 2025 17:14:42 +0200 Subject: [PATCH 2/2] Shorter syntax for Address::double_sha256(). with test Signed-off-by: nullcoset --- src/ecc/address.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ecc/address.rs b/src/ecc/address.rs index 254f85d..a7ad767 100644 --- a/src/ecc/address.rs +++ b/src/ecc/address.rs @@ -236,9 +236,7 @@ impl Address { /// Double SHA-256 hash for legacy addresses fn double_sha256(data: &[u8]) -> [u8; 32] { - let first_hash = Sha256::digest(data); - let second_hash = Sha256::digest(&first_hash); - second_hash.into() + Sha256::digest( Sha256::digest(data) ).into() } } @@ -419,4 +417,18 @@ mod tests { assert!(Address::from_string("RSQ123invalid").is_err()); assert!(!Address::validate_string("not_an_address")); } + + #[test] + fn test_calculate_double_sha() { + fn hash_to_str(data: &[u8; 32]) -> String { + use std::fmt::Write; + let mut ret = String::with_capacity(32*2); + for &by in data { + write!(&mut ret, "{:02x}", by).unwrap(); + } + return ret; + } + let s = Address::double_sha256(b"abc"); + assert_eq!(hash_to_str(&s) , "4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358"); + } } \ No newline at end of file