feat(wallet): an nsec or npub profile watches the chain on a client of its own (Phase 2)

IdentityElectrum hands the nav host the node's client for a profile with a
node, and a StandaloneElectrum for one without, stopping the last before the
next is made. The group wallets and the transaction watcher are made from it,
so they now exist for every profile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-24 13:10:19 +02:00
1 parent 9899c57164
commit da48d7b06c
3 files changed
+238 -11

No files matched your search

@@ -149,6 +149,7 @@ import com.machankura.bennu.compose.ui.composable.MemberWalletScreen
import com.machankura.bennu.compose.ui.composable.NewMemberWalletScreen
import com.machankura.bennu.compose.database.repository.DatabaseMemberWalletRepository
import com.machankura.bennu.compose.wallet.GroupOnchainWallets
import com.machankura.bennu.compose.wallet.IdentityElectrum
import com.machankura.bennu.compose.wallet.GroupTransactionWatcher
import fr.acinq.phoenix.managers.NodeParamsManager
import com.machankura.bennu.compose.ui.composable.navigation.routes.FrostSigningRoute
@@ -237,14 +238,25 @@ fun BennuNavHost(
)
}
// The group wallets this identity is watching, on its own node's Electrum
// client -- null for an nsec or npub profile, which has no node and so no
// chain to watch. Remade with the node and stopped with it, because a switch
// of profile is a restart and a wallet left subscribing on the last one's
// connection is a wallet nobody can see.
// The Electrum client this identity watches the chain on: its node's, or --
// for an nsec or npub profile, which has no node -- one of its own. Remade
// with the identity, the last one stopped first, because a switch of profile
// is a restart and a wallet left subscribing on the last one's connection is
// a wallet nobody can see. See docs/standalone-electrum.md.
val activeIdentityForWallets by sovereignWalletViewModel.activeIdentity.collectAsState()
val groupOnchainWallets = remember(activeIdentityForWallets?.business) {
activeIdentityForWallets?.business?.let { GroupOnchainWallets(it.electrumClient, applicationIOScope) }
val identityElectrum = remember {
IdentityElectrum(IdentityElectrum.standalone(phoenixGlobal.networkMonitor, phoenixGlobal.loggerFactory))
}
DisposableEffect(identityElectrum) {
onDispose { identityElectrum.stop() }
}
val electrum = remember(activeIdentityForWallets?.id, activeIdentityForWallets?.business) {
identityElectrum.clientFor(activeIdentityForWallets)
}
// The group wallets this identity is watching. Remade with the client.
val groupOnchainWallets = remember(electrum) {
electrum?.let { GroupOnchainWallets(it, applicationIOScope) }
}
DisposableEffect(groupOnchainWallets) {
onDispose { groupOnchainWallets?.let { wallets -> applicationIOScope.launch { wallets.stop() } } }
@@ -252,10 +264,10 @@ fun BennuNavHost(
// Sends the group's signed spends to the network from this device too, and
// follows them to a confirmation -- whether or not anybody opens the wallet.
// Same lifetime as the wallets: the identity's node, and nothing longer.
val groupTransactionWatcher = remember(activeIdentityForWallets?.business) {
activeIdentityForWallets?.business?.let {
GroupTransactionWatcher(auxDatabaseManager.auxDatabase, it.electrumClient, applicationIOScope).also { watcher -> watcher.start() }
// Same lifetime as the wallets: the identity's client, and nothing longer.
val groupTransactionWatcher = remember(electrum) {
electrum?.let {
GroupTransactionWatcher(auxDatabaseManager.auxDatabase, it, applicationIOScope).also { watcher -> watcher.start() }
}
}
DisposableEffect(groupTransactionWatcher) {
@@ -0,0 +1,78 @@
package com.machankura.bennu.compose.wallet
import co.touchlab.kermit.Logger
import com.machankura.bennu.compose.identity.Identity
import fr.acinq.lightning.blockchain.electrum.IElectrumClient
import fr.acinq.lightning.logging.LoggerFactory
import fr.acinq.phoenix.managers.NodeParamsManager
import fr.acinq.phoenix.managers.global.NetworkMonitor
import fr.acinq.phoenix.data.WalletId
/**
* The Electrum client the active identity watches the chain on.
*
* A profile with a node watches on the node's client; one with no node -- an nsec
* or an npub -- gets a [StandaloneElectrum] of its own. Never both: a second
* connection from a profile that has one would tell a second server the same
* addresses, and give the wallets a chain that can disagree with the node's (D1 of
* `docs/standalone-electrum.md`). The test is the node client, not the kind, as
* `switchToIdentity` tests `business`.
*
* One per app, held by the nav host. A change of identity stops the last
* standalone client *before* the next is made, so a switch between two nsec
* profiles never has two connections open; a stopped client cannot be restarted,
* so the next is always new.
*/
class IdentityElectrum(
private val standaloneFor: (Identity) -> StandaloneElectrum,
) {
private val logger = Logger.withTag("IdentityElectrum")
private var key: Pair<WalletId, IElectrumClient?>? = null
private var client: IElectrumClient? = null
private var standalone: StandaloneElectrum? = null
/**
* The client for [identity], made the first time it is asked for and kept while
* it stays the same identity on the same node. Null only with no identity.
*/
fun clientFor(identity: Identity?, nodeClient: IElectrumClient? = identity?.business?.electrumClient): IElectrumClient? {
val next = identity?.let { it.id to nodeClient }
if (next == key) return client
stop()
key = next
client = when {
identity == null -> null
nodeClient != null -> nodeClient
else -> {
logger.i { "starting an Electrum client for ${identity.id}, which has no node" }
standaloneFor(identity).also { it.start() }.also { standalone = it }.client
}
}
return client
}
/** Stops the standalone client, if the identity has one. The node's is the node's to stop. */
fun stop() {
standalone?.stop()
standalone = null
client = null
key = null
}
companion object {
/** The real thing: the identity's own Electrum preferences, the device's network, the node's network. */
fun standalone(networkMonitor: NetworkMonitor, loggerFactory: LoggerFactory): (Identity) -> StandaloneElectrum = { identity ->
// Idempotent on every platform, and otherwise only the node's daemon calls it.
networkMonitor.start()
StandaloneElectrum(
chain = NodeParamsManager.chain,
networkState = networkMonitor.networkState,
isTorEnabled = identity.userPrefs.getIsTorEnabled,
customServer = identity.userPrefs.getElectrumServer,
link = ElectrumClientLink(loggerFactory),
)
}
}
}