wallet: optional seed-offset passphrase on import
All checks were successful
ci / test (pull_request) Successful in 6m21s
ci / sdk (pull_request) Successful in 1m28s
android-ci / android (pull_request) Successful in 12m54s
ci / fuzz (pull_request) Successful in 18m31s

Monero's `restore_deterministic_wallet` takes a `seed_offset`: a second
spend secret mixed into the 25 words at restore time, so the same words
with a different offset open a different wallet. It is not the wallet-file
passphrase and must never be sealed, stored, logged or put on an argv.

- `Wallet::restore_wallet` gains `seed_offset: Option<&str>`: None, empty
  or whitespace-only leaves the field out of the JSON entirely; otherwise
  the ends are trimmed and it is sent beside the seed.
- `Node::wallet_restore` passes it through and still seals only the file
  passphrase.
- SDK: `import_wallet` keeps its signature (None) and
  `import_wallet_with_offset` is added (Kotlin `importWalletWithOffset`).
- TUI: a new `ImportOffset` step between the seed and the file passphrase
  (blank Enter skips, a real one is confirmed, a mismatch never dials), and
  `ImportOffsetSeen` after a successful import that used one -- Esc cannot
  leave it, only SEEN, and the offset is not drawn anywhere.
- Docs: WALLET.md, THREAT_MODEL.md, TUI.md, one README sentence.
This commit is contained in:
agent 2026-09-25 00:42:44 -04:00
commit f6a7daf892
No known key found for this signature in database
9 changed files with 623 additions and 34 deletions

View file

