diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt index 4f5b2a8..7be3c6a 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -600,7 +600,7 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { */ suspend fun createWallet(password: String): Result = runCatching { requireWire().createWallet(password) } - .onSuccess { setWalletSeedBackedUp(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) } + .onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) } /** * 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 = runCatching { requireWire().importWallet(password, seed) } - .onSuccess { setWalletSeedBackedUp(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) } + .onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) } /** * Re-read the open wallet's 25-word seed, for the "Back up seed" recovery @@ -631,31 +631,39 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { // of the Keystore-wrapped store under `filesDir/secrets`, which holds the // passphrase and nothing else. // - // An install that predates this flag reads as unconfirmed rather than - // silently backed up: "we never asked" is not "the user wrote it down", and - // the screen's `Back up seed` action is the path out of it. + // 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 _walletSeedBackedUp = MutableStateFlow( - seedBackupPrefs.getBoolean(SEED_BACKED_UP_KEY, false), + private val _walletSeedConfirmed = MutableStateFlow( + if (seedBackupPrefs.contains(SEED_BACKED_UP_KEY)) { + seedBackupPrefs.getBoolean(SEED_BACKED_UP_KEY, false) + } else { + null + }, ) - /** True once the open wallet's seed has been confirmed written down. */ - val walletSeedBackedUp: StateFlow = _walletSeedBackedUp.asStateFlow() + /** + * The stored seed-backup flag: `false` unconfirmed, `true` confirmed or + * imported, `null` nothing stored (an install that predates the flag). + */ + val walletSeedConfirmed: StateFlow = _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) { - setWalletSeedBackedUp(SeedBackupGate.afterConfirm(typed, _walletSeedBackedUp.value)) + setWalletSeedConfirmed(SeedBackupGate.afterConfirm(typed, _walletSeedConfirmed.value == true)) } - private fun setWalletSeedBackedUp(backedUp: Boolean) { - seedBackupPrefs.edit().putBoolean(SEED_BACKED_UP_KEY, backedUp).apply() - _walletSeedBackedUp.value = backedUp + private fun setWalletSeedConfirmed(confirmed: Boolean) { + seedBackupPrefs.edit().putBoolean(SEED_BACKED_UP_KEY, confirmed).apply() + _walletSeedConfirmed.value = confirmed } // --- App-owned wallet child (issue #107) ------------------------------- diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/SeedBackupGate.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/SeedBackupGate.kt index dba352c..ee0c303 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/ui/SeedBackupGate.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/SeedBackupGate.kt @@ -21,9 +21,9 @@ enum class WalletKeyOrigin { * * 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 - * cannot be spent or recovered by anyone, including this app. So the flag is - * not advisory — while it is `false` the wallet screen keeps a warning up and - * hides the receive address and its QR. + * cannot be spent or recovered by anyone, including this app. So the answer is + * not advisory — while it says the seed is unbacked, the wallet screen keeps a + * warning up and hides the receive address and its QR. * * The confirm word and its case-insensitivity mirror the TUI's own seed screen * (`src/tui.rs`, `WalletSetup::CreateSeed`, which compares with @@ -68,15 +68,39 @@ object SeedBackupGate { /** Done is dead until the word is right. */ fun confirmEnabled(typed: String): Boolean = accepts(typed) - /** The persistent strip is up exactly while the seed is unconfirmed. */ - fun warningVisible(backedUp: Boolean): Boolean = !backedUp + /** + * The persistent strip is up while this install has an unconfirmed seed. + * + * [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 once the seed is confirmed. - * Receiving into an unbacked wallet is the one mistake this gate exists to - * prevent, so it is hidden rather than merely warned about. + * The receive address and its QR are shown only while there is no unbacked + * seed to worry about. Receiving into an unbacked wallet is the one mistake + * this gate exists to prevent, so the address is hidden rather than merely + * warned about. */ - fun receiveAddressVisible(backedUp: Boolean): Boolean = backedUp + fun receiveAddressVisible(confirmed: Boolean?, walletReadable: Boolean): Boolean = + !warningVisible(confirmed, walletReadable) /** * The strip's words. One source: the screen renders this verbatim, so the diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/WalletScreen.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/WalletScreen.kt index 65a925e..ec98015 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/ui/WalletScreen.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/WalletScreen.kt @@ -76,7 +76,7 @@ fun WalletScreen( val childError by vm.walletChildError.collectAsStateWithLifecycle() val starting by vm.walletChildStarting.collectAsStateWithLifecycle() val chain by vm.chainSource.collectAsStateWithLifecycle() - val backedUp by vm.walletSeedBackedUp.collectAsStateWithLifecycle() + val seedConfirmed by vm.walletSeedConfirmed.collectAsStateWithLifecycle() val scope = rememberCoroutineScope() // Resolved once per composition: the native library directory is fixed for // the life of the process. @@ -202,12 +202,16 @@ fun WalletScreen( } // The persistent unbacked strip, above everything else on the page. - // `walletOpen` is the core's own "configured" signal — `wallet_balance` - // returns exactly "off" for a wallet the child has not configured — - // so a fresh install with no wallet is not nagged about a seed it - // does not have. - val walletOpen = balance != null && balance != "off" - val unbacked = walletOpen && SeedBackupGate.warningVisible(backedUp) + // `walletReadable` is the core's own evidence that the child has a + // wallet open: `wallet_balance` returns exactly "off" for a chain + // source that was never configured, "err" for a configured one whose + // read failed (no wallet file open yet, or the child is down), and a + // real amount otherwise. The gate uses it only for an install that + // has never created a wallet here, so a first-run user who has + // 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) { UnbackedSeedCard(error = error, onBackUpSeed = { openSeedBackup() }) } @@ -237,7 +241,9 @@ fun WalletScreen( // Hidden until the seed is confirmed written down: XMR into a // wallet whose 25 words were never recorded is unrecoverable, // so the address and its QR are gated, not just warned about. - val addr = address?.takeIf { SeedBackupGate.receiveAddressVisible(backedUp) } + val addr = address?.takeIf { + SeedBackupGate.receiveAddressVisible(seedConfirmed, walletReadable) + } if (addr != null) { Spacer(Modifier.height(8.dp)) Text( @@ -442,8 +448,11 @@ fun WalletScreen( } // The recovery path for a wallet whose seed was missed. The // strip above carries its own copy of this button while the - // seed is unconfirmed, so exactly one is ever on screen. - if (walletOpen && !SeedBackupGate.warningVisible(backedUp)) { + // seed is unconfirmed, so exactly one is ever on screen — + // and this copy is only offered once the child can actually + // 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)) OutlinedButton( onClick = { openSeedBackup() }, diff --git a/android/app/src/test/java/com/siriusdevops/onionwire/ui/SeedBackupGateTest.kt b/android/app/src/test/java/com/siriusdevops/onionwire/ui/SeedBackupGateTest.kt index 1d6d614..9318753 100644 --- a/android/app/src/test/java/com/siriusdevops/onionwire/ui/SeedBackupGateTest.kt +++ b/android/app/src/test/java/com/siriusdevops/onionwire/ui/SeedBackupGateTest.kt @@ -16,6 +16,12 @@ import org.junit.Test */ 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 --------------------------------- @Test @@ -92,7 +98,8 @@ class SeedBackupGateTest { fun a_new_create_resets_a_confirmed_wallet_to_unbacked() { // Create -> confirm -> create again: the second wallet file has its own // seed, and nobody has read it yet. - val first = SeedBackupGate.afterConfirm("SEEN", SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) + val first = + SeedBackupGate.afterConfirm("SEEN", SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) assertTrue(first) assertFalse(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) } @@ -100,33 +107,61 @@ class SeedBackupGateTest { // --- what the screen does with the flag --------------------------------- @Test - fun the_warning_strip_is_up_only_while_the_seed_is_unconfirmed() { - assertTrue(SeedBackupGate.warningVisible(false)) - assertFalse(SeedBackupGate.warningVisible(true)) + fun a_created_wallet_keeps_the_strip_up_until_the_word_is_typed() { + val created = SeedBackupGate.afterWallet(WalletKeyOrigin.Created) + assertTrue(SeedBackupGate.warningVisible(created, readable)) + + 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 fun the_receive_address_is_hidden_while_the_seed_is_unconfirmed() { - assertFalse(SeedBackupGate.receiveAddressVisible(false)) - 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)) + assertFalse(SeedBackupGate.receiveAddressVisible(false, readable)) + assertTrue(SeedBackupGate.receiveAddressVisible(true, readable)) } @Test fun a_created_wallet_hides_its_receive_address_until_the_word_is_typed() { val created = SeedBackupGate.afterWallet(WalletKeyOrigin.Created) - assertFalse(SeedBackupGate.receiveAddressVisible(created)) - assertTrue(SeedBackupGate.warningVisible(created)) + assertFalse(SeedBackupGate.receiveAddressVisible(created, readable)) val confirmed = SeedBackupGate.afterConfirm("seen", created) - assertTrue(SeedBackupGate.receiveAddressVisible(confirmed)) - assertFalse(SeedBackupGate.warningVisible(confirmed)) + assertTrue(SeedBackupGate.receiveAddressVisible(confirmed, readable)) + } + + @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