Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert additional inputs at random index #40

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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