Skip to content

Commit 5b3a85b

Browse files
committed
interfaces, wallet: Expose migrate wallet
1 parent da49418 commit 5b3a85b

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/interfaces/wallet.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct WalletBalances;
5050
struct WalletTx;
5151
struct WalletTxOut;
5252
struct WalletTxStatus;
53+
struct WalletMigrationResult;
5354

5455
using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
5556
using WalletValueMap = std::map<std::string, std::string>;
@@ -332,6 +333,9 @@ class WalletLoader : public ChainClient
332333
//! Restore backup wallet
333334
virtual util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
334335

336+
//! Migrate a wallet
337+
virtual util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) = 0;
338+
335339
//! Return available wallets in wallet directory.
336340
virtual std::vector<std::string> listWalletDir() = 0;
337341

@@ -423,6 +427,15 @@ struct WalletTxOut
423427
bool is_spent = false;
424428
};
425429

430+
//! Migrated wallet info
431+
struct WalletMigrationResult
432+
{
433+
std::unique_ptr<Wallet> wallet;
434+
std::optional<std::string> watchonly_wallet_name;
435+
std::optional<std::string> solvables_wallet_name;
436+
fs::path backup_path;
437+
};
438+
426439
//! Return implementation of Wallet interface. This function is defined in
427440
//! dummywallet.cpp and throws if the wallet component is not compiled.
428441
std::unique_ptr<Wallet> MakeWallet(wallet::WalletContext& context, const std::shared_ptr<wallet::CWallet>& wallet);

src/wallet/interfaces.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ using interfaces::Wallet;
4242
using interfaces::WalletAddress;
4343
using interfaces::WalletBalances;
4444
using interfaces::WalletLoader;
45+
using interfaces::WalletMigrationResult;
4546
using interfaces::WalletOrderForm;
4647
using interfaces::WalletTx;
4748
using interfaces::WalletTxOut;
@@ -631,6 +632,18 @@ class WalletLoaderImpl : public WalletLoader
631632
return util::Error{error};
632633
}
633634
}
635+
util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) override
636+
{
637+
auto res = wallet::MigrateLegacyToDescriptor(name, passphrase, m_context);
638+
if (!res) return util::Error{util::ErrorString(res)};
639+
WalletMigrationResult out{
640+
.wallet = MakeWallet(m_context, res->wallet),
641+
.watchonly_wallet_name = res->watchonly_wallet ? std::make_optional(res->watchonly_wallet->GetName()) : std::nullopt,
642+
.solvables_wallet_name = res->solvables_wallet ? std::make_optional(res->solvables_wallet->GetName()) : std::nullopt,
643+
.backup_path = res->backup_path,
644+
};
645+
return {std::move(out)}; // std::move to work around clang bug
646+
}
634647
std::string getWalletDir() override
635648
{
636649
return fs::PathToString(GetWalletDir());

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4253,7 +4253,7 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
42534253
// Migration successful, unload the wallet locally, then reload it.
42544254
assert(local_wallet.use_count() == 1);
42554255
local_wallet.reset();
4256-
LoadWallet(context, wallet_name, /*load_on_start=*/std::nullopt, options, status, error, warnings);
4256+
res.wallet = LoadWallet(context, wallet_name, /*load_on_start=*/std::nullopt, options, status, error, warnings);
42574257
res.wallet_name = wallet_name;
42584258
} else {
42594259
// Migration failed, cleanup

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ bool FillInputToWeight(CTxIn& txin, int64_t target_weight);
10681068

10691069
struct MigrationResult {
10701070
std::string wallet_name;
1071+
std::shared_ptr<CWallet> wallet;
10711072
std::shared_ptr<CWallet> watchonly_wallet;
10721073
std::shared_ptr<CWallet> solvables_wallet;
10731074
fs::path backup_path;

0 commit comments

Comments
 (0)