Skip to content

Commit 66ea173

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 9fc1a00 commit 66ea173

File tree

1 file changed

+63
-31
lines changed

1 file changed

+63
-31
lines changed

tapchannel/aux_sweeper.go

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

230253
log.Infof("Created %v allocations for commit tx sweep: %v",
@@ -268,6 +291,14 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
268291
}
269292
})
270293

294+
// Similarly, if we have an absolute delay, we'll set it for all
295+
// the vOuts in this packet.
296+
sweepDesc.absoluteDelay.WhenSome(func(expiry uint64) {
297+
for _, vOut := range vPackets[idx].Outputs {
298+
vOut.LockTime = expiry
299+
}
300+
})
301+
271302
err := tapsend.PrepareOutputAssets(ctx, vPackets[idx])
272303
if err != nil {
273304
return lfn.Errf[returnType]("unable to prepare output "+
@@ -355,7 +386,7 @@ func (a *AuxSweeper) signSweepVpackets(vPackets []*tappsbt.VPacket,
355386
// createAndSignSweepVpackets creates vPackets that sweep the funds from the
356387
// channel to the wallet, and then signs them as well.
357388
func (a *AuxSweeper) createAndSignSweepVpackets(
358-
sweepInputs []*cmsg.AssetOutput, signDesc input.SignDescriptor,
389+
sweepInputs []*cmsg.AssetOutput, resReq lnwallet.ResolutionReq,
359390
sweepDesc lfn.Result[tapscriptSweepDesc],
360391
) lfn.Result[[]*tappsbt.VPacket] {
361392

@@ -369,7 +400,7 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
369400
signPkts := func(vPkts []*tappsbt.VPacket,
370401
desc tapscriptSweepDesc) lfn.Result[[]*tappsbt.VPacket] {
371402

372-
err := a.signSweepVpackets(vPkts, signDesc, desc)
403+
err := a.signSweepVpackets(vPkts, resReq.SignDesc, desc)
373404
if err != nil {
374405
return lfn.Err[returnType](err)
375406
}
@@ -378,7 +409,8 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
378409
}
379410

380411
return lfn.AndThen2(
381-
a.createSweepVpackets(sweepInputs, sweepDesc), sweepDesc,
412+
a.createSweepVpackets(sweepInputs, sweepDesc, resReq),
413+
sweepDesc,
382414
signPkts,
383415
)
384416
}
@@ -1596,7 +1628,7 @@ func (a *AuxSweeper) resolveContract(
15961628
// With the sweep desc constructed above, we'll create vPackets for
15971629
// each of the local assets, then sign them all.
15981630
sPkts := a.createAndSignSweepVpackets(
1599-
assetOutputs, req.SignDesc, firstLevelSweepDesc,
1631+
assetOutputs, req, firstLevelSweepDesc,
16001632
)
16011633

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

0 commit comments

Comments
 (0)