diff --git a/src/util/secure_vault.cpp b/src/util/secure_vault.cpp index c40d29b..1f40a5a 100644 --- a/src/util/secure_vault.cpp +++ b/src/util/secure_vault.cpp @@ -48,7 +48,11 @@ void SecureVault::setWalletScope(const std::string& walletFile) { // any other wallet gets a per-wallet tag. A readable sanitized prefix PLUS an 8-hex FNV-1a of the // RAW filename — the hash disambiguates so two distinct files can never share a vault even if their // sanitized forms collide (e.g. "wallet foo.dat" vs "wallet_foo.dat"). - if (walletFile.empty() || walletFile == "wallet.dat") { scope_.clear(); return; } + if (walletFile.empty() || walletFile == "wallet.dat") { + std::lock_guard lk(scopeMutex_); + scope_.clear(); + return; + } std::string s; s.reserve(walletFile.size()); for (unsigned char c : walletFile) @@ -58,11 +62,18 @@ void SecureVault::setWalletScope(const std::string& walletFile) { char suffix[9]; std::snprintf(suffix, sizeof(suffix), "%08x", static_cast(h & 0xffffffffu)); if (s.size() > 48) s.resize(48); - scope_ = s + "-" + suffix; + std::string newScope = s + "-" + suffix; + std::lock_guard lk(scopeMutex_); + scope_ = std::move(newScope); } std::string SecureVault::getVaultPath() const { - const std::string name = scope_.empty() ? std::string("vault.dat") : ("vault-" + scope_ + ".dat"); + std::string scope; + { + std::lock_guard lk(scopeMutex_); + scope = scope_; + } + const std::string name = scope.empty() ? std::string("vault.dat") : ("vault-" + scope + ".dat"); return (fs::path(Platform::getConfigDir()) / name).string(); } diff --git a/src/util/secure_vault.h b/src/util/secure_vault.h index a2f3fbc..f0b5897 100644 --- a/src/util/secure_vault.h +++ b/src/util/secure_vault.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace dragonx { namespace util { @@ -101,6 +102,9 @@ private: const uint8_t* salt, size_t saltLen, uint8_t* key, size_t keyLen) const; + // Guards scope_: setWalletScope() runs on the main thread (wallet switch) while retrieve()/hasVault()/ + // changePin() run on RPC worker threads and read it via getVaultPath(). + mutable std::mutex scopeMutex_; std::string scope_; // "" = default/legacy wallet (vault.dat); else a sanitized wallet-file tag };