refactor: WalletViewModel resolveAliasesInBackground shared between bolt11 and lnurl

This commit is contained in:
2026-06-01 19:38:44 +02:00
parent 8ce83356ec
commit 40f7aa00ae
@@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.SharingStarted
import com.bitcointxoko.gudariwallet.util.lnurlProtocolError
import androidx.fragment.app.FragmentActivity
import com.bitcointxoko.gudariwallet.api.RouteHintHop
import com.bitcointxoko.gudariwallet.data.BalancePrefsStore
import com.bitcointxoko.gudariwallet.data.FiatRateCache
import com.bitcointxoko.gudariwallet.util.BiometricHelper
@@ -413,32 +414,21 @@ class WalletViewModel(
allRouteHints = decoded.routeHints,
routeHintAliases = emptyMap()
)
decoded.payee?.let { pubkey ->
viewModelScope.launch {
val alias = aliasRepo.resolve(pubkey)
val current = _sendState.value
if (alias != null &&
current is SendState.Bolt11Decoded &&
current.payee == pubkey) {
_sendState.value = current.copy(nodeAlias = alias)
resolveAliasesInBackground(
payee = decoded.payee,
routeHints = decoded.routeHints,
readState = { _sendState.value },
copyAlias = { state, alias ->
(state as? SendState.Bolt11Decoded)
?.takeIf { it.payee == decoded.payee }
?.copy(nodeAlias = alias)
},
copyHint = { state, pubkey, alias ->
(state as? SendState.Bolt11Decoded)
?.copy(routeHintAliases = state.routeHintAliases + (pubkey to alias))
}
}
}
decoded.routeHints
.map { it.publicKey }
.distinct()
.forEach { pubkey ->
viewModelScope.launch {
val alias = aliasRepo.resolve(pubkey) ?: return@launch
val current = _sendState.value
if (current is SendState.Bolt11Decoded) {
_sendState.value = current.copy(
routeHintAliases = current.routeHintAliases + (pubkey to alias)
)
}
}
}
}
.onFailure { e ->
Log.e(TAG, "SEND [DECODE ERROR] ${e.message}")
_sendState.value = SendState.Error(parseApiError(e, "Could not decode invoice"))
@@ -791,35 +781,22 @@ class WalletViewModel(
routeHintAliases = emptyMap()
)
decoded.payee?.let { pubkey ->
viewModelScope.launch {
val alias = aliasRepo.resolve(pubkey)
val current = _sendState.value
if (alias != null &&
current is SendState.LnurlInvoiceReady &&
current.payee == pubkey) {
_sendState.value = current.copy(nodeAlias = alias)
resolveAliasesInBackground(
payee = decoded.payee,
routeHints = decoded.routeHints,
readState = { _sendState.value },
copyAlias = { state, alias ->
(state as? SendState.LnurlInvoiceReady)
?.takeIf { it.payee == decoded.payee }
?.copy(nodeAlias = alias)
},
copyHint = { state, pubkey, alias ->
(state as? SendState.LnurlInvoiceReady)
?.copy(routeHintAliases = state.routeHintAliases + (pubkey to alias))
}
}
}
// ── 5. Resolve node aliases for route-hint hops in the background
decoded.routeHints
.map { it.publicKey }
.distinct()
.forEach { pubkey ->
viewModelScope.launch {
val alias = aliasRepo.resolve(pubkey) ?: return@launch
val current = _sendState.value
if (current is SendState.LnurlInvoiceReady) {
_sendState.value = current.copy(
routeHintAliases = current.routeHintAliases + (pubkey to alias)
)
}
}
}
}
}
// ── Step 2: pay the bolt11 that was fetched in fetchLnurlInvoice
// Called after the user confirms in LnurlInvoiceConfirmCard.
@@ -944,4 +921,37 @@ class WalletViewModel(
rawRes = raw
)
}
/**
* Fires background coroutines to resolve node aliases for [payee] and each
* pubkey in [routeHints].
*
* [readState] — returns the current SendState to check it's still relevant.
* [copyAlias] — called with the resolved payee alias; return the new state or
* null to skip the update.
* [copyHint] — called with (pubkey, alias); return the new state or null to
* skip the update.
*/
private fun resolveAliasesInBackground(
payee : String?,
routeHints : List<RouteHintHop>,
readState : () -> SendState,
copyAlias : (SendState, String) -> SendState?,
copyHint : (SendState, String, String) -> SendState?
) {
payee?.let { pubkey ->
viewModelScope.launch {
val alias = aliasRepo.resolve(pubkey) ?: return@launch
val current = readState()
copyAlias(current, alias)?.let { _sendState.value = it }
}
}
routeHints.map { it.publicKey }.distinct().forEach { pubkey ->
viewModelScope.launch {
val alias = aliasRepo.resolve(pubkey) ?: return@launch
val current = readState()
copyHint(current, pubkey, alias)?.let { _sendState.value = it }
}
}
}
}