Skip to content

Commit c61290e

Browse files
committed
tapchannel: update createSweepVpackets to be second level aware
In this commit, we update `createSweepVpackets` to be second level aware. This means that if we detect an auxSigInfo, then we know the vPkt we need to create is actually just the second level vPkt. We also don't need to generate a new script key either. Finally, we'll properly set the absolute delay if the sweep desc indicates as such.
1 parent c8ea90f commit c61290e

File tree

1 file changed

+61
-31
lines changed

1 file changed

+61
-31
lines changed

tapchannel/aux_sweeper.go

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func (a *AuxSweeper) Stop() error {
179179
// set of asset inputs into the backing wallet.
180180
func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
181181
tapscriptDesc lfn.Result[tapscriptSweepDesc],
182+
resReq lnwallet.ResolutionReq,
182183
) lfn.Result[[]*tappsbt.VPacket] {
183184

184185
type returnType = []*tappsbt.VPacket
@@ -192,39 +193,59 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
192193
return lfn.Err[returnType](err)
193194
}
194195

195-
// For each out we want to sweep, we'll construct an allocation that
196-
// we'll use to deliver the funds back to the wallet.
197-
ctx := context.Background()
198196
allocs := make([]*Allocation, 0, len(sweepInputs))
199-
for _, localAsset := range sweepInputs {
200-
// For each output, we'll need to create a new script key to
201-
// use for the sweep transaction.
202-
scriptKey, err := a.cfg.AddrBook.NextScriptKey(
203-
ctx, asset.TaprootAssetsKeyFamily,
197+
ctx := context.Background()
198+
199+
// If this is a second level HTLC sweep, then we already have
200+
// the output information locked in, as this was a pre-signed
201+
// transaction.
202+
if sweepDesc.auxSigInfo.IsSome() {
203+
alloc, err := createSecondLevelHtlcAllocations(
204+
resReq.ChanType, resReq.Initiator, sweepInputs,
205+
resReq.HtlcAmt, resReq.CommitCsvDelay, *resReq.KeyRing,
206+
fn.Some(resReq.ContractPoint.Index),
204207
)
205208
if err != nil {
206209
return lfn.Err[returnType](err)
207210
}
208211

209-
// With the script key created, we can make a new allocation
210-
// that will be used to sweep the funds back to our wallet.
211-
//
212-
// We leave out the internal key here, as we'll make it later
213-
// once we actually have the other set of inputs we need to
214-
// sweep.
215-
allocs = append(allocs, &Allocation{
216-
Type: CommitAllocationToLocal,
217-
// We don't need to worry about sorting, as we'll
218-
// always be the first output index in the transaction.
219-
OutputIndex: 0,
220-
Amount: localAsset.Amount.Val,
221-
AssetVersion: asset.V1,
222-
BtcAmount: tapsend.DummyAmtSats,
223-
ScriptKey: scriptKey,
224-
SortTaprootKeyBytes: schnorr.SerializePubKey(
225-
scriptKey.PubKey,
226-
),
227-
})
212+
allocs = append(allocs, alloc...)
213+
} else {
214+
// Otherwise, for each out we want to sweep, we'll construct an
215+
// allocation that we'll use to deliver the funds back to the
216+
// wallet.
217+
for _, localAsset := range sweepInputs {
218+
// For each output, we'll need to create a new script
219+
// key to use for the sweep transaction.
220+
scriptKey, err := a.cfg.AddrBook.NextScriptKey(
221+
ctx, asset.TaprootAssetsKeyFamily,
222+
)
223+
if err != nil {
224+
return lfn.Err[[]*tappsbt.VPacket](err)
225+
}
226+
227+
// With the script key created, we can make a new
228+
// allocation that will be used to sweep the funds back
229+
// to our wallet.
230+
//
231+
// We leave out the internal key here, as we'll make it
232+
// later once we actually have the other set of inputs
233+
// we need to sweep.
234+
allocs = append(allocs, &Allocation{
235+
Type: CommitAllocationToLocal,
236+
// We don't need to worry about sorting, as
237+
// we'll always be the first output index in the
238+
// transaction.
239+
OutputIndex: 0,
240+
Amount: localAsset.Amount.Val,
241+
AssetVersion: asset.V1,
242+
BtcAmount: tapsend.DummyAmtSats,
243+
ScriptKey: scriptKey,
244+
SortTaprootKeyBytes: schnorr.SerializePubKey(
245+
scriptKey.PubKey,
246+
),
247+
})
248+
}
228249
}
229250

230251
log.Infof("Created %v allocations for commit tx sweep: %v",
@@ -268,6 +289,14 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
268289
}
269290
})
270291

292+
// Similarly, if we have an absolute delay, we'll set it for all
293+
// the vOuts in this packet.
294+
sweepDesc.absoluteDelay.WhenSome(func(expiry uint64) {
295+
for _, vOut := range vPackets[idx].Outputs {
296+
vOut.LockTime = expiry
297+
}
298+
})
299+
271300
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
272301
if err != nil {
273302
return lfn.Errf[returnType]("unable to prepare output "+
@@ -355,7 +384,7 @@ func (a *AuxSweeper) signSweepVpackets(vPackets []*tappsbt.VPacket,
355384
// createAndSignSweepVpackets creates vPackets that sweep the funds from the
356385
// channel to the wallet, and then signs them as well.
357386
func (a *AuxSweeper) createAndSignSweepVpackets(
358-
sweepInputs []*cmsg.AssetOutput, signDesc input.SignDescriptor,
387+
sweepInputs []*cmsg.AssetOutput, resReq lnwallet.ResolutionReq,
359388
sweepDesc lfn.Result[tapscriptSweepDesc],
360389
) lfn.Result[[]*tappsbt.VPacket] {
361390

@@ -369,7 +398,7 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
369398
signPkts := func(vPkts []*tappsbt.VPacket,
370399
desc tapscriptSweepDesc) lfn.Result[[]*tappsbt.VPacket] {
371400

372-
err := a.signSweepVpackets(vPkts, signDesc, desc)
401+
err := a.signSweepVpackets(vPkts, resReq.SignDesc, desc)
373402
if err != nil {
374403
return lfn.Err[returnType](err)
375404
}
@@ -378,7 +407,8 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
378407
}
379408

380409
return lfn.AndThen2(
381-
a.createSweepVpackets(sweepInputs, sweepDesc), sweepDesc,
410+
a.createSweepVpackets(sweepInputs, sweepDesc, resReq),
411+
sweepDesc,
382412
signPkts,
383413
)
384414
}
@@ -1593,7 +1623,7 @@ func (a *AuxSweeper) resolveContract(
15931623
// With the sweep desc constructed above, we'll create vPackets for
15941624
// each of the local assets, then sign them all.
15951625
sPkts := a.createAndSignSweepVpackets(
1596-
assetOutputs, req.SignDesc, firstLevelSweepDesc,
1626+
assetOutputs, req, firstLevelSweepDesc,
15971627
)
15981628

15991629
// With the vPackets fully generated and signed above, we'll serialize

0 commit comments

Comments
 (0)