tui: read the wallet list from the store and load the chosen wallet
The selection screen shipped as presentation only: `F6 w` opened its unavailable state, the rows were never read, and Enter on a wallet said switching was not wired up. The catalogue, the create path and the screen all existed; nothing joined them. - `App::open_wallet_select` builds its rows from `Node::list_wallets` at the moment the screen opens (never cached from startup), and a failed read is the screen's own error state rather than "no saved wallets". - `App::choose_wallet` resolves `WalletChoice::Open(index)` against the catalogue as it is now — the index is a position, and two saved wallets may share an identity key — then loads it. `+ create new wallet` still starts the existing create flow. - `Node::open_wallet(filename)` is the load: it refuses a file the catalogue does not hold, re-points the sealed wallet-child record with a read-modify-write (the generated RPC creds survive), and restarts a running child onto the new file. An idle child takes the change on its next start. - `App::new` starts on the selection screen when the catalogue is non-empty and on the create flow when it is empty, so a restart lands on the wallets it remembers and a first run has no dead empty list. - The stored identity/locator stay catalogue columns: a locator rotates (F4) and identity is the roster key the node already holds, so the live header keeps reading them from the node. - `SELECT_UNAVAILABLE` is gone with the state it described; docs/TUI.md now describes the wired behaviour. Tests (both new): `Node::open_wallet` over an offline node — the chosen file, the creds kept across the read-modify-write, the choice surviving a reopen, and a non-catalogue file refused; and the app path in src/tui.rs over a real store-backed node — start screen both ways, catalogue mapping, row index over a shared identity, the create row, and a wallet recorded after startup appearing the next time the list opens.
This commit is contained in:
parent
d96de1d7f0
commit
8986724848
3 changed files with 439 additions and 29 deletions
166
src/node.rs
166
src/node.rs
|
|
@ -1251,6 +1251,37 @@ impl Node {
|
|||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Switch the wallet child to a catalogue wallet's file.
|
||||
///
|
||||
/// The catalogue is the only directory this app has, so a file it does not
|
||||
/// hold is refused instead of pointing a spend-capable child at a path. A
|
||||
/// running child is restarted onto the new file; an idle one is only
|
||||
/// re-pointed, and the change applies on the next `start_wallet_child`. The
|
||||
/// write is a read-modify-write, so the generated RPC creds survive.
|
||||
pub fn open_wallet(&self, filename: &str) -> Result<(), String> {
|
||||
if !self
|
||||
.list_wallets()?
|
||||
.iter()
|
||||
.any(|w| w.wallet_filename == filename)
|
||||
{
|
||||
return Err(format!("no saved wallet in {filename}"));
|
||||
}
|
||||
let running = self
|
||||
.wallet_child
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.as_ref()
|
||||
.is_some_and(|child| child.wallet_rpc_running());
|
||||
if running {
|
||||
self.stop_wallet_child()?;
|
||||
}
|
||||
self.point_child_at(filename)?;
|
||||
if running {
|
||||
self.start_wallet_child()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The file name a wallet created now should live in. `wallet_filename` is
|
||||
/// UNIQUE and the catalogue is the only directory the app has, so this is
|
||||
/// `<default>-<n>` for the first `n` the catalogue does not already hold.
|
||||
|
|
@ -1990,7 +2021,42 @@ pub fn dir_contains_bytes(dir: &Path, needle: &[u8]) -> bool {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// A catalogue row written straight into the store, for tests that need a saved
|
||||
/// wallet without a wallet RPC.
|
||||
pub(crate) fn test_record_wallet(store: &Store, file: &str, label: &str) {
|
||||
let identity_pubkey: [u8; 32] = store
|
||||
.self_identity()
|
||||
.expect("self identity")
|
||||
.identity_pk
|
||||
.try_into()
|
||||
.expect("32-byte identity");
|
||||
store
|
||||
.create_wallet(&NewWallet {
|
||||
name: (!label.trim().is_empty()).then(|| label.to_string()),
|
||||
identity_pubkey,
|
||||
onion_locator: format!("{file}.onion"),
|
||||
wallet_filename: file.to_string(),
|
||||
})
|
||||
.expect("create wallet");
|
||||
}
|
||||
|
||||
/// A real `Node` with no Tor and a seeded wallet catalogue, for the TUI's own
|
||||
/// tests: `App` reads the catalogue, and the only public `Node` constructor
|
||||
/// bootstraps Tor. `wallets` is `(file name, label)` — a blank label is stored
|
||||
/// as `NULL`, like a wallet created without one.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn test_node_with_wallets(home: &Path, wallets: &[(&str, &str)]) -> Arc<Node> {
|
||||
{
|
||||
let store = Store::open_at_with_passphrase(home, tests::PASS).expect("open store");
|
||||
for (file, label) in wallets {
|
||||
test_record_wallet(&store, file, label);
|
||||
}
|
||||
}
|
||||
Arc::new(tests::offline_node(home, Arc::new(tests::NoDial)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::wallet_child::DEFAULT_MONEROD_RPC_PORT;
|
||||
use std::pin::Pin;
|
||||
|
|
@ -2002,11 +2068,11 @@ mod tests {
|
|||
use tokio::io::AsyncRead as TokioRead;
|
||||
use tokio::io::AsyncWrite as TokioWrite;
|
||||
|
||||
const PASS: &str = "node-ratchet-test-pass";
|
||||
pub(crate) const PASS: &str = "node-ratchet-test-pass";
|
||||
|
||||
/// Never dials: every send queues, which is what leaves the encoded frame
|
||||
/// visible on the outbox row.
|
||||
struct NoDial;
|
||||
pub(crate) struct NoDial;
|
||||
|
||||
impl crate::outbox::Deliverer for NoDial {
|
||||
fn deliver<'a>(
|
||||
|
|
@ -2063,7 +2129,7 @@ mod tests {
|
|||
/// are the only public constructors and both bootstrap Tor and publish an
|
||||
/// onion service, so this is the only way to drive the shipped send and
|
||||
/// receive path offline.
|
||||
fn offline_node(home: &Path, deliverer: Arc<dyn crate::outbox::Deliverer>) -> Node {
|
||||
pub(crate) fn offline_node(home: &Path, deliverer: Arc<dyn crate::outbox::Deliverer>) -> Node {
|
||||
offline_node_with_wallet(home, deliverer, Wallet::disabled())
|
||||
}
|
||||
|
||||
|
|
@ -3204,4 +3270,96 @@ mod tests {
|
|||
.expect_err("no wallet RPC");
|
||||
assert!(err.contains("ONIONWIRE_WALLET_RPC"), "{err}");
|
||||
}
|
||||
|
||||
/// Switching wallets is a catalogue read plus a child re-point: the chosen
|
||||
/// row's file name is what the child opens, and the generated RPC creds in
|
||||
/// the same sealed record survive the read-modify-write.
|
||||
#[tokio::test]
|
||||
async fn open_wallet_points_the_child_at_the_chosen_file_and_keeps_the_creds() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let home = dir.path().join("bob");
|
||||
let node = offline_node(&home, Arc::new(NoDial));
|
||||
{
|
||||
let store = node.store.lock().unwrap();
|
||||
store
|
||||
.save_wallet_child(
|
||||
r#"{"user":"owuser","pass":"ow-secret-pass-xyz","wallet_filename":"onionwire"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
for file in ["onionwire", "onionwire-1"] {
|
||||
store
|
||||
.create_wallet(&NewWallet {
|
||||
name: None,
|
||||
identity_pubkey: [7u8; 32],
|
||||
onion_locator: "abcd.onion".into(),
|
||||
wallet_filename: file.to_string(),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
node.open_wallet("onionwire-1")
|
||||
.expect("open a saved wallet");
|
||||
assert_eq!(
|
||||
node.wallet_child_config().wallet_filename,
|
||||
"onionwire-1",
|
||||
"the child must open the wallet that was chosen"
|
||||
);
|
||||
let sealed = node
|
||||
.store
|
||||
.lock()
|
||||
.unwrap()
|
||||
.load_wallet_child()
|
||||
.unwrap()
|
||||
.expect("sealed record");
|
||||
assert!(
|
||||
sealed.contains("ow-secret-pass-xyz"),
|
||||
"the re-point dropped the generated creds: {sealed}"
|
||||
);
|
||||
assert!(
|
||||
sealed.contains("\"user\":\"owuser\""),
|
||||
"the re-point dropped the generated creds: {sealed}"
|
||||
);
|
||||
|
||||
// A restart re-reads the record, so the choice is remembered.
|
||||
drop(node);
|
||||
let restarted = offline_node(&home, Arc::new(NoDial));
|
||||
assert_eq!(
|
||||
restarted.wallet_child_config().wallet_filename,
|
||||
"onionwire-1",
|
||||
"the chosen wallet must survive a restart"
|
||||
);
|
||||
}
|
||||
|
||||
/// The catalogue is the only directory the app has, so a file it does not
|
||||
/// hold is refused instead of pointing a spend-capable child at a path.
|
||||
#[tokio::test]
|
||||
async fn open_wallet_refuses_a_file_the_catalogue_does_not_hold() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let node = offline_node(&dir.path().join("bob"), Arc::new(NoDial));
|
||||
{
|
||||
let store = node.store.lock().unwrap();
|
||||
store
|
||||
.save_wallet_child(r#"{"user":"owuser","pass":"p","wallet_filename":"onionwire"}"#)
|
||||
.unwrap();
|
||||
store
|
||||
.create_wallet(&NewWallet {
|
||||
name: None,
|
||||
identity_pubkey: [7u8; 32],
|
||||
onion_locator: String::new(),
|
||||
wallet_filename: "onionwire".to_string(),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let err = node
|
||||
.open_wallet("onionwire-9")
|
||||
.expect_err("not a saved wallet");
|
||||
assert!(err.contains("no saved wallet"), "{err}");
|
||||
assert_eq!(
|
||||
node.wallet_child_config().wallet_filename,
|
||||
"onionwire",
|
||||
"a refused switch must leave the child where it was"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue