Skip to content

fix(sdk-api): require previous transactions for v1 non-segwit psbt inputs - #10190

Open
prikshitchawla300 wants to merge 1 commit into
masterfrom
prikshitchawla/cshld-2230-v1-psbt-require-prev-tx
Open

prikshitchawla300 wants to merge 1 commit into
masterfrom
prikshitchawla/cshld-2230-v1-psbt-require-prev-tx

Conversation

@prikshitchawla300

Copy link
Copy Markdown
Contributor

Summary

The legacy sighash used by non-segwit (p2sh) inputs does not commit to the input value, so the full previous transaction (nonWitnessUtxo) is needed to know what an input spends. The v1 PSBT path built and signed p2sh inputs from witnessUtxo only (skipNonWitnessUtxo + withUnsafeNonSegwit). This change makes it require and verify the previous transaction.

Behaviour change

  • Signer (signPsbtRequest): refuses the whole PSBT if any p2sh input has no nonWitnessUtxo, or its nonWitnessUtxo does not hash to the txid the input spends. Signing now runs in safe mode (withUnsafeNonSegwit(..., false)). Segwit and taproot inputs are unchanged.
  • Builder (createTransaction with usePsbt): attaches the previous transaction of every p2sh input instead of using skipNonWitnessUtxo. It is taken from the unspent's prevTx if present, otherwise fetched from the block explorer (same btcExplorerBaseUrl endpoint already used for CPFP). It must match the input's txid, and the spent output must have the unspent's script and value.
  • Fallback: if a previous transaction contradicts the unspent data (PrevTxMismatchError), createTransaction rejects instead of falling back to the legacy format. Other PSBT build errors, including a failed explorer fetch, still fall back as before.

BREAKING CHANGE: v1 signTransaction no longer signs PSBTs whose p2sh inputs only carry witnessUtxo. PSBTs built by this SDK now include the previous transactions. PSBTs built elsewhere must include nonWitnessUtxo for p2sh inputs.

Changes

  • modules/sdk-api/src/v1/signPsbt.ts: nonWitnessUtxo check, safe-mode signing
  • modules/sdk-api/src/v1/transactionBuilder.ts: fetch and verify previous transactions, PrevTxMismatchError
  • modules/sdk-api/src/v1/wallet.ts: no legacy fallback on PrevTxMismatchError
  • Tests in test/unit/v1/signPsbt.ts and test/unit/v1/wallet.ts

Testing

  • sdk-api unit tests: 296 passing
  • New tests cover: p2sh input without nonWitnessUtxo refused, one bad input refuses the whole PSBT, nonWitnessUtxo of another transaction refused, segwit inputs still sign from witnessUtxo, explorer fetch, value / txid / script mismatch rejected without fallback, and a failed fetch still falling back.
  • 8 of the new tests fail on the previous code.

Ticket: CSHLD-2230

…puts

The legacy sighash used by non-segwit (p2sh) inputs does not commit to
the input value, so the full previous transaction is needed to know
what an input spends.

- signPsbtRequest refuses a PSBT if any p2sh input lacks
  nonWitnessUtxo or its nonWitnessUtxo does not hash to the txid the
  input spends, and signs in safe mode instead of withUnsafeNonSegwit.
- The v1 PSBT builder attaches the previous transaction of every p2sh
  input instead of using skipNonWitnessUtxo. It is taken from the
  unspent's prevTx or fetched from the block explorer, and must match
  the input's txid, script and value.
- createTransaction no longer falls back to the legacy format when a
  previous transaction contradicts the unspent data
  (PrevTxMismatchError). Other PSBT build errors still fall back.

BREAKING CHANGE: v1 signTransaction no longer signs PSBTs whose p2sh
inputs only carry witnessUtxo. PSBTs built by this SDK now include the
previous transactions; PSBTs built elsewhere must include
nonWitnessUtxo for p2sh inputs.

Ticket: CSHLD-2230
@prikshitchawla300
prikshitchawla300 requested a review from a team as a code owner November 17, 2026 10:32

@claude claude Bot 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.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@linear-code

linear-code Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

CSHLD-2230

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Compatibility checks are failing on Node.js 27-nightly (experimental, non-blocking). This does not block merge, but it signals an incompatibility to fix before this release becomes supported.

View run

This branch has not been deployed

No deployments
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.

1 participant