Skip to content

Commit 0a5aa75

Browse files
committed
fix: check for viable lightning invoice when setting the BIP21 on text input
1 parent 6be2092 commit 0a5aa75

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,18 @@ class AppViewModel @Inject constructor(
780780
return
781781
}
782782

783+
extractViableLightningInvoice(invoice.params)?.let { lnInvoice ->
784+
_sendUiState.update {
785+
it.copy(
786+
isAddressInputValid = true,
787+
isUnified = true,
788+
decodedInvoice = lnInvoice,
789+
payMethod = SendMethod.LIGHTNING,
790+
)
791+
}
792+
return
793+
}
794+
783795
val maxSendOnchain = walletRepo.balanceState.value.maxSendOnchainSats
784796

785797
if (maxSendOnchain == 0uL) {
@@ -805,6 +817,30 @@ class AppViewModel @Inject constructor(
805817
_sendUiState.update { it.copy(isAddressInputValid = true) }
806818
}
807819

820+
private suspend fun extractViableLightningInvoice(params: Map<String, String>?): LightningInvoice? =
821+
params?.get("lightning")?.let { bolt11 ->
822+
runCatching { decode(bolt11) }.getOrNull()
823+
?.let { it as? Scanner.Lightning }
824+
?.invoice
825+
?.takeIf { lnInv ->
826+
if (lnInv.isExpired) {
827+
Logger.debug(
828+
"Lightning invoice expired in unified URI, defaulting to onchain-only",
829+
context = TAG
830+
)
831+
return@takeIf false
832+
}
833+
val canSend = lightningRepo.canSend(lnInv.amountSatoshis.coerceAtLeast(1u))
834+
if (!canSend) {
835+
Logger.debug(
836+
"Cannot pay unified invoice using LN, defaulting to onchain-only",
837+
context = TAG
838+
)
839+
}
840+
return@takeIf canSend
841+
}
842+
}
843+
808844
private fun showAddressValidationError(
809845
@StringRes titleRes: Int,
810846
@StringRes descriptionRes: Int,
@@ -1056,27 +1092,7 @@ class AppViewModel @Inject constructor(
10561092
return
10571093
}
10581094

1059-
val lnInvoice: LightningInvoice? = invoice.params?.get("lightning")?.let { bolt11 ->
1060-
runCatching { decode(bolt11) }.getOrNull()
1061-
?.let { it as? Scanner.Lightning }
1062-
?.invoice
1063-
?.takeIf { invoice ->
1064-
if (invoice.isExpired) {
1065-
Logger.debug(
1066-
"Lightning invoice expired in unified URI, defaulting to onchain-only",
1067-
context = TAG
1068-
)
1069-
return@takeIf false
1070-
}
1071-
1072-
// Then check sending capacity
1073-
val canSend = lightningRepo.canSend(invoice.amountSatoshis.coerceAtLeast(1u))
1074-
if (!canSend) {
1075-
Logger.debug("Cannot pay unified invoice using LN, defaulting to onchain-only", context = TAG)
1076-
}
1077-
return@takeIf canSend
1078-
}
1079-
}
1095+
val lnInvoice = extractViableLightningInvoice(invoice.params)
10801096
_sendUiState.update {
10811097
it.copy(
10821098
address = invoice.address,

0 commit comments

Comments
 (0)