Skip to content

Commit

Permalink
Merge pull request payjoin#40 from DanGould/rand-input-insert
Browse files Browse the repository at this point in the history
Insert additional inputs at random index
  • Loading branch information
DanGould authored Mar 12, 2023
2 parents 9355584 + 3061dbb commit 39d5719
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 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()
});
.insert(index, bitcoin::psbt::Input { witness_utxo: Some(txo), ..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,23 @@ 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.inputs.insert(
index,
bitcoin::psbt::Input { non_witness_utxo: Some(tx), ..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 39d5719

Please sign in to comment.