wallet: the file-name picker consults the disk, not only the catalogue
Some checks failed
ci / test (pull_request) Failing after 4m25s
ci / sdk (pull_request) Successful in 1m29s
ci / fuzz (pull_request) Successful in 4m57s
android-ci / android (pull_request) Successful in 12m6s

next_wallet_filename returned <default>-<n> for the first n the SQLCipher
catalogue did not hold, and never looked at the wallet directory. monero
writes two files per wallet (<name> and <name>.keys) and refuses a create
that would collide with either, so a file whose catalogue row is gone -
Store::delete_wallet keeps the file, and a create whose record_wallet
failed leaves one - meant every attempt proposed the same taken name and
the daemon answered -21 Cannot create wallet. Already exists. for ever.

Add wallet_name_taken (src/wallet_child.rs): it lists the wallet dir and
answers whether <name> or <name>.keys is already there. next_wallet_filename
now needs the candidate free in both places, and Node::wallet_dir() is the
single source of $ONIONWIRE_HOME/wallet, used by the child spawn too.

Fail closed: an unreadable (or missing) wallet dir is an error, not a
guess. Reads only - nothing here deletes a file the catalogue does not
hold, which may be the only copy of a funded wallet.
This commit is contained in:
rust-dev 2026-09-22 13:41:35 -04:00
commit 59175f6931
No known key found for this signature in database
3 changed files with 196 additions and 9 deletions

View file

@ -9,6 +9,7 @@ use onionwire::wallet::{
self, NOT_CONFIGURED_MSG, NOT_CONNECTED_MSG, Readiness, TransferRow, VIEW_ONLY_MSG, Wallet,
WalletConfig, WalletStatus, readiness,
};
use onionwire::wallet_child;
use onionwire::{PaymentWrite, Store};
use rand::rngs::OsRng;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
@ -1833,3 +1834,61 @@ async fn open_wallet_surfaces_the_rpcs_own_refusal() {
"the passphrase must never be echoed back in an error: {err}"
);
}
// ---------------------------------------------------------------------------
// `next_wallet_filename`'s disk half: monero writes two files per wallet and
// the daemon refuses a create that would collide with either, so the picker
// has to look at the directory, not only at the catalogue.
// ---------------------------------------------------------------------------
/// `<name>` and `<name>.keys` both mean taken; nothing else does.
#[test]
fn wallet_name_taken_sees_the_file_and_its_keys_sibling() {
let dir = tempfile::tempdir().expect("tempdir");
let wallet_dir = dir.path();
std::fs::write(wallet_dir.join("onionwire-1.keys"), b"keys").unwrap();
std::fs::write(wallet_dir.join("onionwire-2"), b"wallet").unwrap();
assert!(
wallet_child::wallet_name_taken(wallet_dir, "onionwire-1").unwrap(),
"the `.keys` sibling is enough to make the name taken"
);
assert!(
wallet_child::wallet_name_taken(wallet_dir, "onionwire-2").unwrap(),
"the wallet file itself is enough"
);
assert!(!wallet_child::wallet_name_taken(wallet_dir, "onionwire-3").unwrap());
assert!(
!wallet_child::wallet_name_taken(wallet_dir, "onionwire").unwrap(),
"a prefix is not a collision"
);
assert!(
!wallet_child::wallet_name_taken(wallet_dir, "onionwire-20").unwrap(),
"neither is a longer name that starts with a taken one"
);
// The files may be the only copy of a funded wallet: naming the collision
// must not remove it.
assert!(wallet_dir.join("onionwire-1.keys").is_file());
assert!(wallet_dir.join("onionwire-2").is_file());
}
/// Fail closed: a directory that cannot be listed is an error, not "every name
/// is free". Guessing here is how the collision comes back.
#[test]
fn wallet_name_taken_fails_closed_when_the_dir_cannot_be_read() {
let dir = tempfile::tempdir().expect("tempdir");
let missing = dir.path().join("no-such-wallet-dir");
let err = wallet_child::wallet_name_taken(&missing, "onionwire-1")
.expect_err("a missing wallet dir must refuse, not answer 'free'")
.to_string();
assert!(
err.contains("wallet dir"),
"the error must name what could not be listed: {err}"
);
// A path that is a file, not a directory, is unreadable too.
let file = dir.path().join("not-a-dir");
std::fs::write(&file, b"x").unwrap();
assert!(wallet_child::wallet_name_taken(&file, "onionwire-1").is_err());
}