Skip to content

Commit

Permalink
Make IntoIter try_preserving_privacy param
Browse files Browse the repository at this point in the history
IntoIter is generic and allows a Database or BTreeMap to be used
instead of the concrete HashMap type
  • Loading branch information
DanGould committed Oct 7, 2024
1 parent 1d7feb1 commit 5866dc7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 9 additions & 6 deletions payjoin/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -508,9 +507,10 @@ impl WantsInputs {
/// A simple consolidation is otherwise chosen if available.
pub fn try_preserving_privacy(
&self,
candidate_inputs: HashMap<Amount, OutPoint>,
candidate_inputs: impl IntoIterator<Item = (Amount, OutPoint)>,
) -> Result<OutPoint, SelectionError> {
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());
}

Expand All @@ -534,7 +534,7 @@ impl WantsInputs {
/// https://eprint.iacr.org/2022/589.pdf
fn avoid_uih(
&self,
candidate_inputs: HashMap<Amount, OutPoint>,
candidate_inputs: impl IntoIterator<Item = (Amount, OutPoint)>,
) -> Result<OutPoint, SelectionError> {
let min_original_out_sats = self
.payjoin_psbt
Expand Down Expand Up @@ -572,9 +572,12 @@ impl WantsInputs {

fn select_first_candidate(
&self,
candidate_inputs: HashMap<Amount, OutPoint>,
candidate_inputs: impl IntoIterator<Item = (Amount, OutPoint)>,
) -> Result<OutPoint, SelectionError> {
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.
Expand Down
3 changes: 1 addition & 2 deletions payjoin/src/receive/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::str::FromStr;
use std::time::{Duration, SystemTime};

Expand Down Expand Up @@ -460,7 +459,7 @@ impl WantsInputs {
/// https://eprint.iacr.org/2022/589.pdf
pub fn try_preserving_privacy(
&self,
candidate_inputs: HashMap<Amount, OutPoint>,
candidate_inputs: impl IntoIterator<Item = (Amount, OutPoint)>,
) -> Result<OutPoint, SelectionError> {
self.inner.try_preserving_privacy(candidate_inputs)
}
Expand Down

0 comments on commit 5866dc7

Please sign in to comment.