Skip to content

Commit ca693a3

Browse files
committed
fix: prevent from display auto invoice without lightning
1 parent ba46ca2 commit ca693a3

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

app/src/main/java/to/bitkit/ui/screens/wallets/receive/ReceiveInvoiceUtils.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fun getInvoiceForTab(
2727
}
2828

2929
ReceiveTab.AUTO -> {
30-
bip21.takeIf { isNodeRunning }.orEmpty()
30+
bip21.takeIf { isNodeRunning && containsLightningParameter(bip21) }.orEmpty()
3131
}
3232

3333
ReceiveTab.SPENDING -> {
@@ -57,6 +57,16 @@ fun removeLightningFromBip21(bip21: String, fallbackAddress: String): String {
5757
return withoutLightning.ifBlank { fallbackAddress }
5858
}
5959

60+
/**
61+
* Checks if a BIP21 URI contains a lightning parameter.
62+
*
63+
* @param bip21 The BIP21 URI to check
64+
* @return true if the URI contains a lightning parameter, false otherwise
65+
*/
66+
private fun containsLightningParameter(bip21: String): Boolean {
67+
return Regex("[?&]lightning=[^&]*").containsMatchIn(bip21)
68+
}
69+
6070
/**
6171
* Returns the appropriate QR code logo resource for the selected tab.
6272
*

app/src/test/java/to/bitkit/ui/screens/wallets/receive/ReceiveInvoiceUtilsTest.kt

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ReceiveInvoiceUtilsTest {
8888
}
8989

9090
@Test
91-
fun `getInvoiceForTab AUTO returns full BIP21 when node is running`() {
91+
fun `getInvoiceForTab AUTO returns full BIP21 when node running and has lightning`() {
9292
val bip21 = "bitcoin:$testAddress?amount=0.001&lightning=$testBolt11"
9393

9494
val result = getInvoiceForTab(
@@ -104,7 +104,7 @@ class ReceiveInvoiceUtilsTest {
104104
}
105105

106106
@Test
107-
fun `getInvoiceForTab AUTO returns empty when node is not running`() {
107+
fun `getInvoiceForTab AUTO returns empty when has lightning but node not running`() {
108108
val bip21 = "bitcoin:$testAddress?amount=0.001&lightning=$testBolt11"
109109

110110
val result = getInvoiceForTab(
@@ -119,6 +119,54 @@ class ReceiveInvoiceUtilsTest {
119119
assertEquals("", result)
120120
}
121121

122+
@Test
123+
fun `getInvoiceForTab AUTO returns empty when BIP21 has no lightning even if node running`() {
124+
val bip21WithoutLightning = "bitcoin:$testAddress?amount=0.001&message=Test"
125+
126+
val result = getInvoiceForTab(
127+
tab = ReceiveTab.AUTO,
128+
bip21 = bip21WithoutLightning,
129+
bolt11 = testBolt11,
130+
cjitInvoice = null,
131+
isNodeRunning = true,
132+
onchainAddress = testAddress
133+
)
134+
135+
assertEquals("", result)
136+
}
137+
138+
@Test
139+
fun `getInvoiceForTab AUTO returns empty when no lightning and node not running`() {
140+
val bip21WithoutLightning = "bitcoin:$testAddress?amount=0.001&message=Test"
141+
142+
val result = getInvoiceForTab(
143+
tab = ReceiveTab.AUTO,
144+
bip21 = bip21WithoutLightning,
145+
bolt11 = testBolt11,
146+
cjitInvoice = null,
147+
isNodeRunning = false,
148+
onchainAddress = testAddress
149+
)
150+
151+
assertEquals("", result)
152+
}
153+
154+
@Test
155+
fun `getInvoiceForTab AUTO detects lightning when it is the first parameter`() {
156+
val bip21LightningFirst = "bitcoin:$testAddress?lightning=$testBolt11&amount=0.001"
157+
158+
val result = getInvoiceForTab(
159+
tab = ReceiveTab.AUTO,
160+
bip21 = bip21LightningFirst,
161+
bolt11 = testBolt11,
162+
cjitInvoice = null,
163+
isNodeRunning = true,
164+
onchainAddress = testAddress
165+
)
166+
167+
assertEquals(bip21LightningFirst, result)
168+
}
169+
122170
@Test
123171
fun `getInvoiceForTab SPENDING returns CJIT invoice when available and node running`() {
124172
val bip21 = "bitcoin:$testAddress?lightning=$testBolt11"

0 commit comments

Comments
 (0)