Skip to content

Commit f132065

Browse files
committed
wallet: Add CWallet::ExportWatchOnly
ExportWatchOnly produces a watchonly wallet file from a CWallet. This can be restored onto another instance of Bitcoin Core to allow that instance to watch the same descriptors, and also have all of the same initial address book and transactions.
1 parent f734d7b commit f132065

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

src/wallet/wallet.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4490,4 +4490,170 @@ util::Result<std::vector<WalletDescInfo>> CWallet::ExportDescriptors(bool export
44904490
}
44914491
return wallet_descriptors;
44924492
}
4493+
4494+
util::Result<std::string> CWallet::ExportWatchOnlyWallet(const fs::path& destination, WalletContext& context) const
4495+
{
4496+
AssertLockHeld(cs_wallet);
4497+
4498+
if (destination.empty()) {
4499+
return util::Error{_("Error: Export destination cannot be empty")};
4500+
}
4501+
if (fs::exists(destination)) {
4502+
return util::Error{strprintf(_("Error: Export destination '%s' already exists"), fs::PathToString(destination))};
4503+
}
4504+
fs::path canonical_dest = fs::canonical(destination.parent_path());
4505+
canonical_dest /= destination.filename();
4506+
4507+
// Get the descriptors from this wallet
4508+
util::Result<std::vector<WalletDescInfo>> exported = ExportDescriptors(/*export_private=*/false);
4509+
if (!exported) {
4510+
return util::Error{util::ErrorString(exported)};
4511+
}
4512+
4513+
// Setup DatabaseOptions to create a new sqlite database
4514+
DatabaseOptions options;
4515+
options.require_existing = false;
4516+
options.require_create = true;
4517+
options.require_format = DatabaseFormat::SQLITE;
4518+
4519+
// Make the wallet with the same flags as this wallet, but without private keys
4520+
options.create_flags = GetWalletFlags() | WALLET_FLAG_DISABLE_PRIVATE_KEYS;
4521+
4522+
// Make the watchonly wallet
4523+
DatabaseStatus status;
4524+
std::vector<bilingual_str> warnings;
4525+
std::string wallet_name = GetName() + "_watchonly";
4526+
bilingual_str error;
4527+
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(wallet_name, options, status, error);
4528+
if (!database) {
4529+
return util::Error{strprintf(_("Wallet file creation failed: %s"), error)};
4530+
}
4531+
WalletContext empty_context;
4532+
empty_context.args = context.args;
4533+
std::shared_ptr<CWallet> watchonly_wallet = CWallet::Create(empty_context, wallet_name, std::move(database), options.create_flags, error, warnings);
4534+
if (!watchonly_wallet) {
4535+
return util::Error{_("Error: Failed to create new watchonly wallet")};
4536+
}
4537+
4538+
{
4539+
LOCK(watchonly_wallet->cs_wallet);
4540+
4541+
// Parse the descriptors and add them to the new wallet
4542+
for (const WalletDescInfo& desc_info : *exported) {
4543+
// Parse the descriptor
4544+
FlatSigningProvider keys;
4545+
std::string parse_err;
4546+
std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_info.descriptor, keys, parse_err, /*require_checksum=*/true);
4547+
assert(descs.size() == 1); // All of our descriptors should be valid, and not multipath
4548+
4549+
// Get the range if there is one
4550+
int32_t range_start = 0;
4551+
int32_t range_end = 0;
4552+
if (desc_info.range) {
4553+
range_start = desc_info.range->first;
4554+
range_end = desc_info.range->second;
4555+
}
4556+
4557+
WalletDescriptor w_desc(std::move(descs.at(0)), desc_info.creation_time, range_start, range_end, desc_info.next_index);
4558+
4559+
// For descriptors that cannot self expand (i.e. needs private keys or cache), retrieve the cache
4560+
uint256 desc_id = w_desc.id;
4561+
if (!w_desc.descriptor->CanSelfExpand()) {
4562+
DescriptorScriptPubKeyMan* desc_spkm = dynamic_cast<DescriptorScriptPubKeyMan*>(GetScriptPubKeyMan(desc_id));
4563+
w_desc.cache = WITH_LOCK(desc_spkm->cs_desc_man, return desc_spkm->GetWalletDescriptor().cache);
4564+
}
4565+
4566+
// Add to the watchonly wallet
4567+
if (auto spkm_res = watchonly_wallet->AddWalletDescriptor(w_desc, keys, "", false); !spkm_res) {
4568+
return util::Error{util::ErrorString(spkm_res)};
4569+
}
4570+
4571+
// Set active spkms as active
4572+
if (desc_info.active) {
4573+
// Determine whether this descriptor is internal
4574+
// This is only set for active spkms
4575+
bool internal = false;
4576+
if (desc_info.internal) {
4577+
internal = *desc_info.internal;
4578+
}
4579+
watchonly_wallet->AddActiveScriptPubKeyMan(desc_id, *w_desc.descriptor->GetOutputType(), internal);
4580+
}
4581+
}
4582+
4583+
{
4584+
// Make a WalletBatch for the watchonly_wallet so that everything else can be written atomically
4585+
WalletBatch watchonly_batch(watchonly_wallet->GetDatabase());
4586+
if (!watchonly_batch.TxnBegin()) {
4587+
return util::Error{strprintf(_("Error: database transaction cannot be executed for new watchonly wallet %s"), watchonly_wallet->GetName())};
4588+
}
4589+
4590+
// Copy minversion
4591+
// Don't use SetMinVersion to account for the newly created wallet having FEATURE_LATEST
4592+
// while the source wallet doesn't.
4593+
watchonly_wallet->LoadMinVersion(GetVersion());
4594+
watchonly_batch.WriteMinVersion(watchonly_wallet->GetVersion());
4595+
4596+
// Copy orderPosNext
4597+
watchonly_batch.WriteOrderPosNext(watchonly_wallet->nOrderPosNext);
4598+
4599+
// Write the best block locator to avoid rescanning on reload
4600+
CBlockLocator best_block_locator;
4601+
{
4602+
WalletBatch local_wallet_batch(GetDatabase());
4603+
if (!local_wallet_batch.ReadBestBlock(best_block_locator)) {
4604+
return util::Error{_("Error: Unable to read wallet's best block locator record")};
4605+
}
4606+
}
4607+
if (!watchonly_batch.WriteBestBlock(best_block_locator)) {
4608+
return util::Error{_("Error: Unable to write watchonly wallet best block locator record")};
4609+
}
4610+
4611+
// Copy the transactions
4612+
for (const auto& [txid, wtx] : mapWallet) {
4613+
const CWalletTx& to_copy_wtx = wtx;
4614+
if (!watchonly_wallet->LoadToWallet(txid, [&](CWalletTx& ins_wtx, bool new_tx) EXCLUSIVE_LOCKS_REQUIRED(watchonly_wallet->cs_wallet) {
4615+
if (!new_tx) return false;
4616+
ins_wtx.SetTx(to_copy_wtx.tx);
4617+
ins_wtx.CopyFrom(to_copy_wtx);
4618+
return true;
4619+
})) {
4620+
return util::Error{strprintf(_("Error: Could not add tx %s to watchonly wallet"), txid.GetHex())};
4621+
}
4622+
watchonly_batch.WriteTx(watchonly_wallet->mapWallet.at(txid));
4623+
}
4624+
4625+
// Copy address book
4626+
for (const auto& [dest, entry] : m_address_book) {
4627+
auto address{EncodeDestination(dest)};
4628+
if (entry.purpose) watchonly_batch.WritePurpose(address, PurposeToString(*entry.purpose));
4629+
if (entry.label) watchonly_batch.WriteName(address, *entry.label);
4630+
for (const auto& [id, request] : entry.receive_requests) {
4631+
watchonly_batch.WriteAddressReceiveRequest(dest, id, request);
4632+
}
4633+
if (entry.previously_spent) watchonly_batch.WriteAddressPreviouslySpent(dest, true);
4634+
}
4635+
4636+
// Copy locked coins that are persisted
4637+
for (const auto& [coin, persisted] : m_locked_coins) {
4638+
if (!persisted) continue;
4639+
watchonly_wallet->LockCoin(coin, &watchonly_batch);
4640+
}
4641+
4642+
4643+
if (!watchonly_batch.TxnCommit()) {
4644+
return util::Error{_("Error: cannot commit db transaction for watchonly wallet export")};
4645+
}
4646+
}
4647+
4648+
// Make a backup of this wallet at the specified destination directory
4649+
watchonly_wallet->BackupWallet(fs::PathToString(canonical_dest));
4650+
}
4651+
4652+
// Delete the watchonly wallet now that it has been exported to the desired location
4653+
fs::path watchonly_path = fs::PathFromString(watchonly_wallet->GetDatabase().Filename()).parent_path();
4654+
watchonly_wallet.reset();
4655+
fs::remove_all(watchonly_path);
4656+
4657+
return fs::PathToString(canonical_dest);
4658+
}
44934659
} // namespace wallet

src/wallet/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10701070

10711071
//! Export the descriptors from this wallet so that they can be imported elsewhere
10721072
util::Result<std::vector<WalletDescInfo>> ExportDescriptors(bool export_private) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1073+
1074+
//! Make a new watchonly wallet file containing the public descriptors from this wallet
1075+
//! The exported watchonly wallet file will be named and placed at the path specified in 'destination'
1076+
util::Result<std::string> ExportWatchOnlyWallet(const fs::path& destination, WalletContext& context) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10731077
};
10741078

10751079
/**

0 commit comments

Comments
 (0)