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