66 "fmt"
77 "math"
88 "net/url"
9+ "slices"
910 "sync"
1011 "sync/atomic"
1112
@@ -325,12 +326,14 @@ func (a *AuxSweeper) createSweepVpackets(sweepInputs []*cmsg.AssetOutput,
325326// signSweepVpackets attempts to sign the vPackets specified using the passed
326327// sign desc and script tree.
327328func (a * AuxSweeper ) signSweepVpackets (vPackets []* tappsbt.VPacket ,
328- signDesc input.SignDescriptor , tapscriptDesc tapscriptSweepDesc ) error {
329+ signDesc input.SignDescriptor , tapTweak , ctrlBlock []byte ,
330+ auxSigDesc lfn.Option [lnwallet.AuxSigDesc ],
331+ secondLevelSigIndex lfn.Option [uint32 ]) error {
329332
330333 // Before we sign below, we also need to generate the tapscript With
331334 // the vPackets prepared, we can now sign the output asset we'll create
332335 // at a later step.
333- for _ , vPacket := range vPackets {
336+ for vPktIndex , vPacket := range vPackets {
334337 if len (vPacket .Inputs ) != 1 {
335338 return fmt .Errorf ("expected single input, got %v" ,
336339 len (vPacket .Inputs ))
@@ -346,12 +349,11 @@ func (a *AuxSweeper) signSweepVpackets(vPackets []*tappsbt.VPacket,
346349 // signature.
347350 signingKey , leafToSign := applySignDescToVIn (
348351 signDesc , vIn , & a .cfg .ChainParams ,
349- tapscriptDesc . scriptTree . TapTweak () ,
352+ tapTweak ,
350353 )
351354
352355 // In this case, the witness isn't special, so we'll set the
353356 // control block now for it.
354- ctrlBlock := tapscriptDesc .ctrlBlockBytes
355357 vIn .TaprootLeafScript [0 ].ControlBlock = ctrlBlock
356358
357359 log .Debugf ("signing vPacket for input=%v" ,
@@ -376,6 +378,52 @@ func (a *AuxSweeper) signSweepVpackets(vPackets []*tappsbt.VPacket,
376378 return fmt .Errorf ("error signing virtual packet, " +
377379 "got no sig" )
378380 }
381+
382+ // At this point, the witness looks like: <sig> <witnessScript>
383+ // <ctrlBlock>. This is a second level transaction, so we have
384+ // another signature that we need to add to the witness this
385+ // additional signature for the multi-sig.
386+ err = lfn .MapOptionZ (
387+ auxSigDesc ,
388+ func (aux lnwallet.AuxSigDesc ) error {
389+ assetSigs , err := cmsg .DecodeAssetSigListRecord (
390+ aux .AuxSig ,
391+ )
392+ if err != nil {
393+ return fmt .Errorf ("error " +
394+ "decoding asset sig list " +
395+ "record: %w" , err )
396+ }
397+ auxSig := assetSigs .Sigs [vPktIndex ]
398+
399+ // With the sig obtained, we'll now insert the
400+ // signature at the specified index.
401+ //nolint:lll
402+ sigIndex , err := secondLevelSigIndex .UnwrapOrErr (
403+ fmt .Errorf ("no sig index" ),
404+ )
405+ if err != nil {
406+ return err
407+ }
408+
409+ auxSigBytes := append (
410+ auxSig .Sig .Val .RawBytes (),
411+ byte (auxSig .SigHashType .Val ),
412+ )
413+
414+ newAsset := vPacket .Outputs [0 ].Asset
415+
416+ //nolint:lll
417+ prevWitness := newAsset .PrevWitnesses [0 ].TxWitness
418+ prevWitness = slices .Insert (
419+ prevWitness , int (sigIndex ), auxSigBytes ,
420+ )
421+ return newAsset .UpdateTxWitness (0 , prevWitness )
422+ },
423+ )
424+ if err != nil {
425+ return err
426+ }
379427 }
380428
381429 return nil
@@ -398,7 +446,10 @@ func (a *AuxSweeper) createAndSignSweepVpackets(
398446 signPkts := func (vPkts []* tappsbt.VPacket ,
399447 desc tapscriptSweepDesc ) lfn.Result [[]* tappsbt.VPacket ] {
400448
401- err := a .signSweepVpackets (vPkts , resReq .SignDesc , desc )
449+ err := a .signSweepVpackets (
450+ vPkts , resReq .SignDesc , nil , nil ,
451+ lfn .None [lnwallet.AuxSigDesc ](), lfn .None [uint32 ](),
452+ )
402453 if err != nil {
403454 return lfn.Err [returnType ](err )
404455 }
0 commit comments