Merge PR #144: tui -- read the wallet list from the store and load the chosen wallet
All checks were successful
ci / test (push) Successful in 5m47s
ci / sdk (push) Successful in 1m24s
android-ci / android (push) Successful in 10m42s
ci / fuzz (push) Successful in 4m52s

# Conflicts:
#	docs/TUI.md
#	src/tui.rs
This commit is contained in:
agent 2026-09-21 12:12:12 -04:00
commit a9117b8991
No known key found for this signature in database
3 changed files with 439 additions and 29 deletions

View file

@ -1263,6 +1263,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.
@ -2002,7 +2033,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;
@ -2014,11 +2080,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>(
@ -2075,7 +2141,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())
}
@ -3216,4 +3282,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"
);
}
}