2019-05-06 14:05:28 -04:00
|
|
|
// Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-10-08 17:48:07 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-17 21:36:37 -03:00
|
|
|
#include <wallet/walletutil.h>
|
2017-10-08 17:48:07 -03:00
|
|
|
|
2019-03-11 19:57:59 -03:00
|
|
|
#include <logging.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
2018-09-21 20:24:29 -03:00
|
|
|
|
2020-10-19 16:34:21 -03:00
|
|
|
#ifdef USE_BDB
|
2020-05-26 20:54:05 -04:00
|
|
|
bool ExistsBerkeleyDatabase(const fs::path& path);
|
2020-10-19 16:34:21 -03:00
|
|
|
#else
|
|
|
|
# define ExistsBerkeleyDatabase(path) (false)
|
|
|
|
#endif
|
2020-10-15 10:50:00 -03:00
|
|
|
#ifdef USE_SQLITE
|
2020-06-11 16:24:17 -04:00
|
|
|
bool ExistsSQLiteDatabase(const fs::path& path);
|
2020-10-15 10:50:00 -03:00
|
|
|
#else
|
|
|
|
# define ExistsSQLiteDatabase(path) (false)
|
|
|
|
#endif
|
2020-05-26 20:54:05 -04:00
|
|
|
|
2017-10-08 17:48:07 -03:00
|
|
|
fs::path GetWalletDir()
|
|
|
|
{
|
|
|
|
fs::path path;
|
|
|
|
|
|
|
|
if (gArgs.IsArgSet("-walletdir")) {
|
2018-01-18 15:15:00 -03:00
|
|
|
path = gArgs.GetArg("-walletdir", "");
|
2017-10-08 17:48:07 -03:00
|
|
|
if (!fs::is_directory(path)) {
|
|
|
|
// If the path specified doesn't exist, we return the deliberately
|
|
|
|
// invalid empty string.
|
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path = GetDataDir();
|
2017-10-26 23:15:40 -03:00
|
|
|
// If a wallets directory exists, use that, otherwise default to GetDataDir
|
|
|
|
if (fs::is_directory(path / "wallets")) {
|
|
|
|
path /= "wallets";
|
|
|
|
}
|
2017-10-08 17:48:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2018-09-21 20:24:29 -03:00
|
|
|
|
|
|
|
std::vector<fs::path> ListWalletDir()
|
|
|
|
{
|
|
|
|
const fs::path wallet_dir = GetWalletDir();
|
2018-10-20 10:29:48 -03:00
|
|
|
const size_t offset = wallet_dir.string().size() + 1;
|
2018-09-21 20:24:29 -03:00
|
|
|
std::vector<fs::path> paths;
|
2019-03-11 19:57:59 -03:00
|
|
|
boost::system::error_code ec;
|
|
|
|
|
|
|
|
for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
|
|
|
|
if (ec) {
|
|
|
|
LogPrintf("%s: %s %s\n", __func__, ec.message(), it->path().string());
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-21 20:24:29 -03:00
|
|
|
|
2020-07-12 23:01:57 -04:00
|
|
|
try {
|
|
|
|
// Get wallet path relative to walletdir by removing walletdir from the wallet path.
|
|
|
|
// This can be replaced by boost::filesystem::lexically_relative once boost is bumped to 1.60.
|
|
|
|
const fs::path path = it->path().string().substr(offset);
|
2018-10-20 10:29:48 -03:00
|
|
|
|
2020-07-12 23:01:57 -04:00
|
|
|
if (it->status().type() == fs::directory_file &&
|
|
|
|
(ExistsBerkeleyDatabase(it->path()) || ExistsSQLiteDatabase(it->path()))) {
|
|
|
|
// Found a directory which contains wallet.dat btree file, add it as a wallet.
|
2018-10-20 10:29:48 -03:00
|
|
|
paths.emplace_back(path);
|
2020-07-12 23:01:57 -04:00
|
|
|
} else if (it.level() == 0 && it->symlink_status().type() == fs::regular_file && ExistsBerkeleyDatabase(it->path())) {
|
|
|
|
if (it->path().filename() == "wallet.dat") {
|
|
|
|
// Found top-level wallet.dat btree file, add top level directory ""
|
|
|
|
// as a wallet.
|
|
|
|
paths.emplace_back();
|
|
|
|
} else {
|
|
|
|
// Found top-level btree file not called wallet.dat. Current bitcoin
|
|
|
|
// software will never create these files but will allow them to be
|
|
|
|
// opened in a shared database environment for backwards compatibility.
|
|
|
|
// Add it to the list of available wallets.
|
|
|
|
paths.emplace_back(path);
|
|
|
|
}
|
2018-09-21 20:24:29 -03:00
|
|
|
}
|
2020-07-12 23:01:57 -04:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
LogPrintf("%s: Error scanning %s: %s\n", __func__, it->path().string(), e.what());
|
|
|
|
it.no_push();
|
2018-09-21 20:24:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
2020-04-29 14:48:43 -04:00
|
|
|
|
|
|
|
bool IsFeatureSupported(int wallet_version, int feature_version)
|
|
|
|
{
|
|
|
|
return wallet_version >= feature_version;
|
|
|
|
}
|
2020-04-29 16:58:12 -04:00
|
|
|
|
|
|
|
WalletFeature GetClosestWalletFeature(int version)
|
|
|
|
{
|
2020-11-16 14:55:32 -03:00
|
|
|
const std::array<WalletFeature, 8> wallet_features{{FEATURE_LATEST, FEATURE_PRE_SPLIT_KEYPOOL, FEATURE_NO_DEFAULT_KEY, FEATURE_HD_SPLIT, FEATURE_HD, FEATURE_COMPRPUBKEY, FEATURE_WALLETCRYPT, FEATURE_BASE}};
|
|
|
|
for (const WalletFeature& wf : wallet_features) {
|
|
|
|
if (version >= wf) return wf;
|
|
|
|
}
|
2020-04-29 16:58:12 -04:00
|
|
|
return static_cast<WalletFeature>(0);
|
|
|
|
}
|