Skip to content

Commit

Permalink
chore: consolidate split-amount
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Nov 7, 2023
1 parent 3815627 commit 75902db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
22 changes: 22 additions & 0 deletions crates/cashu/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Amount> = [8, 2, 1].iter().map(|a| Amount::from_sat(*a)).collect();
assert_eq!(Amount::from_sat(11).split(), amounts);
let amounts: Vec<Amount> = [128, 64, 32, 16, 8, 4, 2, 1]
.iter()
.map(|a| Amount::from_sat(*a))
.collect();
assert_eq!(Amount::from_sat(255).split(), amounts);
}
}
3 changes: 1 addition & 2 deletions crates/cashu/src/nuts/nut00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -56,7 +55,7 @@ pub mod wallet {
pub fn random(amount: Amount) -> Result<Self, wallet::Error> {
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)?;

Expand Down
37 changes: 0 additions & 37 deletions crates/cashu/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Amount> {
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<String> {
let regex = Regex::new(r"https?://[^\s]+").unwrap();
if let Some(capture) = regex.captures(error) {
Expand All @@ -35,25 +20,3 @@ pub fn random_hash() -> Vec<u8> {
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<Amount> = [1, 2, 8].iter().map(|a| Amount::from_sat(*a)).collect();
assert_eq!(split_amount(Amount::from_sat(11)), amounts);
let amounts: Vec<Amount> = [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);
}
}

0 comments on commit 75902db

Please sign in to comment.