From 5ce205d3c4b9548491b542ea98e41aa463e1e9d0 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 25 Apr 2024 14:26:57 +0200 Subject: [PATCH 1/2] feat: ios lsp log hook --- example/ldk/index.ts | 3 +++ .../Classes/LdkChannelManagerPersister.swift | 19 ++++++++++++++++--- lib/ios/Ldk.swift | 18 ++++++++++++++++++ lib/src/lightning-manager.ts | 18 ++++++++++++++++++ lib/src/utils/types.ts | 5 +++++ 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/example/ldk/index.ts b/example/ldk/index.ts index 5db14d4a..c4369695 100644 --- a/example/ldk/index.ts +++ b/example/ldk/index.ts @@ -152,6 +152,9 @@ export const setupLdk = async ( }, trustedZeroConfPeers: [peers.lnd.pubKey], skipRemoteBackups: !backupServerDetails, + lspLogEvent: async (payload) => { + console.log('Log event for LSP:', JSON.stringify(payload)); + }, }); if (lmStart.isErr()) { diff --git a/lib/ios/Classes/LdkChannelManagerPersister.swift b/lib/ios/Classes/LdkChannelManagerPersister.swift index 4577c9ec..61cf3c26 100644 --- a/lib/ios/Classes/LdkChannelManagerPersister.swift +++ b/lib/ios/Classes/LdkChannelManagerPersister.swift @@ -241,6 +241,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister { "reason": reasonString ] ) + return case .DiscardFunding: guard let discardFunding = event.getValueAsDiscardFunding() else { @@ -293,8 +294,21 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister { guard let bumpTransaction = event.getValueAsBumpTransaction() else { return handleEventError(event) } - - LdkEventEmitter.shared.send(withEvent: .native_log, body: "TODO📣: BumpTransaction") + + LdkEventEmitter.shared.send(withEvent: .native_log, body: "BumpTransaction request") + + if let channelClose = bumpTransaction.getValueAsChannelClose() { + let body: [String: Encodable] = [ + "commitment_txid": Data(channelClose.getCommitmentTx()).hexEncodedString(), + "commitment_tx_fee": channelClose.getCommitmentTxFeeSatoshis(), + "pending_htlcs_count": channelClose.getPendingHtlcs().count + ] + + LdkEventEmitter.shared.send(withEvent: .lsp_log, body: body) + return + } + + LdkEventEmitter.shared.send(withEvent: .native_log, body: "BumpTransaction event not handled") return case .ProbeFailed: LdkEventEmitter.shared.send(withEvent: .native_log, body: "Unused Persister event: ProbeFailed") @@ -308,7 +322,6 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister { case .HTLCIntercepted: LdkEventEmitter.shared.send(withEvent: .native_log, body: "Unused Persister event: HTLCIntercepted") return - case .HTLCHandlingFailed: LdkEventEmitter.shared.send(withEvent: .native_log, body: "Unused Persister event: HTLCHandlingFailed") return diff --git a/lib/ios/Ldk.swift b/lib/ios/Ldk.swift index feb81fa1..c83c7e87 100644 --- a/lib/ios/Ldk.swift +++ b/lib/ios/Ldk.swift @@ -25,6 +25,7 @@ enum EventTypes: String, CaseIterable { case network_graph_updated = "network_graph_updated" case channel_manager_restarted = "channel_manager_restarted" case backup_state_update = "backup_state_update" + case lsp_log = "lsp_log" } //***************************************************************** @@ -189,6 +190,7 @@ class Ldk: NSObject { } Logfile.log.setFilePath(logFile) + print("log file: \(path)") return handleResolve(resolve, .log_path_updated) } @@ -494,6 +496,22 @@ class Ldk: NSObject { currentBlockchainHeight = blockHeight addForegroundObserver() + + + + + ///TODO DELETE + let body: [String: Encodable] = [ + "commitment_txid": "Data(channelClose.getCommitmentTx()).hexEncodedString()", + "commitment_tx_fee": "channelClose.getCommitmentTxFeeSatoshis()", + "pending_htlcs_count": 69 + ] + + LdkEventEmitter.shared.send(withEvent: .lsp_log, body: body) + /// + + + return handleResolve(resolve, .channel_manager_init_success) } diff --git a/lib/src/lightning-manager.ts b/lib/src/lightning-manager.ts index 547823f8..b0811281 100644 --- a/lib/src/lightning-manager.ts +++ b/lib/src/lightning-manager.ts @@ -55,6 +55,8 @@ import { TCreatePaymentReq, TBackupServerDetails, IAddress, + TLspLogPayload, + TLspLogEvent, } from './utils/types'; import { appendPath, @@ -136,6 +138,7 @@ class LightningManager { }); trustedZeroConfPeers: string[] = []; broadcastTransaction: TBroadcastTransaction = async (): Promise => {}; + lspLogEvent: TLspLogEvent | undefined; pathFailedSubscription: EmitterSubscription | undefined; paymentFailedSubscription: EmitterSubscription | undefined; paymentSentSubscription: EmitterSubscription | undefined; @@ -225,6 +228,7 @@ class LightningManager { EEventTypes.channel_manager_restarted, this.onChannelManagerRestarted.bind(this), ); + ldk.onEvent(EEventTypes.lsp_log, this.onLspLogEvent.bind(this)); } /** @@ -293,6 +297,7 @@ class LightningManager { trustedZeroConfPeers = [], skipParamCheck = false, skipRemoteBackups = false, + lspLogEvent, }: TLdkStart): Promise> { if (!account) { return err( @@ -367,6 +372,7 @@ class LightningManager { this.watchOutputs = []; this.confirmedWatchOutputs = await this.getConfirmedWatchOutputs(); this.trustedZeroConfPeers = trustedZeroConfPeers; + this.lspLogEvent = lspLogEvent; if (!this.baseStoragePath) { return this.handleStartError( @@ -2051,6 +2057,18 @@ class LightningManager { await this.syncLdk(); } + private async onLspLogEvent(payload: any): Promise { + if (!this.lspLogEvent) { + return; + } + const nodeIdRes = await ldk.nodeId(); + + await this.lspLogEvent({ + body: JSON.stringify(payload), + nodeId: nodeIdRes.isOk() ? nodeIdRes.value : '', + }); + } + /** * Called with each sync to remove expired and unclaimed invoices. * An invoice is expired if there is no payments claimed (received) for it diff --git a/lib/src/utils/types.ts b/lib/src/utils/types.ts index 8d8f2e24..164ee159 100644 --- a/lib/src/utils/types.ts +++ b/lib/src/utils/types.ts @@ -34,6 +34,7 @@ export enum EEventTypes { network_graph_updated = 'network_graph_updated', channel_manager_restarted = 'channel_manager_restarted', backup_state_update = 'backup_state_update', + lsp_log = 'lsp_log', } //LDK event responses @@ -569,6 +570,7 @@ export type TLdkStart = { trustedZeroConfPeers?: string[]; skipParamCheck?: boolean; skipRemoteBackups?: boolean; + lspLogEvent?: TLspLogEvent; }; export interface IAddress { @@ -586,6 +588,9 @@ export type TGetScriptPubKeyHistoryResponse = { height: number; txid: string }; export type TBroadcastTransaction = (rawTx: string) => Promise; +export type TLspLogPayload = { nodeId: string; body: string }; +export type TLspLogEvent = (payload: TLspLogPayload) => Promise; + export type TGetFees = () => Promise; export type TVout = { hex: string; n: number; value: number }; From 85b6938ef7d192dd7dcfd9c63d47a13cf263f8c3 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 25 Apr 2024 15:10:32 +0200 Subject: [PATCH 2/2] feat: android lsp log hook --- .../main/java/com/reactnativeldk/LdkModule.kt | 3 ++- .../classes/LdkChannelManagerPersister.kt | 14 ++++++++++++++ .../Classes/LdkChannelManagerPersister.swift | 2 +- lib/ios/Ldk.swift | 17 ----------------- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt b/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt index ccd1cc93..5be5315c 100644 --- a/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt +++ b/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt @@ -53,7 +53,8 @@ enum class EventTypes { new_channel, network_graph_updated, channel_manager_restarted, - backup_state_update + backup_state_update, + lsp_log } //***************************************************************** diff --git a/lib/android/src/main/java/com/reactnativeldk/classes/LdkChannelManagerPersister.kt b/lib/android/src/main/java/com/reactnativeldk/classes/LdkChannelManagerPersister.kt index e5dcf8be..fcd44b85 100644 --- a/lib/android/src/main/java/com/reactnativeldk/classes/LdkChannelManagerPersister.kt +++ b/lib/android/src/main/java/com/reactnativeldk/classes/LdkChannelManagerPersister.kt @@ -5,8 +5,10 @@ import com.facebook.react.bridge.WritableMap import com.reactnativeldk.* import org.json.JSONArray import org.ldk.batteries.ChannelManagerConstructor +import org.ldk.structs.BumpTransactionEvent import org.ldk.structs.ClosureReason import org.ldk.structs.Event +import org.ldk.structs.Event.BumpTransaction import org.ldk.structs.Option_ThirtyTwoBytesZ import org.ldk.structs.Option_u64Z import org.ldk.structs.PaymentPurpose @@ -190,6 +192,18 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler { (event as? Event.ChannelPending)?.let { channelPending -> } + + (event as? Event.BumpTransaction)?.let { bumpTransaction -> + LdkEventEmitter.send(EventTypes.native_log, "BumpTransaction request") + + (bumpTransaction.bump_transaction as? BumpTransactionEvent.ChannelClose)?.let { channelClose -> + val body = Arguments.createMap() + body.putString("commitment_tx", channelClose.commitment_tx.hexEncodedString()) + body.putInt("commitment_tx_fee", channelClose.commitment_tx_fee_satoshis.toInt()) + body.putInt("pending_htlcs_count", channelClose.pending_htlcs.size) + return LdkEventEmitter.send(EventTypes.lsp_log, body) + } + } } override fun persist_manager(channel_manager_bytes: ByteArray?) { diff --git a/lib/ios/Classes/LdkChannelManagerPersister.swift b/lib/ios/Classes/LdkChannelManagerPersister.swift index 61cf3c26..b4158c40 100644 --- a/lib/ios/Classes/LdkChannelManagerPersister.swift +++ b/lib/ios/Classes/LdkChannelManagerPersister.swift @@ -299,7 +299,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister { if let channelClose = bumpTransaction.getValueAsChannelClose() { let body: [String: Encodable] = [ - "commitment_txid": Data(channelClose.getCommitmentTx()).hexEncodedString(), + "commitment_tx": Data(channelClose.getCommitmentTx()).hexEncodedString(), "commitment_tx_fee": channelClose.getCommitmentTxFeeSatoshis(), "pending_htlcs_count": channelClose.getPendingHtlcs().count ] diff --git a/lib/ios/Ldk.swift b/lib/ios/Ldk.swift index c83c7e87..9fd7d62b 100644 --- a/lib/ios/Ldk.swift +++ b/lib/ios/Ldk.swift @@ -190,7 +190,6 @@ class Ldk: NSObject { } Logfile.log.setFilePath(logFile) - print("log file: \(path)") return handleResolve(resolve, .log_path_updated) } @@ -495,22 +494,6 @@ class Ldk: NSObject { currentBlockchainTipHash = blockHash currentBlockchainHeight = blockHeight addForegroundObserver() - - - - - - ///TODO DELETE - let body: [String: Encodable] = [ - "commitment_txid": "Data(channelClose.getCommitmentTx()).hexEncodedString()", - "commitment_tx_fee": "channelClose.getCommitmentTxFeeSatoshis()", - "pending_htlcs_count": 69 - ] - - LdkEventEmitter.shared.send(withEvent: .lsp_log, body: body) - /// - - return handleResolve(resolve, .channel_manager_init_success) }