Skip to content

Reject psbts whose outputs exceed their inputs - #1041

Merged
newtonick merged 4 commits into
SeedSigner:devfrom
Chaitanya-Keyal:psbt-negative-fee
Oct 22, 2026
Merged

newtonick merged 4 commits into
SeedSigner:devfrom
Chaitanya-Keyal:psbt-negative-fee

Conversation

@Chaitanya-Keyal

@Chaitanya-Keyal Chaitanya-Keyal commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Problem or Issue being addressed

PSBTParser.fee_amount is embit's psbt.fee(), which is just inputs minus outputs

Nothing looks at the sign of that result. A psbt whose outputs spend more than its inputs bring in therefore parses cleanly and walks into the review flow quoting a negative fee on the overview and math screens.

No honest coordinator builds such a transaction and the network would refuse to relay it, so the exposure is misleading arithmetic on screen rather than funds at risk. It still belongs behind a purposeful refusal rather than a nonsensical fee display.

Found by @knorrium in #1032 (comment)

Solution

  • PSBTNegativeFeeError, a PSBTVerificationError subclass raised at the end of _parse_outputs as soon as the fee is known. Framed as a malformed transaction rather than an attack.
  • PSBTNegativeFeeView, a WarningScreen titled "Transaction Problem" that discards the psbt to the main menu, routed from PSBTOverviewView alongside the other PSBTVerificationError subclasses.
  • A zero fee is still accepted. It is a pointless transaction, but not a malformed one, so only a strictly negative fee is rejected.

Screenshots

PSBTNegativeFeeView

This pull request is categorized as a:

  • Bug fix (?)

Checklist

I ran pytest locally

  • All tests passed before submitting the PR

I included screenshots of any new or modified screens

  • Yes

I added or updated tests

  • Yes

I tested this PR hands-on on the following platform(s):

Tested with the negative-fee scenario on testqrs.com, added in kdmukAI-bot/btc-datagen#1.


I have reviewed these notes:

  • Keep your changes limited in scope.
  • If you uncover other issues or improvements along the way, ideally submit those as a separate PR.
  • The more complicated the PR, the harder it is to review, test, and merge.
  • We appreciate your efforts, but we're a small team of volunteers so PR review can be a very slow process.
  • Please only "@" mention a contributor if their input is truly needed to enable further progress.
  • I understand

Comment thread tests/test_psbt_parser.py Resolved
@newtonick

Copy link
Copy Markdown
Collaborator

ACK and reviewed 3952d1d

@PROWLERx15 PROWLERx15 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK ad09655a

Tested this locally on the emulator and verified that it behaves as expected.

In addition to the default scenario provided via negative-fee scenario on testqrs.com, I also tested the PR across different wallet script types. To do this, I used Keith's btc-datagen repository to generate malformed PSBTs locally with some code changes.

I constructed the test transactions using random outputs, tested them, and confirmed that the psbt parser correctly intercepted the malformed transaction for the following wallet types:

Wallet Type Input Output Fees
P2WPKH (Single-Sig Native Segwit) 300,000 sats 2,727,106 sats -2,427,106 sats
P2TR (Single-Sig Taproot) 300,000 sats 2,463,137 sats -2,163,137 sats
P2WSH (2-of-3 Multisig Native Segwit) 300,000 sats 393,246 sats -93,246 sats

@newtonick

Copy link
Copy Markdown
Collaborator

re-ACK 9124a29

embit computes the fee as inputs minus outputs and never looks at the sign of
the result, so a psbt that spends more than it funds parses cleanly and reaches
the review flow quoting a negative fee.

Raise PSBTNegativeFeeError as soon as the fee is known and route it to its own
warning. Malformed rather than hostile: no honest coordinator builds one and
the network would refuse to relay it, so it gets a plain warning, not the
attack framing. A zero fee is still accepted.
The error's docstring argued about a lying coordinator's motives and why
the error exists for the user. Like the other PSBTVerificationError
subclasses, it now just states the problem and that it is not treated as
an attack. The one part of the reasoning that matters, that both totals
are the coordinator's own claims, moves to the check that raises it.
_verify_input_amounts() now checks every input's amount before the outputs
are parsed, so the input total is no longer just the coordinator's claim.
@Chaitanya-Keyal

Copy link
Copy Markdown
Contributor Author

Rebased onto dev after #995 and resolved conflicts. Also added 9994a04: the comment above the negative-fee check no longer says both totals are the coordinator's claims, since _verify_input_amounts() now checks the input amounts first.

@newtonick

Copy link
Copy Markdown
Collaborator

re-ACK 9994a04

@newtonick
newtonick merged commit ebc1e11 into SeedSigner:dev Oct 22, 2026
2 checks passed
@newtonick newtonick moved this from 0.8.8 Needs Review to 0.8.8 Merged in @SeedSigner Development Board Oct 22, 2026
@Chaitanya-Keyal
Chaitanya-Keyal deleted the psbt-negative-fee branch October 22, 2026 17:10
@jdlcdl

jdlcdl commented Nov 5, 2026

Copy link
Copy Markdown

Regarding proposed embit pr152

I have tested seedsigner as of Nov 1 (last merge pr1047), using pr152 atop embit 0.8.2, instead of embit 0.8.0.

All tests for this pr at testqrs dot com still behave exactly as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 0.8.8 Merged

Development

Successfully merging this pull request may close these issues.

4 participants