Skip to content

Commit 640b403

Browse files
committed
fix: don't block receive lightning payments for existing channels even with the user is geoblocked
1 parent 9f7e7e6 commit 640b403

File tree

6 files changed

+12
-45
lines changed

6 files changed

+12
-45
lines changed

app/src/main/java/to/bitkit/repositories/BlocktankRepo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class BlocktankRepo @Inject constructor(
201201
description: String = Env.DEFAULT_INVOICE_MESSAGE,
202202
): Result<IcJitEntry> = withContext(bgDispatcher) {
203203
try {
204-
if (coreService.checkGeoBlock().first) throw ServiceError.GeoBlocked
204+
if (coreService.isGeoBlocked()) throw ServiceError.GeoBlocked
205205
val nodeId = lightningService.nodeId ?: throw ServiceError.NodeNotStarted
206206
val lspBalance = getDefaultLspBalance(clientBalance = amountSats)
207207
val channelSizeSat = amountSats + lspBalance
@@ -230,7 +230,7 @@ class BlocktankRepo @Inject constructor(
230230
channelExpiryWeeks: UInt = DEFAULT_CHANNEL_EXPIRY_WEEKS,
231231
): Result<IBtOrder> = withContext(bgDispatcher) {
232232
try {
233-
if (coreService.checkGeoBlock().first) throw ServiceError.GeoBlocked
233+
if (coreService.isGeoBlocked()) throw ServiceError.GeoBlocked
234234

235235
val options = defaultCreateOrderOptions(clientBalanceSat = spendingBalanceSats)
236236

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import org.lightningdevkit.ldknode.PaymentDetails
3737
import org.lightningdevkit.ldknode.PaymentId
3838
import org.lightningdevkit.ldknode.PeerDetails
3939
import org.lightningdevkit.ldknode.SpendableUtxo
40-
import org.lightningdevkit.ldknode.TransactionDetails
4140
import org.lightningdevkit.ldknode.Txid
4241
import to.bitkit.data.CacheStore
4342
import to.bitkit.data.SettingsStore
@@ -283,9 +282,8 @@ class LightningRepo @Inject constructor(
283282
}
284283

285284
suspend fun updateGeoBlockState() = withContext(bgDispatcher) {
286-
val (isGeoBlocked, shouldBlockLightning) = coreService.checkGeoBlock()
287285
_lightningState.update {
288-
it.copy(isGeoBlocked = isGeoBlocked, shouldBlockLightningReceive = shouldBlockLightning)
286+
it.copy(isGeoBlocked = coreService.isGeoBlocked())
289287
}
290288
}
291289

@@ -355,7 +353,8 @@ class LightningRepo @Inject constructor(
355353
private fun handleLdkEvent(event: Event) {
356354
when (event) {
357355
is Event.ChannelPending,
358-
is Event.ChannelReady -> scope.launch {
356+
is Event.ChannelReady,
357+
-> scope.launch {
359358
refreshChannelCache()
360359
}
361360

@@ -560,10 +559,6 @@ class LightningRepo @Inject constructor(
560559
expirySeconds: UInt = 86_400u,
561560
): Result<String> = executeWhenNodeRunning("Create invoice") {
562561
updateGeoBlockState()
563-
if (lightningState.value.shouldBlockLightningReceive) {
564-
return@executeWhenNodeRunning Result.failure(ServiceError.GeoBlocked)
565-
}
566-
567562
val invoice = lightningService.receive(amountSats, description, expirySeconds)
568563
Result.success(invoice)
569564
}
@@ -1016,7 +1011,6 @@ data class LightningState(
10161011
val channels: List<ChannelDetails> = emptyList(),
10171012
val balances: BalanceDetails? = null,
10181013
val isSyncingWallet: Boolean = false,
1019-
val shouldBlockLightningReceive: Boolean = false,
10201014
val isGeoBlocked: Boolean = false,
10211015
) {
10221016
fun block(): BestBlock? = nodeStatus?.currentBestBlock

app/src/main/java/to/bitkit/repositories/WalletRepo.kt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ class WalletRepo @Inject constructor(
121121
emptyList()
122122
}
123123

124-
val (_, shouldBlockLightningReceive) = coreService.checkGeoBlock()
125124
_walletState.update {
126-
it.copy(receiveOnSpendingBalance = !shouldBlockLightningReceive)
125+
it.copy(isGeoBlocked = coreService.isGeoBlocked())
127126
}
128127
clearBip21State(clearTags = false)
129128
refreshAddressIfNeeded()
@@ -429,16 +428,6 @@ class WalletRepo @Inject constructor(
429428
}
430429
}
431430

432-
suspend fun toggleReceiveOnSpendingBalance(): Result<Unit> = withContext(bgDispatcher) {
433-
if (!_walletState.value.receiveOnSpendingBalance && coreService.checkGeoBlock().second) {
434-
return@withContext Result.failure(ServiceError.GeoBlocked)
435-
}
436-
437-
_walletState.update { it.copy(receiveOnSpendingBalance = !it.receiveOnSpendingBalance) }
438-
439-
return@withContext Result.success(Unit)
440-
}
441-
442431
// Payment ID management
443432
private suspend fun paymentHash(): String? = withContext(bgDispatcher) {
444433
val bolt11 = getBolt11()
@@ -553,7 +542,7 @@ class WalletRepo @Inject constructor(
553542
setBip21Description(description)
554543

555544
val canReceive = lightningRepo.canReceive()
556-
if (canReceive && _walletState.value.receiveOnSpendingBalance) {
545+
if (canReceive) {
557546
lightningRepo.createInvoice(amountSats, description).onSuccess {
558547
setBolt11(it)
559548
}
@@ -575,9 +564,7 @@ class WalletRepo @Inject constructor(
575564

576565
suspend fun shouldRequestAdditionalLiquidity(): Result<Boolean> = withContext(bgDispatcher) {
577566
return@withContext try {
578-
if (!_walletState.value.receiveOnSpendingBalance) return@withContext Result.success(false)
579-
580-
if (coreService.checkGeoBlock().first) return@withContext Result.success(false)
567+
if (coreService.isGeoBlocked()) return@withContext Result.success(false)
581568

582569
val channels = lightningRepo.lightningState.value.channels
583570
if (channels.filterOpen().isEmpty()) return@withContext Result.success(false)
@@ -617,7 +604,7 @@ data class WalletState(
617604
val bip21AmountSats: ULong? = null,
618605
val bip21Description: String = "",
619606
val selectedTags: List<String> = listOf(),
620-
val receiveOnSpendingBalance: Boolean = true,
607+
val isGeoBlocked: Boolean = false,
621608
val walletExists: Boolean = false,
622609
)
623610

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class CoreService @Inject constructor(
135135
}
136136

137137
@Suppress("KotlinConstantConditions")
138-
private suspend fun isGeoBlocked(): Boolean {
138+
suspend fun isGeoBlocked(): Boolean {
139139
if (!Env.isGeoblockingEnabled) {
140140
Logger.verbose("Geoblocking disabled via build config", context = "GeoCheck")
141141
return false
@@ -171,20 +171,6 @@ class CoreService @Inject constructor(
171171
}
172172
}
173173

174-
/**
175-
* This method checks if the device is in a is geo blocked region and if lightning features should be blocked
176-
* @return pair of `isGeoBlocked` to `shouldBlockLightningReceive`
177-
* */
178-
suspend fun checkGeoBlock(): Pair<Boolean, Boolean> {
179-
val geoBlocked = isGeoBlocked()
180-
val shouldBlockLightningReceive = when {
181-
lightningService.hasExternalPeers() -> !lightningService.canReceive()
182-
else -> geoBlocked
183-
}
184-
185-
return Pair(geoBlocked, shouldBlockLightningReceive)
186-
}
187-
188174
suspend fun wipeData(): Result<Unit> = ServiceQueue.CORE.background {
189175
runCatching {
190176
val result = wipeAllDatabases()
@@ -1182,6 +1168,7 @@ class ActivityService(
11821168
// a closed channel's funding UTXO
11831169
findClosedChannelForTransaction(txid, transactionDetails)
11841170
}
1171+
11851172
PaymentDirection.OUTBOUND -> {
11861173
// Check if this transaction is a channel open by checking if it's
11871174
// the funding transaction for an open channel

app/src/main/java/to/bitkit/services/LightningService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ class LightningService @Inject constructor(
739739
/**
740740
* TODO remove, replace all usages with [FeeRate.fromSatPerVbUnchecked]
741741
* */
742+
@Deprecated("replace all usages with [FeeRate.fromSatPerVbUnchecked]")
742743
private fun convertVByteToKwu(satsPerVByte: UInt): FeeRate {
743744
// 1 vbyte = 4 weight units, so 1 sats/vbyte = 250 sats/kwu
744745
val satPerKwu = satsPerVByte.toULong() * 250u

app/src/main/java/to/bitkit/viewmodels/WalletViewModel.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class WalletViewModel @Inject constructor(
8585
bip21AmountSats = state.bip21AmountSats,
8686
bip21Description = state.bip21Description,
8787
selectedTags = state.selectedTags,
88-
receiveOnSpendingBalance = state.receiveOnSpendingBalance,
8988
)
9089
}
9190
if (state.walletExists && restoreState == RestoreState.InProgress.Wallet) {
@@ -306,7 +305,6 @@ data class MainUiState(
306305
val peers: List<PeerDetails> = emptyList(),
307306
val channels: List<ChannelDetails> = emptyList(),
308307
val isRefreshing: Boolean = false,
309-
val receiveOnSpendingBalance: Boolean = true,
310308
val bip21AmountSats: ULong? = null,
311309
val bip21Description: String = "",
312310
val selectedTags: List<String> = listOf(),

0 commit comments

Comments
 (0)