Merge g230 via gui_backup_formats

This commit is contained in:
Luke Dashjr 2024-06-21 19:28:12 +00:00
commit af27148d94
6 changed files with 55 additions and 18 deletions

View File

@ -43,6 +43,11 @@ struct WalletContext;
using isminefilter = std::underlying_type<isminetype>::type;
} // namespace wallet
enum class WalletBackupFormat {
Raw, // Literal db copy
DbDump, // DumpWallet plaintext low-level db dump
};
namespace interfaces {
class Handler;
@ -84,8 +89,10 @@ public:
//! Abort a rescan.
virtual void abortRescan() = 0;
virtual bool canBackupToDbDump() = 0;
//! Back up wallet.
virtual bool backupWallet(const std::string& filename) = 0;
virtual bool backupWallet(const std::string& filename, const WalletBackupFormat format, bilingual_str& error) = 0;
//! Get wallet name.
virtual std::string getWalletName() = 0;

View File

@ -210,19 +210,36 @@ void WalletView::encryptWallet()
void WalletView::backupWallet()
{
QString filetype_str;
//: Name of the wallet data file format.
QString supported_formats = tr("Wallet Data") + QLatin1String(" (*.dat)");
if (walletModel->wallet().canBackupToDbDump()) {
//: Name of the wallet data file format.
supported_formats += QLatin1String(";;") + tr("Wallet Database Dump File") + QLatin1String(" (*.walletdbdump)");
}
QString filename = GUIUtil::getSaveFileName(this,
tr("Backup Wallet"), QString(),
//: Name of the wallet data file format.
tr("Wallet Data") + QLatin1String(" (*.dat)"), nullptr);
supported_formats,
&filetype_str);
if (filename.isEmpty())
return;
if (!walletModel->wallet().backupWallet(filename.toLocal8Bit().data())) {
Q_EMIT message(tr("Backup Failed"), tr("There was an error trying to save the wallet data to %1.").arg(filename),
CClientUIInterface::MSG_ERROR);
WalletBackupFormat filetype;
if (filetype_str == "walletdbdump") {
filetype = WalletBackupFormat::DbDump;
} else {
filetype = WalletBackupFormat::Raw;
}
bilingual_str error;
if (!walletModel->wallet().backupWallet(filename.toLocal8Bit().data(), filetype, error)) {
if (error.empty()) {
Q_EMIT message(tr("Backup Failed"), tr("There was an error trying to save the wallet data to %1.").arg(filename), CClientUIInterface::MSG_ERROR);
} else {
Q_EMIT message(tr("Backup Failed"), tr("There was an error trying to save the wallet data to %1: %2").arg(filename).arg(QString::fromStdString(error.translated)), CClientUIInterface::MSG_ERROR);
}
else {
} else {
Q_EMIT message(tr("Backup Successful"), tr("The wallet data was successfully saved to %1.").arg(filename),
CClientUIInterface::MSG_INFORMATION);
}

View File

@ -21,15 +21,8 @@ namespace wallet {
static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
uint32_t DUMP_VERSION = 1;
bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error)
bool DumpWallet(WalletDatabase& db, bilingual_str& error, const std::string& dump_filename)
{
// Get the dumpfile
std::string dump_filename = args.GetArg("-dumpfile", "");
if (dump_filename.empty()) {
error = _("No dump file provided. To use dump, -dumpfile=<filename> must be provided.");
return false;
}
fs::path path = fs::PathFromString(dump_filename);
path = fs::absolute(path);
if (fs::exists(path)) {

View File

@ -16,7 +16,7 @@ class ArgsManager;
namespace wallet {
class WalletDatabase;
bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error);
bool DumpWallet(WalletDatabase& db, bilingual_str& error, const std::string& dump_filename);
bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings);
} // namespace wallet

View File

@ -21,6 +21,7 @@
#include <util/ui_change_type.h>
#include <wallet/coincontrol.h>
#include <wallet/context.h>
#include <wallet/dump.h>
#include <wallet/feebumper.h>
#include <wallet/fees.h>
#include <wallet/types.h>
@ -161,7 +162,18 @@ public:
return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase);
}
void abortRescan() override { m_wallet->AbortRescan(); }
bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
bool canBackupToDbDump() override {
return (m_wallet->GetDatabase().Format() != "bdb");
}
bool backupWallet(const std::string& filename, const WalletBackupFormat format, bilingual_str& error) override {
switch (format) {
case WalletBackupFormat::DbDump:
return DumpWallet(m_wallet->GetDatabase(), error, filename);
case WalletBackupFormat::Raw:
return m_wallet->BackupWallet(filename);
}
return false;
}
std::string getWalletName() override { return m_wallet->GetName(); }
util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) override
{

View File

@ -194,6 +194,14 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
DatabaseOptions options;
ReadDatabaseArgs(args, options);
options.require_existing = true;
// Get the dumpfile
std::string dump_filename = args.GetArg("-dumpfile", "");
if (dump_filename.empty()) {
tfm::format(std::cerr, "No dump file provided. To use dump, -dumpfile=<filename> must be provided.\n");
return false;
}
DatabaseStatus status;
bilingual_str error;
std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
@ -202,7 +210,7 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
return false;
}
bool ret = DumpWallet(args, *database, error);
bool ret = DumpWallet(*database, error, dump_filename);
if (!ret && !error.empty()) {
tfm::format(std::cerr, "%s\n", error.original);
return ret;