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

Refactor replacement outputs to be an Iterator of ref TxOuts #529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions payjoin/src/receive/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,25 @@ impl WantsOutputs {
pub fn substitute_receiver_script(
self,
output_script: &Script,
) -> Result<WantsOutputs, OutputSubstitutionError> {
) -> Result<Self, OutputSubstitutionError> {
let output_value = self.original_psbt.unsigned_tx.output[self.change_vout].value;
let outputs = vec![TxOut { value: output_value, script_pubkey: output_script.into() }];
self.replace_receiver_outputs(outputs, output_script)
let outputs = [TxOut { value: output_value, script_pubkey: output_script.into() }];
self.replace_receiver_outputs(outputs.iter(), output_script)
}

/// Replace **all** receiver outputs with one or more provided outputs.
/// The drain script specifies which address to *drain* coins to. An output corresponding to
/// that address must be included in `replacement_outputs`. The value of that output may be
/// increased or decreased depending on the receiver's input contributions and whether the
/// receiver needs to pay for additional miner fees (e.g. in the case of adding many outputs).
pub fn replace_receiver_outputs(
pub fn replace_receiver_outputs<'a>(
self,
replacement_outputs: Vec<TxOut>,
replacement_outputs: impl Iterator<Item = &'a TxOut>,
drain_script: &Script,
) -> Result<WantsOutputs, OutputSubstitutionError> {
) -> Result<Self, OutputSubstitutionError> {
let mut payjoin_psbt = self.original_psbt.clone();
let mut outputs = vec![];
let mut replacement_outputs = replacement_outputs.clone();
let mut replacement_outputs: Vec<TxOut> = replacement_outputs.cloned().collect();
let mut rng = rand::thread_rng();
// Substitute the existing receiver outputs, keeping the sender/receiver output ordering
for (i, original_output) in self.original_psbt.unsigned_tx.output.iter().enumerate() {
Expand Down Expand Up @@ -343,7 +343,7 @@ impl WantsOutputs {
// Update the payjoin PSBT outputs
payjoin_psbt.outputs = vec![Default::default(); outputs.len()];
payjoin_psbt.unsigned_tx.output = outputs;
Ok(WantsOutputs {
Ok(Self {
original_psbt: self.original_psbt,
payjoin_psbt,
params: self.params,
Expand Down
12 changes: 6 additions & 6 deletions payjoin/src/receive/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,23 +373,23 @@ impl WantsOutputs {
pub fn substitute_receiver_script(
self,
output_script: &Script,
) -> Result<WantsOutputs, OutputSubstitutionError> {
) -> Result<Self, OutputSubstitutionError> {
let inner = self.v1.substitute_receiver_script(output_script)?;
Ok(WantsOutputs { v1: inner, context: self.context })
Ok(Self { v1: inner, context: self.context })
}

/// Replace **all** receiver outputs with one or more provided outputs.
/// The drain script specifies which address to *drain* coins to. An output corresponding to
/// that address must be included in `replacement_outputs`. The value of that output may be
/// increased or decreased depending on the receiver's input contributions and whether the
/// receiver needs to pay for additional miner fees (e.g. in the case of adding many outputs).
pub fn replace_receiver_outputs(
pub fn replace_receiver_outputs<'a>(
self,
replacement_outputs: Vec<TxOut>,
replacement_outputs: impl Iterator<Item = &'a TxOut>,
drain_script: &Script,
) -> Result<WantsOutputs, OutputSubstitutionError> {
) -> Result<Self, OutputSubstitutionError> {
let inner = self.v1.replace_receiver_outputs(replacement_outputs, drain_script)?;
Ok(WantsOutputs { v1: inner, context: self.context })
Ok(Self { v1: inner, context: self.context })
}

/// Proceed to the input contribution step.
Expand Down
2 changes: 1 addition & 1 deletion payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ mod integration {

let payjoin = match custom_outputs {
Some(txos) => payjoin.replace_receiver_outputs(
txos,
txos.iter(),
drain_script.expect("drain_script should be provided with custom_outputs"),
)?,
None => payjoin.substitute_receiver_script(
Expand Down