Skip to content

fix/track self payment outputs - #642

Open
henrypeters wants to merge 2 commits into
bitcoindevkit:masterfrom
henrypeters:fix/track-self-payment-outputs
Open

henrypeters wants to merge 2 commits into
bitcoindevkit:masterfrom
henrypeters:fix/track-self-payment-outputs

Conversation

@henrypeters

@henrypeters henrypeters commented Nov 9, 2026 •

Copy link
Copy Markdown

Summary

build_fee_bump identifies change outputs by checking if they belong to the wallet's internal keychain, then strips them before building the replacement transaction. When a user intentionally sends to their own address (a self-payment) and later fee-bumps, build_fee_bump cannot distinguish the self-payment recipient from auto-generated change, so it strips it, silently losing the recipient from the replacement transaction.

Fix

I fixed this solutions:

  • Warning at transaction creation time: create_tx now checks each recipient against the wallet's known script pubkeys. If a match is found, it returns CreateTxError::PaymentToOwnAddress { index } instead of silently proceeding with the transaction. The user must call .allow_self_payment() on the TxBuilder to opt in.

  • Tracking for build_fee_bump: When .allow_self_payment() is used, create_tx records the output indices of self-payment recipients in a new ChangeSet field:

              pub self_payment_recipients: BTreeMap<Txid, Option<BTreeSet<u32>>>
    

build_fee_bump checks this map before removing change from the transaction, tracked outputs are preserved, while untracked outputs is then seen as change and therefore removed.
Also, Entries are removed when transactions are confirmed(apply_update) or evicted(apply_evicted_txs), keeping storage minimal. Only unconfirmed self-payment transactions are tracked.

Testing

Existing tests updated: All existing tests that used wallet-owned addresses as dummy recipients have been updated to call .allow_self_payment() where the self-payment is incidental to what the test is actually testing, or switched to external addresses where appropriate.
New dedicated tests in tests/self_payment.rs:

  • test_payment_to_own_address_is_rejected
  • test_payment_to_own_address_allowed_with_flag
  • test_self_payment_tracked_in_changeset
  • test_build_fee_bump_preserves_self_payment_recipient

@henrypeters
henrypeters requested a review from a team as a code owner November 9, 2026 12:16
@github-project-automation github-project-automation Bot moved this to Triage in BDK Wallet Nov 9, 2026
@henrypeters henrypeters changed the title Fix/track self payment outputs fix/track self payment outputs Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant