Skip to content

Fix segwit transaction parsing in PSBT#5684

Closed
mpbagot wants to merge 1 commit into
rust-bitcoin:masterfrom
mpbagot:feature/psbt-segwit-fix
Closed

Fix segwit transaction parsing in PSBT#5684
mpbagot wants to merge 1 commit into
rust-bitcoin:masterfrom
mpbagot:feature/psbt-segwit-fix

Conversation

@mpbagot

@mpbagot mpbagot commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

In PSBT, the unsigned transaction must always be encoded in a non-SegWit format (see BIP-174). Serialised PSBT transactions with a segwit encoded unsigned tx should be rejected as invalid, as demonstrated by invalid PSBT test vector 19 from BIP-174.

Adjust PSBT global map parsing to reject segwit unsigned transactions.
Introduce a PSBT error variant for segwit unsigned transactions.
Add test vector 19 to psbt module tests.

In PSBT, the unsigned transaction must always be encoded in a non-SegWit
format. Serialised PSBT transactions with a segwit encoded unsigned tx
should be rejected as invalid, as demonstrated by invalid PSBT test
vector 19 from BIP-174.

Adjust PSBT global map parsing to reject segwit unsigned transactions.
Introduce a PSBT error variant for segwit unsigned transactions.
Add test vector 19 to psbt module tests.
@github-actions github-actions Bot added the C-bitcoin PRs modifying the bitcoin crate label Feb 17, 2026
@mpbagot mpbagot marked this pull request as ready for review February 17, 2026 17:50
@tcharding

Copy link
Copy Markdown
Member

I haven't reviewed yet but I wonder if this should be done against rust-psbt instead? Are we ready to metaphorically slap a deprecated sign on the psbt module @nyonson and take full responsibility over in rust-psbt repo?

@nyonson

nyonson commented Feb 18, 2026

Copy link
Copy Markdown
Contributor

I haven't reviewed yet but I wonder if this should be done against rust-psbt instead? Are we ready to metaphorically slap a deprecated sign on the psbt module @nyonson and take full responsibility over in rust-psbt repo?

Sounds good to me. Maybe the metaphorical slap could be removing the line indicating PSBT support from the top of the root README?

@tcharding tcharding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 040d44e

@tcharding

Copy link
Copy Markdown
Member

To discuss the dev path of PSBT I wrote this: #5700

// serialization (0x0001 after the version). Bytes there
// could mean 0 inputs + 1 output or segwit. Since we already
// decoded 0 inputs successfully we can assume a failure to
// decode outputs is actually segwit serialisation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In 040d44e

I don't buy this assumption. I feel this just makes the error reporting more convoluted and confusing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does the unit test you provided actually fail without this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does the unit test you provided actually fail without this change?

Not exactly. It fails to parse either way, but the error before is due to an out of range output amount value. I could try and put together another test case that parses successfully but uses segwit if you'd prefer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, there will not exist any test case that passes with the old code and fails with the new, because you haven't changed any error paths, just the way that the error is constructed.

I wouldn't mind expanding the output-parsing error (and the locktime-parsing error) to say something like "this might be caused by putting a segwit-encoded transaction into a PSBT". But we can't assume this is the case, and it seems kinda unlikely to me given that there shouldn't be any software out there that produces PSBTs like this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Woops. Sorry. It's been a bit since I actually looked over this.

If a segwit encoded transaction parses as valid, then we can't discount it because 0-in 1-out is a valid transaction format, and as you said a failure to parse is equally likely to be something like a bad amount value in an output as it is to be an invalid encoding for the whole transaction.

Should I just close this then? Or is adding to the docs on the error/function to clarify the (slim) possibility of an unexpected segwit tx encoding an acceptable middle-ground?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would suggest closing this.

@mpbagot mpbagot closed this Mar 3, 2026
@mpbagot

mpbagot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Closed because it's an incorrect solution to a largely non-existent issue. See the discussion above: #5684 (comment)

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

Labels

C-bitcoin PRs modifying the bitcoin crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants