android: start/stop the monero-wallet-rpc child from WalletScreen, bundle the binary
All checks were successful
android-ci / android (pull_request) Successful in 8m59s

Implements the Android half of issue #107. Stacked on `wt/t_8a8ff353`
(PR #108) because the UniFFI wallet-child surface is not on `main` yet.

Binary
------

`android/app/src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so` is a real
`monero-wallet-rpc` cross-compiled for `aarch64-linux-android` from
monero-project/monero v0.18.4.4 (516e5355) via upstream's own
`contrib/depends` + toolchain.cmake path, then stripped with the target's own
strip. 19 954 224 bytes, sha256
d9126788030ecd1ccdb40c7764e66e9a14d158a147ad335b256f0577681b5ad4.

It ships through `jniLibs` and not as an asset on purpose: since API 29 the
kernel refuses `exec()` on a file in the app's data directory, so the only
executable directory an app has is `applicationInfo.nativeLibraryDir`, which
the package manager fills from `lib/<abi>/` at install time. That requires
`android:extractNativeLibs="true"` (`packaging.jniLibs.useLegacyPackaging`),
which `app/build.gradle.kts` now sets, plus `keepDebugSymbols` for this one
entry so AGP's strip pass cannot rewrite the bytes CI verifies.

`scripts/build-monero-wallet-rpc-android.sh` reproduces the build and
`android/MONERO_BINARIES.md` records the four host quirks that cost the most
time (CMake must be 3.x; NDK r17b clang60 needs a libtinfo.so.5 shim; the
generated toolchain.cmake puts the HOST `/usr` in CMAKE_FIND_ROOT_PATH, which
breaks the link with a host libpgm.so and defines HAVE_HIDAPI against host
headers; the CMake target is `wallet_rpc_server`, not `monero-wallet-rpc`).

CI re-verifies the blob rather than taking it on trust:
`scripts/build-android-local.sh` checks `android/monero-binaries.sha256` before
Gradle, and after packaging pulls `lib/<abi>/libmonerowalletrpc.so` back out of
the APK and compares its sha256 with the committed file.

`arti` is NOT bundled, so a `.onion` daemon cannot be started yet. The app says
that instead of pretending: `arti_bin` is set to the path the binary would
occupy, and the screen refuses an `.onion` daemon while it is absent.
Loopback and clearnet daemons work.

UI
--

`WalletScreen` gains a "wallet daemon" card above the existing
Create / Import / Invoice / Tip actions, which are untouched:

* a status line from `Wire::wallet_status()` rendered verbatim —
  `off` / `connecting` / `syncing` / `ready` / `exited` plus the core's
  chain-source label (`receive-only` / `local node` / `remote onion daemon` /
  `remote daemon`). A died child reads `exited`; it is never folded into `off`
  and never into `ready`;
* Start / Stop calling `start_wallet_child` / `stop_wallet_child` through
  `WireViewModel`, on `Dispatchers.IO` (the spawn is synchronous inside the node
  and the status read re-decrypts the sealed record);
* a `host:port` field that saves a remote daemon via `set_chain_source`, plus a
  Receive-only button;
* honest failure states instead of a spinner: no bundled binary, no chain
  source, a `.onion` daemon with no arti, or a start that failed — each gets its
  own sentence, and Start is disabled with the reason on screen.

`WireViewModel` owns the 2s wallet-child poller, the same way it owns the chat
poller, so leaving the wallet screen and coming back cannot stack a second loop.
`ui/WalletChildStatusText.kt` is the pure model (mirrors the
`ChatRowInput`/`OutboxRowInput` convention) and carries the state mapping, the
status line and the Start/Stop gates; `WalletChildStatusTextTest` covers them on
the JVM.

Verified
--------

`./gradlew :app:testDebugUnitTest` passes, `scripts/build-android-local.sh`
produces `dist/onionwire-0.1.0-android-arm64-v8a.apk` (27 884 277 bytes,
versionName 0.1.0 / versionCode 100) with
`lib/arm64-v8a/libmonerowalletrpc.so` inside it at the committed sha256, and
`extractNativeLibs="true"` in the merged release manifest.

NOT verified: the binary was never executed. The build host has no arm64-v8a
AVD (all three existing AVDs are x86_64) and no aarch64 sysroot for qemu-user,
so nothing here proves `monero-wallet-rpc` starts, opens its wallet file and
answers JSON-RPC under Android's runtime. `android/MONERO_BINARIES.md` says so
plainly and lists the emulator steps to close the gap.
This commit is contained in:
apk-dev 2026-09-18 10:19:18 -04:00 • committed by agent
commit 0134396588
No known key found for this signature in database
11 changed files with 1440 additions and 1 deletions

View file

@ -11,6 +11,9 @@ import com.siriusdevops.onionwire.crypto.StoredPassphraseStore
import com.siriusdevops.onionwire.keepalive.KeepAliveService
import com.siriusdevops.onionwire.keepalive.KeepAliveStatus
import com.siriusdevops.onionwire.keepalive.NodeHost
import com.siriusdevops.onionwire.wallet.NO_WALLET_RPC_MESSAGE
import com.siriusdevops.onionwire.wallet.WalletChildBinaries
import com.siriusdevops.onionwire.wallet.WalletChildBinaryPaths
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
@ -22,6 +25,8 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uniffi.onionwire_sdk.ChainChoice
import uniffi.onionwire_sdk.ChainConfig
import uniffi.onionwire_sdk.ChatMessage
import uniffi.onionwire_sdk.FriendInfo
import uniffi.onionwire_sdk.InviteInfo
@ -29,6 +34,7 @@ import uniffi.onionwire_sdk.OutboxRow
import uniffi.onionwire_sdk.RotateOutcome
import uniffi.onionwire_sdk.SendState
import uniffi.onionwire_sdk.TransferRow
import uniffi.onionwire_sdk.WalletChildStatus
import uniffi.onionwire_sdk.Wire
import uniffi.onionwire_sdk.decodeInvite
import java.io.File
@ -562,4 +568,201 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
}.getOrElse {
"this wallet file can spend. anyone with its passphrase or the RPC creds can move the balance. write down the seed."
}
// --- App-owned wallet child (issue #107) -------------------------------
// The app owns `monero-wallet-rpc` instead of asking the user to start one
// and export `ONIONWIRE_WALLET_RPC`. The core's own states are surfaced
// verbatim: off / connecting / syncing / ready / exited. Nothing here
// invents a "starting…" state, and a child that died is never folded into
// "off" — see `ui/WalletChildStatusText.kt`.
/** The last `wallet_status()` snapshot, or null before the first read. */
private val _walletChild = MutableStateFlow<WalletChildStatus?>(null)
val walletChild: StateFlow<WalletChildStatus?> = _walletChild.asStateFlow()
/**
* The last failed start/stop/read, display-ready. Sticky on purpose, like
* [ChatState.error]: a transient store read that fails once must not blink
* off the screen on the next successful tick.
*/
private val _walletChildError = MutableStateFlow<String?>(null)
val walletChildError: StateFlow<String?> = _walletChildError.asStateFlow()
/** The stored chain source + binary settings, for prefilling the editor. */
private val _chainSource = MutableStateFlow<ChainConfig?>(null)
val chainSource: StateFlow<ChainConfig?> = _chainSource.asStateFlow()
private var walletChildPoller: Job? = null
/**
* What this install can actually spawn. Read from
* `applicationInfo.nativeLibraryDir` — the only directory Android hands out
* executable — so the screen can say "not bundled" instead of offering a
* Start that can only fail.
*/
fun bundledBinaries(): WalletChildBinaryPaths =
WalletChildBinaries.resolve(
getApplication<Application>().applicationInfo.nativeLibraryDir,
)
/**
* Starts the one wallet-child refresh loop for the wallet screen. Re-entering
* the screen while the loop is alive is a no-op: navigating to the wallet
* and back must never stack a second poller.
*/
fun openWalletChild() {
if (walletChildPoller?.isActive == true) return
walletChildPoller = viewModelScope.launch {
while (currentCoroutineContext().isActive) {
refreshWalletChild()
delay(WALLET_CHILD_TICK_MS)
}
}
}
/** Stops the loop when the wallet screen leaves. */
fun closeWalletChild() {
walletChildPoller?.cancel()
walletChildPoller = null
}
/** Drops the last failure. The user has acted on it. */
fun clearWalletChildError() {
_walletChildError.value = null
}
/**
* One `wallet_status()` read.
*
* `Wire::wallet_status` is synchronous in Rust and re-reads the sealed
* wallet-child record out of the SQLCipher store, so it goes to
* [Dispatchers.IO] — a store decrypt on the main thread is an ANR waiting
* for a slow device. It never dials and never spawns, so a 2s cadence is
* cheap.
*/
suspend fun refreshWalletChild() {
val read = withContext(Dispatchers.IO) { runCatching { requireWire().walletStatus() } }
read.fold(
onSuccess = { _walletChild.value = it },
onFailure = { _walletChildError.value = it.message ?: it.toString() },
)
}
/** Re-reads the stored chain source so the editor can prefill from it. */
suspend fun refreshChainSource() {
val read = withContext(Dispatchers.IO) { runCatching { requireWire().chainSource() } }
read.onSuccess { _chainSource.value = it }
}
/**
* Saves a daemon the user typed, then leaves it to [startWalletChild] to
* spawn against it. `set_chain_source` only stores: the core validates the
* config before anything is written and a rejected config leaves the stored
* one — and any running child — untouched.
*
* [bins] is what this install resolved; the paths handed to the core are
* absolute, because nothing in an APK sandbox resolves a bare
* `monero-wallet-rpc` on `PATH`.
*/
suspend fun saveRemoteDaemon(
host: String,
port: Int,
bins: WalletChildBinaryPaths,
): Result<Unit> {
val rpc = bins.walletRpc
?: return Result.failure(IllegalStateException(NO_WALLET_RPC_MESSAGE))
return withContext(Dispatchers.IO) {
runCatching {
requireWire().setChainSource(
ChainConfig(
kind = ChainChoice.REMOTE,
monerodBin = "",
monerodDatadir = "",
host = host,
port = port.toUShort(),
walletRpcBin = rpc,
// Inert unless the host is a `.onion`; the screen refuses
// an onion daemon outright while no `arti` is bundled.
artiBin = expectedArtiPath(),
walletFilename = WalletChildBinaries.WALLET_FILENAME,
),
)
}
}.onSuccess {
_walletChildError.value = null
refreshChainSource()
refreshWalletChild()
}
}
/**
* Back to receive-only: no child, no daemon, no chain traffic. The binary
* paths are still stored so switching back to a daemon does not need them
* re-derived.
*/
suspend fun setReceiveOnly(bins: WalletChildBinaryPaths): Result<Unit> =
withContext(Dispatchers.IO) {
runCatching {
requireWire().setChainSource(
ChainConfig(
kind = ChainChoice.NONE,
monerodBin = "",
monerodDatadir = "",
host = "",
port = 0u,
walletRpcBin = bins.walletRpc.orEmpty(),
artiBin = expectedArtiPath(),
walletFilename = WalletChildBinaries.WALLET_FILENAME,
),
)
}
}.onSuccess {
_walletChildError.value = null
refreshChainSource()
refreshWalletChild()
}
/**
* Spawns the child (and its daemon/SOCKS siblings) and returns what the core
* reached.
*
* The returned state is `connecting` at best on the first call — the wallet
* RPC does not answer the instant the process exists — and that is reported
* as-is. The spawn is synchronous inside the node and a `.onion` source
* waits up to 20s for its SOCKS listener, so this never runs on the main
* thread.
*/
suspend fun startWalletChild(): Result<WalletChildStatus> {
_walletChildError.value = null
val started = withContext(Dispatchers.IO) {
runCatching { requireWire().startWalletChild() }
}
started.fold(
onSuccess = { _walletChild.value = it },
onFailure = { _walletChildError.value = it.message ?: it.toString() },
)
return started
}
/** Stops the child and its siblings. Idempotent in the core. */
suspend fun stopWalletChild(): Result<Unit> {
_walletChildError.value = null
val stopped = withContext(Dispatchers.IO) {
runCatching { requireWire().stopWalletChild() }
}
stopped.fold(
onSuccess = { refreshWalletChild() },
onFailure = { _walletChildError.value = it.message ?: it.toString() },
)
return stopped
}
/** The absolute path the core would be handed for the `arti` SOCKS child. */
private fun expectedArtiPath(): String = WalletChildBinaries.expectedPath(
getApplication<Application>().applicationInfo.nativeLibraryDir,
WalletChildBinaries.ARTI_LIB,
)
}
/** Cadence of the wallet-child refresh loop, in milliseconds. */
private const val WALLET_CHILD_TICK_MS: Long = 2_000