diff --git a/crates/cashu/src/amount.rs b/crates/cashu/src/amount.rs index 07c7b3c74..211ba276f 100644 --- a/crates/cashu/src/amount.rs +++ b/crates/cashu/src/amount.rs @@ -84,3 +84,25 @@ impl core::iter::Sum for Amount { Amount::from(sats) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_split_amount() { + assert_eq!(Amount::from_sat(1).split(), vec![Amount::from_sat(1)]); + assert_eq!(Amount::from_sat(2).split(), vec![Amount::from_sat(2)]); + assert_eq!( + Amount::from_sat(3).split(), + vec![Amount::from_sat(2), Amount::from_sat(1)] + ); + let amounts: Vec = [8, 2, 1].iter().map(|a| Amount::from_sat(*a)).collect(); + assert_eq!(Amount::from_sat(11).split(), amounts); + let amounts: Vec = [128, 64, 32, 16, 8, 4, 2, 1] + .iter() + .map(|a| Amount::from_sat(*a)) + .collect(); + assert_eq!(Amount::from_sat(255).split(), amounts); + } +} diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index 2c67a92ef..1862f737d 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -35,7 +35,6 @@ pub mod wallet { use crate::nuts::nut01; use crate::secret::Secret; use crate::url::UncheckedUrl; - use crate::utils::split_amount; use crate::{error, Amount}; /// Blinded Messages [NUT-00] @@ -56,7 +55,7 @@ pub mod wallet { pub fn random(amount: Amount) -> Result { let mut blinded_messages = BlindedMessages::default(); - for amount in split_amount(amount) { + for amount in amount.split() { let secret = Secret::new(); let (blinded, r) = blind_message(secret.as_bytes(), None)?; diff --git a/crates/cashu/src/utils.rs b/crates/cashu/src/utils.rs index e451526c1..690f7f844 100644 --- a/crates/cashu/src/utils.rs +++ b/crates/cashu/src/utils.rs @@ -5,21 +5,6 @@ use bitcoin::hashes::Hash; use rand::prelude::*; use regex::Regex; -use crate::Amount; - -/// Split amount into cashu denominations (powers of 2) -pub fn split_amount(amount: Amount) -> Vec { - let mut chunks = Vec::new(); - let value = amount.to_sat(); - for i in 0..64 { - let mask = 1 << i; - if (value & mask) != 0 { - chunks.push(Amount::from_sat(2u64.pow(i as u32))); - } - } - chunks -} - pub fn extract_url_from_error(error: &str) -> Option { let regex = Regex::new(r"https?://[^\s]+").unwrap(); if let Some(capture) = regex.captures(error) { @@ -35,25 +20,3 @@ pub fn random_hash() -> Vec { let hash = Sha256::hash(&random_bytes); hash.to_byte_array().to_vec() } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_split_amount() { - assert_eq!(split_amount(Amount::from_sat(1)), vec![Amount::from_sat(1)]); - assert_eq!(split_amount(Amount::from_sat(2)), vec![Amount::from_sat(2)]); - assert_eq!( - split_amount(Amount::from_sat(3)), - vec![Amount::from_sat(1), Amount::from_sat(2)] - ); - let amounts: Vec = [1, 2, 8].iter().map(|a| Amount::from_sat(*a)).collect(); - assert_eq!(split_amount(Amount::from_sat(11)), amounts); - let amounts: Vec = [1, 2, 4, 8, 16, 32, 64, 128] - .iter() - .map(|a| Amount::from_sat(*a)) - .collect(); - assert_eq!(split_amount(Amount::from_sat(255)), amounts); - } -}