Skip to content

Commit

Permalink
Allow any script instead of just known address types
Browse files Browse the repository at this point in the history
  • Loading branch information
spacebear21 committed Jun 26, 2024
1 parent f06bb1c commit 4bc8fe3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
9 changes: 5 additions & 4 deletions payjoin-cli/src/app/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,15 @@ impl App {
.map_err(|e| log::warn!("Failed to contribute inputs: {}", e));

_ = provisional_payjoin
.try_substituting_output_address(|| {
bitcoind
.try_substitute_receiver_output(|| {
Ok(bitcoind
.get_new_address(None, None)
.map_err(|e| Error::Server(e.into()))?
.require_network(network)
.map_err(|e| Error::Server(e.into()))
.map_err(|e| Error::Server(e.into()))?
.script_pubkey())
})
.map_err(|e| log::warn!("Failed to substitute output address: {}", e));
.map_err(|e| log::warn!("Failed to substitute output: {}", e));

let payjoin_proposal = provisional_payjoin.finalize_proposal(
|psbt: &Psbt| {
Expand Down
9 changes: 5 additions & 4 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,15 @@ impl App {
.map_err(|e| log::warn!("Failed to contribute inputs: {}", e));

_ = provisional_payjoin
.try_substituting_output_address(|| {
bitcoind
.try_substitute_receiver_output(|| {
Ok(bitcoind
.get_new_address(None, None)
.map_err(|e| Error::Server(e.into()))?
.require_network(network)
.map_err(|e| Error::Server(e.into()))
.map_err(|e| Error::Server(e.into()))?
.script_pubkey())
})
.map_err(|e| log::warn!("Failed to substitute output address: {}", e));
.map_err(|e| log::warn!("Failed to substitute output: {}", e));

let payjoin_proposal = provisional_payjoin.finalize_proposal(
|psbt: &Psbt| {
Expand Down
11 changes: 5 additions & 6 deletions payjoin/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,16 @@ impl ProvisionalProposal {
self.params.disable_output_substitution
}

/// If output substitution is enabled, replace the receiver's output address with a new one.
pub fn try_substituting_output_address(
/// If output substitution is enabled, replace the receiver's output script with a new one.
pub fn try_substitute_receiver_output(
&mut self,
generate_address: impl Fn() -> Result<bitcoin::Address, Error>,
generate_script: impl Fn() -> Result<bitcoin::ScriptBuf, Error>,
) -> Result<(), Error> {
if self.params.disable_output_substitution {
return Err(Error::Server("Output substitution is disabled.".into()));
}
let substitute_address = generate_address()?;
self.payjoin_psbt.unsigned_tx.output[self.owned_vouts[0]].script_pubkey =
substitute_address.script_pubkey();
let substitute_script = generate_script()?;
self.payjoin_psbt.unsigned_tx.output[self.owned_vouts[0]].script_pubkey = substitute_script;
Ok(())
}

Expand Down
8 changes: 4 additions & 4 deletions payjoin/src/receive/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ impl ProvisionalProposal {
self.inner.is_output_substitution_disabled()
}

/// If output substitution is enabled, replace the receiver's output address with a new one.
pub fn try_substituting_output_address(
/// If output substitution is enabled, replace the receiver's output script with a new one.
pub fn try_substitute_receiver_output(
&mut self,
generate_address: impl Fn() -> Result<bitcoin::Address, Error>,
generate_script: impl Fn() -> Result<bitcoin::ScriptBuf, Error>,
) -> Result<(), Error> {
self.inner.try_substituting_output_address(generate_address)
self.inner.try_substitute_receiver_output(generate_script)
}

pub fn finalize_proposal(
Expand Down
8 changes: 4 additions & 4 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ mod integration {
bitcoin::OutPoint { txid: selected_utxo.txid, vout: selected_utxo.vout };
payjoin.contribute_witness_input(txo_to_contribute, outpoint_to_contribute);

_ = payjoin.try_substituting_output_address(|| {
Ok(receiver.get_new_address(None, None).unwrap().assume_checked())
_ = payjoin.try_substitute_receiver_output(|| {
Ok(receiver.get_new_address(None, None).unwrap().assume_checked().script_pubkey())
});
let payjoin_proposal = payjoin
.finalize_proposal(
Expand Down Expand Up @@ -699,8 +699,8 @@ mod integration {
bitcoin::OutPoint { txid: selected_utxo.txid, vout: selected_utxo.vout };
payjoin.contribute_witness_input(txo_to_contribute, outpoint_to_contribute);

_ = payjoin.try_substituting_output_address(|| {
Ok(receiver.get_new_address(None, None).unwrap().assume_checked())
_ = payjoin.try_substitute_receiver_output(|| {
Ok(receiver.get_new_address(None, None).unwrap().assume_checked().script_pubkey())
});
let payjoin_proposal = payjoin
.finalize_proposal(
Expand Down

0 comments on commit 4bc8fe3

Please sign in to comment.