From 5866dc7371397244c77f26d0cc0074915174a190 Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 7 Oct 2024 14:10:19 -0400 Subject: [PATCH] Make IntoIter try_preserving_privacy param IntoIter is generic and allows a Database or BTreeMap to be used instead of the concrete HashMap type --- payjoin/src/receive/mod.rs | 15 +++++++++------ payjoin/src/receive/v2/mod.rs | 3 +-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/payjoin/src/receive/mod.rs b/payjoin/src/receive/mod.rs index 44ce7a31..c3bcdcd5 100644 --- a/payjoin/src/receive/mod.rs +++ b/payjoin/src/receive/mod.rs @@ -25,7 +25,6 @@ //! [reference implementation](https://github.com/payjoin/rust-payjoin/tree/master/payjoin-cli) use std::cmp::{max, min}; -use std::collections::HashMap; use bitcoin::base64::prelude::BASE64_STANDARD; use bitcoin::base64::Engine; @@ -508,9 +507,10 @@ impl WantsInputs { /// A simple consolidation is otherwise chosen if available. pub fn try_preserving_privacy( &self, - candidate_inputs: HashMap, + candidate_inputs: impl IntoIterator, ) -> Result { - if candidate_inputs.is_empty() { + let mut candidate_inputs = candidate_inputs.into_iter().peekable(); + if candidate_inputs.peek().is_none() { return Err(InternalSelectionError::Empty.into()); } @@ -534,7 +534,7 @@ impl WantsInputs { /// https://eprint.iacr.org/2022/589.pdf fn avoid_uih( &self, - candidate_inputs: HashMap, + candidate_inputs: impl IntoIterator, ) -> Result { let min_original_out_sats = self .payjoin_psbt @@ -572,9 +572,12 @@ impl WantsInputs { fn select_first_candidate( &self, - candidate_inputs: HashMap, + candidate_inputs: impl IntoIterator, ) -> Result { - candidate_inputs.values().next().cloned().ok_or(InternalSelectionError::NotFound.into()) + candidate_inputs + .into_iter() + .next() + .map_or(Err(InternalSelectionError::NotFound.into()), |(_, outpoint)| Ok(outpoint)) } /// Add the provided list of inputs to the transaction. diff --git a/payjoin/src/receive/v2/mod.rs b/payjoin/src/receive/v2/mod.rs index 91fdbe69..e717bdc2 100644 --- a/payjoin/src/receive/v2/mod.rs +++ b/payjoin/src/receive/v2/mod.rs @@ -1,4 +1,3 @@ -use std::collections::HashMap; use std::str::FromStr; use std::time::{Duration, SystemTime}; @@ -460,7 +459,7 @@ impl WantsInputs { /// https://eprint.iacr.org/2022/589.pdf pub fn try_preserving_privacy( &self, - candidate_inputs: HashMap, + candidate_inputs: impl IntoIterator, ) -> Result { self.inner.try_preserving_privacy(candidate_inputs) }