| 25 | |
| 26 | namespace wallet { |
| 27 | bool VerifyWallets(WalletContext& context) |
| 28 | { |
| 29 | interfaces::Chain& chain = *context.chain; |
| 30 | ArgsManager& args = *Assert(context.args); |
| 31 | |
| 32 | if (args.IsArgSet("-walletdir")) { |
| 33 | const fs::path wallet_dir{args.GetPathArg("-walletdir")}; |
| 34 | std::error_code error; |
| 35 | // The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory |
| 36 | // It also lets the fs::exists and fs::is_directory checks below pass on windows, since they return false |
| 37 | // if a path has trailing slashes, and it strips trailing slashes. |
| 38 | fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error); |
| 39 | if (error || !fs::exists(canonical_wallet_dir)) { |
| 40 | chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), fs::PathToString(wallet_dir))); |
| 41 | return false; |
| 42 | } else if (!fs::is_directory(canonical_wallet_dir)) { |
| 43 | chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), fs::PathToString(wallet_dir))); |
| 44 | return false; |
| 45 | // The canonical path transforms relative paths into absolute ones, so we check the non-canonical version |
| 46 | } else if (!wallet_dir.is_absolute()) { |
| 47 | chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), fs::PathToString(wallet_dir))); |
| 48 | return false; |
| 49 | } |
| 50 | args.ForceSetArg("-walletdir", fs::PathToString(canonical_wallet_dir)); |
| 51 | } |
| 52 | |
| 53 | LogInfo("Using wallet directory %s", fs::PathToString(GetWalletDir())); |
| 54 | // Print general DB information |
| 55 | LogDBInfo(); |
| 56 | |
| 57 | chain.initMessage(_("Verifying wallet(s)…")); |
| 58 | |
| 59 | // For backwards compatibility if an unnamed top level wallet exists in the |
| 60 | // wallets directory, include it in the default list of wallets to load. |
| 61 | if (!args.IsArgSet("wallet")) { |
| 62 | DatabaseOptions options; |
| 63 | DatabaseStatus status; |
| 64 | ReadDatabaseArgs(args, options); |
| 65 | bilingual_str error_string; |
| 66 | options.require_existing = true; |
| 67 | options.verify = false; |
| 68 | if (MakeWalletDatabase("", options, status, error_string)) { |
| 69 | common::SettingsValue wallets(common::SettingsValue::VARR); |
| 70 | wallets.push_back(""); // Default wallet name is "" |
| 71 | // Pass write=false because no need to write file and probably |
| 72 | // better not to. If unnamed wallet needs to be added next startup |
| 73 | // and the setting is empty, this code will just run again. |
| 74 | chain.overwriteRwSetting("wallet", std::move(wallets), interfaces::SettingsAction::SKIP_WRITE); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Keep track of each wallet absolute path to detect duplicates. |
| 79 | std::set<fs::path> wallet_paths; |
| 80 | |
| 81 | for (const auto& wallet : chain.getSettingsList("wallet")) { |
| 82 | if (!wallet.isStr()) { |
| 83 | chain.initError(_("Invalid value detected for '-wallet' or '-nowallet'. " |
| 84 | "'-wallet' requires a string value, while '-nowallet' accepts only '1' to disable all wallets")); |
no test coverage detected