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

Fix check outputs #338

Merged
merged 2 commits into from
Aug 12, 2024
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
28 changes: 25 additions & 3 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ impl ContextV1 {
}

fn check_outputs(&self, proposal: &Psbt) -> InternalResult<OutputStats> {
let mut original_outputs = proposal.unsigned_tx.output.iter().enumerate().peekable();
let mut original_outputs =
self.original_psbt.unsigned_tx.output.iter().enumerate().peekable();
let mut total_value = bitcoin::Amount::ZERO;
let mut contributed_fee = bitcoin::Amount::ZERO;
let mut total_weight = Weight::ZERO;
Expand All @@ -821,7 +822,7 @@ impl ContextV1 {
{
if proposed_txout.value < original_output.value {
contributed_fee = original_output.value - proposed_txout.value;
ensure!(contributed_fee < max_fee_contrib, FeeContributionExceedsMaximum);
ensure!(contributed_fee <= max_fee_contrib, FeeContributionExceedsMaximum);
//The remaining fee checks are done in the caller
}
original_outputs.next();
Expand Down Expand Up @@ -1058,7 +1059,7 @@ mod test {
let ctx = super::ContextV1 {
original_psbt,
disable_output_substitution: false,
fee_contribution: None,
fee_contribution: Some((bitcoin::Amount::from_sat(182), 0)),
min_fee_rate: FeeRate::ZERO,
payee,
input_type: InputType::SegWitV0 { ty: SegWitV0Type::Pubkey, nested: true },
Expand All @@ -1084,6 +1085,27 @@ mod test {
ctx.process_proposal(proposal).unwrap();
}

#[test]
#[should_panic]
fn test_receiver_steals_sender_change() {
let original_psbt = Psbt::from_str(ORIGINAL_PSBT).unwrap();
eprintln!("original: {:#?}", original_psbt);
let ctx = create_v1_context();
let mut proposal = Psbt::from_str(PAYJOIN_PROPOSAL).unwrap();
eprintln!("proposal: {:#?}", proposal);
for output in proposal.outputs_mut() {
output.bip32_derivation.clear();
}
for input in proposal.inputs_mut() {
input.bip32_derivation.clear();
}
proposal.inputs_mut()[0].witness_utxo = None;
// Steal 0.5 BTC from the sender output and add it to the receiver output
proposal.unsigned_tx.output[0].value -= bitcoin::Amount::from_btc(0.5).unwrap();
proposal.unsigned_tx.output[1].value += bitcoin::Amount::from_btc(0.5).unwrap();
ctx.process_proposal(proposal).unwrap();
}

#[test]
#[cfg(feature = "v2")]
fn req_ctx_ser_de_roundtrip() {
Expand Down
Loading