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 kotlinx.coroutines.flow.SharingStarted
import com.bitcointxoko.gudariwallet.util.lnurlProtocolError import com.bitcointxoko.gudariwallet.util.lnurlProtocolError
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import com.bitcointxoko.gudariwallet.api.RouteHintHop
import com.bitcointxoko.gudariwallet.data.BalancePrefsStore import com.bitcointxoko.gudariwallet.data.BalancePrefsStore
import com.bitcointxoko.gudariwallet.data.FiatRateCache import com.bitcointxoko.gudariwallet.data.FiatRateCache
import com.bitcointxoko.gudariwallet.util.BiometricHelper import com.bitcointxoko.gudariwallet.util.BiometricHelper
@@ -413,31 +414,20 @@ class WalletViewModel(
allRouteHints = decoded.routeHints, allRouteHints = decoded.routeHints,
routeHintAliases = emptyMap() routeHintAliases = emptyMap()
) )
decoded.payee?.let { pubkey -> resolveAliasesInBackground(
viewModelScope.launch { payee = decoded.payee,
val alias = aliasRepo.resolve(pubkey) routeHints = decoded.routeHints,
val current = _sendState.value readState = { _sendState.value },
if (alias != null && copyAlias = { state, alias ->
current is SendState.Bolt11Decoded && (state as? SendState.Bolt11Decoded)
current.payee == pubkey) { ?.takeIf { it.payee == decoded.payee }
_sendState.value = current.copy(nodeAlias = alias) ?.copy(nodeAlias = alias)
} },
} copyHint = { state, pubkey, alias ->
} (state as? SendState.Bolt11Decoded)
decoded.routeHints ?.copy(routeHintAliases = state.routeHintAliases + (pubkey to alias))
.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 -> .onFailure { e ->
Log.e(TAG, "SEND [DECODE ERROR] ${e.message}") Log.e(TAG, "SEND [DECODE ERROR] ${e.message}")
@@ -791,33 +781,20 @@ class WalletViewModel(
routeHintAliases = emptyMap() routeHintAliases = emptyMap()
) )
decoded.payee?.let { pubkey -> resolveAliasesInBackground(
viewModelScope.launch { payee = decoded.payee,
val alias = aliasRepo.resolve(pubkey) routeHints = decoded.routeHints,
val current = _sendState.value readState = { _sendState.value },
if (alias != null && copyAlias = { state, alias ->
current is SendState.LnurlInvoiceReady && (state as? SendState.LnurlInvoiceReady)
current.payee == pubkey) { ?.takeIf { it.payee == decoded.payee }
_sendState.value = current.copy(nodeAlias = alias) ?.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)
)
}
}
} }
)
} }
} }
@@ -944,4 +921,37 @@ class WalletViewModel(
rawRes = raw 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 }
}
}
}
} }