android: block the wallet screen on an unbacked seed, and make it re-showable
All checks were successful
android-ci / android (pull_request) Successful in 10m35s

A created wallet's seed was two lines of small text at the bottom of the
wallet page, dismissed by one tap, gone for good once the screen was left.
A real user created a wallet, never saw the seed, and the app had no way to
show it again.

- `SeedBackupGate` (new, pure Kotlin): the confirm word `SEEN` and its
  case-insensitive match mirror the TUI's own `WalletSetup::CreateSeed`
  gate; create starts unbacked, import is backed up without the gate, and
  only a correct word can confirm.
- The seed card is the whole body until the word is typed and Done is
  tapped: no Cancel, no copy button (the Android clipboard is readable by
  every other app), Done dead until `SEEN`.
- "Back up seed" re-reads the seed through the new `Wire::wallet_seed()`
  (`WireViewModel.walletSeed()`), so a seed missed the first time is not
  lost. Its failure is shown next to the button, not at the bottom of a
  long scroll.
- While unconfirmed: a warning strip stays at the top of the wallet screen
  and the receive address and Show QR are hidden — XMR received into a
  wallet whose 25 words were never written down is unrecoverable.
- The flag lives in app-local prefs (`wallet_seed_backup`), not in the
  Keystore-wrapped store, and is reset by `createWallet` itself so no
  caller can forget it.

The stored flag is nullable, and the null is load-bearing. `false` means
this install created a wallet and nobody has confirmed the seed, so the
strip is up whether or not the balance reads. `null` means nothing is
stored — an install that predates the flag, or one that has never created
a wallet here — and there the strip waits for `wallet_balance` to return a
real amount, the core's own evidence that the child has a wallet open.
Without that distinction a first-run user who had configured a chain
source but not yet created a wallet (`wallet_balance` returns "err") would
be warned about a seed they do not have.

The app-bar Back still leaves the screen, deliberately — it is the TUI's
`Esc`. It costs nothing now, because leaving no longer loses the seed.

Verification: `./gradlew :app:testDebugUnitTest` green, 135 tests
(`SeedBackupGateTest` 18 of them).
This commit is contained in:
agent 2026-09-20 12:09:48 -04:00
commit 118191c541
No known key found for this signature in database
4 changed files with 588 additions and 17 deletions

View file

@ -1,6 +1,8 @@
package com.siriusdevops.onionwire
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.core.app.NotificationManagerCompat
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
@ -11,6 +13,8 @@ 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.ui.SeedBackupGate
import com.siriusdevops.onionwire.ui.WalletKeyOrigin
import com.siriusdevops.onionwire.wallet.ChainStorage
import com.siriusdevops.onionwire.wallet.NO_WALLET_RPC_MESSAGE
import com.siriusdevops.onionwire.wallet.WalletChildBinaries
@ -588,11 +592,32 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
suspend fun sendTip(pubkeyHex: String, amountXmr: String, memo: String = ""): Result<Unit> =
runCatching { requireWire().sendTip(pubkeyHex, amountXmr, memo) }
/**
* Create a wallet and read its seed once. The seed-backup flag is reset
* here, not by the screen: every successful create is a new seed nobody has
* read, and a caller that forgot to reset it would leave a spend key
* unrecoverable. See [SeedBackupGate.afterWallet].
*/
suspend fun createWallet(password: String): Result<String> =
runCatching { requireWire().createWallet(password) }
.onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) }
/**
* Import a wallet from a user-supplied seed. That seed is already on paper —
* the user just pasted it — so the wallet counts as backed up without the
* confirm step.
*/
suspend fun importWallet(password: String, seed: String): Result<Unit> =
runCatching { requireWire().importWallet(password, seed) }
.onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) }
/**
* Re-read the open wallet's 25-word seed, for the "Back up seed" recovery
* path. A spend key: only ever called from that explicit user action, shown
* on the blocking card, and dropped. Never logged and never persisted.
*/
suspend fun walletSeed(): Result<String> =
runCatching { requireWire().walletSeed() }
fun spendAuthorityWarning(): String = runCatching {
requireWire().spendAuthorityWarning()
@ -600,6 +625,47 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
"this wallet file can spend. anyone with its passphrase or the RPC creds can move the balance. write down the seed."
}
// --- Seed backup gate --------------------------------------------------
// Whether the open wallet's seed has been confirmed written down. This is
// NOT a secret — it is a boolean in app-local prefs, deliberately kept out
// of the Keystore-wrapped store under `filesDir/secrets`, which holds the
// passphrase and nothing else.
//
// The flow is nullable, and the null is load-bearing: nothing stored means
// "this install has never created or imported a wallet through this build",
// which is not the same as "the user created one and has not confirmed it".
// See [SeedBackupGate.warningVisible] for why the screen needs both.
private val seedBackupPrefs: SharedPreferences =
app.getSharedPreferences(SEED_BACKUP_PREFS, Context.MODE_PRIVATE)
private val _walletSeedConfirmed = MutableStateFlow(
if (seedBackupPrefs.contains(SEED_BACKED_UP_KEY)) {
seedBackupPrefs.getBoolean(SEED_BACKED_UP_KEY, false)
} else {
null
},
)
/**
* The stored seed-backup flag: `false` unconfirmed, `true` confirmed or
* imported, `null` nothing stored (an install that predates the flag).
*/
val walletSeedConfirmed: StateFlow<Boolean?> = _walletSeedConfirmed.asStateFlow()
/**
* One confirm step from the blocking seed card. The gate decides: a wrong
* word leaves the flag where it was, so this can never un-confirm a wallet.
*/
fun confirmWalletSeed(typed: String) {
setWalletSeedConfirmed(SeedBackupGate.afterConfirm(typed, _walletSeedConfirmed.value == true))
}
private fun setWalletSeedConfirmed(confirmed: Boolean) {
seedBackupPrefs.edit().putBoolean(SEED_BACKED_UP_KEY, confirmed).apply()
_walletSeedConfirmed.value = confirmed
}
// --- 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
@ -920,3 +986,13 @@ suspend fun walletChildRefreshLoop(
/** Cadence of the wallet-child refresh loop, in milliseconds. */
private const val WALLET_CHILD_TICK_MS: Long = 2_000
/**
* App-local prefs file for the seed-backup flag. Not a secret, so it is a plain
* prefs file rather than anything under `filesDir/secrets`.
*/
private const val SEED_BACKUP_PREFS: String = "wallet_seed_backup"
/** Key inside [SEED_BACKUP_PREFS]: has the open wallet's seed been written down? */
private const val SEED_BACKED_UP_KEY: String = "backed_up"