11package to.bitkit.repositories
22
3- import android.net.Uri
43import com.google.firebase.messaging.FirebaseMessaging
4+ import com.synonym.bitkitcore.LightningInvoice
5+ import com.synonym.bitkitcore.Scanner
56import com.synonym.bitkitcore.createWithdrawCallbackUrl
7+ import com.synonym.bitkitcore.decode
68import com.synonym.bitkitcore.getLnurlInvoice
79import kotlinx.coroutines.CoroutineDispatcher
810import kotlinx.coroutines.delay
@@ -39,7 +41,7 @@ import to.bitkit.services.CoreService
3941import to.bitkit.services.LdkNodeEventBus
4042import to.bitkit.services.LightningService
4143import to.bitkit.services.LnUrlWithdrawResponse
42- import to.bitkit.services.LnUrlWithdrawService
44+ import to.bitkit.services.LnurlService
4345import to.bitkit.services.NodeEventHandler
4446import to.bitkit.utils.Logger
4547import to.bitkit.utils.ServiceError
@@ -59,7 +61,7 @@ class LightningRepo @Inject constructor(
5961 private val blocktankNotificationsService : BlocktankNotificationsService ,
6062 private val firebaseMessaging : FirebaseMessaging ,
6163 private val keychain : Keychain ,
62- private val lnUrlWithdrawService : LnUrlWithdrawService ,
64+ private val lnurlService : LnurlService ,
6365 private val cacheStore : CacheStore ,
6466) {
6567 private val _lightningState = MutableStateFlow (LightningState ())
@@ -401,12 +403,19 @@ class LightningRepo @Inject constructor(
401403 Result .success(invoice)
402404 }
403405
404- suspend fun createLnurlInvoice (
405- address : String ,
406- amountSatoshis : ULong ,
407- ): Result <String > = executeWhenNodeRunning(" getLnUrlInvoice" ) {
408- val invoice = getLnurlInvoice(address, amountSatoshis)
409- Result .success(invoice)
406+ suspend fun fetchLnurlInvoice (
407+ callbackUrl : String ,
408+ amountSats : ULong ,
409+ comment : String? = null,
410+ ): Result <LightningInvoice > {
411+ return runCatching {
412+ // TODO use bitkit-core getLnurlInvoice if it works with callbackUrl
413+ val bolt11 = lnurlService.fetchLnurlInvoice(callbackUrl, amountSats, comment).pr
414+ val decoded = (decode(bolt11) as Scanner .Lightning ).invoice
415+ return @runCatching decoded
416+ }.onFailure {
417+ Logger .error(" Error fetching lnurl invoice, url: $callbackUrl , amount: $amountSats , comment: $comment " , it)
418+ }
410419 }
411420
412421 suspend fun handleLnUrlWithdraw (
@@ -416,40 +425,7 @@ class LightningRepo @Inject constructor(
416425 ): Result <LnUrlWithdrawResponse > = executeWhenNodeRunning(" create LnUrl withdraw callback" ) {
417426 val callbackUrl = createWithdrawCallbackUrl(k1 = k1, callback = callback, paymentRequest = paymentRequest)
418427 Logger .debug(" handleLnUrlWithdraw callbackUrl generated:$callbackUrl " )
419- val formattedCallbackUrl = callbackUrl.removeDuplicateQueryParams()
420- Logger .debug(" handleLnUrlWithdraw formatted callbackUrl:$formattedCallbackUrl " )
421- lnUrlWithdrawService.fetchWithdrawInfo(formattedCallbackUrl)
422- }
423-
424- /* *
425- * Extension function to remove duplicate query parameters from a URL string
426- * Keeps the first occurrence of each parameter
427- */
428- private fun String.removeDuplicateQueryParams (): String { // TODO REMOVE AFTER CORE FIX
429- return try {
430- val uri = Uri .parse(this )
431- val builder = uri.buildUpon().clearQuery()
432-
433- // Track seen parameters to avoid duplicates
434- val seenParams = mutableSetOf<String >()
435-
436- // Get all query parameter names
437- uri.queryParameterNames.forEach { paramName ->
438- if (! seenParams.contains(paramName)) {
439- // Add only the first occurrence of each parameter
440- val value = uri.getQueryParameter(paramName)
441- if (value != null ) {
442- builder.appendQueryParameter(paramName, value)
443- seenParams.add(paramName)
444- }
445- }
446- }
447-
448- builder.build().toString()
449- } catch (e: Exception ) {
450- // Return original string if parsing fails
451- this
452- }
428+ lnurlService.fetchWithdrawInfo(callbackUrl)
453429 }
454430
455431 suspend fun payInvoice (bolt11 : String , sats : ULong? = null): Result <PaymentId > =
0 commit comments