Skip to content

Commit 2e68b19

Browse files
committed
fix(receive): enable LN toggle when having external node even if geoblocked
1 parent 2e4ee6b commit 2e68b19

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ class WalletRepo @Inject constructor(
102102
suspend fun refreshBip21(force: Boolean = false): Result<Unit> = withContext(bgDispatcher) {
103103
Logger.debug("Refreshing bip21 (force: $force)", context = TAG)
104104

105-
if (coreService.checkGeoBlock().second) {
106-
_walletState.update {
107-
it.copy(receiveOnSpendingBalance = false)
108-
}
105+
val shouldBlockLightning = coreService.checkGeoBlock().second
106+
_walletState.update {
107+
it.copy(receiveOnSpendingBalance = !shouldBlockLightning)
109108
}
110109

111110
// Reset invoice state

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import to.bitkit.async.ServiceQueue
5858
import to.bitkit.data.CacheStore
5959
import to.bitkit.env.Env
6060
import to.bitkit.ext.amountSats
61-
import to.bitkit.models.LnPeer
6261
import to.bitkit.models.toCoreNetwork
6362
import to.bitkit.utils.AppError
6463
import to.bitkit.utils.Logger
@@ -143,25 +142,14 @@ class CoreService @Inject constructor(
143142
}
144143
}
145144

146-
private suspend fun getLspPeers(): List<LnPeer> {
147-
val blocktankPeers = Env.trustedLnPeers
148-
// TODO get from blocktank info when lightningService.setup sets trustedPeers0conf using BT API
149-
// pseudocode idea:
150-
// val blocktankPeers = getInfo(refresh = true)?.nodes?.map { LnPeer(nodeId = it.pubkey, address = "TO_DO") }.orEmpty()
151-
return blocktankPeers
152-
}
153-
154-
suspend fun getConnectedPeers(): List<LnPeer> = lightningService.peers.orEmpty()
155-
156-
suspend fun hasExternalNode() = getConnectedPeers().any { connectedPeer -> connectedPeer !in getLspPeers() }
157-
158145
/**
159-
* This method checks if the device is geo blocked and if should block lighting features
160-
* @return Pair(isGeoBlocked, shouldBlockLightning)*/
146+
* This method checks if the device is in a is geo blocked region and if lightning features should be blocked
147+
* @return pair of `isGeoBlocked` to `shouldBlockLightning`
148+
* */
161149
suspend fun checkGeoBlock(): Pair<Boolean, Boolean> {
162150
val geoBlocked = isGeoBlocked()
163151
val shouldBlockLightning = when {
164-
hasExternalNode() -> false
152+
lightningService.hasExternalPeers() -> false
165153
else -> geoBlocked
166154
}
167155

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import java.util.Locale
5757
import java.util.TimeZone
5858
import javax.inject.Inject
5959
import javax.inject.Singleton
60+
import kotlin.collections.any
6061
import kotlin.io.path.Path
6162
import kotlin.time.Duration
6263
import kotlin.time.Duration.Companion.seconds
@@ -318,6 +319,17 @@ class LightningService @Inject constructor(
318319
Logger.warn("Peer disconnect error: $peer", LdkError(e))
319320
}
320321
}
322+
323+
private fun getLspPeers(): List<LnPeer> {
324+
val lspPeers = Env.trustedLnPeers
325+
// TODO get from blocktank info.nodes[] when setup uses it to set trustedPeers0conf
326+
// pseudocode idea:
327+
// val lspPeers = getInfo(refresh = true)?.nodes?.map { LnPeer(nodeId = it.pubkey, address = "TO_DO") }
328+
return lspPeers
329+
}
330+
331+
fun hasExternalPeers() = peers?.any { it !in getLspPeers() } == true
332+
321333
// endregion
322334

323335
// region channels

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class WalletRepoTest : BaseUnitTest() {
136136
}
137137

138138
@Test
139-
fun `refreshBip21 should set receiveOnSpendingBalance as false if shouldBlockLightning is true`() = test {
139+
fun `refreshBip21 should set receiveOnSpendingBalance false when shouldBlockLightning is true`() = test {
140140
wheneverBlocking { coreService.checkGeoBlock() }.thenReturn(Pair(true, true))
141141
whenever(lightningRepo.newAddress()).thenReturn(Result.success("newAddress"))
142142
whenever(addressChecker.getAddressInfo(any())).thenReturn(mock())
@@ -147,6 +147,18 @@ class WalletRepoTest : BaseUnitTest() {
147147
assertEquals(false, sut.walletState.value.receiveOnSpendingBalance)
148148
}
149149

150+
@Test
151+
fun `refreshBip21 should set receiveOnSpendingBalance true when shouldBlockLightning is false`() = test {
152+
wheneverBlocking { coreService.checkGeoBlock() }.thenReturn(Pair(true, false))
153+
whenever(lightningRepo.newAddress()).thenReturn(Result.success("newAddress"))
154+
whenever(addressChecker.getAddressInfo(any())).thenReturn(mock())
155+
156+
val result = sut.refreshBip21()
157+
158+
assertTrue(result.isSuccess)
159+
assertEquals(true, sut.walletState.value.receiveOnSpendingBalance)
160+
}
161+
150162
@Test
151163
fun `refreshBip21 should generate new address when current has transactions`() = test {
152164
val testAddress = "testAddress"

0 commit comments

Comments
 (0)