| 3828 | } |
| 3829 | |
| 3830 | ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const FlatSigningProvider& signing_provider, const std::string& label, bool internal) |
| 3831 | { |
| 3832 | AssertLockHeld(cs_wallet); |
| 3833 | |
| 3834 | if (!IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) { |
| 3835 | WalletLogPrintf("Cannot add WalletDescriptor to a non-descriptor wallet\n"); |
| 3836 | return nullptr; |
| 3837 | } |
| 3838 | |
| 3839 | auto spk_man = GetDescriptorScriptPubKeyMan(desc); |
| 3840 | if (spk_man) { |
| 3841 | WalletLogPrintf("Update existing descriptor: %s\n", desc.descriptor->ToString()); |
| 3842 | spk_man->UpdateWalletDescriptor(desc); |
| 3843 | } else { |
| 3844 | auto new_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc)); |
| 3845 | spk_man = new_spk_man.get(); |
| 3846 | |
| 3847 | // Save the descriptor to memory |
| 3848 | m_spk_managers[new_spk_man->GetID()] = std::move(new_spk_man); |
| 3849 | } |
| 3850 | |
| 3851 | // Add the private keys to the descriptor |
| 3852 | for (const auto& entry : signing_provider.keys) { |
| 3853 | const CKey& key = entry.second; |
| 3854 | spk_man->AddDescriptorKey(key, key.GetPubKey()); |
| 3855 | } |
| 3856 | |
| 3857 | // Top up key pool, the manager will generate new scriptPubKeys internally |
| 3858 | if (!spk_man->TopUp()) { |
| 3859 | WalletLogPrintf("Could not top up scriptPubKeys\n"); |
| 3860 | return nullptr; |
| 3861 | } |
| 3862 | |
| 3863 | // Apply the label if necessary |
| 3864 | // Note: we disable labels for ranged descriptors |
| 3865 | if (!desc.descriptor->IsRange()) { |
| 3866 | auto script_pub_keys = spk_man->GetScriptPubKeys(); |
| 3867 | if (script_pub_keys.empty()) { |
| 3868 | WalletLogPrintf("Could not generate scriptPubKeys (cache is empty)\n"); |
| 3869 | return nullptr; |
| 3870 | } |
| 3871 | |
| 3872 | CTxDestination dest; |
| 3873 | if (!internal && ExtractDestination(script_pub_keys.at(0), dest)) { |
| 3874 | SetAddressBook(dest, label, "receive"); |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | // Save the descriptor to DB |
| 3879 | spk_man->WriteDescriptor(); |
| 3880 | |
| 3881 | return spk_man; |
| 3882 | } |
| 3883 | |
| 3884 | CAmount CWalletTx::GetOutputValueOut(const CWallet& wallet, unsigned int output_index) const { |
| 3885 | CAmount ret; |