@@ -232,6 +232,9 @@ class LightningManager {
232232 this . onChannelManagerRestarted . bind ( this ) ,
233233 ) ;
234234 ldk . onEvent ( EEventTypes . lsp_log , this . onLspLogEvent . bind ( this ) ) ;
235+ ldk . onEvent ( EEventTypes . used_close_address , ( address : string ) => {
236+ this . saveAddressToFile ( address ) . catch ( console . error ) ;
237+ } ) ;
235238 }
236239
237240 /**
@@ -364,14 +367,7 @@ class LightningManager {
364367 this . account = account ;
365368 this . network = network ;
366369 this . addresses = await this . readAddressesFromFile ( ) ;
367- this . getAddress = async ( ) => {
368- const addressObj = await getAddress ( ) ;
369- const address = addressObj ?. address ;
370- if ( address ) {
371- this . saveAddressToFile ( address ) . then ( ) ;
372- }
373- return addressObj ;
374- } ;
370+ this . getAddress = getAddress ;
375371 this . getScriptPubKeyHistory = getScriptPubKeyHistory ;
376372 this . getFees = getFees ;
377373 this . broadcastTransaction = broadcastTransaction ;
@@ -452,6 +448,7 @@ class LightningManager {
452448 // ldk.setLogLevel(ELdkLogLevels.trace, true),
453449 ldk . initKeysManager ( {
454450 seed : this . account . seed ,
451+ address : closeAddress . address ,
455452 channelCloseDestinationScriptPublicKey : closeAddress . publicKey ,
456453 channelCloseWitnessProgram : witnessProgram ,
457454 channelCloseWitnessProgramVersion : witnessProgramVersion ,
@@ -513,6 +510,7 @@ class LightningManager {
513510 ldk . nodeStateDump ( ) . catch ( console . error ) ;
514511
515512 this . cleanupBroadcastedTxs ( ) . catch ( console . error ) ;
513+ this . resetAddressFileIfUnused ( ) . catch ( console . error ) ;
516514
517515 this . isStarting = false ;
518516 const result = ok ( 'Node started' ) ;
@@ -769,6 +767,37 @@ class LightningManager {
769767 return ok ( 'Watch transactions checked' ) ;
770768 } ;
771769
770+ /*
771+ * Previously we were writing all addresses to file even
772+ * if they may not have been used in a channel shutdown script or
773+ * force close sweeping. So reset the file if there is no evidence of prior channels.
774+ * */
775+ resetAddressFileIfUnused = async ( ) : Promise < void > => {
776+ //If no channel files found and no addresses are being used, reset the address file.
777+ const channelFilesRes = await ldk . listChannelFiles ( ) ;
778+ if ( channelFilesRes . isErr ( ) ) {
779+ return ;
780+ }
781+
782+ if ( channelFilesRes . value . length > 0 ) {
783+ return ;
784+ }
785+
786+ const accountPath = appendPath ( this . baseStoragePath , this . account . name ) ;
787+ const writeRes = await ldk . writeToFile ( {
788+ fileName : ELdkFiles . addresses ,
789+ path : accountPath ,
790+ content : JSON . stringify ( [ ] ) ,
791+ remotePersist : false ,
792+ } ) ;
793+
794+ this . addresses = [ ] ;
795+
796+ if ( writeRes . isErr ( ) ) {
797+ console . error ( writeRes . error ) ;
798+ }
799+ } ;
800+
772801 saveAddressToFile = async ( address : string ) : Promise < Result < boolean > > => {
773802 if ( ! address ) {
774803 return err ( 'No address provided' ) ;
@@ -1822,6 +1851,7 @@ class LightningManager {
18221851 changeDestinationScript : changeDestinationScript ,
18231852 } ;
18241853 const res = await ldk . reconstructAndSpendOutputs ( req ) ;
1854+ await this . saveAddressToFile ( address . address ) ;
18251855
18261856 if ( res . isOk ( ) ) {
18271857 reconstructedTxs ++ ;
@@ -2127,6 +2157,7 @@ class LightningManager {
21272157 res : TChannelManagerSpendableOutputs ,
21282158 ) : Promise < void > {
21292159 const spendableOutputs = await this . getLdkSpendableOutputs ( ) ;
2160+
21302161 res . outputsSerialized . forEach ( ( o ) => {
21312162 if ( ! spendableOutputs . includes ( o ) ) {
21322163 spendableOutputs . push ( o ) ;
@@ -2172,6 +2203,9 @@ class LightningManager {
21722203 return ;
21732204 }
21742205
2206+ //Address was used to sweep to so wallet needs to watch it
2207+ await this . saveAddressToFile ( address . address ) ;
2208+
21752209 await ldk . writeToLogFile (
21762210 'info' ,
21772211 `Created tx spending outputs: ${ spendRes . value } ` ,
0 commit comments