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