Repository navigation
Conversation
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.
2 tasks done
|
tACK at Thanks for fixing this! Unit tests:
Itests: Without the fix: Following the PR description, I reverted only the
I haven't reviewed the code in depth or tested other backends. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Description
Fixes #11570.
With
skip_finalizein 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:FinalScriptSigis 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_finalizeis not changed.Other changes:
skip_finalizecomment inlightning.protoanddocs/psbt.mdnow explain nested SegWit inputs, and thatchan_pendingreports the TXID to expect.TestPsbtVerifynow fails when an expected error is missing, and each case gets its own intent.Steps to Test
The new itests open a batch of two PSBT channels funded by a nested SegWit coin:
psbt skip finalize nested segwitis the flow from psbt_verify with skip_finalize accepts nested SegWit inputs, so the channel is keyed to a txid that is never published #11570: the first channel usesno_publishandskip_finalize, and the second one publishes the batch.psbt external funding nested segwit: a third party funds, signs and publishes the batch, and both channels skip the finalize step.To see the bug, undo the
Verifychange inlnwallet/chanfunding/psbt_assembler.goand keep the new helper. TheTestPsbtVerifySkipFinalize*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
Code Style and Documentation
docs/release-notes/unreleased/<PR number>.md, or theno-changeloglabel is applied for changes that do not need one.📝 Please see our Contribution Guidelines for further guidance.