From 9df904648e87a9b7c41c322cf14c42482e34e710 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Thu, 24 Jul 2025 09:00:06 +0100 Subject: [PATCH] fix(cashu): preserve original case for custom CurrencyUnit values Custom currency units were incorrectly being converted to uppercase during deserialization. This change ensures custom units maintain their original case while still allowing case-insensitive matching for standard units. --- CHANGELOG.md | 2 ++ crates/cashu/src/nuts/nut00/mod.rs | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2b663d..f4c11e57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ - cdk-mintd: Updated to use new mint lifecycle methods for improved service management ([thesimplekid]). - cdk-integration-tests: Updated test utilities to use new mint lifecycle management ([thesimplekid]). +### Fixed +- cashu: Fixed CurrencyUnit custom units preserving original case instead of being converted to uppercase ([thesimplekid]). ## [0.11.0](https://github.com/cashubtc/cdk/releases/tag/v0.11.0) diff --git a/crates/cashu/src/nuts/nut00/mod.rs b/crates/cashu/src/nuts/nut00/mod.rs index 3996800f..cbcbf061 100644 --- a/crates/cashu/src/nuts/nut00/mod.rs +++ b/crates/cashu/src/nuts/nut00/mod.rs @@ -591,14 +591,14 @@ impl CurrencyUnit { impl FromStr for CurrencyUnit { type Err = Error; fn from_str(value: &str) -> Result { - let value = &value.to_uppercase(); - match value.as_str() { + let upper_value = value.to_uppercase(); + match upper_value.as_str() { "SAT" => Ok(Self::Sat), "MSAT" => Ok(Self::Msat), "USD" => Ok(Self::Usd), "EUR" => Ok(Self::Eur), "AUTH" => Ok(Self::Auth), - c => Ok(Self::Custom(c.to_string())), + _ => Ok(Self::Custom(value.to_string())), } } } @@ -977,4 +977,12 @@ mod tests { .unwrap(); assert_eq!(b.len(), 1); } + + #[test] + fn custom_unit_ser_der() { + let unit = CurrencyUnit::Custom(String::from("test")); + let serialized = serde_json::to_string(&unit).unwrap(); + let deserialized: CurrencyUnit = serde_json::from_str(&serialized).unwrap(); + assert_eq!(unit, deserialized) + } }