@@ -9,7 +9,7 @@ import Foundation
99import LightningDevKit
1010
1111class LdkChannelManagerPersister : Persister , ExtendedChannelManagerPersister {
12- //Custom function to manage any unlikely missing info from the event object
12+ // Custom function to manage any unlikely missing info from the event object
1313 func handleEventError( _ event: Event ) {
1414 LdkEventEmitter . shared. send (
1515 withEvent: . native_log,
@@ -25,14 +25,14 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
2525 guard let fundingGeneration = event. getValueAsFundingGenerationReady ( ) else {
2626 return handleEventError ( event)
2727 }
28-
28+
2929 LdkEventEmitter . shared. send (
3030 withEvent: . channel_manager_funding_generation_ready,
3131 body: [
3232 " temp_channel_id " : Data ( fundingGeneration. getTemporaryChannelId ( ) . getA ( ) ?? [ ] ) . hexEncodedString ( ) ,
3333 " output_script " : Data ( fundingGeneration. getOutputScript ( ) ) . hexEncodedString ( ) ,
3434 " user_channel_id " : Data ( fundingGeneration. getUserChannelId ( ) ) . hexEncodedString ( ) ,
35- " value_satoshis " : fundingGeneration. getChannelValueSatoshis ( ) ,
35+ " value_satoshis " : fundingGeneration. getChannelValueSatoshis ( )
3636 ]
3737 )
3838 return
@@ -60,7 +60,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
6060 body: body
6161 )
6262
63- //Save to disk for TX history
63+ // Save to disk for TX history
6464 persistPaymentClaimed ( body)
6565 return
6666 case . PaymentSent:
@@ -82,11 +82,11 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
8282 body: body
8383 )
8484
85- //Save to disk for tx history
85+ // Save to disk for tx history
8686 persistPaymentSent ( body)
8787 return
8888 case . OpenChannelRequest:
89- //Use if we ever manually accept inbound channels. Setting in initConfig.
89+ // Use if we ever manually accept inbound channels. Setting in initConfig.
9090 guard let openChannelRequest = event. getValueAsOpenChannelRequest ( ) else {
9191 return handleEventError ( event)
9292 }
@@ -100,7 +100,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
100100 " requires_zero_conf " : openChannelRequest. getChannelType ( ) . requiresZeroConf ( ) ,
101101 " supports_zero_conf " : openChannelRequest. getChannelType ( ) . supportsZeroConf ( ) ,
102102 " requires_anchors_zero_fee_htlc_tx " : openChannelRequest. getChannelType ( ) . requiresAnchorsZeroFeeHtlcTx ( )
103- ] as [ String : Any ]
103+ ] as [ String : Any ]
104104 )
105105 return
106106 case . PaymentPathSuccessful:
@@ -116,8 +116,8 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
116116 body: [
117117 " payment_id " : paymentId,
118118 " payment_hash " : paymentHash,
119- " path_hops " : paymentPathSuccessful. getPath ( ) . getHops ( ) . map { $0. asJson } ,
120- ] as [ String : Any ]
119+ " path_hops " : paymentPathSuccessful. getPath ( ) . getHops ( ) . map { $0. asJson }
120+ ] as [ String : Any ]
121121 )
122122 return
123123 case . PaymentPathFailed:
@@ -136,15 +136,15 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
136136 " payment_failed_permanently " : paymentPathFailed. getPaymentFailedPermanently ( ) ,
137137 " short_channel_id " : String ( paymentPathFailed. getShortChannelId ( ) ?? 0 ) ,
138138 " path_hops " : paymentPathFailed. getPath ( ) . getHops ( ) . map { $0. asJson }
139- ] as [ String : Any ]
139+ ] as [ String : Any ]
140140 )
141141
142142 persistPaymentSent (
143143 [
144144 " payment_id " : paymentId,
145145 " payment_hash " : paymentHash,
146146 " unix_timestamp " : Int ( Date ( ) . timeIntervalSince1970) ,
147- " state " : paymentPathFailed. getPaymentFailedPermanently ( ) ? " failed " : " pending "
147+ " state " : paymentPathFailed. getPaymentFailedPermanently ( ) ? " failed " : " pending "
148148 ]
149149 )
150150 return
@@ -160,23 +160,23 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
160160 withEvent: . channel_manager_payment_failed,
161161 body: [
162162 " payment_id " : paymentId,
163- " payment_hash " : paymentHash,
163+ " payment_hash " : paymentHash
164164 ]
165165 )
166166
167- //MARK: Mark as failed
167+ // MARK: Mark as failed
168168
169169 persistPaymentSent (
170170 [
171171 " payment_id " : paymentId,
172172 " payment_hash " : paymentHash,
173173 " unix_timestamp " : Int ( Date ( ) . timeIntervalSince1970) ,
174- " state " : " failed "
174+ " state " : " failed "
175175 ]
176176 )
177177 return
178178 case . PaymentForwarded:
179- //Unused on mobile
179+ // Unused on mobile
180180 return
181181 case . PendingHTLCsForwardable:
182182 guard let pendingHtlcsForwardable = event. getValueAsPendingHtlcsForwardable ( ) else {
@@ -186,7 +186,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
186186 LdkEventEmitter . shared. send (
187187 withEvent: . channel_manager_pending_htlcs_forwardable,
188188 body: [
189- " time_forwardable " : pendingHtlcsForwardable. getTimeForwardable ( ) ,
189+ " time_forwardable " : pendingHtlcsForwardable. getTimeForwardable ( )
190190 ]
191191 )
192192 return
@@ -198,7 +198,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
198198 LdkEventEmitter . shared. send (
199199 withEvent: . channel_manager_spendable_outputs,
200200 body: [
201- " outputsSerialized " : spendableOutputs. getOutputs ( ) . map { Data ( $0. write ( ) ) . hexEncodedString ( ) } ,
201+ " outputsSerialized " : spendableOutputs. getOutputs ( ) . map { Data ( $0. write ( ) ) . hexEncodedString ( ) }
202202 ]
203203 )
204204 return
@@ -208,13 +208,16 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
208208 }
209209
210210 let reasonString : String
211+ var peerMessage : String ? = nil
212+
211213 switch channelClosed. getReason ( ) . getValueType ( ) {
212214 case . CommitmentTxConfirmed:
213215 reasonString = " CommitmentTxConfirmed "
214216 case . CounterpartyCoopClosedUnfundedChannel:
215217 reasonString = " CounterpartyCoopClosedUnfundedChannel "
216218 case . CounterpartyForceClosed:
217219 reasonString = " CounterpartyForceClosed "
220+ peerMessage = channelClosed. getReason ( ) . getValueAsCounterpartyForceClosed ( ) ? . getPeerMsg ( ) . getA ( )
218221 case . DisconnectedPeer:
219222 reasonString = " DisconnectedPeer "
220223 case . FundingBatchClosure:
@@ -244,17 +247,18 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
244247 body: [
245248 " user_channel_id " : Data ( channelClosed. getUserChannelId ( ) ) . hexEncodedString ( ) ,
246249 " channel_id " : Data ( channelClosed. getChannelId ( ) . getA ( ) ?? [ ] ) . hexEncodedString ( ) ,
247- " reason " : reasonString
250+ " reason " : reasonString,
251+ " peer_message " : peerMessage
248252 ]
249253 )
250-
254+
251255 return
252256 case . DiscardFunding:
253257 guard let discardFunding = event. getValueAsDiscardFunding ( ) else {
254258 return handleEventError ( event)
255259 }
256260
257- //Wallet should probably "lock" the UTXOs spent in funding transactions until the funding transaction either confirms, or this event is generated.
261+ // Wallet should probably "lock" the UTXOs spent in funding transactions until the funding transaction either confirms, or this event is generated.
258262 LdkEventEmitter . shared. send (
259263 withEvent: . channel_manager_discard_funding,
260264 body: [
@@ -287,7 +291,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
287291 body: body
288292 )
289293
290- //Save to disk for TX history
294+ // Save to disk for TX history
291295 persistPaymentClaimed ( body)
292296 return
293297 case . ChannelReady:
@@ -300,7 +304,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
300304 guard let bumpTransaction = event. getValueAsBumpTransaction ( ) else {
301305 return handleEventError ( event)
302306 }
303-
307+
304308 LdkEventEmitter . shared. send ( withEvent: . native_log, body: " BumpTransaction request " )
305309
306310 if let channelClose = bumpTransaction. getValueAsChannelClose ( ) {
@@ -309,11 +313,11 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
309313 " commitment_tx_fee " : channelClose. getCommitmentTxFeeSatoshis ( ) ,
310314 " pending_htlcs_count " : channelClose. getPendingHtlcs ( ) . count
311315 ]
312-
316+
313317 LdkEventEmitter . shared. send ( withEvent: . lsp_log, body: body)
314318 return
315319 }
316-
320+
317321 LdkEventEmitter . shared. send ( withEvent: . native_log, body: " BumpTransaction event not handled " )
318322 return
319323 case . ProbeFailed:
@@ -413,23 +417,23 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
413417 }
414418 }
415419
416- //Replace entry if payment hash exists (Confirmed payment replacing pending)
420+ // Replace entry if payment hash exists (Confirmed payment replacing pending)
417421 var paymentReplaced = false
418422 for (index, existingPayment) in payments. enumerated ( ) {
419423 if let existingPaymentHash = existingPayment [ " payment_hash " ] as? String , let newPaymentHash = payment [ " payment_hash " ] as? String {
420424 if existingPaymentHash == newPaymentHash {
421- //Don't replace a successful payment. If a 2nd wallet tries to pay an invoice the first successful one should not be overwritten.
425+ // Don't replace a successful payment. If a 2nd wallet tries to pay an invoice the first successful one should not be overwritten.
422426 if existingPayment [ " state " ] as? String == " successful " {
423427 return
424428 }
425-
426- payments [ index] = mergeObj ( payments [ index] , payment) //Merges update into orginal entry
429+
430+ payments [ index] = mergeObj ( payments [ index] , payment) // Merges update into orginal entry
427431 paymentReplaced = true
428432 }
429433 }
430434 }
431435
432- //No existing payment found, append as new payment
436+ // No existing payment found, append as new payment
433437 if !paymentReplaced {
434438 payments. append ( payment)
435439 }
@@ -472,18 +476,18 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
472476 }
473477 }
474478
475- //Replace entry if payment hash exists (Confirmed payment replacing pending)
479+ // Replace entry if payment hash exists (Confirmed payment replacing pending)
476480 var paymentReplaced = false
477481 for (index, existingPayment) in payments. enumerated ( ) {
478482 if let existingPaymentId = existingPayment [ " payment_id " ] as? String , let newPaymentId = payment [ " payment_id " ] as? String {
479483 if existingPaymentId == newPaymentId {
480- payments [ index] = mergeObj ( payments [ index] , payment) //Merges update into orginal entry
484+ payments [ index] = mergeObj ( payments [ index] , payment) // Merges update into orginal entry
481485 paymentReplaced = true
482486 }
483487 }
484488 }
485489
486- //No existing payment found, append as new payment
490+ // No existing payment found, append as new payment
487491 if !paymentReplaced {
488492 payments. append ( payment)
489493 }
0 commit comments