Skip to content

Commit 49fd09c

Browse files
authored
Merge pull request #434 from synonymdev/feat/ldk-node-serializable
feat: use serializable types from updated ldk-node
2 parents f39004e + 7826c49 commit 49fd09c

38 files changed

+744
-1223
lines changed

app/detekt-baseline.xml

Lines changed: 0 additions & 131 deletions
Large diffs are not rendered by default.

app/src/main/java/to/bitkit/env/Env.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package to.bitkit.env
33
import android.os.Build
44
import org.lightningdevkit.ldknode.LogLevel
55
import org.lightningdevkit.ldknode.Network
6+
import org.lightningdevkit.ldknode.PeerDetails
67
import to.bitkit.BuildConfig
78
import to.bitkit.ext.ensureDir
9+
import to.bitkit.ext.parse
810
import to.bitkit.models.BlocktankNotificationType
9-
import to.bitkit.models.LnPeer
1011
import to.bitkit.utils.Logger
1112
import java.io.File
1213
import kotlin.io.path.Path
@@ -24,8 +25,8 @@ internal object Env {
2425
// TODO: remove this to load from BT API instead
2526
val trustedLnPeers
2627
get() = when (network) {
27-
Network.REGTEST -> listOf(Peers.btStaging)
28-
Network.TESTNET -> listOf(Peers.btStaging)
28+
Network.REGTEST -> listOf(Peers.staging)
29+
Network.TESTNET -> listOf(Peers.staging)
2930
else -> TODO("Not yet implemented")
3031
}
3132

@@ -143,10 +144,8 @@ internal object Env {
143144
}
144145

145146
object Peers {
146-
val btStaging = LnPeer(
147-
nodeId = "028a8910b0048630d4eb17af25668cdd7ea6f2d8ae20956e7a06e2ae46ebcb69fc",
148-
address = "34.65.86.104:9400",
149-
)
147+
val staging =
148+
PeerDetails.parse("028a8910b0048630d4eb17af25668cdd7ea6f2d8ae20956e7a06e2ae46ebcb69fc@34.65.86.104:9400")
150149
}
151150

152151
object ElectrumServers {

app/src/main/java/to/bitkit/ext/LightningBalance.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package to.bitkit.ext
22

3-
import to.bitkit.models.LightningBalance
3+
import org.lightningdevkit.ldknode.LightningBalance
44

55
fun LightningBalance.amountSats(): ULong {
66
return when (this) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package to.bitkit.ext
2+
3+
import org.lightningdevkit.ldknode.PeerDetails
4+
5+
val PeerDetails.host get() = address.substringBefore(":")
6+
7+
val PeerDetails.port get() = address.substringAfter(":")
8+
9+
val PeerDetails.uri get() = "$nodeId@$address"
10+
11+
fun PeerDetails.Companion.parse(uri: String): PeerDetails {
12+
val parts = uri.split("@")
13+
require(parts.size == 2) { "Invalid uri format, expected: '<nodeId>@<host>:<port>', got: '$uri'" }
14+
15+
val nodeId = parts[0]
16+
17+
val addressParts = parts[1].split(":")
18+
require(addressParts.size == 2) { "Invalid uri format, expected: '<nodeId>@<host>:<port>', got: '$uri'" }
19+
20+
val host = addressParts[0]
21+
val port = addressParts[1]
22+
val address = "$host:$port"
23+
24+
return PeerDetails(
25+
nodeId = nodeId,
26+
address = address,
27+
isConnected = false,
28+
isPersisted = false,
29+
)
30+
}
31+
32+
fun PeerDetails.Companion.from(nodeId: String, host: String, port: String) = PeerDetails(
33+
nodeId = nodeId,
34+
address = "$host:$port",
35+
isConnected = false,
36+
isPersisted = false,
37+
)

app/src/main/java/to/bitkit/fcm/WakeNodeWorker.kt

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import to.bitkit.utils.Logger
3737
import to.bitkit.utils.withPerformanceLogging
3838
import kotlin.time.Duration.Companion.minutes
3939

40+
@Suppress("LongParameterList")
4041
@HiltWorker
4142
class WakeNodeWorker @AssistedInject constructor(
4243
@Assisted private val appContext: Context,
@@ -120,75 +121,16 @@ class WakeNodeWorker @AssistedInject constructor(
120121
val showDetails = settingsStore.data.first().showNotificationDetails
121122
val openBitkitMessage = "Open Bitkit to see details"
122123
when (event) {
123-
is Event.PaymentReceived -> {
124-
bestAttemptContent?.title = "Payment Received"
125-
val sats = event.amountMsat / 1000u
126-
// Save for UI to pick up
127-
NewTransactionSheetDetails.save(
128-
appContext,
129-
NewTransactionSheetDetails(
130-
type = NewTransactionSheetType.LIGHTNING,
131-
direction = NewTransactionSheetDirection.RECEIVED,
132-
paymentHashOrTxId = event.paymentHash,
133-
sats = sats.toLong(),
134-
)
135-
)
136-
val content = if (showDetails) "$BITCOIN_SYMBOL $sats" else openBitkitMessage
137-
bestAttemptContent?.body = content
138-
if (self.notificationType == incomingHtlc) {
139-
self.deliver()
140-
}
141-
}
124+
is Event.PaymentReceived -> onPaymentReceived(event, showDetails, openBitkitMessage)
142125

143126
is Event.ChannelPending -> {
144127
self.bestAttemptContent?.title = "Channel Opened"
145128
self.bestAttemptContent?.body = "Pending"
146129
// Don't deliver, give a chance for channelReady event to update the content if it's a turbo channel
147130
}
148131

149-
is Event.ChannelReady -> {
150-
if (self.notificationType == cjitPaymentArrived) {
151-
self.bestAttemptContent?.title = "Payment received"
152-
self.bestAttemptContent?.body = "Via new channel"
153-
154-
lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
155-
val sats = channel.amountOnClose
156-
val content = if (showDetails) "$BITCOIN_SYMBOL $sats" else openBitkitMessage
157-
self.bestAttemptContent?.title = content
158-
val cjitEntry = channel.let { blocktankRepo.getCjitEntry(it) }
159-
if (cjitEntry != null) {
160-
// Save for UI to pick up
161-
NewTransactionSheetDetails.save(
162-
appContext,
163-
NewTransactionSheetDetails(
164-
type = NewTransactionSheetType.LIGHTNING,
165-
direction = NewTransactionSheetDirection.RECEIVED,
166-
sats = sats.toLong(),
167-
)
168-
)
169-
activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel)
170-
}
171-
}
172-
} else if (self.notificationType == orderPaymentConfirmed) {
173-
self.bestAttemptContent?.title = "Channel opened"
174-
self.bestAttemptContent?.body = "Ready to send"
175-
}
176-
self.deliver()
177-
}
178-
179-
is Event.ChannelClosed -> {
180-
self.bestAttemptContent?.title = "Channel closed"
181-
self.bestAttemptContent?.body = "Reason: ${event.reason}"
182-
183-
if (self.notificationType == mutualClose) {
184-
self.bestAttemptContent?.body = "Balance moved from spending to savings"
185-
} else if (self.notificationType == orderPaymentConfirmed) {
186-
self.bestAttemptContent?.title = "Channel failed to open in the background"
187-
self.bestAttemptContent?.body = "Please try again"
188-
}
189-
190-
self.deliver()
191-
}
132+
is Event.ChannelReady -> onChannelReady(event, showDetails, openBitkitMessage)
133+
is Event.ChannelClosed -> onChannelClosed(event)
192134

193135
is Event.PaymentSuccessful -> Unit
194136
is Event.PaymentClaimable -> Unit
@@ -205,6 +147,78 @@ class WakeNodeWorker @AssistedInject constructor(
205147
}
206148
}
207149

150+
private suspend fun onChannelClosed(event: Event.ChannelClosed) {
151+
self.bestAttemptContent?.title = "Channel closed"
152+
self.bestAttemptContent?.body = "Reason: ${event.reason}"
153+
154+
if (self.notificationType == mutualClose) {
155+
self.bestAttemptContent?.body = "Balance moved from spending to savings"
156+
} else if (self.notificationType == orderPaymentConfirmed) {
157+
self.bestAttemptContent?.title = "Channel failed to open in the background"
158+
self.bestAttemptContent?.body = "Please try again"
159+
}
160+
161+
self.deliver()
162+
}
163+
164+
private suspend fun onPaymentReceived(
165+
event: Event.PaymentReceived,
166+
showDetails: Boolean,
167+
openBitkitMessage: String,
168+
) {
169+
bestAttemptContent?.title = "Payment Received"
170+
val sats = event.amountMsat / 1000u
171+
// Save for UI to pick up
172+
NewTransactionSheetDetails.save(
173+
appContext,
174+
NewTransactionSheetDetails(
175+
type = NewTransactionSheetType.LIGHTNING,
176+
direction = NewTransactionSheetDirection.RECEIVED,
177+
paymentHashOrTxId = event.paymentHash,
178+
sats = sats.toLong(),
179+
)
180+
)
181+
val content = if (showDetails) "$BITCOIN_SYMBOL $sats" else openBitkitMessage
182+
bestAttemptContent?.body = content
183+
if (self.notificationType == incomingHtlc) {
184+
self.deliver()
185+
}
186+
}
187+
188+
private suspend fun onChannelReady(
189+
event: Event.ChannelReady,
190+
showDetails: Boolean,
191+
openBitkitMessage: String,
192+
) {
193+
if (self.notificationType == cjitPaymentArrived) {
194+
self.bestAttemptContent?.title = "Payment received"
195+
self.bestAttemptContent?.body = "Via new channel"
196+
197+
lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
198+
val sats = channel.amountOnClose
199+
val content = if (showDetails) "$BITCOIN_SYMBOL $sats" else openBitkitMessage
200+
self.bestAttemptContent?.title = content
201+
val cjitEntry = channel.let { blocktankRepo.getCjitEntry(it) }
202+
if (cjitEntry != null) {
203+
// Save for UI to pick up
204+
NewTransactionSheetDetails.save(
205+
appContext,
206+
NewTransactionSheetDetails(
207+
type = NewTransactionSheetType.LIGHTNING,
208+
direction = NewTransactionSheetDirection.RECEIVED,
209+
sats = sats.toLong(),
210+
)
211+
)
212+
activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel)
213+
}
214+
}
215+
} else if (self.notificationType == orderPaymentConfirmed) {
216+
self.bestAttemptContent?.title = "Channel opened"
217+
self.bestAttemptContent?.body = "Ready to send"
218+
}
219+
self.deliver()
220+
}
221+
208222
private suspend fun deliver() {
209223
lightningRepo.stop()
210224

0 commit comments

Comments
 (0)