Skip to content

Commit 5730f62

Browse files
committed
tapchannel: refactor out commit packet witness creation
We're doing the same thing in three places, this commit unifies the code by using the same function everywhere.
1 parent 3ed142f commit 5730f62

File tree

3 files changed

+70
-102
lines changed

3 files changed

+70
-102
lines changed

tapchannel/aux_closer.go

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,53 @@ func createCloseAlloc(isLocal bool, outputSum uint64,
156156
}, nil
157157
}
158158

159+
// signCommitVirtualPackets signs the commit virtual packets with the funding
160+
// witness, which is just the script and control block for the OP_TRUE spend.
161+
func signCommitVirtualPackets(ctx context.Context,
162+
vPackets []*tappsbt.VPacket) error {
163+
164+
// Now that we've added all the relevant vPackets, we'll prepare the
165+
// funding witness which includes the OP_TRUE ctrl block.
166+
fundingWitness, err := fundingSpendWitness().Unpack()
167+
if err != nil {
168+
return fmt.Errorf("unable to make funding witness: %w", err)
169+
}
170+
171+
// With all the vPackets created, we'll create output commitments from
172+
// them, as we'll need them to ship the transaction off to the porter.
173+
for idx := range vPackets {
174+
err = tapsend.PrepareOutputAssets(ctx, vPackets[idx])
175+
if err != nil {
176+
return fmt.Errorf("unable to prepare output "+
177+
"assets: %w", err)
178+
}
179+
180+
// With the packets prepared, we'll swap in the correct witness
181+
// for each of them. We need to do this _after_ calling
182+
// PrepareOutputAsset, because that method will overwrite any
183+
// asset in the virtual outputs. Which means we'll also need to
184+
// set the witness on _every_ output of the packet, to make sure
185+
// each split output's root asset reference also gets the
186+
// correct witness.
187+
for outIdx := range vPackets[idx].Outputs {
188+
outAsset := vPackets[idx].Outputs[outIdx].Asset
189+
190+
// There is always only a single input, as we're
191+
// spending a single funding output w/ each vPkt.
192+
const inputIndex = 0
193+
err := outAsset.UpdateTxWitness(
194+
inputIndex, fundingWitness,
195+
)
196+
if err != nil {
197+
return fmt.Errorf("error updating witness: %w",
198+
err)
199+
}
200+
}
201+
}
202+
203+
return nil
204+
}
205+
159206
// fundingSpendWitness creates a complete witness to spend the OP_TRUE funding
160207
// script of an asset funding output.
161208
func fundingSpendWitness() lfn.Result[wire.TxWitness] {
@@ -377,35 +424,12 @@ func (a *AuxChanCloser) AuxCloseOutputs(
377424
return none, fmt.Errorf("unable to distribute coins: %w", err)
378425
}
379426

380-
// With the vPackets created we'll now prepare all the split
381-
// information encoded in the vPackets.
382-
fundingWitness, err := fundingSpendWitness().Unpack()
383-
if err != nil {
384-
return none, fmt.Errorf("unable to make funding "+
385-
"witness: %w", err)
386-
}
387-
ctx := context.Background()
388-
for idx := range vPackets {
389-
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
390-
if err != nil {
391-
return none, fmt.Errorf("unable to prepare output "+
392-
"assets: %w", err)
393-
}
394-
395-
for outIdx := range vPackets[idx].Outputs {
396-
outAsset := vPackets[idx].Outputs[outIdx].Asset
397-
398-
// There is always only a single input, which is the
399-
// funding output.
400-
const inputIndex = 0
401-
err := outAsset.UpdateTxWitness(
402-
inputIndex, fundingWitness,
403-
)
404-
if err != nil {
405-
return none, fmt.Errorf("error updating "+
406-
"witness: %w", err)
407-
}
408-
}
427+
// We can now add the witness for the OP_TRUE spend of the commitment
428+
// output to the vPackets.
429+
ctxb := context.Background()
430+
if err := signCommitVirtualPackets(ctxb, vPackets); err != nil {
431+
return none, fmt.Errorf("error signing commit virtual "+
432+
"packets: %w", err)
409433
}
410434

411435
// With the outputs prepared, we can now create the set of output

tapchannel/aux_sweeper.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,42 +1592,15 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
15921592
}
15931593
}
15941594

