**Built**, phases 1–4, one commit each: `9899c571`, `da48d7b0`, `3f5de588` and
the commit that changed this paragraph. Written against `3aba04a0`, with the
`lightning-kmp-app` submodule at `e0d2b82`, which the build did not touch.
`StandaloneElectrum` is the loop and `IdentityElectrum` is where the nav host
gets a client. The phases are kept as written; where the build chose differently
the table below says so.
| what the plan said | what it turned out to be |
|---|---|
| the connect seam is a `suspend (ServerAddress, Duration) -> Unit` | an interface, `ElectrumLink`, with `connect`, `disconnect` and `stop`. The loop drops the connection on every change of control, and a test has to see that as a status, not a socket |
| the resolution is a plain function the nav host calls inside `remember` | a class, `IdentityElectrum`, held for the app's life. A `remember` keyed on the identity computes the new client *before*`DisposableEffect` disposes the old one, so a switch between two nsec profiles would have had both open for a moment. The class stops the last before it makes the next |
| the standalone client reads the network monitor | and starts it: `NetworkMonitor.start()` is otherwise called only by the node's daemon, so a device that opens an nsec profile first would never have learnt it was online. `start()` is idempotent on all three platforms |
| `wallets` becomes non-null at every screen | at every screen but the profile's. `ActiveProfileScreen` and `MemberWalletsViewModel` keep a nullable `wallets`, because the previews and `ReadOnlyEntrancesJvmTest` compose the profile without an identity. In the app it is never null there |
| the nav host passes a non-null `wallets` | it still holds a nullable one, null with no identity, and each wallet route opens with `groupOnchainWallets ?: return@composable`: the frame between a profile switch and the startup screen has no chain to show |
| the tests that drove the refusal are rewritten or deleted | the addresses view model's *cannot watch* test is deleted; the group send view model's *no node* test became *no share*, the other way to be refused the form; the rest pass a `GroupOnchainWallets` over an empty `FakeElectrum` |
| `GroupWalletViewModel`'s `watcher` | stays nullable: the tests default it, and nothing in the app passes null |
A real Electrum server has not been reached through this client yet. Every phase
was checked with `:composeApp:jvmTest` (1,485 tests),
`:composeApp:compileDebugKotlinAndroid` and `m3-audit.sh --check`. Phase 5, the
The headline: **every consumer already takes an `IElectrumClient`, and every
"cannot watch" branch in the app is `wallets == null`. So the work is a connection
loop, about sixty lines ported from the node's daemon, and one `remember` in
`BennuNavHost` that hands it to whoever has no node. No screen learns a new state.
Five of them lose one.**
---
## What is already there
| fact | where | what it means here |
|---|---|---|
| `GroupOnchainWallets`, `GroupOnchainWallet` and `GroupTransactionWatcher` take an `IElectrumClient`, not a `PhoenixBusiness` | [wallet/](../composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/) | nothing below the nav host knows where the client came from |
| both are made from `activeIdentity?.business?.electrumClient` and are null without it | [BennuNavHost.kt:246](../composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/navigation/BennuNavHost.kt) | the one place the null enters |
| every `canWatch` is `wallets != null`, and `CannotWatch` / `CANNOT_WATCH` are the same test | `GroupWalletViewModel:124`, `MemberWalletViewModel:71`, `GroupWalletAddressesViewModel:65`, `BennuNavHost:1222` | with a client for everyone, the condition never holds |
| an nsec or npub identity's preferences already carry Tor and a custom Electrum server, *"saved anyway"* | [IdentityWriter.kt:64](../composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/identity/IdentityWriter.kt) `prepareIdentity` | the configuration exists and nothing reads it yet |
| the server lists are public top-level vals, and `platformElectrumRegtestConf` is a public `expect fun` | `lightning-kmp-app/.../data/ElectrumServers.kt` | a random server can be picked without an `AppConfigurationManager`, which needs a business |
| `AppConnectionsDaemon` is the only reconnect loop, and its constructor takes a `PhoenixBusiness` | `lightning-kmp-app/.../managers/AppConnectionsDaemon.kt:241` | it cannot be reused. Its Electrum branch is what gets ported |
| `ElectrumClient.connect` returns after one attempt; reconnecting is the caller's job | `lightning-kmp/.../ElectrumClient.kt:75` | the loop is not optional |
| `ElectrumClient.stop()` is final: *"the client cannot be used after it has been closed"* | `ElectrumClient.kt:296` | one client per identity, made anew on a switch |
| `NetworkMonitor` lives on `PhoenixGlobal`, not the business, and `start()` is idempotent on every platform | `PhoenixGlobal.kt:38`, the three `NetworkMonitor.*.kt` | available without a node, and safe to start from a second place |
| `FakeElectrum` implements `IElectrumClient` for the wallet tests | [FakeElectrum.kt](../composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/wallet/FakeElectrum.kt) | the downstream tests need nothing new |
## Decisions
**D1. Only a profile with no node gets one.** A mnemonic profile keeps the node's
client. A second connection from the same device would tell a second server the
same addresses, and would give the group wallets a view of the chain that can
disagree with the node's. The rule is `identity.business?.electrumClient ?:
standalone`, tested on `business`, not on the kind, as `switchToIdentity` does.
**D2. It lives in `composeApp`, not the submodule.** The loop is small, and the
submodule's managers are all shaped around a business. Picking a random server is
a `when (chain)` over the public lists. That copies five lines of
`AppConfigurationManager.randomElectrumServer`, which is cheaper than a submodule
commit to move them. `Testnet4` and `Signet` are `TODO()` there, and they are
"no server" here, never a crash. A profile on those networks reads as "connecting",
the same as a node with no server configured.
**D3. The identity's own preferences choose the server**, exactly as
`BusinessManager` does for a node: `userPrefs.getElectrumServer` if set, a random
server for `NodeParamsManager.chain` if not, and a reconnect whenever either
preference changes. The same profile, given a seed later, keeps the same server.
**D4. Tor behaves as it does for the node, and that is not much.** This fork's
socket builder is plain TCP. `isTorEnabled` only swaps the random list for onion
addresses, which plain TCP cannot reach, and `requireOnionIfTorEnabled` refuses a
clearnet custom server. The standalone client copies both rules, so a profile
with Tor on never falls back to a clearnet connection against the user's choice.
Its wallets then stay on "connecting", which is what a mnemonic profile with Tor
on already shows today. A real Tor transport is its own plan; see
[Out of scope](#out-of-scope).
**D5. It connects when the identity becomes active, not when a wallet screen
opens.** `GroupTransactionWatcher` sends the group's signed spends and follows
them to a confirmation *"whether or not anybody opens the wallet"*. A lazy
client would make that true only for mnemonic profiles. A bare connection reveals
the device's IP and nothing else. Addresses are still subscribed only when a
wallet is first asked for, as they are now. **This is a behaviour change:** an
nsec or npub profile made no Electrum connection before, and from Phase 2 it
makes one on every start. The note's rollout section must say so.
**D6. An npub watches too.** Watching needs a descriptor, and descriptors come
from group-signed events, which a read-only profile can already read. Rebroadcasting
a transaction somebody else signed needs no key. Every send stays gated on
`canSign` and the share, as now, so an npub sees balances and proposes nothing.
**D7. Foreground only.** The client lives in `applicationIOScope`, as
`GroupOnchainWallets` does. A mnemonic node is also run headless by Android's
WorkManager, for payments. A nodeless profile has no payment to wake up for, and
a watch that only runs in the background would answer a question nobody is asking.
---
## Phase 1 — the connection loop
**Half a day, most of it in the test.**
`wallet/StandaloneElectrum.kt`: owns one `ElectrumClient`, and exposes it as
`client: IElectrumClient`, with `start()` and `stop()`. Inside is the daemon's
Electrum branch with the business taken out:
- collects `networkMonitor.networkState`, `userPrefs.getIsTorEnabled` and
`userPrefs.getElectrumServer`, combined; any change disconnects and starts the
loop again, as `forceDisconnect` does;
- on `Connection.CLOSED`, waits `connectionPause(attempt)`, the same table, then
calls `connect` with the same handshake-timeout ladder;
-`stop()` cancels the loop and calls `ElectrumClient.stop()`. A stopped instance
is never restarted: the client's own comment says it cannot be, so a switch
makes a new one.
The connect itself goes behind a small seam: a
`suspend (ServerAddress, Duration) -> Unit` defaulting to `client.connect(…,
TcpSocket.Builder(), …)`. The loop can then be tested against a status flow the
test drives, with no socket.
**Test** in `jvmTest`, `StandaloneElectrumJvmTest`, on a `TestScope`:
- a custom server is used when set, and a random server from the chain's list when not;
- a closed connection is retried after the pause, and the attempt count resets on
`ESTABLISHED`;
- losing the network disconnects, and getting it back connects;
- Tor on with a clearnet custom server that requires onion never calls `connect`;
- after `stop()`, nothing is retried and the client is stopped.
## Phase 2 — one client per identity, handed to the wallets
**Half a day.**
In `BennuNavHost`, the two `remember(activeIdentityForWallets?.business)` blocks