@ -1306,7 +1306,29 @@ enum WalletSetup {
CreatePass { entry: PassphraseEntry },
CreateSeed { seed: Zeroizing<String>, typed: String },
ImportSeed { buf: Zeroizing<String> },
ImportPass { seed: Zeroizing<String>, entry: PassphraseEntry },
/// The optional seed offset passphrase, asked between the seed and the
/// wallet-file passphrase. It is a second spend secret mixed into the seed
/// at restore time, so it needs its own two-prompt entry (not the file
/// passphrase's) and is never drawn.
ImportOffset {
seed: Zeroizing<String>,
entry: PassphraseEntry,
},
/// Import, carrying the offset (empty means none) through both file
/// passphrase prompts, including the mismatch path, which zeroizes both
/// secrets.
ImportPass {
seed: Zeroizing<String>,
offset: Zeroizing<String>,
entry: PassphraseEntry,
},
/// A restore that used an offset: the app keeps neither the offset nor a
/// copy of the pasted words, so this one notice is all the user gets. The
/// wallet file already exists; `Esc` does not leave this screen and only
/// `SEEN` opens the wallet.
ImportOffsetSeen {
typed: String,
},
/// The wallet file the child is pointed at is not open because this install
/// has no passphrase for it. Same prompt idiom as the create path: one
/// hidden entry, `Enter` submits, `Esc` cancels, and the buffer is zeroized
@ -2387,11 +2409,26 @@ impl App {
};
if key.code == KeyCode::Esc {
match setup {
WalletSetup::CreatePass { entry }
| WalletSetup::ImportPass { entry, .. }
| WalletSetup::UnlockPass { entry, .. } => {
// The wallet file already exists at this point, and the offset
// is not saved anywhere: Esc must not silently drop the one
// notice that says so. Only `SEEN` leaves this screen.
WalletSetup::ImportOffsetSeen { .. } => return Ok(()),
WalletSetup::CreatePass { entry } | WalletSetup::UnlockPass { entry, .. } => {
entry.close();
}
WalletSetup::ImportPass {
seed,
offset,
entry,
} => {
entry.close();
seed.zeroize();
offset.zeroize();
}
WalletSetup::ImportOffset { seed, entry } => {
entry.close();
seed.zeroize();
}
WalletSetup::CreateSeed { seed, .. } => seed.zeroize(),
WalletSetup::ImportSeed { buf } => buf.zeroize(),
WalletSetup::CreateWarn { .. } => {}
@ -2477,14 +2514,59 @@ impl App {
self.screen = Screen::Main;
return Ok(());
}
self.screen = Screen::WalletSetup(WalletSetup::ImportPass {
self.screen = Screen::WalletSetup(WalletSetup::ImportOffset {
seed,
entry: PassphraseEntry::new(),
});
}
_ => {}
},
WalletSetup::ImportPass { entry, seed } => match key.code {
WalletSetup::ImportOffset { seed, entry } => match key.code {
KeyCode::Backspace => entry.backspace(),
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
entry.push(c);
}
KeyCode::Enter => {
let first = entry.take_first();
let offset = entry.take_buf();
if let Some(first) = first {
if first.as_str() != offset.as_str() {
entry.close();
seed.zeroize();
self.alert = Some("offset passphrases do not match".into());
self.screen = Screen::Main;
return Ok(());
}
entry.close();
let seed = std::mem::replace(seed, Zeroizing::new(String::new()));
self.screen = Screen::WalletSetup(WalletSetup::ImportPass {
seed,
offset,
entry: PassphraseEntry::new(),
});
} else if offset.trim().is_empty() {
// Blank Enter skips the offset: no confirm, no RPC field.
entry.close();
let seed = std::mem::replace(seed, Zeroizing::new(String::new()));
self.screen = Screen::WalletSetup(WalletSetup::ImportPass {
seed,
offset: Zeroizing::new(String::new()),
entry: PassphraseEntry::new(),
});
} else {
self.screen = Screen::WalletSetup(WalletSetup::ImportOffset {
seed: std::mem::replace(seed, Zeroizing::new(String::new())),
entry: PassphraseEntry::with_first(offset.as_str()),
});
}
}
_ => {}
},
WalletSetup::ImportPass {
entry,
seed,
offset,
} => match key.code {
KeyCode::Backspace => entry.backspace(),
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
entry.push(c);
@ -2496,17 +2578,37 @@ impl App {
if first.as_str() != pass.as_str() {
entry.close();
seed.zeroize();
offset.zeroize();
self.alert = Some("passphrases do not match".into());
self.screen = Screen::Main;
return Ok(());
}
let result = self
.rt
.block_on(self.node.wallet_restore(pass.as_str(), seed.as_str()));
// Empty or whitespace-only means "no offset", and the
// core then leaves the field out of the request.
let offset_arg = match offset.trim() {
"" => None,
other => Some(other),
};
let used_offset = offset_arg.is_some();
let result = self.rt.block_on(self.node.wallet_restore(
pass.as_str(),
seed.as_str(),
offset_arg,
));
seed.zeroize();
offset.zeroize();
match result {
Ok(_) => {
self.status_note = Some("wallet imported".into());
if used_offset {
// The offset is not saved anywhere: hold on
// the notice until the user types SEEN.
self.screen =
Screen::WalletSetup(WalletSetup::ImportOffsetSeen {
typed: String::new(),
});
return Ok(());
}
return self.open_wallet();
}
Err(e) => {
@ -2516,14 +2618,28 @@ impl App {
}
} else {
let seed = std::mem::replace(seed, Zeroizing::new(String::new()));
let offset = std::mem::replace(offset, Zeroizing::new(String::new()));
self.screen = Screen::WalletSetup(WalletSetup::ImportPass {
seed,
offset,
entry: PassphraseEntry::with_first(pass.as_str()),
});
}
}
_ => {}
},
WalletSetup::ImportOffsetSeen { typed } => match key.code {
KeyCode::Char(c) if c.is_ascii_alphabetic() => {
typed.push(c);
if typed.eq_ignore_ascii_case("SEEN") {
return self.open_wallet();
}
}
KeyCode::Backspace => {
typed.pop();
}
_ => {}
},
WalletSetup::UnlockPass { filename, entry } => match key.code {
KeyCode::Backspace => entry.backspace(),
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
@ -3401,13 +3517,18 @@ match screen {
"c create i import r qr w wallets s/x child 1 pruned 2 full 3 testnet Esc back ? help"
}
Screen::WalletSetup(WalletSetup::CreateWarn { .. }) => "type SPEND Esc cancel ? help",
Screen::WalletSetup(
WalletSetup::CreatePass { .. }
| WalletSetup::ImportPass { .. }
| WalletSetup::UnlockPass { .. },
) => {
Screen::WalletSetup(WalletSetup::CreatePass { .. } | WalletSetup::UnlockPass { .. }) => {
"Enter submit Esc cancel ? help"
}
Screen::WalletSetup(WalletSetup::ImportOffset { entry, .. }) => {
if entry.first().is_some() {
"Enter submit Esc cancel ? help"
} else {
"blank Enter skips Esc cancel ? help"
}
}
Screen::WalletSetup(WalletSetup::ImportPass { .. }) => "Enter submit Esc cancel ? help",
Screen::WalletSetup(WalletSetup::ImportOffsetSeen { .. }) => "type SEEN ? help",
Screen::WalletSetup(WalletSetup::CreateSeed { .. }) => "type SEEN Esc cancel ? help",
Screen::WalletSetup(WalletSetup::ImportSeed { .. }) => "paste seed Enter Esc ? help",
Screen::WalletsSettings { .. } => wallets_settings_footer_hint(),
@ -5301,6 +5422,23 @@ fn draw_wallet_setup(f: &mut Frame, setup: &WalletSetup) {
"passphrase for the imported wallet file\n\nEnter continues Esc cancels".into()
},
),
WalletSetup::ImportOffset { entry, .. } => (
"seed offset passphrase",
if entry.first().is_some() {
"re-type offset passphrase\n\nEnter submits Esc cancels".into()
} else {
"optional. not the wallet file passphrase. blank Enter skips. a wrong offset \
opens a different wallet and does not error.\n\nEnter continues Esc cancels"
.into()
},
),
WalletSetup::ImportOffsetSeen { typed } => (
"offset not saved",
format!(
"this offset was not saved. write it down. a later seed backup is a different \
25 words that open this wallet without the offset. type SEEN\n{typed}"
),
),
WalletSetup::UnlockPass { filename, .. } => (
"wallet passphrase",
format!(
@ -5657,3 +5795,219 @@ mod wallet_store_tests {
assert_eq!(app.status_note.as_deref(), Some("opened wallet fresh"));
}
}
/// The optional seed-offset step sits between the pasted seed and the wallet-file
/// passphrase, and the notice it ends on is the only copy of a secret the app
/// deliberately does not keep.
#[cfg(test)]
mod wallet_offset_tests {
use super::*;
use crate::node::test_node_with_wallets;
/// The wallet is off in this harness (`test_node_with_wallets` uses
/// `Wallet::disabled()`), so the RPC step is not reachable here: these tests
/// pin the screen transitions, the locked footer strings, and the fact that a
/// mismatch never reaches the node. The request bytes are `tests/wallet.rs`.
fn app(home: &std::path::Path) -> (App, tokio::runtime::Runtime) {
let rt = tokio::runtime::Runtime::new().expect("tokio runtime");
let _guard = rt.enter();
let node = test_node_with_wallets(home, &[]);
let app = App::new(node, rt.handle().clone()).expect("app");
(app, rt)
}
fn key(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
}
fn type_str(app: &mut App, s: &str) {
for c in s.chars() {
app.handle_key(key(KeyCode::Char(c))).expect("typed key");
}
}
fn offset_screen(seed: &str) -> Screen {
Screen::WalletSetup(WalletSetup::ImportOffset {
seed: Zeroizing::new(seed.to_string()),
entry: PassphraseEntry::new(),
})
}
/// The seed step is untouched and leads to the offset, whose first prompt
/// advertises exactly the blank-Enter skip.
#[test]
fn the_seed_step_leads_to_the_offset_step() {
let dir = tempfile::tempdir().unwrap();
let (mut app, _rt) = app(&dir.path().join("home"));
app.screen = Screen::WalletSetup(WalletSetup::ImportSeed {
buf: Zeroizing::new("lumber tacit lemon ivory risk".into()),
});
assert_eq!(footer_hints(&app.screen), "paste seed Enter Esc ? help");
app.handle_key(key(KeyCode::Enter)).expect("enter");
assert!(
matches!(
app.screen,
Screen::WalletSetup(WalletSetup::ImportOffset { .. })
),
"the seed goes to the offset prompt, not straight to the passphrase"
);
assert_eq!(
footer_hints(&app.screen),
"blank Enter skips Esc cancel ? help"
);
}
/// A blank (or whitespace-only) offset is skipped without a confirm, and the
/// passphrase step carries an empty offset — which means "no field" later.
#[test]
fn a_blank_offset_enter_skips_the_confirm() {
for blank in ["", " "] {
let dir = tempfile::tempdir().unwrap();
let (mut app, _rt) = app(&dir.path().join("home"));
app.screen = offset_screen("lumber tacit lemon ivory risk");
type_str(&mut app, blank);
app.handle_key(key(KeyCode::Enter)).expect("enter");
let Screen::WalletSetup(WalletSetup::ImportPass { offset, entry, .. }) = &app.screen
else {
panic!("a blank offset must go straight to the file passphrase");
};
assert!(offset.is_empty(), "blank {blank:?} became {offset:?}");
assert!(
entry.first().is_none(),
"a blank offset must not be confirmed"
);
}
}
/// A real offset is asked twice; the second prompt offers submit and Esc.
#[test]
fn a_non_empty_offset_is_confirmed() {
let dir = tempfile::tempdir().unwrap();
let (mut app, _rt) = app(&dir.path().join("home"));
app.screen = offset_screen("lumber tacit lemon ivory risk");
type_str(&mut app, "harbor-offset");
app.handle_key(key(KeyCode::Enter)).expect("enter");
let Screen::WalletSetup(WalletSetup::ImportOffset { entry, .. }) = &app.screen else {
panic!("a non-empty offset must be confirmed");
};
assert_eq!(entry.first(), Some("harbor-offset"));
assert_eq!(
footer_hints(&app.screen),
"Enter submit Esc cancel ? help"
);
}
/// A mismatch returns to the main view with the locked alert and never asks
/// the node for anything.
#[test]
fn a_mismatched_offset_never_reaches_the_node() {
let dir = tempfile::tempdir().unwrap();
let (mut app, _rt) = app(&dir.path().join("home"));
app.screen = offset_screen("lumber tacit lemon ivory risk");
type_str(&mut app, "harbor-offset");
app.handle_key(key(KeyCode::Enter)).expect("enter");
type_str(&mut app, "harbor-offse");
app.handle_key(key(KeyCode::Enter)).expect("enter");
assert!(matches!(app.screen, Screen::Main));
assert_eq!(
app.alert.as_deref(),
Some("offset passphrases do not match")
);
}
/// The offset prompt does not echo what was typed, the same way the file
/// passphrase does not.
#[test]
fn the_offset_is_not_drawn() {
let mut entry = PassphraseEntry::new();
for c in "harbor-offset".chars() {
entry.push(c);
}
let setup = WalletSetup::ImportOffset {
seed: Zeroizing::new("lumber tacit lemon ivory risk".to_string()),
entry,
};
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24))
.expect("test terminal");
term.draw(|f| draw_wallet_setup(f, &setup)).expect("draw");
let frame = term.backend().buffer().clone();
let mut drawn = String::new();
for y in 0..frame.area.height {
for x in 0..frame.area.width {
drawn.push_str(frame[(x, y)].symbol());
}
}
assert!(
!drawn.contains("harbor-offset"),
"the offset must never be drawn: {drawn}"
);
assert!(
drawn.contains("seed offset passphrase"),
"the prompt itself is shown: {drawn}"
);
}
/// The notice is the one thing Esc cannot dismiss: the wallet file exists,
/// and neither the offset nor the pasted words are stored anywhere.
#[test]
fn esc_does_not_leave_the_offset_notice() {
let dir = tempfile::tempdir().unwrap();
let (mut app, _rt) = app(&dir.path().join("home"));
app.screen = Screen::WalletSetup(WalletSetup::ImportOffsetSeen {
typed: String::new(),
});
assert_eq!(footer_hints(&app.screen), "type SEEN ? help");
app.handle_key(key(KeyCode::Esc)).expect("esc");
assert!(
matches!(
app.screen,
Screen::WalletSetup(WalletSetup::ImportOffsetSeen { .. })
),
"Esc must not leave the offset notice"
);
}
/// Only `SEEN` (case-insensitively, like the create path) moves on, and it
/// moves on by opening the wallet.
#[test]
fn only_seen_leaves_the_offset_notice_for_the_wallet() {
let dir = tempfile::tempdir().unwrap();
let (mut app, _rt) = app(&dir.path().join("home"));
app.screen = Screen::WalletSetup(WalletSetup::ImportOffsetSeen {
typed: String::new(),
});
type_str(&mut app, "see");
assert!(
matches!(
app.screen,
Screen::WalletSetup(WalletSetup::ImportOffsetSeen { .. })
),
"a prefix is not the word"
);
// The fourth letter is the one that proceeds: it opens the wallet, and
// this install has none, so the open is what fails. Nothing before the
// full word gets that far.
assert!(
app.status_note.is_none(),
"a prefix must not run any import side effect"
);
let err = app
.handle_key(key(KeyCode::Char('n')))
.expect_err("SEEN must attempt to open the wallet");
assert!(
err.contains("receive address") || err.to_ascii_lowercase().contains("not configured"),
"expected the open to fail on the missing wallet, got {err}"
);
}
}