Skip to content

Commit eee3800

Browse files
authored
Merge pull request #625 from synonymdev/fix/address-validation
fix: address validation
2 parents 12e169b + 83a470e commit eee3800

7 files changed

Lines changed: 348 additions & 25 deletions

File tree

app/src/main/java/to/bitkit/models/Network.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ fun Network.toCoreNetworkType(): NetworkType = when (this) {
2424
Network.SIGNET -> NetworkType.SIGNET
2525
Network.REGTEST -> NetworkType.REGTEST
2626
}
27+
28+
fun NetworkType.toLdkNetwork(): Network = when (this) {
29+
NetworkType.BITCOIN -> Network.BITCOIN
30+
NetworkType.TESTNET -> Network.TESTNET
31+
NetworkType.SIGNET -> Network.SIGNET
32+
NetworkType.REGTEST -> Network.REGTEST
33+
}

app/src/main/java/to/bitkit/utils/Bip21Utils.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import to.bitkit.models.SATS_IN_BTC
44

55
object Bip21Utils {
66

7+
private const val BIP21_PREFIX = "bitcoin:"
8+
9+
/**
10+
* Checks if a BIP21 URI is duplicated (contains multiple bitcoin: prefixes).
11+
* Workaround for https://github.com/synonymdev/bitkit-core/issues/63
12+
* @return true if the input contains duplicated BIP21 URIs, false otherwise
13+
*/
14+
fun isDuplicatedBip21(input: String): Boolean {
15+
val lowercased = input.lowercase()
16+
val firstIndex = lowercased.indexOf(BIP21_PREFIX)
17+
if (firstIndex == -1) return false
18+
19+
val secondIndex = lowercased.indexOf(BIP21_PREFIX, firstIndex + BIP21_PREFIX.length)
20+
return secondIndex != -1
21+
}
22+
723
fun buildBip21Url(
824
bitcoinAddress: String,
925
amountSats: ULong? = null,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package to.bitkit.utils
2+
3+
import org.lightningdevkit.ldknode.Network
4+
5+
/**
6+
* Helper for validating Bitcoin network compatibility of addresses and invoices
7+
*/
8+
object NetworkValidationHelper {
9+
/**
10+
* Check if an address/invoice network mismatches the current app network
11+
* @param addressNetwork The network detected from the address/invoice
12+
* @param currentNetwork The app's current network (typically Env.network)
13+
* @return true if there's a mismatch (address won't work on current network)
14+
*/
15+
fun isNetworkMismatch(addressNetwork: Network?, currentNetwork: Network): Boolean {
16+
if (addressNetwork == null) return false
17+
18+
// Special case: regtest uses testnet prefixes (m, n, 2, tb1)
19+
if (currentNetwork == Network.REGTEST && addressNetwork == Network.TESTNET) {
20+
return false
21+
}
22+
23+
return addressNetwork != currentNetwork
24+
}
25+
}

0 commit comments

Comments
 (0)