-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBip21Utils.kt
More file actions
73 lines (58 loc) · 2.44 KB
/
Copy pathBip21Utils.kt
File metadata and controls
73 lines (58 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package to.bitkit.utils
import to.bitkit.models.SATS_IN_BTC
object Bip21Utils {
private const val BIP21_PREFIX = "bitcoin:"
/**
* Checks if a BIP21 URI is duplicated (contains multiple bitcoin: prefixes).
* Workaround for https://github.com/synonymdev/bitkit-core/issues/63
* @return true if the input contains duplicated BIP21 URIs, false otherwise
*/
fun isDuplicatedBip21(input: String): Boolean {
val lowercased = input.lowercase()
val firstIndex = lowercased.indexOf(BIP21_PREFIX)
if (firstIndex == -1) return false
val secondIndex = lowercased.indexOf(BIP21_PREFIX, firstIndex + BIP21_PREFIX.length)
return secondIndex != -1
}
fun buildBip21Url(
bitcoinAddress: String,
amountSats: ULong? = null,
label: String? = null,
message: String? = "Bitkit",
lightningInvoice: String? = null
): String {
val builder = StringBuilder("bitcoin:$bitcoinAddress")
val queryParams = mutableListOf<String>()
// Add amount if specified (convert from sats to BTC)
amountSats?.let {
queryParams.add("amount=${formatBtcAmount(amountSats)}")
}
// Add optional parameters
label?.let { queryParams.add("label=${it.encodeToUrl()}") }
message?.let { queryParams.add("message=${it.encodeToUrl()}") }
// Add query parameters if any exist
if (queryParams.isNotEmpty()) {
builder.append("?${queryParams.joinToString("&")}")
}
// Add lightning parameter if invoice exists
if (!lightningInvoice.isNullOrBlank()) {
val separator = if (queryParams.isEmpty()) "?" else "&"
val encodedInvoice = lightningInvoice.encodeToUrl()
if (encodedInvoice.isNotBlank()) {
builder.append("${separator}lightning=${lightningInvoice.encodeToUrl()}")
}
}
return builder.toString()
}
private fun formatBtcAmount(sats: ULong): String {
val fullBtc = sats / SATS_IN_BTC.toULong()
val remainderSats = sats % SATS_IN_BTC.toULong()
return if (remainderSats == 0uL) {
fullBtc.toString()
} else {
val remainderStr = remainderSats.toString().padStart(8, '0')
"$fullBtc.${remainderStr.trimEnd('0')}"
}
}
}
fun String.encodeToUrl(): String = runCatching { java.net.URLEncoder.encode(this, "UTF-8") }.getOrElse { "" }