Skip to content

chanfunding: fix skip_finalize TXID for nested SegWit inputs - #11581

Open
starius wants to merge 7 commits into
lightningnetwork:masterfrom
starius:fix-11570
Open

starius wants to merge 7 commits into
lightningnetwork:masterfrom
starius:fix-11570

Conversation

@starius

@starius starius commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Change Description

Fixes #11570.

With skip_finalize in the PSBT verify step, lnd never sees the signed funding transaction, so it used the TXID of the unsigned transaction for the channel point. That is only correct if signing doesn't change the TXID. Signing a nested SegWit input (P2SH-P2WPKH or P2SH-P2WSH) sets its scriptSig to a push of its redeem script, and the TXID covers the scriptSig. So the channel waited for a transaction that was never published, and its capacity sat in a 2-of-2 output that lnd didn't watch.

Now lnd computes the TXID of the signed transaction. It builds a copy of the unsigned transaction in which every input has the scriptSig it will have once it is signed. By consensus (BIP-141), that is empty for native SegWit and exactly one push of the redeem script for nested SegWit. Signing only adds witness data, which is not part of the TXID, so the copy has the TXID of the signed transaction. The PSBT itself is not changed.

With skip_finalize, lnd validates the input scripts and UTXO information, rejecting:

  • legacy inputs, even if their witness UTXO is set (the old check didn't look at the script then);
  • nested SegWit inputs without their redeem script, or with a redeem script that doesn't match the output;
  • inputs with missing or inconsistent UTXO information;
  • inputs whose FinalScriptSig is already set to an unexpected value.

These validation failures leave the intent unchanged, allowing the user to correct the PSBT and retry. For native SegWit inputs the TXID is the same as before. The normal flow with psbt_finalize is not changed.

Other changes:

  • The skip_finalize comment in lightning.proto and docs/psbt.md now explain nested SegWit inputs, and that chan_pending reports the TXID to expect.
  • TestPsbtVerify now fails when an expected error is missing, and each case gets its own intent.

Steps to Test

make unit pkg=lnwallet/chanfunding
make itest icase=nested_segwit

The new itests open a batch of two PSBT channels funded by a nested SegWit coin:

To see the bug, undo the Verify change in lnwallet/chanfunding/psbt_assembler.go and keep the new helper. The TestPsbtVerifySkipFinalize* unit tests then fail, and both new itests fail because the channel TXID is not the TXID of the published transaction.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

The loop of TestPsbtVerify only failed when a case returned a different
error than the expected one. A case that expected an error but got none
still passed. Now the expected error is required, and no error is
allowed otherwise.

All cases also shared one intent, so a case could leave state behind for
the next one. The "skip verify" case, for example, closed the PsbtReady
channel of the intent. Each case now gets its own intent.
Add txWithFinalSigScripts. It returns a copy of the unsigned transaction
of a PSBT in which every input has the signature script it must have once
it is signed. By consensus (BIP-141), the signature script of a native
SegWit input stays empty, and the one of a nested SegWit input is exactly
one push of its redeem script. Signing only adds witness data on top of
that, and witness data is not part of the TXID. So the copy has the TXID
of the signed transaction.

The helper rejects every input whose final signature script can't be
known before signing: legacy inputs, P2SH inputs whose redeem script is
missing or is not a matching witness program, and inputs whose UTXO
information is missing or inconsistent. A non-witness UTXO must hash to
the outpoint of the input, and a final signature script that is already
in the PSBT must be the expected one.

The next commit uses the helper for channels that skip the PSBT finalize
step.
With the skip_finalize flag of the PSBT verify step, lnd never sees the
signed funding transaction. The intent used the TXID of the unsigned
transaction of the PSBT for the channel point instead. That only works if
signing doesn't change the TXID, which is true for native SegWit inputs.
Signing a nested SegWit input also sets its signature script to a push
of its redeem script, and the TXID covers the signature script. So a
channel funded by a nested SegWit input waited for a transaction that
was never published, while its capacity sat in a 2-of-2 output that lnd
didn't watch.

The intent now uses a copy of the unsigned transaction in which every
input has its final signature script. That copy has the TXID of the
signed transaction. A PSBT for which the copy can't be built is rejected
before the state of the intent changes, so the user can fix the PSBT and
retry, or retry without skip_finalize.

Fixes lightningnetwork#11570.
The comment on skip_finalize said that only signatures can be added to
the verified PSBT, so its TXID can't change. But signing a nested SegWit
input also sets its signature script, which does change the TXID.

Describe that lnd now computes the TXID of the signed transaction, that
nested SegWit inputs need their redeem script in the PSBT, which inputs
are rejected, and where the expected TXID is reported. Also be precise
about what signing may add before the transaction is published: only
witness data and the signature scripts of nested SegWit inputs.
Explain that with skip_finalize lnd computes the TXID of the signed
funding transaction, that this TXID differs from the unsigned one for
nested SegWit inputs, and that the PSBT must therefore contain their
redeem scripts.

The example script told users to make sure the TXID does not change.
With nested SegWit inputs it always changes when they are signed, so the
script now points to the expected TXID that is shown once the channels
are pending.
Add two cases for a batch of two PSBT channels that a nested SegWit coin
funds. In the first case, the opener funds the batch with its own coin,
the first channel skips the finalize step, and the second channel
publishes the batch transaction. This is the flow from lightningnetwork#11570. In the
second case, a third party funds, signs and publishes the batch, so both
channels skip the finalize step.

Signing the nested SegWit input changes the TXID of the batch
transaction. In both cases, the channels must use the TXID of the
published transaction and get confirmed.
@github-actions github-actions Bot added the ci-hold CI is held; remove this label to run CI label Nov 14, 2026
@danieldzansi

Copy link
Copy Markdown

tACK at 2c9993264

Thanks for fixing this!

Unit tests: make unit pkg=lnwallet/chanfunding passes, including the new tests:

  • TestTxWithFinalSigScripts
  • TestPsbtVerifySkipFinalize
  • TestPsbtVerifySkipFinalizeReject
  • TestPsbtVerifySkipFinalizeBatch
  • TestPsbtFinalizeNested

Itests: make itest icase=nested_segwit (btcd backend). Both new itests pass:

--- PASS: .../psbt-psbt_skip_finalize_nested_segwit (9.92s)
--- PASS: .../psbt-psbt_external_funding_nested_segwit (11.60s)

Without the fix: Following the PR description, I reverted only the Verify change from ea4d93e21 and kept the helper.

  • TestPsbtVerifySkipFinalize (P2SH-P2WKH, P2SH-P2WSH, native and nested SegWit) and TestPsbtVerifySkipFinalizeBatch fail because the channel uses the unsigned TXID instead of the signed one.
  • TestPsbtVerifySkipFinalizeReject fails with An error is expected but got nil, because the old code accepts a nested input without its redeem script.
  • psbt_skip_finalize_nested_segwit fails at lnd_psbt_test.go:602: the channel's funding TXID doesn't match the published transaction's TXID. The harness stopped after that, so I didn't run the external funding itest without the fix.

I haven't reviewed the code in depth or tested other backends.

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.

psbt_verify with skip_finalize accepts nested SegWit inputs, so the channel is keyed to a txid that is never published

2 participants