|
3 | 3 | //! APIs to expose as relevant typestates. |
4 | 4 |
|
5 | 5 | use std::cmp::{max, min}; |
| 6 | +use std::collections::HashSet; |
6 | 7 |
|
7 | 8 | use bitcoin::psbt::Psbt; |
8 | 9 | use bitcoin::secp256k1::rand::seq::SliceRandom; |
@@ -290,7 +291,13 @@ impl WantsInputs { |
290 | 291 | .map(|input| input.sequence) |
291 | 292 | .unwrap_or_default(); |
292 | 293 |
|
293 | | - let inputs = inputs.into_iter().collect::<Vec<_>>(); |
| 294 | + // Collect existing PSBT outpoints so duplicate inputs are filtered out. |
| 295 | + let mut seen_outpoints: HashSet<_> = |
| 296 | + self.payjoin_psbt.unsigned_tx.input.iter().map(|txin| txin.previous_output).collect(); |
| 297 | + let inputs: Vec<_> = inputs |
| 298 | + .into_iter() |
| 299 | + .filter(|input| seen_outpoints.insert(input.txin.previous_output)) |
| 300 | + .collect(); |
294 | 301 |
|
295 | 302 | // Insert contributions at random indices for privacy |
296 | 303 | let mut rng = rand::thread_rng(); |
@@ -652,15 +659,14 @@ mod tests { |
652 | 659 | let wants_inputs = wants_inputs.contribute_inputs(vec![input_pair_1.clone()]).unwrap(); |
653 | 660 | assert_eq!(wants_inputs.receiver_inputs.len(), 1); |
654 | 661 | assert_eq!(wants_inputs.receiver_inputs[0], input_pair_1); |
655 | | - // Contribute the same input again, and a new input. |
656 | | - // TODO: if we ever decide to fix contribute duplicate inputs, we need to update this test. |
| 662 | + // Contribute the same input again (should be filtered out) and a new input. |
657 | 663 | let wants_inputs = wants_inputs |
658 | 664 | .contribute_inputs(vec![input_pair_2.clone(), input_pair_1.clone()]) |
659 | 665 | .unwrap(); |
660 | | - assert_eq!(wants_inputs.receiver_inputs.len(), 3); |
| 666 | + // Only input_pair_2 should be added input_pair_1 is a duplicate and should be filtered out hence the length is 2. |
| 667 | + assert_eq!(wants_inputs.receiver_inputs.len(), 2); |
661 | 668 | assert_eq!(wants_inputs.receiver_inputs[0], input_pair_1); |
662 | 669 | assert_eq!(wants_inputs.receiver_inputs[1], input_pair_2); |
663 | | - assert_eq!(wants_inputs.receiver_inputs[2], input_pair_1); |
664 | 670 | } |
665 | 671 |
|
666 | 672 | #[test] |
|
0 commit comments