Compare commits

..
Author SHA1 Message Date
agent
3431a9eda9
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 10m39s
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. An install that predates the flag reads as
  unconfirmed: "we never asked" is not "the user wrote it down", and
  "Back up seed" is the way out.

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, 132 tests
(`SeedBackupGateTest` 15 of them).
2026-09-20 12:09:48 -04:00
4 changed files with 47 additions and 123 deletions

View file

@ -600,7 +600,7 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
*/ */
suspend fun createWallet(password: String): Result<String> = suspend fun createWallet(password: String): Result<String> =
runCatching { requireWire().createWallet(password) } runCatching { requireWire().createWallet(password) }
.onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) } .onSuccess { setWalletSeedBackedUp(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) }
/** /**
* Import a wallet from a user-supplied seed. That seed is already on paper — * Import a wallet from a user-supplied seed. That seed is already on paper —
@ -609,7 +609,7 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
*/ */
suspend fun importWallet(password: String, seed: String): Result<Unit> = suspend fun importWallet(password: String, seed: String): Result<Unit> =
runCatching { requireWire().importWallet(password, seed) } runCatching { requireWire().importWallet(password, seed) }
.onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) } .onSuccess { setWalletSeedBackedUp(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) }
/** /**
* Re-read the open wallet's 25-word seed, for the "Back up seed" recovery * Re-read the open wallet's 25-word seed, for the "Back up seed" recovery
@ -631,39 +631,31 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
// of the Keystore-wrapped store under `filesDir/secrets`, which holds the // of the Keystore-wrapped store under `filesDir/secrets`, which holds the
// passphrase and nothing else. // passphrase and nothing else.
// //
// The flow is nullable, and the null is load-bearing: nothing stored means // An install that predates this flag reads as unconfirmed rather than
// "this install has never created or imported a wallet through this build", // silently backed up: "we never asked" is not "the user wrote it down", and
// which is not the same as "the user created one and has not confirmed it". // the screen's `Back up seed` action is the path out of it.
// See [SeedBackupGate.warningVisible] for why the screen needs both.
private val seedBackupPrefs: SharedPreferences = private val seedBackupPrefs: SharedPreferences =
app.getSharedPreferences(SEED_BACKUP_PREFS, Context.MODE_PRIVATE) app.getSharedPreferences(SEED_BACKUP_PREFS, Context.MODE_PRIVATE)
private val _walletSeedConfirmed = MutableStateFlow( private val _walletSeedBackedUp = MutableStateFlow(
if (seedBackupPrefs.contains(SEED_BACKED_UP_KEY)) { seedBackupPrefs.getBoolean(SEED_BACKED_UP_KEY, false),
seedBackupPrefs.getBoolean(SEED_BACKED_UP_KEY, false)
} else {
null
},
) )
/** /** True once the open wallet's seed has been confirmed written down. */
* The stored seed-backup flag: `false` unconfirmed, `true` confirmed or val walletSeedBackedUp: StateFlow<Boolean> = _walletSeedBackedUp.asStateFlow()
* 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 * 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. * word leaves the flag where it was, so this can never un-confirm a wallet.
*/ */
fun confirmWalletSeed(typed: String) { fun confirmWalletSeed(typed: String) {
setWalletSeedConfirmed(SeedBackupGate.afterConfirm(typed, _walletSeedConfirmed.value == true)) setWalletSeedBackedUp(SeedBackupGate.afterConfirm(typed, _walletSeedBackedUp.value))
} }
private fun setWalletSeedConfirmed(confirmed: Boolean) { private fun setWalletSeedBackedUp(backedUp: Boolean) {
seedBackupPrefs.edit().putBoolean(SEED_BACKED_UP_KEY, confirmed).apply() seedBackupPrefs.edit().putBoolean(SEED_BACKED_UP_KEY, backedUp).apply()
_walletSeedConfirmed.value = confirmed _walletSeedBackedUp.value = backedUp
} }
// --- App-owned wallet child (issue #107) ------------------------------- // --- App-owned wallet child (issue #107) -------------------------------

