wallet: actually open the wallet file, and remember its passphrase
All checks were successful
ci / test (pull_request) Successful in 6m14s
ci / sdk (pull_request) Successful in 1m44s
android-ci / android (pull_request) Successful in 12m56s
ci / fuzz (pull_request) Successful in 5m1s

`monero-wallet-rpc` starts with no wallet open and keeps one only for the
life of its process, so a restart, a Lock/unlock or a wallet re-point left
a live child answering `-13 No wallet file` to every read while the file
sat on disk. The app now opens the configured catalogue file itself
(`Wallet::open_wallet`, over RPC — never `--wallet-file`/`--password` on
argv, which `ps` exposes) after the child is up, and keeps the passphrase
that does it, sealed with the store data key beside the RPC creds under
its own AAD.

- core: `Wallet::open_wallet`, `WalletConfig::wallet_passphrase`
  (+ `with_wallet_passphrase`, redacted Debug, kept out of the endpoint
  config), `Error::is_unreachable` for the one retryable class (the port
  binds a moment after spawn; a timeout is not a start in progress).
- store: `wallet_config.sealed_passphrase` column (additive, AAD
  `onionwire-store-v1:wallet-passphrase`), read-modify-written so the
  endpoint/login survive; `wallet_save_config` keeps the stored one.
- node: `start_wallet_child` opens the file, `open_wallet_with_passphrase`
  opens a file with a passphrase the user just typed and seals it only
  once a live daemon accepted it, `wallet_status` grows `wallet_open` and
  `needs_passphrase`. A child the RPC refuses is stopped rather than left
  inert; an install with no stored passphrase still starts, and says so.
- tui: one-shot prompt (`WalletSetup::UnlockPass`) on a wallet that needs
  it, reachable from the saved-wallet picker and from `s`.
- sdk: `openWalletWithPassphrase`, the two status fields, README flow.
- docs: WALLET/THREAT_MODEL/README — the passphrase is kept, and the
  consequence (the store passphrase is sufficient to spend) is stated
  rather than implied.

Tests: 4 new node tests drive a real child lifecycle against a loopback
wallet RPC (open on restart, no open without a passphrase, refused
passphrase, sealed-on-success), a `tests/wallet.rs` test asserts the bytes
sent (`open_wallet` + filename + password), and `tests/store.rs` covers
the sealed column and the plaintext->encrypted upgrade with it set.
Mutation-checked: stubbing the two open paths fails all four.
This commit is contained in:
agent 2026-09-22 12:52:08 -04:00
commit ed29131a67
No known key found for this signature in database
12 changed files with 1262 additions and 54 deletions

View file

@ -663,3 +663,83 @@ fn a_wallet_without_a_label_or_locator_stores_nulls_and_rejects_a_blank_filename
.expect_err("an empty filename cannot name a wallet file");
assert!(err.to_string().contains("filename"), "{err}");
}
/// The wallet file's passphrase lives in the same sealed row as the RPC login
/// and survives a re-open — the wallet child is restarted far more often than
/// the store is created, and a wallet nobody can open is the bug this exists
/// for. A wrong store passphrase still must not open the store at all.
#[test]
fn the_wallet_passphrase_round_trips_sealed_and_a_wrong_store_passphrase_does_not_open_it() {
use onionwire::wallet::WalletConfig;
let dir = tempfile::tempdir().expect("tempdir");
let cfg = WalletConfig::new("http://127.0.0.1:18083", "ow:cfg-secret-xyz", false)
.with_wallet_passphrase("walletfile-pass-abc");
{
let store = Store::open_at_with_passphrase(dir.path(), "store-pass").expect("open");
assert!(
store.load_wallet_config().expect("load").is_none(),
"a fresh store remembers no passphrase"
);
store.save_wallet_config(&cfg).expect("save");
}
let store = Store::open_at_with_passphrase(dir.path(), "store-pass").expect("reopen");
let got = store.load_wallet_config().expect("load").expect("stored");
assert_eq!(got.wallet_passphrase.as_str(), "walletfile-pass-abc");
assert_eq!(got.rpc_login.as_str(), "ow:cfg-secret-xyz");
// Sealed on disk, like the login: the passphrase is not in the db in the
// clear, and neither is the login.
for name in ["onionwire.db", "onionwire.db-wal", "onionwire.db-shm"] {
let Ok(bytes) = fs::read(dir.path().join(name)) else {
continue;
};
assert!(
!contains(&bytes, b"walletfile-pass-abc"),
"{name} holds the wallet passphrase in the clear"
);
assert!(
!contains(&bytes, b"cfg-secret-xyz"),
"{name} holds the RPC login in the clear"
);
}
// The wrong store passphrase is still refused outright.
assert!(
Store::open_at_with_passphrase(dir.path(), "not-the-store-pass").is_err(),
"a wrong store passphrase must not open the store"
);
// Clearing the F7 endpoint (an empty rpc_url) must not throw the wallet
// passphrase away: it belongs to the wallet file, not to the endpoint.
let store = Store::open_at_with_passphrase(dir.path(), "store-pass").expect("reopen");
let cleared = WalletConfig::new("", "", false).with_wallet_passphrase(
store
.load_wallet_config()
.expect("load")
.expect("stored")
.wallet_passphrase
.as_str(),
);
store.save_wallet_config(&cleared).expect("save cleared");
let got = store
.load_wallet_config()
.expect("load")
.expect("a remembered passphrase keeps the row");
assert_eq!(got.rpc_url, "");
assert_eq!(got.wallet_passphrase.as_str(), "walletfile-pass-abc");
// And with nothing left to remember, the row goes away.
store
.save_wallet_config(&WalletConfig::new("", "", false))
.expect("save empty");
assert!(
store.load_wallet_config().expect("load").is_none(),
"an empty endpoint and no passphrase is an empty row"
);
}
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
haystack.windows(needle.len()).any(|w| w == needle)
}

View file

@ -1761,3 +1761,75 @@ async fn an_in_process_cuprate_node_answers_the_typed_daemon_client() {
"the unrestricted RPC was still accepting after shutdown"
);
}
/// The call the whole restart story hangs on. Assert the bytes the client
/// **sent**: a mock that answers whatever it is asked stays green for a client
/// that never issued `open_wallet` at all — which is exactly how a wallet came
/// back from a restart answering `-13 No wallet file` while the suite passed.
#[tokio::test]
async fn open_wallet_issues_the_rpc_method_with_the_file_and_passphrase() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let log = Arc::new(Mutex::new(Vec::new()));
tokio::spawn(serve_recording(
listener,
Arc::clone(&log),
Arc::new(|_: &str| rpc_result("{}")),
));
recording_wallet(port)
.open_wallet("onionwire-1", "walletfile-pass")
.await
.expect("open_wallet");
let sent = log.lock().unwrap().join("\n");
assert!(
sent.contains(r#""method":"open_wallet""#),
"the client never asked to open the file: {sent}"
);
assert!(sent.contains(r#""filename":"onionwire-1""#), "{sent}");
assert!(sent.contains(r#""password":"walletfile-pass""#), "{sent}");
}
/// Same refusals as `create_wallet`: an empty file name or an empty passphrase
/// is a local error, before any socket is opened.
#[tokio::test]
async fn open_wallet_refuses_a_blank_filename_or_passphrase_locally() {
let w = Wallet::disabled();
let err = w.open_wallet(" ", "pw").await.expect_err("no filename");
assert!(err.to_string().contains("filename"), "{err}");
let err = w
.open_wallet("onionwire-1", "")
.await
.expect_err("no passphrase");
assert!(err.to_string().contains("passphrase"), "{err}");
}
/// A refused passphrase is the RPC's own message, not a rewritten generic one:
/// the caller has to be able to tell it apart from a daemon that never answered.
#[tokio::test]
async fn open_wallet_surfaces_the_rpcs_own_refusal() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(serve_recording(
listener,
Arc::new(Mutex::new(Vec::new())),
Arc::new(|_: &str| {
r#"{"jsonrpc":"2.0","id":"0","error":{"code":-1,"message":"Failed to open wallet"}}"#
.to_string()
}),
));
let err = recording_wallet(port)
.open_wallet("onionwire-1", "wrong-pass")
.await
.expect_err("the RPC refused");
assert!(
err.to_string().contains("Failed to open wallet"),
"the RPC's own reason is what a caller needs: {err}"
);
assert!(
!err.to_string().contains("wrong-pass"),
"the passphrase must never be echoed back in an error: {err}"
);
}