From 118191c5412aa489d5c2cf3f8c8b1bc764861af2 Mon Sep 17 00:00:00 2001 From: agent Date: Sun, 20 Sep 2026 12:09:48 -0400 Subject: [PATCH] android: block the wallet screen on an unbacked seed, and make it re-showable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- .../siriusdevops/onionwire/WireViewModel.kt | 76 ++++++ .../onionwire/ui/SeedBackupGate.kt | 113 +++++++++ .../siriusdevops/onionwire/ui/WalletScreen.kt | 240 ++++++++++++++++-- .../onionwire/ui/SeedBackupGateTest.kt | 176 +++++++++++++ 4 files changed, 588 insertions(+), 17 deletions(-) create mode 100644 android/app/src/main/java/com/siriusdevops/onionwire/ui/SeedBackupGate.kt create mode 100644 android/app/src/test/java/com/siriusdevops/onionwire/ui/SeedBackupGateTest.kt 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 6f26632..7be3c6a 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -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 = 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 = 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 = 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 = + 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 = _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" + 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 new file mode 100644 index 0000000..ee0c303 --- /dev/null +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/SeedBackupGate.kt @@ -0,0 +1,113 @@ +package com.siriusdevops.onionwire.ui + +/** + * Where the open wallet's key material came from. + * + * The distinction is the whole gate. A seed the user *supplied* (import) is a + * seed the user already has on paper, so that wallet is backed up from the + * start. A seed the app *generated* (create) has been read by nobody until it + * is put on screen, so that wallet starts unbacked and stays unbacked until the + * user proves it is written down. + */ +enum class WalletKeyOrigin { + Created, + Imported, +} + +/** + * The seed-backup gate: every decision the wallet screen makes about an + * unconfirmed seed, as pure Kotlin — no Compose, no Android, no prefs — so the + * rules are covered by `SeedBackupGateTest` on the JVM without a device. + * + * 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 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 + * `eq_ignore_ascii_case("SEEN")`) instead of inventing a second convention for + * the same wallet. + */ +object SeedBackupGate { + + /** The word that releases the block. The TUI's word, not a new one. */ + const val CONFIRM_WORD: String = "SEEN" + + /** + * True when [typed] releases the block. + * + * Trimmed and case-insensitive, so a soft keyboard's auto-capitalised `Seen` + * and a trailing space from a paste both count. Everything else — including + * the empty string — stays blocked. + */ + fun accepts(typed: String): Boolean = + typed.trim().equals(CONFIRM_WORD, ignoreCase = true) + + /** + * The flag right after a wallet is created or imported. + * + * A create always resets it: the wallet file is new, and the seed it was + * built from has never been read by anyone. + */ + fun afterWallet(origin: WalletKeyOrigin): Boolean = when (origin) { + WalletKeyOrigin.Created -> false + WalletKeyOrigin.Imported -> true + } + + /** + * The flag after one confirm step. + * + * Only a correct [typed] can flip it to `true`; a wrong word can never + * un-confirm a wallet that is already backed up. + */ + fun afterConfirm(typed: String, backedUp: Boolean): Boolean = + backedUp || accepts(typed) + + /** Done is dead until the word is right. */ + fun confirmEnabled(typed: String): Boolean = accepts(typed) + + /** + * 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 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(confirmed: Boolean?, walletReadable: Boolean): Boolean = + !warningVisible(confirmed, walletReadable) + + /** + * The strip's words. One source: the screen renders this verbatim, so the + * reason the address disappeared travels with the hiding. + */ + const val WARNING: String = + "seed not backed up. write down the 25 words before you receive anything — " + + "a wallet whose seed was never written down cannot be recovered. " + + "the receive address stays hidden until you do." +} 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 4fbeeac..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,6 +76,7 @@ fun WalletScreen( val childError by vm.walletChildError.collectAsStateWithLifecycle() val starting by vm.walletChildStarting.collectAsStateWithLifecycle() val chain by vm.chainSource.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. @@ -92,7 +93,11 @@ fun WalletScreen( var error by remember { mutableStateOf(null) } var composer by remember { mutableStateOf(null) } var setup by remember { mutableStateOf(null) } - var backupSeed by remember { mutableStateOf(null) } + // The seed currently on the blocking card: set by a successful create, or by + // "Back up seed" re-reading it from the wallet child. Null means no card. + // Never persisted and never logged — it is a spend key. + var pendingSeed by remember { mutableStateOf(null) } + var seedConfirm by remember { mutableStateOf("") } var daemonText by remember { mutableStateOf("") } var daemonError by remember { mutableStateOf(null) } @@ -103,6 +108,26 @@ fun WalletScreen( runCatching { transfers = vm.walletRecentTransfers(25) } } + /** + * "Back up seed": re-read the open wallet's seed from the wallet child and + * put it on the blocking card. This is the only path to a seed that was + * created before this screen existed, so the failure is shown rather than + * swallowed — it is the difference between "the child is not running" and + * "there is nothing to recover". + */ + fun openSeedBackup() { + scope.launch { + error = null + vm.walletSeed().fold( + onSuccess = { seed -> + pendingSeed = seed + seedConfirm = "" + }, + onFailure = { error = it.message ?: it.toString() }, + ) + } + } + LaunchedEffect(Unit) { reload() vm.refreshChainSource() @@ -151,6 +176,46 @@ fun WalletScreen( .screenContentInsets() .verticalScroll(rememberScrollState()), ) { + // The blocking seed step. While a seed is on screen this card is the + // whole body: no Cancel, and Done stays dead until the confirm word + // is typed, so the one-shot seed cannot be dismissed unread. The + // app-bar Back still leaves the screen — that is the TUI's `Esc` — + // and it costs nothing now, because leaving no longer loses the + // seed: the wallet stays unbacked (strip up, receive address hidden) + // until "Back up seed" puts this card back on screen. + val seed = pendingSeed + if (seed != null) { + SeedBackupCard( + seed = seed, + typed = seedConfirm, + onTyped = { seedConfirm = it }, + onDone = { + // The gate owns the transition: a wrong word (which the + // disabled Done button already blocks) cannot confirm. + vm.confirmWalletSeed(seedConfirm) + pendingSeed = null + seedConfirm = "" + }, + ) + error?.let { ErrorLine(it) } + return@Column + } + + // The persistent unbacked strip, above everything else on the page. + // `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() }) + } + Card( modifier = Modifier .fillMaxWidth() @@ -173,7 +238,12 @@ fun WalletScreen( style = MaterialTheme.typography.headlineSmall, fontFamily = FontFamily.Monospace, ) - val addr = address + // 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(seedConfirmed, walletReadable) + } if (addr != null) { Spacer(Modifier.height(8.dp)) Text( @@ -333,7 +403,9 @@ fun WalletScreen( onCancel = { setup = null }, onCreated = { seed -> setup = null - backupSeed = seed + // The card is the whole body from here until SEEN. + pendingSeed = seed + seedConfirm = "" scope.launch { reload() } }, onImported = { @@ -374,36 +446,170 @@ fun WalletScreen( modifier = Modifier.weight(1f), ) { Text("Import") } } + // 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 — + // 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() }, + modifier = Modifier + .fillMaxWidth() + .testTag("wallet-seed-backup-action"), + ) { Text("Back up seed") } + } } } - backupSeed?.let { seed -> - Text( - "write this seed down once. anyone with it can spend.", - style = MaterialTheme.typography.labelSmall, - modifier = Modifier.padding(horizontal = 12.dp), - ) - Text( - seed, - style = MaterialTheme.typography.bodySmall, - fontFamily = FontFamily.Monospace, - modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp), - ) - TextButton(onClick = { backupSeed = null }) { Text("I wrote it down") } + // While the strip is up the error belongs next to the button that + // caused it: this page is one long scroll and the daemon card sits + // between the strip and the bottom, so a failed `wallet_seed` read + // would otherwise land off-screen. + if (!unbacked) { + error?.let { ErrorLine(it) } } + } + } +} +/** The one error line every wallet action shares. */ +@Composable +private fun ErrorLine(text: String) { + Text( + text, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.error, + modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp), + ) +} + +/** + * The persistent unbacked strip: up at the top of the wallet screen from the + * moment a create succeeds until the seed is confirmed written down, and again + * on any install whose wallet predates the flag. + * + * It is not a toast and not a dialog — leaving the screen does not dismiss it, + * because the state it reports is the wallet's, not the screen's. Its button is + * the recovery path for a seed that was created before the user knew to look. + */ +@Composable +private fun UnbackedSeedCard( + error: String?, + onBackUpSeed: () -> Unit, +) { + Card( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp, vertical = 8.dp) + .testTag("wallet-seed-warning"), + colors = CardDefaults.cardColors( + containerColor = MaterialTheme.colorScheme.errorContainer, + ), + ) { + Column(Modifier.padding(12.dp)) { + Text( + SeedBackupGate.WARNING, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onErrorContainer, + ) + Spacer(Modifier.height(8.dp)) + OutlinedButton( + onClick = onBackUpSeed, + modifier = Modifier + .fillMaxWidth() + .testTag("wallet-seed-backup-action"), + ) { Text("Back up seed") } error?.let { + Spacer(Modifier.height(6.dp)) Text( it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error, - modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp), ) } } } } +/** + * The blocking seed step: the 25 words, and a Done that is dead until the + * confirm word is typed. + * + * There is no Cancel and no copy button. No Cancel, because a seed this card is + * showing for the first time has never been written down anywhere else, so + * dismissing it unread is how a wallet becomes unrecoverable. No copy button, + * because the Android clipboard is readable by every other app on the device + * (and by the clipboard history on Android 13+), which would hand the spend key + * to whoever asks for it next. + * + * The seed is a Kotlin `String`, so it cannot be wiped in place the way the + * TUI's `Zeroizing` is; dropping the reference is the strongest thing + * available here. That is another reason it is never written to disk. + */ +@Composable +private fun SeedBackupCard( + seed: String, + typed: String, + onTyped: (String) -> Unit, + onDone: () -> Unit, +) { + Card( + modifier = Modifier + .fillMaxWidth() + .padding(12.dp) + .testTag("wallet-seed-card"), + colors = CardDefaults.cardColors( + containerColor = MaterialTheme.colorScheme.surfaceVariant, + ), + ) { + Column(Modifier.padding(12.dp)) { + Text("Back up this seed", style = MaterialTheme.typography.titleSmall) + Spacer(Modifier.height(8.dp)) + Text( + "These 25 words are the only way to recover this wallet, and anyone who " + + "reads them can spend it. Write them down now — this is the only copy.", + style = MaterialTheme.typography.bodySmall, + ) + Spacer(Modifier.height(8.dp)) + Text( + seed, + style = MaterialTheme.typography.bodyMedium, + fontFamily = FontFamily.Monospace, + modifier = Modifier + .fillMaxWidth() + .testTag("wallet-seed-words"), + ) + Spacer(Modifier.height(12.dp)) + Text( + "Type ${SeedBackupGate.CONFIRM_WORD} to confirm you have written it down.", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Spacer(Modifier.height(4.dp)) + OutlinedTextField( + value = typed, + onValueChange = onTyped, + singleLine = true, + label = { Text("type ${SeedBackupGate.CONFIRM_WORD}") }, + modifier = Modifier + .fillMaxWidth() + .testTag("wallet-seed-confirm-input"), + ) + Spacer(Modifier.height(8.dp)) + Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { + Button( + onClick = onDone, + enabled = SeedBackupGate.confirmEnabled(typed), + modifier = Modifier.testTag("wallet-seed-confirm-done"), + ) { Text("Done") } + } + } + } +} + @Composable private fun TransferCard(t: TransferRow) { Card( 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 new file mode 100644 index 0000000..9318753 --- /dev/null +++ b/android/app/src/test/java/com/siriusdevops/onionwire/ui/SeedBackupGateTest.kt @@ -0,0 +1,176 @@ +package com.siriusdevops.onionwire.ui + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The seed-backup gate's rules, on the JVM. + * + * These are the decisions the wallet screen renders from: whether the created + * seed card is still blocking, whether the persistent warning is up, and + * whether the receive address is on screen at all. Pure logic, so no + * Robolectric and no Compose test dependency — same shape as + * `WalletChildStatusTextTest` and `OutboxDetailTest`. + */ +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 + fun the_confirm_word_is_the_tui_word() { + // `src/tui.rs` `WalletSetup::CreateSeed` compares against "SEEN". A + // second convention here would be a different gate than the TUI's. + assertEquals("SEEN", SeedBackupGate.CONFIRM_WORD) + } + + @Test + fun a_correct_word_releases_the_block_whatever_its_case_or_padding() { + assertTrue(SeedBackupGate.accepts("SEEN")) + // The TUI compares with `eq_ignore_ascii_case`. + assertTrue(SeedBackupGate.accepts("seen")) + assertTrue(SeedBackupGate.accepts("SeEn")) + // A soft keyboard auto-capitalises and a paste can carry whitespace. + assertTrue(SeedBackupGate.accepts(" seen ")) + } + + @Test + fun a_blank_or_wrong_word_stays_blocked() { + assertFalse(SeedBackupGate.accepts("")) + assertFalse(SeedBackupGate.accepts(" ")) + assertFalse(SeedBackupGate.accepts("SEE")) + // A prefix is not the word: the TUI only fires on the whole match. + assertFalse(SeedBackupGate.accepts("SEENN")) + assertFalse(SeedBackupGate.accepts("seen.")) + // The other word in this flow is the spend-authority one. + assertFalse(SeedBackupGate.accepts("SPEND")) + } + + @Test + fun done_is_dead_until_the_word_is_right() { + assertFalse(SeedBackupGate.confirmEnabled("")) + assertFalse(SeedBackupGate.confirmEnabled("SEENN")) + assertTrue(SeedBackupGate.confirmEnabled("SEEN")) + assertTrue(SeedBackupGate.confirmEnabled("seen")) + } + + // --- the state transitions ---------------------------------------------- + + @Test + fun a_create_starts_unbacked() { + assertFalse(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) + } + + @Test + fun an_import_is_backed_up_without_the_gate() { + // The user pasted the seed to get here, so there is nothing to confirm. + assertTrue(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) + } + + @Test + fun a_successful_confirm_marks_the_wallet_backed_up() { + val created = SeedBackupGate.afterWallet(WalletKeyOrigin.Created) + assertTrue(SeedBackupGate.afterConfirm("SEEN", created)) + } + + @Test + fun a_wrong_word_leaves_an_unbacked_wallet_unbacked() { + assertFalse(SeedBackupGate.afterConfirm("", false)) + assertFalse(SeedBackupGate.afterConfirm("nope", false)) + } + + @Test + fun a_wrong_word_cannot_un_confirm_a_backed_up_wallet() { + // Done is disabled for a wrong word, but the flag must not be a way to + // walk a confirmed wallet back into the hidden-address state. + assertTrue(SeedBackupGate.afterConfirm("", true)) + assertTrue(SeedBackupGate.afterConfirm("SPEND", true)) + } + + @Test + 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)) + assertTrue(first) + assertFalse(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) + } + + // --- what the screen does with the flag --------------------------------- + + @Test + 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, 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, readable)) + + val confirmed = SeedBackupGate.afterConfirm("seen", created) + 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 + fun the_strip_says_why_the_address_is_gone() { + // The hiding is only fair if the strip names it; assert the strip is + // not empty and actually mentions the seed and the receive address. + val text = SeedBackupGate.WARNING + assertTrue(text.contains("seed")) + assertTrue(text.contains("receive address")) + assertTrue(text.contains("recovered")) + } +} -- 2.52.0