View file

@ -21,9 +21,9 @@ enum class WalletKeyOrigin {
* *
* The gate exists because a seed shown once and then lost is an unrecoverable * The gate exists because a seed shown once and then lost is an unrecoverable
* wallet: XMR received into a wallet whose 25 words were never written down * wallet: XMR received into a wallet whose 25 words were never written down
* cannot be spent or recovered by anyone, including this app. So the answer is * cannot be spent or recovered by anyone, including this app. So the flag is
* not advisory — while it says the seed is unbacked, the wallet screen keeps a * not advisory — while it is `false` the wallet screen keeps a warning up and
* warning up and hides the receive address and its QR. * hides the receive address and its QR.
* *
* The confirm word and its case-insensitivity mirror the TUI's own seed screen * The confirm word and its case-insensitivity mirror the TUI's own seed screen
* (`src/tui.rs`, `WalletSetup::CreateSeed`, which compares with * (`src/tui.rs`, `WalletSetup::CreateSeed`, which compares with
@ -68,39 +68,15 @@ object SeedBackupGate {
/** Done is dead until the word is right. */ /** Done is dead until the word is right. */
fun confirmEnabled(typed: String): Boolean = accepts(typed) fun confirmEnabled(typed: String): Boolean = accepts(typed)
/** /** The persistent strip is up exactly while the seed is unconfirmed. */
* The persistent strip is up while this install has an unconfirmed seed. fun warningVisible(backedUp: Boolean): Boolean = !backedUp
*
* [confirmed] is the stored flag, and it is nullable on purpose:
*
* - `false` — this install created a wallet and nobody has confirmed the
* seed. The wallet exists whether or not its balance reads, so the strip
* is up regardless.
* - `true` — confirmed, or imported. Nothing to say.
* - `null` — nothing stored: an install that predates this flag, or one
* that has never created or imported a wallet here. "We never asked" is
* not "the user wrote it down", but it is also not evidence that a wallet
* exists — so the strip waits for [walletReadable], the core's own proof
* that the child has a wallet open.
*
* Without that last distinction the strip would fire on every install that
* has configured a chain source but not yet created a wallet, which is
* exactly the state a first-run user is in.
*/
fun warningVisible(confirmed: Boolean?, walletReadable: Boolean): Boolean = when (confirmed) {
true -> false
false -> true
null -> walletReadable
}
/** /**
* The receive address and its QR are shown only while there is no unbacked * The receive address and its QR are shown only once the seed is confirmed.
* seed to worry about. Receiving into an unbacked wallet is the one mistake * Receiving into an unbacked wallet is the one mistake this gate exists to
* this gate exists to prevent, so the address is hidden rather than merely * prevent, so it is hidden rather than merely warned about.
* warned about.
*/ */
fun receiveAddressVisible(confirmed: Boolean?, walletReadable: Boolean): Boolean = fun receiveAddressVisible(backedUp: Boolean): Boolean = backedUp
!warningVisible(confirmed, walletReadable)
/** /**
* The strip's words. One source: the screen renders this verbatim, so the * The strip's words. One source: the screen renders this verbatim, so the

View file

@ -76,7 +76,7 @@ fun WalletScreen(
val childError by vm.walletChildError.collectAsStateWithLifecycle() val childError by vm.walletChildError.collectAsStateWithLifecycle()
val starting by vm.walletChildStarting.collectAsStateWithLifecycle() val starting by vm.walletChildStarting.collectAsStateWithLifecycle()
val chain by vm.chainSource.collectAsStateWithLifecycle() val chain by vm.chainSource.collectAsStateWithLifecycle()
val seedConfirmed by vm.walletSeedConfirmed.collectAsStateWithLifecycle() val backedUp by vm.walletSeedBackedUp.collectAsStateWithLifecycle()
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
// Resolved once per composition: the native library directory is fixed for // Resolved once per composition: the native library directory is fixed for
// the life of the process. // the life of the process.
@ -202,16 +202,12 @@ fun WalletScreen(
} }
// The persistent unbacked strip, above everything else on the page. // The persistent unbacked strip, above everything else on the page.
// `walletReadable` is the core's own evidence that the child has a // `walletOpen` is the core's own "configured" signal — `wallet_balance`
// wallet open: `wallet_balance` returns exactly "off" for a chain // returns exactly "off" for a wallet the child has not configured —
// source that was never configured, "err" for a configured one whose // so a fresh install with no wallet is not nagged about a seed it
// read failed (no wallet file open yet, or the child is down), and a // does not have.
// real amount otherwise. The gate uses it only for an install that val walletOpen = balance != null && balance != "off"
// has never created a wallet here, so a first-run user who has val unbacked = walletOpen && SeedBackupGate.warningVisible(backedUp)
// configured a daemon but not yet created anything is not nagged
// about a seed they do not have.
val walletReadable = balance != null && balance != "off" && balance != "err"
val unbacked = SeedBackupGate.warningVisible(seedConfirmed, walletReadable)
if (unbacked) { if (unbacked) {
UnbackedSeedCard(error = error, onBackUpSeed = { openSeedBackup() }) UnbackedSeedCard(error = error, onBackUpSeed = { openSeedBackup() })
} }
@ -241,9 +237,7 @@ fun WalletScreen(
// Hidden until the seed is confirmed written down: XMR into a // Hidden until the seed is confirmed written down: XMR into a
// wallet whose 25 words were never recorded is unrecoverable, // wallet whose 25 words were never recorded is unrecoverable,
// so the address and its QR are gated, not just warned about. // so the address and its QR are gated, not just warned about.
val addr = address?.takeIf { val addr = address?.takeIf { SeedBackupGate.receiveAddressVisible(backedUp) }
SeedBackupGate.receiveAddressVisible(seedConfirmed, walletReadable)
}
if (addr != null) { if (addr != null) {
Spacer(Modifier.height(8.dp)) Spacer(Modifier.height(8.dp))
Text( Text(
@ -448,11 +442,8 @@ fun WalletScreen(
} }
// The recovery path for a wallet whose seed was missed. The // The recovery path for a wallet whose seed was missed. The
// strip above carries its own copy of this button while the // strip above carries its own copy of this button while the
// seed is unconfirmed, so exactly one is ever on screen — // seed is unconfirmed, so exactly one is ever on screen.
// and this copy is only offered once the child can actually if (walletOpen && !SeedBackupGate.warningVisible(backedUp)) {
// read a wallet, which is the only state in which the seed
// read can succeed.
if (walletReadable && !SeedBackupGate.warningVisible(seedConfirmed, walletReadable)) {
Spacer(Modifier.height(8.dp)) Spacer(Modifier.height(8.dp))
OutlinedButton( OutlinedButton(
onClick = { openSeedBackup() }, onClick = { openSeedBackup() },

View file

@ -16,12 +16,6 @@ import org.junit.Test
*/ */
class SeedBackupGateTest { class SeedBackupGateTest {
/** A wallet the child can actually read: `wallet_balance` returned a value. */
private val readable = true
/** `wallet_balance` said "off" (no chain source) or "err" (nothing open). */
private val notReadable = false
// --- the confirm word is the TUI's word --------------------------------- // --- the confirm word is the TUI's word ---------------------------------
@Test @Test
@ -98,8 +92,7 @@ class SeedBackupGateTest {
fun a_new_create_resets_a_confirmed_wallet_to_unbacked() { fun a_new_create_resets_a_confirmed_wallet_to_unbacked() {
// Create -> confirm -> create again: the second wallet file has its own // Create -> confirm -> create again: the second wallet file has its own
// seed, and nobody has read it yet. // seed, and nobody has read it yet.
val first = val first = SeedBackupGate.afterConfirm("SEEN", SeedBackupGate.afterWallet(WalletKeyOrigin.Created))
SeedBackupGate.afterConfirm("SEEN", SeedBackupGate.afterWallet(WalletKeyOrigin.Created))
assertTrue(first) assertTrue(first)
assertFalse(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) assertFalse(SeedBackupGate.afterWallet(WalletKeyOrigin.Created))
} }
@ -107,61 +100,33 @@ class SeedBackupGateTest {
// --- what the screen does with the flag --------------------------------- // --- what the screen does with the flag ---------------------------------
@Test @Test
fun a_created_wallet_keeps_the_strip_up_until_the_word_is_typed() { fun the_warning_strip_is_up_only_while_the_seed_is_unconfirmed() {
val created = SeedBackupGate.afterWallet(WalletKeyOrigin.Created) assertTrue(SeedBackupGate.warningVisible(false))
assertTrue(SeedBackupGate.warningVisible(created, readable)) assertFalse(SeedBackupGate.warningVisible(true))
val confirmed = SeedBackupGate.afterConfirm("seen", created)
assertFalse(SeedBackupGate.warningVisible(confirmed, readable))
}
@Test
fun an_imported_wallet_never_shows_the_strip() {
val backedUp = SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)
assertFalse(SeedBackupGate.warningVisible(backedUp, readable))
assertFalse(SeedBackupGate.warningVisible(backedUp, notReadable))
}
@Test
fun a_created_wallet_keeps_the_strip_up_even_before_its_balance_reads() {
// A create is proof a wallet exists, so a child that has not answered
// yet must not be read as "no wallet to warn about".
assertTrue(SeedBackupGate.warningVisible(false, notReadable))
} }
@Test @Test
fun the_receive_address_is_hidden_while_the_seed_is_unconfirmed() { fun the_receive_address_is_hidden_while_the_seed_is_unconfirmed() {
assertFalse(SeedBackupGate.receiveAddressVisible(false, readable)) assertFalse(SeedBackupGate.receiveAddressVisible(false))
assertTrue(SeedBackupGate.receiveAddressVisible(true, readable)) assertTrue(SeedBackupGate.receiveAddressVisible(true))
}
@Test
fun an_imported_wallet_keeps_its_receive_address_and_loses_the_strip() {
val backedUp = SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)
assertTrue(SeedBackupGate.receiveAddressVisible(backedUp))
assertFalse(SeedBackupGate.warningVisible(backedUp))
} }
@Test @Test
fun a_created_wallet_hides_its_receive_address_until_the_word_is_typed() { fun a_created_wallet_hides_its_receive_address_until_the_word_is_typed() {
val created = SeedBackupGate.afterWallet(WalletKeyOrigin.Created) val created = SeedBackupGate.afterWallet(WalletKeyOrigin.Created)
assertFalse(SeedBackupGate.receiveAddressVisible(created, readable)) assertFalse(SeedBackupGate.receiveAddressVisible(created))
assertTrue(SeedBackupGate.warningVisible(created))
val confirmed = SeedBackupGate.afterConfirm("seen", created) val confirmed = SeedBackupGate.afterConfirm("seen", created)
assertTrue(SeedBackupGate.receiveAddressVisible(confirmed, readable)) assertTrue(SeedBackupGate.receiveAddressVisible(confirmed))
} assertFalse(SeedBackupGate.warningVisible(confirmed))
@Test
fun an_install_that_never_created_a_wallet_here_waits_for_a_readable_wallet() {
// `null` is "nothing stored": an install that predates the flag, or one
// that has only configured a chain source. `wallet_balance` returns
// "err" for a configured chain whose child has no wallet open, and a
// first-run user is in exactly that state — nagging them about a seed
// they do not have is the false positive this rule exists to prevent.
assertFalse(SeedBackupGate.warningVisible(null, notReadable))
assertTrue(SeedBackupGate.receiveAddressVisible(null, notReadable))
}
@Test
fun an_unconfirmed_wallet_that_predates_the_flag_is_warned_about_once_it_reads() {
// The user this gate is for: they created a wallet on an older build,
// so nothing is stored, but the child can read the wallet. That is a
// real wallet whose seed nobody has confirmed.
assertTrue(SeedBackupGate.warningVisible(null, readable))
assertFalse(SeedBackupGate.receiveAddressVisible(null, readable))
} }
@Test @Test