11package com.reactnativeldk.classes
22
3+ import com.facebook.react.bridge.Arguments
4+ import com.reactnativeldk.*
35import org.ldk.batteries.ChannelManagerConstructor
46import org.ldk.structs.Event
7+ import org.ldk.structs.Option_u64Z
8+ import org.ldk.structs.PaymentPurpose
59
610class LdkChannelManagerPersister {
711 var channelManagerPersister = object : ChannelManagerConstructor .EventHandler {
812 override fun handle_event (event : Event ) {
9- // TODO
13+ (event as ? Event .FundingGenerationReady )?.let { fundingGenerationReady ->
14+ val body = Arguments .createMap()
15+ body.putHexString(" temp_channel_id" , fundingGenerationReady.temporary_channel_id)
16+ body.putHexString(" output_script" , fundingGenerationReady.output_script)
17+ body.putInt(" user_channel_id" , fundingGenerationReady.user_channel_id.toInt())
18+ body.putInt(" value_satoshis" , fundingGenerationReady.channel_value_satoshis.toInt())
19+ return LdkEventEmitter .send(EventTypes .channel_manager_funding_generation_ready, body)
20+ }
21+
22+ (event as ? Event .PaymentReceived )?.let { paymentReceived ->
23+ val body = Arguments .createMap()
24+ body.putHexString(" payment_hash" , paymentReceived.payment_hash)
25+ body.putInt(" amount" , paymentReceived.amt.toInt())
26+ (paymentReceived.purpose as ? PaymentPurpose .InvoicePayment )?.let {
27+ body.putHexString(" payment_preimage" , it.payment_preimage)
28+ body.putHexString(" payment_secret" , it.payment_secret)
29+ }
30+ (paymentReceived.purpose as ? PaymentPurpose .SpontaneousPayment )?.let {
31+ body.putHexString(" spontaneous_payment_preimage" , it.spontaneous_payment)
32+ }
33+ return LdkEventEmitter .send(EventTypes .channel_manager_payment_received, body)
34+ }
35+
36+ (event as ? Event .PaymentSent )?.let { paymentSent ->
37+ val body = Arguments .createMap()
38+ body.putHexString(" payment_id" , paymentSent.payment_id)
39+ body.putHexString(" payment_preimage" , paymentSent.payment_preimage)
40+ body.putHexString(" payment_hash" , paymentSent.payment_hash)
41+ body.putInt(" fee_paid_msat" , (paymentSent.fee_paid_msat as Option_u64Z .Some ).some.toInt())
42+ return LdkEventEmitter .send(EventTypes .channel_manager_payment_sent, body)
43+ }
44+
45+ (event as ? Event .OpenChannelRequest )?.let { openChannelRequest ->
46+ // Use if we ever manually accept inbound channels. Setting in initConfig.
47+ val body = Arguments .createMap()
48+ body.putHexString(" temp_channel_id" , openChannelRequest.temporary_channel_id)
49+ body.putHexString(" counterparty_node_id" , openChannelRequest.counterparty_node_id)
50+ body.putInt(" push_msat" , openChannelRequest.push_msat.toInt())
51+ body.putInt(" funding_satoshis" , openChannelRequest.funding_satoshis.toInt())
52+ body.putHexString(" channel_type" , openChannelRequest.channel_type.write())
53+ return LdkEventEmitter .send(EventTypes .channel_manager_open_channel_request, body)
54+ }
55+
56+ (event as ? Event .PaymentPathSuccessful )?.let { paymentPathSuccessful ->
57+ val body = Arguments .createMap()
58+
59+ body.putHexString(" payment_id" , paymentPathSuccessful.payment_id)
60+ body.putHexString(" payment_hash" , paymentPathSuccessful.payment_hash)
61+
62+ val path = Arguments .createArray()
63+ paymentPathSuccessful.path.iterator().forEach { path.pushMap(it.asJson) }
64+ body.putArray(" path" , path)
65+
66+ return LdkEventEmitter .send(EventTypes .channel_manager_payment_path_successful, body)
67+ }
68+
69+ (event as ? Event .PaymentPathFailed )?.let { paymentPathFailed ->
70+ val body = Arguments .createMap()
71+ body.putHexString(" payment_id" , paymentPathFailed.payment_id)
72+ body.putHexString(" payment_hash" , paymentPathFailed.payment_hash)
73+ body.putBoolean(" rejected_by_dest" , paymentPathFailed.rejected_by_dest)
74+ body.putInt(" short_channel_id" , (paymentPathFailed.short_channel_id as Option_u64Z .Some ).some.toInt())
75+ val path = Arguments .createArray()
76+ paymentPathFailed.path.iterator().forEach { path.pushMap(it.asJson) }
77+ body.putArray(" path" , path)
78+ body.putString(" network_update" , paymentPathFailed.network_update.toString()) // TODO could be more detailed
79+
80+ return LdkEventEmitter .send(EventTypes .channel_manager_payment_path_failed, body)
81+ }
82+
83+ (event as ? Event .PaymentFailed )?.let { paymentFailed ->
84+ val body = Arguments .createMap()
85+ body.putHexString(" payment_id" , paymentFailed.payment_id)
86+ body.putHexString(" payment_hash" , paymentFailed.payment_hash)
87+ return LdkEventEmitter .send(EventTypes .channel_manager_payment_failed, body)
88+ }
89+
90+ (event as ? Event .PaymentForwarded )?.let { paymentForwarded ->
91+ // Unused on mobile
92+ }
93+
94+ (event as ? Event .PendingHTLCsForwardable )?.let { pendingHTLCsForwardable ->
95+ val body = Arguments .createMap()
96+ body.putInt(" time_forwardable" , pendingHTLCsForwardable.time_forwardable.toInt())
97+ return LdkEventEmitter .send(EventTypes .channel_manager_pending_htlcs_forwardable, body)
98+ }
99+
100+ (event as ? Event .SpendableOutputs )?.let { spendableOutputs ->
101+ val body = Arguments .createMap()
102+ val outputs = Arguments .createArray()
103+ spendableOutputs.outputs.iterator().forEach {
104+ outputs.pushHexString(it.write())
105+ }
106+ body.putArray(" outputs" , outputs)
107+ return LdkEventEmitter .send(EventTypes .channel_manager_spendable_outputs, body)
108+ }
109+
110+ (event as ? Event .ChannelClosed )?.let { channelClosed ->
111+ val body = Arguments .createMap()
112+ body.putInt(" user_channel_id" , channelClosed.user_channel_id.toInt())
113+ body.putHexString(" channel_id" , channelClosed.channel_id)
114+ body.putHexString(" reason" , channelClosed.reason.write())
115+ return LdkEventEmitter .send(EventTypes .channel_manager_channel_closed, body)
116+ }
117+
118+ (event as ? Event .DiscardFunding )?.let { discardFunding ->
119+ val body = Arguments .createMap()
120+ body.putHexString(" channel_id" , discardFunding.channel_id)
121+ body.putHexString(" tx" , discardFunding.transaction)
122+ return LdkEventEmitter .send(EventTypes .channel_manager_discard_funding, body)
123+ }
10124 }
11125
12126 override fun persist_manager (channel_manager_bytes : ByteArray? ) {
13- // TODO
127+ if (channel_manager_bytes != null ) {
128+ val body = Arguments .createMap()
129+ body.putHexString(" channel_manager" , channel_manager_bytes)
130+ LdkEventEmitter .send(EventTypes .persist_manager, body)
131+ }
14132 }
15133
16134 override fun persist_network_graph (network_graph : ByteArray? ) {
17- // TODO
135+ if (network_graph != null ) {
136+ val body = Arguments .createMap()
137+ body.putHexString(" network_graph" , network_graph)
138+ LdkEventEmitter .send(EventTypes .persist_graph, body)
139+ }
18140 }
19141 }
20142}
0 commit comments