1595-
// Now that we've added all the relevant vPackets, we'll prepare the
1596-
// funding witness which includes the OP_TRUE ctrl block.
1597-
fundingWitness, err := fundingSpendWitness().Unpack()
1598-
if err != nil {
1599-
return fmt.Errorf("unable to make funding witness: %w", err)
1595+
// We can now add the witness for the OP_TRUE spend of the commitment
1596+
// output to the vPackets.
1597+
vPackets := maps.Values(vPktsByAssetID)
1598+
if err := signCommitVirtualPackets(ctxb, vPackets); err != nil {
1599+
return fmt.Errorf("error signing commit virtual "+
1600+
"packets: %w", err)
16001601
}
16011602

1602-
// With all the vPackets created, we'll create output commitments from
1603-
// them, as we'll need them to ship the transaction off to the porter.
1604-
vPkts := maps.Values(vPktsByAssetID)
1605-
ctx := context.Background()
1606-
for idx := range vPkts {
1607-
err := tapsend.PrepareOutputAssets(ctx, vPkts[idx])
1608-
if err != nil {
1609-
return fmt.Errorf("unable to prepare output "+
1610-
"assets: %w", err)
1611-
}
1612-
1613-
// With the packets prepared, we'll swap in the correct witness
1614-
// for each of them.
1615-
for outIdx := range vPkts[idx].Outputs {
1616-
outAsset := vPkts[idx].Outputs[outIdx].Asset
1617-
1618-
// There is always only a single input, as we're
1619-
// sweeping a single contract w/ each vPkt.
1620-
const inputIndex = 0
1621-
err := outAsset.UpdateTxWitness(
1622-
inputIndex, fundingWitness,
1623-
)
1624-
if err != nil {
1625-
return fmt.Errorf("error updating "+
1626-
"witness: %w", err)
1627-
}
1628-
}
1629-
}
1630-
outCommitments, err := tapsend.CreateOutputCommitments(vPkts)
1603+
outCommitments, err := tapsend.CreateOutputCommitments(vPackets)
16311604
if err != nil {
16321605
return fmt.Errorf("unable to create output "+
16331606
"commitments: %w", err)
@@ -1641,12 +1614,12 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
16411614
"allocations: %w", err)
16421615
}
16431616
exclusionCreator := tapsend.NonAssetExclusionProofs(anchorAllocations)
1644-
for idx := range vPkts {
1645-
vPkt := vPkts[idx]
1617+
for idx := range vPackets {
1618+
vPkt := vPackets[idx]
16461619
for outIdx := range vPkt.Outputs {
16471620
proofSuffix, err := tapsend.CreateProofSuffixCustom(
16481621
req.CommitTx, vPkt, outCommitments, outIdx,
1649-
vPkts, exclusionCreator,
1622+
vPackets, exclusionCreator,
16501623
)
16511624
if err != nil {
16521625
return fmt.Errorf("unable to create "+
@@ -1663,7 +1636,7 @@ func (a *AuxSweeper) importCommitTx(req lnwallet.ResolutionReq,
16631636
// With all the vPKts created, we can now ship the transaction off to
16641637
// the porter for final delivery.
16651638
return shipChannelTxn(
1666-
a.cfg.TxSender, req.CommitTx, outCommitments, vPkts,
1639+
a.cfg.TxSender, req.CommitTx, outCommitments, vPackets,
16671640
int64(req.CommitFee),
16681641
)
16691642
}

tapchannel/commitment.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -563,41 +563,12 @@ func GenerateCommitmentAllocations(prevState *cmsg.Commitment,
563563
err)
564564
}
565565

566-
// The root asset of the split commitment will still commit to the full
567-
// witness value. Therefore, we need to update the root asset witness to
568-
// what it would be at broadcast time.
569-
fundingWitness, err := fundingSpendWitness().Unpack()
570-
if err != nil {
571-
return nil, nil, fmt.Errorf("unable to make funding "+
572-
"witness: %w", err)
573-
}
574-
575-
// Prepare the output assets for each virtual packet, then create the
576-
// output commitments.
577-
ctx := context.Background()
578-
for idx := range vPackets {
579-
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
580-
if err != nil {
581-
return nil, nil, fmt.Errorf("unable to prepare output "+
582-
"assets: %w", err)
583-
}
584-
585-
// With the packets prepared, we'll swap in the correct witness
586-
// for each of them.
587-
for outIdx := range vPackets[idx].Outputs {
588-
outAsset := vPackets[idx].Outputs[outIdx].Asset
589-
590-
// There is always only a single input, as we're
591-
// sweeping a single contract w/ each vPkt.
592-
const inputIndex = 0
593-
err := outAsset.UpdateTxWitness(
594-
inputIndex, fundingWitness,
595-
)
596-
if err != nil {
597-
return nil, nil, fmt.Errorf("error updating "+
598-
"witness: %w", err)
599-
}
600-
}
566+
// We can now add the witness for the OP_TRUE spend of the commitment
567+
// output to the vPackets.
568+
ctxb := context.Background()
569+
if err := signCommitVirtualPackets(ctxb, vPackets); err != nil {
570+
return nil, nil, fmt.Errorf("error signing commit virtual "+
571+
"packets: %w", err)
601572
}
602573

603574
outCommitments, err := tapsend.CreateOutputCommitments(vPackets)

0 commit comments

Comments
 (0)