Merge branch 'onionwire/t_dc9b74a6-onionwire-wallet-optional-seed-offset-pa' into onionwire/t_0329f5bf-onionwire-wallet-unlocked-balance-restor
Some checks failed
android-ci / android (pull_request) Has been cancelled
ci / test (pull_request) Successful in 6m38s
ci / sdk (pull_request) Successful in 1m35s
ci / fuzz (pull_request) Successful in 5m39s

This commit is contained in:
sirius 2026-09-26 00:56:18 +00:00
commit 097772dd11
4 changed files with 462 additions and 18 deletions

View file

@ -634,13 +634,49 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
.onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Created)) } .onSuccess { setWalletSeedConfirmed(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, optionally mixed with the seed
* the user just pasted it — so the wallet counts as backed up without the * offset passphrase that seed was created with.
* confirm step. *
* A blank (or whitespace) [offset] is the plain import: the words alone
* restore that wallet, and — like today — it counts as backed up the moment
* it succeeds, because the user is holding the seed.
*
* A non-empty offset is **not** an ordinary import. The offset is not the
* wallet-file passphrase and is not sealed, stored or logged anywhere: a
* later "Back up seed" re-reads the *derived* 25 words, which restore the
* same balance only without the offset. So this path deliberately does not
* touch the seed flag. It leaves [walletOffsetPending] set, and the wallet
* screen's blocking card settles both flags when the user types the confirm
* word — see [SeedBackupGate.offsetAck]. A process kill in between is why
* the pending flag lives in prefs rather than in the screen.
*
* [offset] is trimmed here and handed to the core, whose own blank check is
* the same rule: whitespace is an absent offset, never an empty string.
*/ */
suspend fun importWallet(password: String, seed: String): Result<Unit> = suspend fun importWallet(
runCatching { requireWire().importWallet(password, seed) } password: String,
.onSuccess { setWalletSeedConfirmed(SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)) } seed: String,
offset: String = "",
): Result<Unit> {
val trimmed = offset.trim()
val result = if (trimmed.isEmpty()) {
runCatching { requireWire().importWallet(password, seed) }
} else {
// The offset is a UniFFI `String` for the life of this call, the
// same limit `openWalletWithPassphrase` documents. Never logged and
// never put in an error string: the core's failures are about the
// restore, not the offset, and a message built here is one a crash
// report would carry.
runCatching { requireWire().importWalletWithOffset(password, seed, trimmed) }
}
return result.onSuccess {
val pending = SeedBackupGate.offsetPendingAfterImport(trimmed)
setWalletOffsetPending(pending)
if (!pending) {
setWalletSeedConfirmed(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
@ -769,6 +805,47 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
_walletSeedConfirmed.value = confirmed _walletSeedConfirmed.value = confirmed
} }
// --- Seed-offset acknowledgement ---------------------------------------
// An import that used a seed-offset passphrase leaves a second, narrower
// obligation: the offset is not sealed, so the only copy is whatever the
// user wrote down. This flag is that obligation, persisted next to the seed
// one (not in the SDK, not in the store) so a process kill before the
// acknowledgement cannot lose it.
private val _walletOffsetPending = MutableStateFlow(
seedBackupPrefs.getBoolean(OFFSET_PENDING_KEY, false),
)
/**
* True while this install is waiting on the offset acknowledgement card.
* Its own key, deliberately not [SEED_BACKED_UP_KEY]: the two facts have
* different lifetimes — a create clears the seed flag without touching this
* one, and acknowledging an offset marks the seed backed up.
*/
val walletOffsetPending: StateFlow<Boolean> = _walletOffsetPending.asStateFlow()
/**
* One confirm step from the offset card. Like [confirmWalletSeed], the gate
* owns the transition: a wrong word changes neither flag, and no value is
* created for a flag that was never stored.
*/
fun confirmWalletOffset(typed: String) {
val next = SeedBackupGate.offsetAck(
typed,
pending = _walletOffsetPending.value,
confirmed = _walletSeedConfirmed.value,
)
if (next.pending != _walletOffsetPending.value) setWalletOffsetPending(next.pending)
if (next.confirmed != null && next.confirmed != _walletSeedConfirmed.value) {
setWalletSeedConfirmed(next.confirmed)
}
}
private fun setWalletOffsetPending(pending: Boolean) {
seedBackupPrefs.edit().putBoolean(OFFSET_PENDING_KEY, pending).apply()
_walletOffsetPending.value = pending
}
// --- App-owned wallet child (issue #107) ------------------------------- // --- App-owned wallet child (issue #107) -------------------------------
// The app owns `monero-wallet-rpc` instead of asking the user to start one // 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 // and export `ONIONWIRE_WALLET_RPC`. The core's own states are surfaced
@ -1101,3 +1178,13 @@ private const val SEED_BACKUP_PREFS: String = "wallet_seed_backup"
/** Key inside [SEED_BACKUP_PREFS]: has the open wallet's seed been written down? */ /** Key inside [SEED_BACKUP_PREFS]: has the open wallet's seed been written down? */
private const val SEED_BACKED_UP_KEY: String = "backed_up" private const val SEED_BACKED_UP_KEY: String = "backed_up"
/**
* Key inside [SEED_BACKUP_PREFS]: is this install still waiting on the
* acknowledgement that an import's seed-offset passphrase was written down?
*
* A second key, not a second value of [SEED_BACKED_UP_KEY]: the seed flag is
* install-wide (`false` after any create), while this one is about one import's
* offset and is cleared by its own card — see [SeedBackupGate.offsetAck].
*/
private const val OFFSET_PENDING_KEY: String = "offset_pending"

View file

@ -86,6 +86,11 @@ object SeedBackupGate {
* Without that last distinction the strip would fire on every install that * 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 * has configured a chain source but not yet created a wallet, which is
* exactly the state a first-run user is in. * exactly the state a first-run user is in.
*
* This is the **seed** warning only. An unacknowledged seed offset is a
* different fact with different words — see [card] and [OFFSET_WARNING] —
* so folding it in here would put the create-seed text on an import that
* never had a create.
*/ */
fun warningVisible(confirmed: Boolean?, walletReadable: Boolean): Boolean = when (confirmed) { fun warningVisible(confirmed: Boolean?, walletReadable: Boolean): Boolean = when (confirmed) {
true -> false true -> false
@ -98,9 +103,85 @@ object SeedBackupGate {
* seed to worry about. Receiving into an unbacked wallet is the one mistake * 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 * this gate exists to prevent, so the address is hidden rather than merely
* warned about. * warned about.
*
* [offsetPending] counts too: an offset that was never written down is a
* backup this wallet does not have, so the address stays off the screen
* until the user acknowledges it, exactly as for an unwritten seed.
*/ */
fun receiveAddressVisible(confirmed: Boolean?, walletReadable: Boolean): Boolean = fun receiveAddressVisible(
!warningVisible(confirmed, walletReadable) confirmed: Boolean?,
walletReadable: Boolean,
offsetPending: Boolean = false,
): Boolean = card(confirmed, walletReadable, offsetPending) == WalletGateCard.None
/** Which blocking card the wallet screen is under, if any. */
enum class WalletGateCard {
/** No block: balance, address and QR are all on screen. */
None,
/** The create path's unbacked seed: write down the 25 words. */
UnbackedSeed,
/** An import whose offset was not saved: write the offset down. */
OffsetNotSaved,
}
/**
* The one place that decides which card — if any — the wallet screen is
* under. The offset card wins when both are set: it is about the wallet that
* was just opened, and the seed card cannot be shown for an import that was
* never gated.
*/
fun card(
confirmed: Boolean?,
walletReadable: Boolean,
offsetPending: Boolean = false,
): WalletGateCard = when {
offsetPending -> WalletGateCard.OffsetNotSaved
warningVisible(confirmed, walletReadable) -> WalletGateCard.UnbackedSeed
else -> WalletGateCard.None
}
/**
* Whether this install is waiting on an offset acknowledgement after an
* import. A blank (or whitespace) offset is the plain [importWallet] path:
* the words alone restore that wallet, so nothing is pending. Only a real
* offset — one mixed into the seed at restore time, never sealed and never
* stored — is a backup the user still has to make by hand.
*/
fun offsetPendingAfterImport(offset: String): Boolean = offset.trim().isNotEmpty()
/** The two flags after the offset card's one confirm step. */
data class OffsetAck(
/** Is the install still waiting on the offset acknowledgement? */
val pending: Boolean,
/**
* The install's seed flag. `null` means "nothing stored" and is passed
* through untouched — a wrong word must not write a value into prefs
* that was never there.
*/
val confirmed: Boolean?,
)
/**
* The transition the offset card's Done makes.
*
* Only the right [typed] word clears the pending flag and marks the wallet
* backed up — the words were an import, so once the offset is acknowledged
* there is nothing left to confirm. Anything else leaves both flags exactly
* where they were, so this can never un-confirm a wallet.
*
* The two flags are genuinely separate: `pending` is about *this* import's
* offset, `confirmed` is about the wallet file's seed, and a screen that
* collapsed them would either drop the offset card too early or leave the
* receive address hidden forever.
*/
fun offsetAck(typed: String, pending: Boolean, confirmed: Boolean?): OffsetAck =
if (accepts(typed)) {
OffsetAck(pending = false, confirmed = afterWallet(WalletKeyOrigin.Imported))
} else {
OffsetAck(pending = pending, confirmed = confirmed)
}
/** /**
* 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
@ -110,4 +191,15 @@ object SeedBackupGate {
"seed not backed up. write down the 25 words before you receive anything — " + "seed not backed up. write down the 25 words before you receive anything — " +
"a wallet whose seed was never written down cannot be recovered. " + "a wallet whose seed was never written down cannot be recovered. " +
"the receive address stays hidden until you do." "the receive address stays hidden until you do."
/**
* The offset card's words. Deliberately **not** [WARNING]: an offset import
* has words the user already holds, and the thing that was lost is the
* offset, not the seed. The "Back up seed" button would show a *different*
* 25 words — the plain-seed wallet, with no offset — which is the trap this
* card exists to name.
*/
const val OFFSET_WARNING: String =
"this offset was not saved. write it down. a later seed backup is a " +
"different 25 words that open this wallet without the offset."
} }

View file

@ -46,6 +46,7 @@ import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
@ -65,6 +66,18 @@ import uniffi.onionwire_sdk.WalletChildStatus
private enum class PayKind { Invoice, Tip } private enum class PayKind { Invoice, Tip }
private enum class SetupKind { Create, Import } private enum class SetupKind { Create, Import }
/**
* The offset field's helper. Visible, not a placeholder: a placeholder
* disappears the moment the user types, which is exactly when the sentence
* "a wrong offset does not fail — it opens a different wallet" matters most.
*/
private const val OFFSET_HELPER: String =
"Not the wallet file passphrase. Leave blank if this seed has none. " +
"A wrong offset does not fail — it opens a different wallet."
/** The re-type gate's only message. Shown on the card, never sent anywhere. */
private const val OFFSET_MISMATCH: String = "offset passphrases do not match"
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun WalletScreen( fun WalletScreen(
@ -78,6 +91,7 @@ fun WalletScreen(
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 seedConfirmed by vm.walletSeedConfirmed.collectAsStateWithLifecycle()
val offsetPending by vm.walletOffsetPending.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.
@ -99,6 +113,10 @@ fun WalletScreen(
// Never persisted and never logged — it is a spend key. // Never persisted and never logged — it is a spend key.
var pendingSeed by remember { mutableStateOf<String?>(null) } var pendingSeed by remember { mutableStateOf<String?>(null) }
var seedConfirm by remember { mutableStateOf("") } var seedConfirm by remember { mutableStateOf("") }
// The offset card's own confirm field. Like `seedConfirm` it holds the
// confirm *word*, never the offset itself — the offset is dropped the moment
// the import card closes.
var offsetConfirm by remember { mutableStateOf("") }
var daemonText by remember { mutableStateOf("") } var daemonText by remember { mutableStateOf("") }
var daemonError by remember { mutableStateOf<String?>(null) } var daemonError by remember { mutableStateOf<String?>(null) }
@ -222,6 +240,27 @@ fun WalletScreen(
return@Column return@Column
} }
// The offset wall, the twin of the seed card above: an import that
// used a seed-offset passphrase leaves the offset itself on paper or
// nowhere — it is not sealed and never stored — so this is the only
// chance to say so. Keyed off a persisted flag rather than screen
// state, so a kill or a Back before SEEN brings it straight back.
if (offsetPending) {
OffsetNotSavedCard(
typed = offsetConfirm,
onTyped = { offsetConfirm = it },
onDone = {
// The gate owns both flags: only the right word clears
// the pending flag, and only then does the wallet count
// as an import that is backed up.
vm.confirmWalletOffset(offsetConfirm)
offsetConfirm = ""
},
)
error?.let { ErrorLine(it) }
return@Column
}
// 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 // `walletReadable` is the core's own evidence that the child has a
// wallet open: `wallet_balance` returns exactly "off" for a chain // wallet open: `wallet_balance` returns exactly "off" for a chain
@ -232,8 +271,8 @@ fun WalletScreen(
// configured a daemon but not yet created anything is not nagged // configured a daemon but not yet created anything is not nagged
// about a seed they do not have. // about a seed they do not have.
val walletReadable = balance != null && balance != "off" && balance != "err" val walletReadable = balance != null && balance != "off" && balance != "err"
val unbacked = SeedBackupGate.warningVisible(seedConfirmed, walletReadable) val gateCard = SeedBackupGate.card(seedConfirmed, walletReadable, offsetPending)
if (unbacked) { if (gateCard == SeedBackupGate.WalletGateCard.UnbackedSeed) {
UnbackedSeedCard(error = error, onBackUpSeed = { openSeedBackup() }) UnbackedSeedCard(error = error, onBackUpSeed = { openSeedBackup() })
} }
@ -263,7 +302,7 @@ fun WalletScreen(
// 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(seedConfirmed, walletReadable) SeedBackupGate.receiveAddressVisible(seedConfirmed, walletReadable, offsetPending)
} }
if (addr != null) { if (addr != null) {
Spacer(Modifier.height(8.dp)) Spacer(Modifier.height(8.dp))
@ -437,7 +476,7 @@ fun WalletScreen(
}, },
onError = { error = walletFailureText(it) }, onError = { error = walletFailureText(it) },
create = { pw -> vm.createWallet(pw) }, create = { pw -> vm.createWallet(pw) },
importWallet = { pw, seed -> vm.importWallet(pw, seed) }, importWallet = { pw, seed, offset -> vm.importWallet(pw, seed, offset) },
scope = scope, scope = scope,
) )
} else { } else {
@ -491,7 +530,7 @@ fun WalletScreen(
// caused it: this page is one long scroll and the daemon card sits // 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 // between the strip and the bottom, so a failed `wallet_seed` read
// would otherwise land off-screen. // would otherwise land off-screen.
if (!unbacked) { if (gateCard == SeedBackupGate.WalletGateCard.None) {
error?.let { ErrorLine(it) } error?.let { ErrorLine(it) }
} }
} }
@ -638,6 +677,72 @@ internal fun SeedBackupCard(
} }
} }
/**
* The offset wall: up from the moment an import with a seed-offset passphrase
* succeeds until the user confirms the offset is written down.
*
* It is the seed card's twin and it exists for one fact: the offset is not
* sealed, not stored, and not recoverable from this app. The words a later
* "Back up seed" would show are the *derived* mnemonic — a wallet that opens
* without the offset — so the offset on paper is the only copy of this wallet.
*
* No secret is on this card: no offset, no seed, no address. The field takes
* the confirm word, and Done is dead until it is right, exactly like
* [SeedBackupCard]. The app-bar Back still leaves the screen, and that costs
* nothing: the pending flag is persisted, so the card is back on the next visit
* — and after a process kill — until the word is typed.
*/
@Composable
private fun OffsetNotSavedCard(
typed: String,
onTyped: (String) -> Unit,
onDone: () -> Unit,
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(12.dp)
.testTag("wallet-offset-card"),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant,
),
) {
Column(Modifier.padding(12.dp)) {
Text("Write down this offset", style = MaterialTheme.typography.titleSmall)
Spacer(Modifier.height(8.dp))
Text(
SeedBackupGate.OFFSET_WARNING,
style = MaterialTheme.typography.bodySmall,
modifier = Modifier.testTag("wallet-offset-warning"),
)
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-offset-confirm-input"),
)
Spacer(Modifier.height(8.dp))
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
OutlinedButton(
onClick = onDone,
enabled = SeedBackupGate.confirmEnabled(typed),
modifier = Modifier.testTag("wallet-offset-confirm-done"),
) { Text("Done") }
}
}
}
}
@Composable @Composable
private fun TransferCard(t: TransferRow) { private fun TransferCard(t: TransferRow) {
Card( Card(
@ -759,12 +864,18 @@ private fun WalletSetupCard(
onImported: () -> Unit, onImported: () -> Unit,
onError: (String) -> Unit, onError: (String) -> Unit,
create: suspend (String) -> Result<String>, create: suspend (String) -> Result<String>,
importWallet: suspend (String, String) -> Result<Unit>, importWallet: suspend (String, String, String) -> Result<Unit>,
scope: CoroutineScope, scope: CoroutineScope,
) { ) {
var accepted by remember { mutableStateOf(kind != SetupKind.Create) } var accepted by remember { mutableStateOf(kind != SetupKind.Create) }
var password by remember { mutableStateOf("") } var password by remember { mutableStateOf("") }
var seed by remember { mutableStateOf("") } var seed by remember { mutableStateOf("") }
// The seed-offset passphrase. Never logged, never shown: a spend secret the
// same order as the seed. Both fields die with this composable, which is what
// "clear it when the card closes" means here — there is no other reference.
var offset by remember { mutableStateOf("") }
var offsetAgain by remember { mutableStateOf("") }
var offsetError by remember { mutableStateOf<String?>(null) }
val ready = password.isNotEmpty() && (kind == SetupKind.Create || seed.isNotBlank()) val ready = password.isNotEmpty() && (kind == SetupKind.Create || seed.isNotBlank())
Card( Card(
@ -803,6 +914,58 @@ private fun WalletSetupCard(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) )
Spacer(Modifier.height(8.dp)) Spacer(Modifier.height(8.dp))
// A quiet outlined field under the seed, masked like any other
// secret on this card — plus a re-type, because a store-once
// secret with no second entry is a secret one typo away from
// opening a different wallet, and a wrong offset does not fail:
// it silently opens something else.
OutlinedTextField(
value = offset,
onValueChange = {
offset = it
offsetError = null
},
singleLine = true,
label = { Text("offset passphrase (optional)") },
visualTransformation = PasswordVisualTransformation(),
isError = offsetError != null,
supportingText = {
Text(
OFFSET_HELPER,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
modifier = Modifier
.fillMaxWidth()
.testTag("wallet-offset-input"),
)
Spacer(Modifier.height(8.dp))
OutlinedTextField(
value = offsetAgain,
onValueChange = {
offsetAgain = it
offsetError = null
},
singleLine = true,
label = { Text("re-type offset passphrase") },
visualTransformation = PasswordVisualTransformation(),
isError = offsetError != null,
modifier = Modifier
.fillMaxWidth()
.testTag("wallet-offset-retype"),
)
val offsetProblem = offsetError
if (offsetProblem != null) {
Spacer(Modifier.height(4.dp))
Text(
offsetProblem,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.error,
modifier = Modifier.testTag("wallet-offset-error"),
)
}
Spacer(Modifier.height(8.dp))
} }
OutlinedTextField( OutlinedTextField(
value = password, value = password,
@ -822,10 +985,30 @@ private fun WalletSetupCard(
onSuccess = onCreated, onSuccess = onCreated,
onFailure = { onError(it.message ?: it.toString()) }, onFailure = { onError(it.message ?: it.toString()) },
) )
SetupKind.Import -> importWallet(password, seed.trim()).fold( SetupKind.Import -> {
onSuccess = { onImported() }, // The re-type gate runs before the RPC and
onFailure = { onError(it.message ?: it.toString()) }, // never leaves the screen: a mismatched
) // offset is not a failed import, and there
// is nothing here worth dialling for. Both
// fields are cleared so the user types the
// whole offset again rather than repairing
// one of two strings they cannot see.
val typedOffset = offset
if (typedOffset.isNotBlank() && typedOffset != offsetAgain) {
offset = ""
offsetAgain = ""
offsetError = OFFSET_MISMATCH
return@launch
}
offsetError = null
// `typedOffset` is never logged, never put
// in an error string, and dropped with this
// frame — see [WireViewModel.importWallet].
importWallet(password, seed.trim(), typedOffset).fold(
onSuccess = { onImported() },
onFailure = { onError(it.message ?: it.toString()) },
)
}
} }
} }
}, },

View file

@ -2,6 +2,8 @@ package com.siriusdevops.onionwire.ui
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
@ -173,4 +175,84 @@ class SeedBackupGateTest {
assertTrue(text.contains("receive address")) assertTrue(text.contains("receive address"))
assertTrue(text.contains("recovered")) assertTrue(text.contains("recovered"))
} }
// --- a seed offset that was not saved -----------------------------------
@Test
fun only_a_real_offset_leaves_an_acknowledgement_pending() {
// Blank is the plain import: those words alone restore the wallet.
assertFalse(SeedBackupGate.offsetPendingAfterImport(""))
// Whitespace is blank, the same rule the core's `wallet_restore` applies.
assertFalse(SeedBackupGate.offsetPendingAfterImport(" "))
assertTrue(SeedBackupGate.offsetPendingAfterImport("hunter2"))
assertTrue(SeedBackupGate.offsetPendingAfterImport(" hunter2 "))
}
@Test
fun an_unacknowledged_offset_hides_the_receive_address() {
// Same hiding as an unconfirmed seed, for the same reason: receiving
// into a wallet whose only backup is on someone's desk is the mistake
// this gate exists to prevent. It is also set the imported way, so the
// seed flag alone would have shown the address.
val imported = SeedBackupGate.afterWallet(WalletKeyOrigin.Imported)
assertTrue(SeedBackupGate.receiveAddressVisible(imported, readable))
assertFalse(SeedBackupGate.receiveAddressVisible(imported, readable, offsetPending = true))
assertFalse(SeedBackupGate.receiveAddressVisible(false, readable, offsetPending = true))
}
@Test
fun a_pending_offset_is_the_card_the_screen_is_under() {
assertEquals(
SeedBackupGate.WalletGateCard.OffsetNotSaved,
SeedBackupGate.card(confirmed = true, walletReadable = readable, offsetPending = true),
)
assertEquals(
SeedBackupGate.WalletGateCard.UnbackedSeed,
SeedBackupGate.card(confirmed = false, walletReadable = readable),
)
assertEquals(
SeedBackupGate.WalletGateCard.None,
SeedBackupGate.card(confirmed = true, walletReadable = readable),
)
}
@Test
fun the_right_word_clears_the_pending_flag_and_backs_the_import_up() {
val ack = SeedBackupGate.offsetAck("SEEN", pending = true, confirmed = true)
assertFalse(ack.pending)
// An import's words are on paper, so once the offset is acknowledged
// there is nothing else to confirm.
assertEquals(true, ack.confirmed)
// The confirm word is the TUI's own: trimmed and case-insensitive, like
// every other SEEN in this flow.
assertFalse(SeedBackupGate.offsetAck(" seen ", pending = true, confirmed = null).pending)
}
@Test
fun a_wrong_word_changes_neither_flag() {
val wrong = SeedBackupGate.offsetAck("nope", pending = true, confirmed = true)
assertTrue(wrong.pending)
assertEquals(true, wrong.confirmed)
// The one that must never happen: a wrong word walking a confirmed
// wallet back into the hidden-address state.
val unconfirmed = SeedBackupGate.offsetAck("", pending = true, confirmed = false)
assertTrue(unconfirmed.pending)
assertEquals(false, unconfirmed.confirmed)
// `null` means "nothing stored" and stays null: a wrong word must not
// invent a stored value for an install that never created a wallet.
assertNull(SeedBackupGate.offsetAck("", pending = true, confirmed = null).confirmed)
}
@Test
fun the_offset_card_does_not_reuse_the_seed_warnings_words() {
val text = SeedBackupGate.OFFSET_WARNING
assertTrue(text.contains("offset"))
assertTrue(text.contains("different 25 words"))
// The create-seed text is the wrong fact for an offset import, and the
// two are separate constants so neither can drift into the other.
assertNotEquals(SeedBackupGate.WARNING, text)
}
} }