Skip to content

Commit

Permalink
Insert additional inputs at random index
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Mar 1, 2023
1 parent 9355584 commit f29c5ec
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions payjoin/src/receiver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use error::{InternalRequestError, InternalSelectionError};
pub use error::{RequestError, SelectionError};
use optional_parameters::Params;
use rand::seq::SliceRandom;
use rand::Rng;

use crate::fee_rate::FeeRate;
use crate::psbt::Psbt;
Expand Down Expand Up @@ -381,14 +382,20 @@ impl PayjoinProposal {
self.owned_vouts.choose(&mut rand::thread_rng()).expect("owned_vouts is empty");
self.psbt.unsigned_tx.output[*vout_to_augment].value += txo_value;

// Insert contribution at random index for privacy
let mut rng = rand::thread_rng();
let index = rng.gen_range(0..=self.psbt.unsigned_tx.input.len());
self.psbt
.inputs
.push(bitcoin::psbt::Input { witness_utxo: Some(txo), ..Default::default() });
self.psbt.unsigned_tx.input.push(bitcoin::TxIn {
previous_output: outpoint,
sequence: original_sequence,
..Default::default()
});
self.psbt.unsigned_tx.input.insert(
index,
bitcoin::TxIn {
previous_output: outpoint,
sequence: original_sequence,
..Default::default()
},
);
}

pub fn contribute_non_witness_input(&mut self, tx: bitcoin::Transaction, outpoint: OutPoint) {
Expand All @@ -402,15 +409,22 @@ impl PayjoinProposal {
self.owned_vouts.choose(&mut rand::thread_rng()).expect("owned_vouts is empty");
self.psbt.unsigned_tx.output[*vout_to_augment].value += txo_value;

// Insert contribution at random index for privacy
let mut rng = rand::thread_rng();
let index = rng.gen_range(0..=self.psbt.unsigned_tx.input.len());

// Add the new input to the PSBT
self.psbt
.inputs
.push(bitcoin::psbt::Input { non_witness_utxo: Some(tx), ..Default::default() });
self.psbt.unsigned_tx.input.push(bitcoin::TxIn {
previous_output: outpoint,
sequence: original_sequence,
..Default::default()
});
self.psbt.unsigned_tx.input.insert(
index,
bitcoin::TxIn {
previous_output: outpoint,
sequence: original_sequence,
..Default::default()
},
);
}

/// Just replace an output address with
Expand Down

0 comments on commit f29c5ec

Please sign in to comment.