diff --git a/src/tui.rs b/src/tui.rs index da3543d..a210c30 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1377,8 +1377,19 @@ impl SetupSource { } } - /// The value the `chain` row prints. Every case spells itself out — colour - /// is never the only signal. + /// Does the app spawn the wallet child for this source itself? + /// + /// Both local cases are app-owned: `ChainSource::Local` (this box's + /// `monerod`) and `ChainSource::InProcessCuprate` (in this app) bring up a + /// `monero-wallet-rpc` of the app's own, with generated creds on a port it + /// picked, so the `url`/`login` rows describe no endpoint the app dials. + fn is_app_owned(self) -> bool { + matches!(self, Self::Local | Self::InProcess) + } + + /// The chain source's own label. Every case spells itself out — colour is + /// never the only signal. `WalletSetupConfigState::chain_display` adds what + /// this build can actually do with it. fn word(self) -> &'static str { match self { Self::ReceiveOnly => "receive-only · no chain, no spend", @@ -1619,12 +1630,44 @@ impl WalletSetupConfigState { /// Persist the draft through the node's own validators and answer with the /// line the status row shows. Nothing is written by hand here: a rejected /// form leaves the running wallet and the stored chain source untouched. + /// + /// Both halves are attempted even when one is refused — a wallet-RPC refusal + /// 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 cfg = WalletConfig::new(&self.rpc_url, self.rpc_login.as_str(), self.no_spend); - if let Err(e) = node.wallet_save_config(&cfg) { - return format!("not saved · {e}"); + let wallet = self.save_wallet_config(node); + let chain = self.save_chain_source(node); + if wallet.is_ok() && chain.is_ok() { + return "saved · not dialled yet — ctrl-s starts it, ctrl-t tests the endpoint" + .to_string(); } - let chain = match self.source { + format!( + "not saved · {} · {}", + saved_half("wallet config", &wallet), + saved_half("chain source", &chain) + ) + } + + /// The wallet RPC half of a save. + /// + /// An app-owned chain source (see [`SetupSource::is_app_owned`]) brings up + /// its own `monero-wallet-rpc`: the child generates its own creds and port, + /// and `Node::start_wallet_child_with` replaces the stored client after the + /// spawn, so an empty `url` describes an endpoint the app never dials and + /// must not veto the write. `Wallet::from_config` keeps refusing an empty + /// endpoint — that is the externally run RPC path, where it is the truth. + fn save_wallet_config(&self, node: &Node) -> Result<(), String> { + if self.rpc_url.trim().is_empty() && self.source.is_app_owned() { + return Ok(()); + } + let cfg = WalletConfig::new(&self.rpc_url, self.rpc_login.as_str(), self.no_spend); + node.wallet_save_config(&cfg) + } + + /// The chain-source half of a save. `Ok(())` with nothing written is the + /// untouched in-process row: it keeps whatever F6 stored. + fn save_chain_source(&self, node: &Node) -> Result<(), String> { + match self.source { SetupSource::ReceiveOnly => node.set_receive_only(), SetupSource::Local => node.set_local_monerod_at( Path::new(&self.monerod_bin), @@ -1633,18 +1676,12 @@ impl WalletSetupConfigState { ), SetupSource::Remote => match parse_daemon_addr(&self.daemon) { Some((host, port)) => node.set_remote_daemon(&host, port), - None => return "not saved · daemon address must be host:port".to_string(), + None => Err("daemon address must be host:port".to_string()), }, + SetupSource::InProcess if !self.chain_touched => Ok(()), // Untouched: leave whatever held it before, including the in-process // choice F6 used to own, rather than silently rewriting its datadir. - SetupSource::InProcess if !self.chain_touched => Ok(()), SetupSource::InProcess => node.set_in_process_cuprate(self.cuprate_network, self.prune), - }; - match chain { - Ok(()) => { - "saved · not dialled yet — ctrl-s starts it, ctrl-t tests the endpoint".to_string() - } - Err(e) => format!("wallet config saved · chain source: {e}"), } } @@ -1711,6 +1748,15 @@ fn parse_daemon_addr(raw: &str) -> Option<(String, u16)> { Some((host.trim().to_string(), port)) } +/// One half of the F7 `last` line. A success says so; a failure carries the +/// validator's own words, so both halves read the same way whichever failed. +fn saved_half(what: &str, result: &Result<(), String>) -> String { + match result { + Ok(()) => format!("{what} saved"), + Err(e) => format!("{what}: {e}"), + } +} + /// 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)] @@ -7724,6 +7770,118 @@ mod wallet_setup_config_tests { } } + /// The app-owned chain source is the one path that needs no external wallet + /// RPC: `url`/`login` name an endpoint only `monero-wallet-rpc` uses, and on + /// this path the app spawns the child with its own generated creds and port + /// (`Node::start_wallet_child_with` replaces the stored client after the + /// spawn). An empty pair must therefore not veto the save — that refusal is + /// `Wallet::from_config`'s, and it is about an *externally run* endpoint. + #[test] + fn an_app_owned_source_saves_without_a_typed_wallet_rpc() { + let dir = tempfile::tempdir().unwrap(); + let home = dir.path().join("home"); + let (mut app, _rt) = app(&home); + app.handle_key(key(KeyCode::F(7))).expect("F7"); + pick_source(&mut app, SetupSource::InProcess); + focus_row(&mut app, SETUP_ROW_NETWORK); + app.handle_key(key(KeyCode::Char(' '))).expect("space"); + assert_eq!(state_of(&app).cuprate_network, ChainNetwork::Testnet); + + let state = state_of(&app); + assert!( + state.rpc_url.is_empty() && state.rpc_login.is_empty(), + "this test is only meaningful with an empty endpoint: {state:?}" + ); + assert_eq!( + app.node.wallet_child_config().source, + ChainSource::None, + "nothing is stored before the save" + ); + + app.handle_key(key(KeyCode::Enter)).expect("enter"); + + let note = state_of(&app) + .note + .as_deref() + .expect("a status line") + .to_string(); + assert!(note.starts_with("saved"), "the save must land: {note}"); + match &app.node.wallet_child_config().source { + ChainSource::InProcessCuprate { network, .. } => assert_eq!( + *network, + ChainNetwork::Testnet, + "the network pick must reach the stored source" + ), + other => panic!("expected the in-process source to be stored, got {other:?}"), + } + } + + /// One write must not cancel the other: a wallet-config refusal still leaves + /// the chain-source write done, and the `last` line says both. + #[test] + fn a_wallet_rpc_refusal_does_not_cancel_the_chain_source_write() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, _rt) = app(&dir.path().join("home")); + app.handle_key(key(KeyCode::F(7))).expect("F7"); + pick_source(&mut app, SetupSource::Remote); + focus_row(&mut app, SETUP_ROW_DAEMON); + app.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL)) + .expect("ctrl-u"); + type_str(&mut app, "127.0.0.1:18089"); + assert!(state_of(&app).rpc_url.is_empty()); + + app.handle_key(key(KeyCode::Enter)).expect("enter"); + + let note = state_of(&app) + .note + .as_deref() + .expect("a status line") + .to_string(); + assert!( + note.contains("wallet RPC endpoint required"), + "the endpoint refusal must be reported: {note}" + ); + assert!( + note.contains("chain source saved"), + "the chain-source write must be reported, not dropped: {note}" + ); + match &app.node.wallet_child_config().source { + ChainSource::Remote { host, port } => { + assert_eq!(host, "127.0.0.1"); + assert_eq!(*port, 18089); + } + other => panic!("the remote source must still be written, got {other:?}"), + } + } + + /// The other half of the same rule: a *typed* endpoint with no login is a + /// broken wallet config, and saying so is the point. The app-owned source + /// does not excuse a half-typed endpoint. + #[test] + fn a_typed_endpoint_without_a_login_is_still_refused() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, _rt) = app(&dir.path().join("home")); + app.handle_key(key(KeyCode::F(7))).expect("F7"); + type_str(&mut app, URL); + pick_source(&mut app, SetupSource::InProcess); + + app.handle_key(key(KeyCode::Enter)).expect("enter"); + + let note = state_of(&app) + .note + .as_deref() + .expect("a status line") + .to_string(); + assert!( + note.contains("credentials"), + "a typed endpoint with no login must still be refused: {note}" + ); + assert!( + app.node.wallet_config().is_none(), + "a refused endpoint must not be stored" + ); + } + /// F6 is the wallet again: the chain picker and the child's start/stop keys /// moved to F7, so pressing them there changes nothing and the store is not /// touched behind the user's back.