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