@@ -1637,14 +1637,26 @@ class LightningManager {
16371637 * Returns previously broadcasted transactions saved in storgare.
16381638 * @returns {Promise<TLdkBroadcastedTransactions> }
16391639 */
1640- async getLdkBroadcastedTxs ( ) : Promise < TLdkBroadcastedTransactions > {
1640+ async getLdkBroadcastedTxs (
1641+ includeConfirmed : boolean = false ,
1642+ ) : Promise < TLdkBroadcastedTransactions > {
1643+ let txs : TLdkBroadcastedTransactions = [ ] ;
16411644 const res = await ldk . readFromFile ( {
16421645 fileName : ELdkFiles . broadcasted_transactions ,
16431646 } ) ;
16441647 if ( res . isOk ( ) ) {
1645- return parseData ( res . value . content , [ ] ) ;
1648+ txs = parseData ( res . value . content , [ ] ) ;
16461649 }
1647- return [ ] ;
1650+
1651+ if ( includeConfirmed ) {
1652+ const confirmedRes = await ldk . readFromFile ( {
1653+ fileName : ELdkFiles . confirmed_broadcasted_transactions ,
1654+ } ) ;
1655+ if ( confirmedRes . isOk ( ) ) {
1656+ txs = txs . concat ( parseData ( confirmedRes . value . content , [ ] ) ) ;
1657+ }
1658+ }
1659+ return txs ;
16481660 }
16491661
16501662 async cleanupBroadcastedTxs ( ) : Promise < void > {
@@ -1766,33 +1778,41 @@ class LightningManager {
17661778 return err ( 'Unable to retrieve change_destination_script.' ) ;
17671779 }
17681780
1769- const txs = await this . getLdkBroadcastedTxs ( ) ;
1781+ const txs = await this . getLdkBroadcastedTxs ( true ) ;
17701782 if ( ! txs . length ) {
17711783 return ok ( 'No outputs to reconstruct as no cached transactions found.' ) ;
17721784 }
17731785
17741786 let txsToBroadcast = 0 ;
1775- for ( const hexTx of txs ) {
1776- const tx = bitcoin . Transaction . fromHex ( hexTx ) ;
1777- const txData = await this . getTransactionData ( tx . getId ( ) ) ;
17781787
1779- const txsRes = await ldk . spendRecoveredForceCloseOutputs ( {
1780- transaction : hexTx ,
1781- confirmationHeight : txData ?. height ?? 0 ,
1782- changeDestinationScript,
1783- } ) ;
1788+ const processTxs = async ( useInner : boolean ) : Promise < void > => {
1789+ for ( const hexTx of txs ) {
1790+ const tx = bitcoin . Transaction . fromHex ( hexTx ) ;
1791+ const txData = await this . getTransactionData ( tx . getId ( ) ) ;
17841792
1785- if ( txsRes . isErr ( ) ) {
1786- await ldk . writeToLogFile ( 'error' , txsRes . error . message ) ;
1787- console . error ( txsRes . error . message ) ;
1788- continue ;
1789- }
1793+ const txsRes = await ldk . spendRecoveredForceCloseOutputs ( {
1794+ transaction : hexTx ,
1795+ confirmationHeight : txData ?. height ?? 0 ,
1796+ changeDestinationScript,
1797+ useInner,
1798+ } ) ;
1799+
1800+ if ( txsRes . isErr ( ) ) {
1801+ await ldk . writeToLogFile ( 'error' , txsRes . error . message ) ;
1802+ console . error ( txsRes . error . message ) ;
1803+ continue ;
1804+ }
17901805
1791- for ( const createdTx of txsRes . value ) {
1792- txsToBroadcast ++ ;
1793- await this . broadcastTransaction ( createdTx ) ;
1806+ for ( const createdTx of txsRes . value ) {
1807+ txsToBroadcast ++ ;
1808+ await this . broadcastTransaction ( createdTx ) ;
1809+ }
17941810 }
1795- }
1811+ } ;
1812+
1813+ //Process first using the inner (ldk keychain) and then using the custom keychain
1814+ await processTxs ( true ) ;
1815+ await processTxs ( false ) ;
17961816
17971817 return ok ( `Attempting to reconstruct ${ txsToBroadcast } transactions.` ) ;
17981818 }
0 commit comments