@@ -1250,6 +1250,42 @@ func (a *AuxSweeper) importCommitScriptKeys(req lnwallet.ResolutionReq) error {
1250
1250
return nil
1251
1251
}
1252
1252
1253
+ // importOutputScriptKey imports the output script key that this scriptDesc can
1254
+ // spend into the local addr book.
1255
+ func (a * AuxSweeper ) importOutputScriptKeys (desc tapscriptSweepDescs ) error {
1256
+ ctxb := context .Background ()
1257
+
1258
+ importScriptKey := func (desc tapscriptSweepDesc ) error {
1259
+ scriptTree := desc .scriptTree .Tree ()
1260
+
1261
+ outputKey := asset .NewScriptKey (scriptTree .TaprootKey ).PubKey
1262
+ scriptKey := asset.ScriptKey {
1263
+ PubKey : outputKey ,
1264
+ TweakedScriptKey : & asset.TweakedScriptKey {
1265
+ RawKey : keychain.KeyDescriptor {
1266
+ PubKey : scriptTree .InternalKey ,
1267
+ },
1268
+ Tweak : scriptTree .TapscriptRoot ,
1269
+ },
1270
+ }
1271
+
1272
+ log .Debugf ("Importing script_keys=%v" , spew .Sdump (scriptKey ))
1273
+
1274
+ return a .cfg .AddrBook .InsertScriptKey (ctxb , scriptKey , true )
1275
+ }
1276
+
1277
+ if err := importScriptKey (desc .firstLevel ); err != nil {
1278
+ return err
1279
+ }
1280
+
1281
+ return lfn .MapOptionZ (
1282
+ desc .secondLevel ,
1283
+ func (secondary tapscriptSweepDesc ) error {
1284
+ return importScriptKey (secondary )
1285
+ },
1286
+ )
1287
+ }
1288
+
1253
1289
// importOutputProofs imports the output proofs into the pending asset funding
1254
1290
// into our local database. This preps us to be able to detect force closes.
1255
1291
func importOutputProofs (scid lnwire.ShortChannelID ,
@@ -1618,34 +1654,10 @@ func (a *AuxSweeper) resolveContract(
1618
1654
return lfn.Err [returnType ](err )
1619
1655
}
1620
1656
1621
- // To be able to construct all the proofs we need to spend later, we'll
1622
- // make sure that this commitment transaction exists in our database.
1623
- // If not, then we'll complete the proof, register the script keys, and
1624
- // ship the pre-signed commitment transaction.
1625
- ctx := context .Background ()
1626
- commitParcel , err := a .cfg .TxSender .QueryParcels (
1627
- ctx , fn .Some (req .CommitTx .TxHash ()), false ,
1628
- )
1629
- if err != nil {
1630
- return lfn.Err [returnType ](err )
1631
- }
1632
- if len (commitParcel ) == 0 {
1633
- log .Infof ("First time seeing commit_txid=%v, importing" ,
1634
- req .CommitTx .TxHash ())
1635
-
1636
- err := a .importCommitTx (req , commitState , fundingInfo )
1637
- if err != nil {
1638
- return lfn .Errf [returnType ]("unable to import " +
1639
- "commitment txn: %w" , err )
1640
- }
1641
- } else {
1642
- log .Infof ("Commitment commit_txid=%v already imported, " +
1643
- "skipping" , req .CommitTx .TxHash ())
1644
- }
1645
-
1646
1657
var (
1647
- sweepDesc lfn.Result [tapscriptSweepDescs ]
1648
- assetOutputs []* cmsg.AssetOutput
1658
+ sweepDesc lfn.Result [tapscriptSweepDescs ]
1659
+ assetOutputs []* cmsg.AssetOutput
1660
+ needsSecondLevel bool
1649
1661
)
1650
1662
1651
1663
switch req .Type {
@@ -1754,6 +1766,8 @@ func (a *AuxSweeper) resolveContract(
1754
1766
// desc.
1755
1767
sweepDesc = localHtlcTimeoutSweepDesc (req )
1756
1768
1769
+ needsSecondLevel = true
1770
+
1757
1771
// In this case, we've broadcast a commitment, with an incoming HTLC
1758
1772
// that we can sweep. We'll annotate the sweepDesc with the information
1759
1773
// needed to sweep both this output, as well as the second level
@@ -1768,26 +1782,61 @@ func (a *AuxSweeper) resolveContract(
1768
1782
// desc.
1769
1783
sweepDesc = localHtlcSucessSweepDesc (req )
1770
1784
1785
+ needsSecondLevel = true
1786
+
1771
1787
default :
1772
1788
return lfn .Errf [returnType ]("unknown resolution type: %v" ,
1773
1789
req .Type )
1774
1790
// TODO(roasbeef): need to do HTLC revocation casesj:w
1775
1791
}
1776
1792
1793
+ tapSweepDesc , err := sweepDesc .Unpack ()
1794
+ if err != nil {
1795
+ return lfn.Err [tlv.Blob ](err )
1796
+ }
1797
+
1798
+ // Now that we know what output we're sweeping, before we proceed, we'll
1799
+ // import the relevant script key to disk. This way, we'll properly
1800
+ // recognize spends of it.
1801
+ if err := a .importOutputScriptKeys (tapSweepDesc ); err != nil {
1802
+ return lfn .Errf [tlv.Blob ]("unable to import output script " +
1803
+ "key: %w" , err )
1804
+ }
1805
+
1806
+ // To be able to construct all the proofs we need to spend later, we'll
1807
+ // make sure that this commitment transaction exists in our database. If
1808
+ // not, then we'll complete the proof, register the script keys, and
1809
+ // ship the pre-signed commitment transaction.
1810
+ ctx := context .Background ()
1811
+ commitParcel , err := a .cfg .TxSender .QueryParcels (
1812
+ ctx , fn .Some (req .CommitTx .TxHash ()), false ,
1813
+ )
1814
+ if err != nil {
1815
+ return lfn.Err [returnType ](err )
1816
+ }
1817
+ if len (commitParcel ) == 0 {
1818
+ log .Infof ("First time seeing commit_txid=%v, importing" ,
1819
+ req .CommitTx .TxHash ())
1820
+
1821
+ err := a .importCommitTx (req , commitState , fundingInfo )
1822
+ if err != nil {
1823
+ return lfn .Errf [returnType ]("unable to import " +
1824
+ "commitment txn: %w" , err )
1825
+ }
1826
+ } else {
1827
+ log .Infof ("Commitment commit_txid=%v already imported, " +
1828
+ "skipping" , req .CommitTx .TxHash ())
1829
+ }
1830
+
1777
1831
// The input proofs above were made originally using the fake commit tx
1778
1832
// as an anchor. We now know the real commit tx, so we'll swap that in
1779
1833
// to ensure the outpoints used below are correct.
1780
1834
for _ , assetOut := range assetOutputs {
1781
1835
assetOut .Proof .Val .AnchorTx = * req .CommitTx
1782
1836
}
1783
1837
1784
- log .Infof ("Sweeping %v asset outputs: %v" , len (assetOutputs ),
1785
- limitSpewer .Sdump (assetOutputs ))
1786
-
1787
- tapSweepDesc , err := sweepDesc .Unpack ()
1788
- if err != nil {
1789
- return lfn.Err [tlv.Blob ](err )
1790
- }
1838
+ log .Infof ("Sweeping %v asset outputs (second_level=%v): %v" ,
1839
+ len (assetOutputs ), needsSecondLevel , spew .Sdump (assetOutputs ))
1791
1840
1792
1841
// With the sweep desc constructed above, we'll create vPackets for each
1793
1842
// of the local assets, then sign them all.
0 commit comments