Skip to content

Commit

Permalink
just pass a list of outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebear21 committed Jul 30, 2024
1 parent c8b703a commit 2a58510
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
5 changes: 2 additions & 3 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl App {
})?;
log::trace!("check4");

let mut provisional_payjoin = payjoin.identify_receiver_outputs(|output_script| {
let provisional_payjoin = payjoin.identify_receiver_outputs(|output_script| {
if let Ok(address) = bitcoin::Address::from_script(output_script, network) {
bitcoind
.get_address_info(&address)
Expand All @@ -334,8 +334,7 @@ impl App {
}
})?;

let mut provisional_payjoin = provisional_payjoin
.try_substitute_receiver_outputs(None::<fn() -> Result<Vec<bitcoin::TxOut>, Error>>)?;
let mut provisional_payjoin = provisional_payjoin.try_substitute_receiver_outputs(None)?;

_ = try_contributing_inputs(&mut provisional_payjoin.inner, &bitcoind)
.map_err(|e| log::warn!("Failed to contribute inputs: {}", e));
Expand Down
16 changes: 7 additions & 9 deletions payjoin/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,21 @@ impl WantsOutputs {
generate_script: impl Fn() -> Result<bitcoin::ScriptBuf, Error>,
) -> Result<WantsInputs, Error> {
let output_value = self.payjoin_psbt.unsigned_tx.output[self.owned_vouts[0]].value;
let generate_outputs =
|| Ok(vec![TxOut { value: output_value, script_pubkey: generate_script()? }]);
self.try_substitute_receiver_outputs(Some(generate_outputs))
let outputs = vec![TxOut { value: output_value, script_pubkey: generate_script()? }];
self.try_substitute_receiver_outputs(Some(outputs))
}

pub fn try_substitute_receiver_outputs(
self,
generate_outputs: Option<impl Fn() -> Result<Vec<TxOut>, Error>>, // TODO: this sucks when
// passing None
outputs: Option<Vec<TxOut>>,
) -> Result<WantsInputs, Error> {
let mut payjoin_psbt = self.payjoin_psbt.clone();
match generate_outputs {
Some(gen_outputs) => {
match outputs {
Some(o) => {
if self.params.disable_output_substitution {
return Err(Error::Server("Output substitution is disabled.".into()));
}
let mut replacement_outputs = gen_outputs()?.into_iter();
let mut replacement_outputs = o.into_iter();
let mut outputs = vec![];
for (i, output) in self.payjoin_psbt.unsigned_tx.output.iter().enumerate() {
if self.owned_vouts.contains(&i) {
Expand Down Expand Up @@ -790,7 +788,7 @@ mod test {
.require_network(network))
})
.expect("Receiver output should be identified")
.try_substitute_receiver_outputs(None::<fn() -> Result<Vec<TxOut>, Error>>)
.try_substitute_receiver_outputs(None)
.expect("Substitute outputs should do nothing")
.provisional_proposal();
let payjoin = payjoin.apply_fee(None);
Expand Down
2 changes: 1 addition & 1 deletion payjoin/src/receive/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl WantsOutputs {

pub fn try_substitute_receiver_outputs(
self,
generate_outputs: Option<impl Fn() -> Result<Vec<TxOut>, Error>>,
generate_outputs: Option<Vec<TxOut>>,
) -> Result<WantsInputs, Error> {
let inner = self.inner.try_substitute_receiver_outputs(generate_outputs)?;
Ok(WantsInputs { inner, context: self.context })
Expand Down
7 changes: 1 addition & 6 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,12 +757,7 @@ mod integration {
.expect("Receiver should have at least one output");

let mut payjoin = payjoin
// TODO:
// a- `None` approach is a pita because type inference doesn't work
// b- hard to substitute outputs when output value from payjoin_psbt is not accessible
.try_substitute_receiver_outputs(
None::<fn() -> Result<Vec<bitcoin::TxOut>, payjoin::Error>>,
)
.try_substitute_receiver_outputs(None)
.expect("Could not substitute outputs");

// Select receiver payjoin inputs. TODO Lock them.
Expand Down

0 comments on commit 2a58510

Please sign in to comment.