From a9a589d2d18d43be0fec07817334c94e1d834c78 Mon Sep 17 00:00:00 2001 From: agent Date: Sun, 27 Sep 2026 23:51:59 -0400 Subject: [PATCH] tui: the F7 screen carries the wallet-rpc path it will spawn --- src/tui.rs | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 191 insertions(+), 11 deletions(-) diff --git a/src/tui.rs b/src/tui.rs index 782b107..5f86d54 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; use std::io::{self, IsTerminal, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::{Arc, mpsc}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; @@ -22,7 +22,7 @@ use crate::store::{ Friend, FriendProfile, Message, OutboxEntry, OutboxStatus, Payment, WalletRecord, }; use crate::wallet::{self, ChainNetwork, ChainSource, WalletConfig}; -use crate::wallet_child::DEFAULT_MONEROD_RPC_PORT; +use crate::wallet_child::{self, DEFAULT_MONEROD_RPC_PORT}; const C_ACCENT: Color = Color::Rgb(232, 165, 75); const C_TEXT: Color = Color::Rgb(196, 214, 176); @@ -1404,13 +1404,16 @@ impl SetupSource { const SETUP_ROW_URL: usize = 0; const SETUP_ROW_LOGIN: usize = 1; const SETUP_ROW_NO_SPEND: usize = 2; -const SETUP_ROW_CHAIN: usize = 3; -const SETUP_ROW_DAEMON: usize = 4; -const SETUP_ROW_MONEROD: usize = 5; -const SETUP_ROW_DATADIR: usize = 6; -const SETUP_ROW_PRUNE: usize = 7; -const SETUP_ROW_NETWORK: usize = 8; -const SETUP_ROWS: usize = 9; +/// The `monero-wallet-rpc` the app spawns. Empty means "resolve": the row shows +/// what the app found ([`wallet_rpc_row_value`]), and a typed path wins. +const SETUP_ROW_WALLET_RPC: usize = 3; +const SETUP_ROW_CHAIN: usize = 4; +const SETUP_ROW_DAEMON: usize = 5; +const SETUP_ROW_MONEROD: usize = 6; +const SETUP_ROW_DATADIR: usize = 7; +const SETUP_ROW_PRUNE: usize = 8; +const SETUP_ROW_NETWORK: usize = 9; +const SETUP_ROWS: usize = 10; /// The F7 screen's draft: plain data taken once when the screen opens /// (`Node::wallet_config` plus `Node::wallet_child_config`), never a handle onto @@ -1426,6 +1429,9 @@ pub struct WalletSetupConfigState { rpc_login: Zeroizing, no_spend: bool, source: SetupSource, + /// The `monero-wallet-rpc` path the user typed. Empty means "resolve", in + /// which case the row prints [`wallet_rpc_row_value`]'s answer instead. + wallet_rpc_bin: String, /// `host:port` for the remote daemon. daemon: String, monerod_bin: String, @@ -1464,6 +1470,7 @@ impl std::fmt::Debug for WalletSetupConfigState { ) .field("no_spend", &self.no_spend) .field("source", &self.source) + .field("wallet_rpc_bin", &self.wallet_rpc_bin) .field("daemon", &self.daemon) .field("monerod_bin", &self.monerod_bin) .field("datadir", &self.datadir) @@ -1492,6 +1499,9 @@ impl WalletSetupConfigState { .unwrap_or_else(|| Zeroizing::new(String::new())), no_spend: wallet.as_ref().map(|c| c.no_spend).unwrap_or(false), source: SetupSource::ReceiveOnly, + // The stored path when the user set one; empty means "resolve", and + // the row then prints what the resolver found (or that nothing is). + wallet_rpc_bin: child.wallet_rpc_bin.display().to_string(), daemon: format!("127.0.0.1:{DEFAULT_MONEROD_RPC_PORT}"), monerod_bin: "monerod".to_string(), // The default a save would use if the user never types one; a stored @@ -1567,6 +1577,7 @@ impl WalletSetupConfigState { match self.field { SETUP_ROW_URL => self.rpc_url.push(c), SETUP_ROW_LOGIN => self.rpc_login.push(c), + SETUP_ROW_WALLET_RPC => self.wallet_rpc_bin.push(c), SETUP_ROW_DAEMON => self.daemon.push(c), SETUP_ROW_MONEROD => self.monerod_bin.push(c), SETUP_ROW_DATADIR => self.datadir.push(c), @@ -1578,6 +1589,7 @@ impl WalletSetupConfigState { match self.field { SETUP_ROW_URL => self.rpc_url.pop(), SETUP_ROW_LOGIN => self.rpc_login.pop(), + SETUP_ROW_WALLET_RPC => self.wallet_rpc_bin.pop(), SETUP_ROW_DAEMON => self.daemon.pop(), SETUP_ROW_MONEROD => self.monerod_bin.pop(), SETUP_ROW_DATADIR => self.datadir.pop(), @@ -1591,6 +1603,7 @@ impl WalletSetupConfigState { match self.field { SETUP_ROW_URL => self.rpc_url.clear(), SETUP_ROW_LOGIN => self.rpc_login.zeroize(), + SETUP_ROW_WALLET_RPC => self.wallet_rpc_bin.clear(), SETUP_ROW_DAEMON => self.daemon.clear(), SETUP_ROW_MONEROD => self.monerod_bin.clear(), SETUP_ROW_DATADIR => self.datadir.clear(), @@ -1620,6 +1633,7 @@ impl WalletSetupConfigState { } SETUP_ROW_URL => self.rpc_url.push(' '), SETUP_ROW_LOGIN => self.rpc_login.push(' '), + SETUP_ROW_WALLET_RPC => self.wallet_rpc_bin.push(' '), SETUP_ROW_DAEMON => self.daemon.push(' '), SETUP_ROW_MONEROD => self.monerod_bin.push(' '), SETUP_ROW_DATADIR => self.datadir.push(' '), @@ -1635,19 +1649,43 @@ impl WalletSetupConfigState { /// must not silently drop a chain source the user just picked — and the /// answer names each half, so one line shape covers every outcome. fn save(&mut self, node: &Node) -> String { + let bins = self.save_wallet_rpc_bin(node); let wallet = self.save_wallet_config(node); let chain = self.save_chain_source(node); - if wallet.is_ok() && chain.is_ok() { + if bins.is_ok() && wallet.is_ok() && chain.is_ok() { return "saved · not dialled yet — ctrl-s starts it, ctrl-t tests the endpoint" .to_string(); } format!( - "not saved · {} · {}", + "not saved · {} · {} · {}", + saved_half("wallet-rpc binary", &bins), saved_half("wallet config", &wallet), saved_half("chain source", &chain) ) } + /// The `wallet-rpc` row's half of a save. + /// + /// An empty row is not a value: it means "resolve", and what gets stored is + /// exactly what [`crate::wallet_child::default_wallet_rpc_bin`] answers — + /// the bundled copy beside the executable when there is one, the bare name + /// otherwise, so the field is never left empty for a source that spawns. + /// A typed path is the user's choice and is stored as typed. + fn save_wallet_rpc_bin(&self, node: &Node) -> Result<(), String> { + let typed = self.wallet_rpc_bin.trim(); + let want = if typed.is_empty() { + wallet_child::default_wallet_rpc_bin() + } else { + PathBuf::from(typed) + }; + let mut cfg = node.wallet_child_config(); + if cfg.wallet_rpc_bin == want { + return Ok(()); + } + cfg.wallet_rpc_bin = want; + node.set_wallet_child_config(&cfg) + } + /// The wallet RPC half of a save. /// /// An app-owned chain source (see [`SetupSource::is_app_owned`]) brings up @@ -1709,6 +1747,17 @@ impl WalletSetupConfigState { } } + /// What the `binary` row prints: the user's typed path, else what the app + /// resolved, else the "not found" wording. Read on every draw, so the answer + /// is what is true now — a binary dropped beside the app shows up on the + /// next repaint instead of on the next restart. + fn wallet_rpc_row_value(&self) -> String { + wallet_rpc_row_value( + self.wallet_rpc_bin.as_str(), + wallet_child::resolve_wallet_rpc_bin(Path::new("")).as_deref(), + ) + } + fn prune_display(&self) -> &'static str { if self.prune { "yes · pruned chain store" @@ -1774,6 +1823,21 @@ fn saved_half(what: &str, result: &Result<(), String>) -> String { } } +/// The `binary` row's value: the user's typed path when there is one (their +/// choice wins), else what the app resolved for itself, else the "not found" +/// wording. A bare name is never printed as though it were a path that works — +/// that is the failure this row exists to prevent. +fn wallet_rpc_row_value(typed: &str, resolved: Option<&Path>) -> String { + let typed = typed.trim(); + if !typed.is_empty() { + return typed.to_string(); + } + match resolved { + Some(path) => format!("{} (auto)", path.display()), + None => wallet_child::NO_WALLET_RPC_MESSAGE.to_string(), + } +} + /// A function key the TUI answers before the per-screen `match`; the screen's /// own key handler never sees it. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -4866,6 +4930,16 @@ pub fn wallet_setup_config_lines(width: u16, s: &WalletSetupConfigState) -> Vec< "view-only", s.no_spend_display(), )); + // The binary the app will spawn. Shown as the path that was resolved, or as + // the fact that there is none — never as a bare name that only fails at the + // spawn three seconds later. + out.push(setup_row_line( + w, + s, + SETUP_ROW_WALLET_RPC, + "binary", + &s.wallet_rpc_row_value(), + )); out.push(Line::default()); out.push(menu_section_line(w, "CHAIN SOURCE")); out.push(setup_row_line( @@ -7975,4 +8049,110 @@ mod wallet_setup_config_tests { "F6 must not still advertise the keys it lost: {footer}" ); } + + /// The `wallet-rpc` row's value: what the user typed when they typed + /// something, else the path the app resolved, else the "not found" wording. + /// A bare name is never shown as if it were a path that works. + #[test] + fn the_wallet_rpc_row_shows_a_path_or_says_nothing_is_there() { + let resolved = std::path::Path::new("/opt/onionwire/monero-wallet-rpc"); + assert_eq!( + wallet_rpc_row_value("", None), + crate::wallet_child::NO_WALLET_RPC_MESSAGE, + "nothing found must say so instead of showing a bare name" + ); + let auto = wallet_rpc_row_value("", Some(resolved)); + assert!( + auto.contains("/opt/onionwire/monero-wallet-rpc"), + "the resolved path must be shown: {auto}" + ); + assert!(auto.contains("auto"), "and marked as resolved: {auto}"); + assert_eq!( + wallet_rpc_row_value("/usr/local/bin/monero-wallet-rpc", Some(resolved)), + "/usr/local/bin/monero-wallet-rpc", + "a typed path is the user's choice and wins" + ); + } + + /// The row is in the `Tab` order and in the drawn body: a setting the user + /// cannot reach or see is not a setting. + #[test] + fn the_wallet_rpc_row_is_reachable_and_drawn() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, _rt) = app(&dir.path().join("home")); + app.handle_key(key(KeyCode::F(7))).expect("F7"); + focus_row(&mut app, SETUP_ROW_WALLET_RPC); + assert_eq!(state_of(&app).field, SETUP_ROW_WALLET_RPC); + let drawn = frame_on(&app, 100, 30); + assert!( + drawn.contains("binary"), + "the row must be on screen:\n{drawn}" + ); + // The row is in the `Tab` order and counted: from the last row one `Tab` + // wraps to the first, and the row just above the chain picker is this + // one (the binary belongs to the wallet-RPC section, not the chain one). + focus_row(&mut app, SETUP_ROW_NETWORK); + app.handle_key(key(KeyCode::Tab)).expect("tab"); + assert_eq!( + state_of(&app).field, + SETUP_ROW_URL, + "Tab must wrap from the last row to the first" + ); + focus_row(&mut app, SETUP_ROW_CHAIN); + app.handle_key(key(KeyCode::BackTab)).expect("backtab"); + assert_eq!( + state_of(&app).field, + SETUP_ROW_WALLET_RPC, + "the binary row sits directly above the chain picker" + ); + } + + /// A typed `wallet-rpc` path is the binary the child spawns: it reaches the + /// store through the node's own write path, not by a hand-written record. + #[test] + fn a_typed_wallet_rpc_path_is_stored() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, _rt) = app(&dir.path().join("home")); + let typed = dir.path().join("elsewhere").join("monero-wallet-rpc"); + app.handle_key(key(KeyCode::F(7))).expect("F7"); + focus_row(&mut app, SETUP_ROW_WALLET_RPC); + app.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL)) + .expect("ctrl-u"); + type_str(&mut app, &typed.display().to_string()); + app.handle_key(key(KeyCode::Enter)).expect("enter"); + + assert_eq!( + app.node.wallet_child_config().wallet_rpc_bin, + typed, + "the typed path must be the stored binary" + ); + } + + /// An empty row means "resolve", and what lands in the store is exactly the + /// resolver's answer — the same value `Node`'s own fill uses, so the screen + /// and the spawn can never disagree about which binary this app runs. + #[test] + fn an_empty_wallet_rpc_row_saves_what_the_resolver_answers() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, _rt) = app(&dir.path().join("home")); + app.handle_key(key(KeyCode::F(7))).expect("F7"); + fill_endpoint(&mut app); + pick_source(&mut app, SetupSource::Remote); + focus_row(&mut app, SETUP_ROW_WALLET_RPC); + app.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL)) + .expect("ctrl-u"); + assert!( + state_of(&app).wallet_rpc_bin.is_empty(), + "this test is only meaningful with an empty row" + ); + + app.handle_key(key(KeyCode::Enter)).expect("enter"); + + let stored = app.node.wallet_child_config().wallet_rpc_bin; + assert_eq!(stored, crate::wallet_child::default_wallet_rpc_bin()); + assert!( + !stored.as_os_str().is_empty(), + "the store never holds an empty binary for a source that spawns" + ); + } }