Skip to content

fix(bitcoin-psbt): Validate UTXO types for witness and non-witness scripts - #4749

Open
sergei-boiko-trustwallet wants to merge 2 commits into
masterfrom
improve/bitcoin-psbt-witness
Open

fix(bitcoin-psbt): Validate UTXO types for witness and non-witness scripts#4749
sergei-boiko-trustwallet wants to merge 2 commits into
masterfrom
improve/bitcoin-psbt-witness

Conversation

@sergei-boiko-trustwallet

Copy link
Copy Markdown
Contributor

This pull request strengthens the validation logic for PSBT (Partially Signed Bitcoin Transactions) input types by enforcing stricter rules on which script types are allowed for witness and non-witness UTXOs. It also adds comprehensive tests to ensure these rules are correctly enforced and that invalid PSBTs are rejected with appropriate errors.

Validation logic improvements:

  • Updated UtxoPsbt methods to require that only P2WPKH script types are allowed for witness UTXOs, and only P2PK/P2PKH script types are allowed for non-witness UTXOs. Attempting to use an invalid script type for a given UTXO type now results in a SigningError::Error_invalid_utxo error.

Testing enhancements:

  • Added new tests in psbt.rs to verify that:
    • A witness UTXO with an unexpected script type (e.g., P2PK or P2PKH) is rejected.
    • A non-witness UTXO with a P2WPKH script is also rejected.

These changes ensure the PSBT signing process is more robust and compliant with expected Bitcoin transaction standards.

Copilot AI 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.

Pull request overview

This PR tightens PSBT input validation in the Bitcoin signing flow by rejecting mismatches between the PSBT UTXO container type (witness_utxo vs non_witness_utxo) and the script type, and adds regression tests to ensure invalid PSBTs fail with Error_invalid_utxo.

Changes:

  • Enforce witness_utxo vs non_witness_utxo script-type constraints in UtxoPsbt.
  • Add tests covering invalid script types for witness and non-witness UTXOs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
rust/tw_tests/tests/chains/bitcoin/bitcoin_sign/psbt.rs Adds negative tests ensuring mismatched UTXO/script types are rejected.
rust/chains/tw_bitcoin/src/modules/psbt_request/utxo_psbt.rs Introduces script-type checks keyed off whether the PSBT input uses witness vs non-witness UTXO data.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rust/chains/tw_bitcoin/src/modules/psbt_request/utxo_psbt.rs
Comment thread rust/tw_tests/tests/chains/bitcoin/bitcoin_sign/psbt.rs Outdated
@github-actions

github-actions Bot commented Apr 28, 2026

Copy link
Copy Markdown

Binary size comparison

➡️ aarch64-apple-ios:

- 14.37 MB
+ 14.37 MB 	 +1 KB

➡️ aarch64-apple-ios-sim:

- 14.38 MB
+ 14.38 MB 	 +1 KB

➡️ aarch64-linux-android:

- 18.81 MB
+ 18.82 MB 	 +1 KB

➡️ armv7-linux-androideabi:

- 16.24 MB
+ 16.24 MB 	 +1 KB

➡️ wasm32-unknown-emscripten:

- 13.73 MB
+ 13.73 MB 	 +1 KB

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

@PandaKitten96 PandaKitten96 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It looks good

@PandaKitten96 PandaKitten96 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this works nicely! :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants