Skip to content

Conversation

@DashCoreAutoGuix
Copy link
Owner

@DashCoreAutoGuix DashCoreAutoGuix commented Dec 6, 2025

Summary

Backport of bitcoin#25595

This PR adds proper PSBT input verification by calling VerifyScript on finalized inputs rather than just checking for non-empty fields. This ensures that signatures are actually valid before proceeding.

Changes

  • Added PSBTInputSignedAndVerified() function that performs script verification
  • Updated SignPSBTInput() to use the new verification function
  • Updated AnalyzePSBT() to use the new verification function

Dash Adaptations

Original commit: 2ac71d2

Test Plan

  • Build succeeds
  • Existing PSBT tests pass (requires dash_hash module)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced PSBT finalization to include comprehensive signature verification against precomputed transaction data, ensuring stricter validation of signed inputs before finalization.

✏️ Tip: You can customize this high-level summary in your review settings.

…being empty

Backport of bitcoin#25595

Original commit: 2ac71d2

Adapted for Dash:
- Removed witness_utxo check (Dash does not support SegWit)
- Removed final_script_witness from PSBTInputSigned (not in Dash)
- Adapted VerifyScript calls for Dash API (no witness parameter)
- Test changes omitted: requires test_framework/psbt.py from bitcoin#25625
@coderabbitai
Copy link

coderabbitai bot commented Dec 6, 2025

Walkthrough

The PR introduces a new verification function PSBTInputSignedAndVerified that validates PSBT input signatures against non-witness UTXOs and precomputed transaction data. This function replaces simple signed checks in finalization logic, adding thorough verification of scriptSig against scriptPubKey using VerifyScript.

Changes

Cohort / File(s) Summary
PSBT Verification API
src/psbt.h, src/psbt.cpp
Added public function PSBTInputSignedAndVerified that validates PSBT input signatures by verifying non-witness UTXO presence, prevout matching, and scriptSig against scriptPubKey with optional PrecomputedTransactionData. Updated comment for existing PSBTInputSigned function to clarify it checks for non-null finalized fields.
Finalization Logic Update
src/node/psbt.cpp
Replaced PSBTInputSigned(input) check with PSBTInputSignedAndVerified(psbtx, i, &txdata) in finalization condition, introducing verification against precomputed data when determining input finality.
Test Formatting
test/functional/rpc_psbt.py
Whitespace-only changes; inserted blank lines around end-of-file main guard and after assertion.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • VerifyScript integration: Examine how PrecomputedTransactionData is passed and utilized in the new verification function to ensure correct signature validation.
  • Control flow changes: Verify that replacing PSBTInputSigned with PSBTInputSignedAndVerified in finalization logic maintains expected behavior and doesn't inadvertently change signing flow or error handling.
  • UTXO validation logic: Confirm that non-witness UTXO presence checks and prevout matching correctly handle missing or mismatched data scenarios.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: replacing simple field checks with proper verification of PSBT inputs using VerifyScript.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch backport-0.25-batch-480-pr-25595

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9cf919c and 27d2485.

📒 Files selected for processing (4)
  • src/node/psbt.cpp (1 hunks)
  • src/psbt.cpp (2 hunks)
  • src/psbt.h (1 hunks)
  • test/functional/rpc_psbt.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
src/**/*.{cpp,h,cc,cxx,hpp}

📄 CodeRabbit inference engine (CLAUDE.md)

C++20 codebase should be placed under src/

Files:

  • src/node/psbt.cpp
  • src/psbt.cpp
  • src/psbt.h
**

⚙️ CodeRabbit configuration file

**: # CodeRabbit AI Review Instructions for Dash Backports

Your Role

You are reviewing Bitcoin Core backports to Dash Core. Your ONLY job is to validate that the Dash commit faithfully represents the original Bitcoin commit with minimal, necessary adaptations.

Critical Validation Rules

1. File Operations Must Match (AUTO-REJECT if violated)

  • If Bitcoin modifies an existing file → Dash MUST modify (not create new)
  • If Bitcoin creates a new file → Dash creates
  • If Bitcoin deletes a file → Dash deletes
  • Common failure: Bitcoin modifies keys.txt, Dash creates new file with 58 keys

2. Size Ratio Check (80-150% of Bitcoin)

  • Count functional lines changed (exclude comments/whitespace)
  • Dash changes should be 80-150% of Bitcoin's size
  • Red flag: 2-line Bitcoin fix becoming 150+ lines in Dash

3. No Scope Creep

  • Reject if you see: "TODO:", "FIXME:", "while we're here", "also fix"
  • No unrelated refactoring or style changes
  • Only Bitcoin's intended changes + minimal Dash adaptations

4. Bitcoin-Specific Code Detection

  • Auto-reject witness/segwit code: msg_wtxidrelay, MSG_WTX, witness imports
  • Auto-reject RBF (replace-by-fee) functionality
  • Note: PSBT is supported in Dash (don't flag)

5. Mandatory Adaptations Only

  • bitcoindash in strings/paths
  • BitcoinDash in user-facing text
  • Port numbers: 8332→9998 (RPC), 8333→9999 (P2P)
  • Hardcoded test values specific to Dash
  • No other changes unless absolutely required

6. Completeness Check

  • All files changed in Bitcoin must be present
  • Extra files need clear justification (Dash-specific compatibility)
  • Missing files = incomplete backport

Review Process

  1. First: Check file operations match exactly
  2. Second: Calculate size ratio
  3. Third: Scan for scope creep patterns
  4. Fourth: Detect Bitcoin-specific code
  5. Fifth: Verify all changes are minimal adaptations

Output Format

VALIDATION: [PASS/FAIL]

File Operatio...

Files:

  • src/node/psbt.cpp
  • src/psbt.cpp
  • test/functional/rpc_psbt.py
  • src/psbt.h
test/functional/**/*.py

📄 CodeRabbit inference engine (CLAUDE.md)

Functional tests should be placed in test/functional/ and written in Python

Files:

  • test/functional/rpc_psbt.py
🧠 Learnings (4)
📓 Common learnings
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T22:03:12.364Z
Learning: During multiple verification attempts of Bitcoin Core commit 06d469c26b backport to Dash PR #566, DashCoreAutoGuix consistently identified scope creep in interface_usdt_utxocache.py where additional pruning test functionality was added beyond the original Bitcoin commit. The user provided comprehensive fixes including both scope creep removal and missing mempool test file additions, but couldn't push due to authentication restrictions. The scope creep fix was identified as the priority to resolve CI failures.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-31T01:14:55.631Z
Learning: DashCoreAutoGuix successfully completed a complex Bitcoin Core backport (PR #29412) for block mutation detection by implementing the IsBlockMutated function, adding net processing integration, creating comprehensive unit tests, and properly adapting all Bitcoin-specific witness code for Dash compatibility. The backport maintains full security functionality while respecting Dash's non-witness transaction architecture.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T20:34:29.061Z
Learning: During Dash backport verification of Bitcoin Core commit 06d469c26b, scope creep was detected when additional pruning test functionality was added to interface_usdt_utxocache.py beyond what was in the original Bitcoin commit. The fix involved removing the extra test block while maintaining the core compiler flag fixes for USDT compilation errors.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T19:54:21.426Z
Learning: In Dash backports from Bitcoin Core, including necessary compilation fixes (such as API compatibility changes like UniValue get_int() → getInt<int>()) alongside the core backport is standard and expected practice. These compatibility fixes ensure the backported code compiles in Dash's evolved codebase while preserving Bitcoin's original functionality and intent.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-29T21:29:32.827Z
Learning: DashCoreAutoGuix successfully fixed scope creep in Bitcoin Core commit fcdb39d3ee backport by removing the parse test case from src/test/uint256_tests.cpp that was not part of the original Bitcoin commit. The fix was implemented in commit 16748115ce and verified through range-diff analysis.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-29T22:13:54.797Z
Learning: In Dash backports from Bitcoin Core, witness transaction-related code (MSG_WTX, wtxid) should be replaced with regular transaction handling (MSG_TX, txid) for compatibility, as demonstrated in the p2p_filter.py test fix where MSG_WTX was replaced with MSG_TX and irr_wtxid usage was replaced with irr_txid.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-30T14:45:15.700Z
Learning: In Dash backports from Bitcoin Core test files, the `address_to_scriptpubkey` function should be imported from `test_framework.wallet` instead of `test_framework.address` as the import location differs between Bitcoin and Dash test frameworks.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-29T17:13:35.087Z
Learning: When backporting Bitcoin Core commits that use Python's textwrap.dedent() function in test files, the textwrap import statement needs to be explicitly added if it's missing in the Dash test file.
📚 Learning: 2025-07-28T22:03:12.364Z
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T22:03:12.364Z
Learning: During multiple verification attempts of Bitcoin Core commit 06d469c26b backport to Dash PR #566, DashCoreAutoGuix consistently identified scope creep in interface_usdt_utxocache.py where additional pruning test functionality was added beyond the original Bitcoin commit. The user provided comprehensive fixes including both scope creep removal and missing mempool test file additions, but couldn't push due to authentication restrictions. The scope creep fix was identified as the priority to resolve CI failures.

Applied to files:

  • src/node/psbt.cpp
  • src/psbt.cpp
📚 Learning: 2025-07-28T20:34:29.061Z
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T20:34:29.061Z
Learning: During Dash backport verification of Bitcoin Core commit 06d469c26b, scope creep was detected when additional pruning test functionality was added to interface_usdt_utxocache.py beyond what was in the original Bitcoin commit. The fix involved removing the extra test block while maintaining the core compiler flag fixes for USDT compilation errors.

Applied to files:

  • src/node/psbt.cpp
  • test/functional/rpc_psbt.py
📚 Learning: 2025-07-29T17:13:35.087Z
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-29T17:13:35.087Z
Learning: When backporting Bitcoin Core commits that use Python's textwrap.dedent() function in test files, the textwrap import statement needs to be explicitly added if it's missing in the Dash test file.

Applied to files:

  • test/functional/rpc_psbt.py
🧬 Code graph analysis (2)
src/node/psbt.cpp (1)
src/psbt.cpp (2)
  • PSBTInputSignedAndVerified (184-209)
  • PSBTInputSignedAndVerified (184-184)
src/psbt.h (1)
src/psbt.cpp (4)
  • PSBTInputSigned (241-244)
  • PSBTInputSigned (241-241)
  • PSBTInputSignedAndVerified (184-209)
  • PSBTInputSignedAndVerified (184-184)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: x86_64-apple-darwin / Build depends
  • GitHub Check: x86_64-pc-linux-gnu_nowallet / Build depends
  • GitHub Check: x86_64-w64-mingw32 / Build depends
  • GitHub Check: arm-linux-gnueabihf / Build depends
  • GitHub Check: x86_64-pc-linux-gnu / Build depends
  • GitHub Check: Lint / Run linters
🔇 Additional comments (4)
test/functional/rpc_psbt.py (1)

507-509: LGTM - Formatting-only change.

The blank line addition before the main guard is a minor formatting adjustment with no functional impact.

src/node/psbt.cpp (1)

61-85: LGTM - Correct integration of the new verification function.

The change from a simple signed check to PSBTInputSignedAndVerified(psbtx, i, &txdata) properly implements the backport's intent of verifying signatures rather than just checking for non-empty fields. The txdata is correctly precomputed earlier in the function (line 26) and passed by address.

src/psbt.cpp (1)

268-270: LGTM - Correct integration of verification in SignPSBTInput.

The early return when the input is already signed and verified is appropriate. The txdata parameter can be nullptr (handled by the verification function), allowing this to work for both dummy signature creation and actual signing paths.

src/psbt.h (1)

871-872: Performance: Consider passing psbt by const reference instead of by value.

The PartiallySignedTransaction struct contains std::optional<CMutableTransaction>, vectors, and maps. Passing it by value copies the entire structure on every call, which is expensive. This function is called in hot paths like SignPSBTInput and AnalyzePSBT.

Please verify whether the original Bitcoin Core commit (2ac71d2) also passes by value. If so, this is a faithful backport but may warrant a follow-up optimization. If not, the signature should be corrected:

-bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata);
+bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata);
⛔ Skipped due to learnings
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T22:03:12.364Z
Learning: During multiple verification attempts of Bitcoin Core commit 06d469c26b backport to Dash PR #566, DashCoreAutoGuix consistently identified scope creep in interface_usdt_utxocache.py where additional pruning test functionality was added beyond the original Bitcoin commit. The user provided comprehensive fixes including both scope creep removal and missing mempool test file additions, but couldn't push due to authentication restrictions. The scope creep fix was identified as the priority to resolve CI failures.
Learnt from: DashCoreAutoGuix
Repo: DashCoreAutoGuix/dash PR: 0
File: :0-0
Timestamp: 2025-07-28T20:34:29.061Z
Learning: During Dash backport verification of Bitcoin Core commit 06d469c26b, scope creep was detected when additional pruning test functionality was added to interface_usdt_utxocache.py beyond what was in the original Bitcoin commit. The fix involved removing the extra test block while maintaining the core compiler flag fixes for USDT compilation errors.

