Skip to content

Commit 4807f73

Browse files
committed
refactor: Implement restorewallet() logic in the wallet section
Currently restorewallet() logic is written in the RPC layer and it can´t be reused by GUI. So it reimplements this in the wallet and interface sections and then, GUI can access it.
1 parent f727d81 commit 4807f73

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/interfaces/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ class WalletClient : public ChainClient
322322
//! Return default wallet directory.
323323
virtual std::string getWalletDir() = 0;
324324

325+
//! Restore backup wallet
326+
virtual std::unique_ptr<Wallet> restoreWallet(const std::string& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
327+
325328
//! Return available wallets in wallet directory.
326329
virtual std::vector<std::string> listWalletDir() = 0;
327330

src/wallet/interfaces.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,12 @@ class WalletClientImpl : public WalletClient
552552
options.require_existing = true;
553553
return MakeWallet(m_context, LoadWallet(m_context, name, true /* load_on_start */, options, status, error, warnings));
554554
}
555+
std::unique_ptr<Wallet> restoreWallet(const std::string& backup_file, const std::string& wallet_name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
556+
{
557+
DatabaseStatus status;
558+
559+
return MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings));
560+
}
555561
std::string getWalletDir() override
556562
{
557563
return fs::PathToString(GetWalletDir());

src/wallet/wallet.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,31 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
357357
return wallet;
358358
}
359359

360+
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const std::string& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
361+
{
362+
DatabaseOptions options;
363+
options.require_existing = true;
364+
365+
if (!fs::exists(fs::u8path(backup_file))) {
366+
error = Untranslated("Backup file does not exist");
367+
status = DatabaseStatus::FAILED_BAD_PATH;
368+
return nullptr;
369+
}
370+
371+
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
372+
373+
if (fs::exists(wallet_path) || !TryCreateDirectories(wallet_path)) {
374+
error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(wallet_path)));
375+
status = DatabaseStatus::FAILED_ALREADY_EXISTS;
376+
return nullptr;
377+
}
378+
379+
auto wallet_file = wallet_path / "wallet.dat";
380+
fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
381+
382+
return LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
383+
}
384+
360385
/** @defgroup mapWallet
361386
*
362387
* @{

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context);
6060
std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);
6161
std::shared_ptr<CWallet> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
6262
std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
63+
std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const std::string& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
6364
std::unique_ptr<interfaces::Handler> HandleLoadWallet(WalletContext& context, LoadWalletFn load_wallet);
6465
std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
6566

0 commit comments

Comments
 (0)