Comment on lines +184 to +209
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
{
CTxOut utxo;
assert(psbt.inputs.size() >= input_index);
const PSBTInput& input = psbt.inputs[input_index];

if (input.non_witness_utxo) {
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
COutPoint prevout = psbt.tx->vin[input_index].prevout;
if (prevout.n >= input.non_witness_utxo->vout.size()) {
return false;
}
if (input.non_witness_utxo->GetHash() != prevout.hash) {
return false;
}
utxo = input.non_witness_utxo->vout[prevout.n];
} else {
return false;
}

if (txdata) {
return VerifyScript(input.final_script_sig, utxo.scriptPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL});
} else {
return VerifyScript(input.final_script_sig, utxo.scriptPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, MissingDataBehavior::FAIL});
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Critical: Off-by-one error in bounds assertion.

Line 187 uses >= which allows input_index == psbt.inputs.size(), but the subsequent access at line 188 would be out-of-bounds.

Apply this diff to fix the assertion:

-    assert(psbt.inputs.size() >= input_index);
+    assert(input_index < psbt.inputs.size());

Additionally, as noted in the header review, the function signature passes psbt by value which is inefficient. Consider using const PartiallySignedTransaction& psbt instead.

🤖 Prompt for AI Agents
In src/psbt.cpp around lines 184 to 209, the bounds assertion is inverted and
allows an out-of-range access; change the assertion to ensure input_index is
strictly less than psbt.inputs.size() (e.g. assert(input_index <
psbt.inputs.size())) and update the function signature to take the PSBT by const
reference (const PartiallySignedTransaction& psbt) to avoid unnecessary copying;
make corresponding symbol/name adjustments in the function body if needed.

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.

2 participants