Repository: bitcoin/bitcoin
File: src/wallet/scriptpubkeyman.cpp (C++)

Code before:
                }
                desc_spk_man->AddDescriptorKey(key, key.GetPubKey());
            }
            desc_spk_man->TopUp();
            auto desc_spks_set = desc_spk_man->GetScriptPubKeys();
            desc_spks.insert(desc_spks.end(), desc_spks_set.begin(), desc_spks_set.end());

            out.desc_spkms.push_back(std::move(desc_spk_man));
        }

        // Remove the scriptPubKeys from our current set
        for (const CScript& desc_spk : desc_spks) {
            auto del_it = spks.find(desc_spk);
            assert(del_it != spks.end());
            assert(IsMine(desc_spk) != ISMINE_NO);
            it = spks.erase(del_it);
        }
    }

    // Multisigs are special. They don't show up as ISMINE_SPENDABLE unless they are in a P2SH
    // So we have to check if any of our scripts are a multisig and if so, add the P2SH
    for (const auto& script_pair : mapScripts) {
        const CScript script = script_pair.second;

        // Get birthdate from script meta
        uint64_t creation_time = 0;
        const auto& it = m_script_metadata.find(CScriptID(script));
        if (it != m_script_metadata.end()) {
            creation_time = it->second.nCreateTime;
        }

        std::vector<std::vector<unsigned char>> sols;
        TxoutType type = Solver(script, sols);
        if (type == TxoutType::MULTISIG) {
            CScript sh_spk = GetScriptForDestination(ScriptHash(script));
            CTxDestination witdest = WitnessV0ScriptHash(script);
            CScript witprog = GetScriptForDestination(witdest);
            CScript sh_wsh_spk = GetScriptForDestination(ScriptHash(witprog));

            // We only want the multisigs that we have not already seen, i.e. they are not watchonly and not spendable
            // For P2SH, a multisig is not ISMINE_NO when:
            // * All keys are in the wallet
            // * The multisig itself is watch only
            // * The P2SH is watch only
            // For P2SH-P2WSH, if the script is in the wallet, then it will have the same conditions as P2SH.
            // For P2WSH, a multisig is not ISMINE_NO when, other than the P2SH conditions:
            // * The P2WSH script is in the wallet and it is being watched
            std::vector<std::vector<unsigned char>> keys(sols.begin() + 1, sols.begin() + sols.size() - 1);
            if (HaveWatchOnly(sh_spk) || HaveWatchOnly(script) || HaveKeys(keys, *this) || (HaveCScript(CScriptID(witprog)) && HaveWatchOnly(witprog))) {
                // The above emulates IsMine for these 3 scriptPubKeys, so double check that by running IsMine
                assert(IsMine(sh_spk) != ISMINE_NO || IsMine(witprog) != ISMINE_NO || IsMine(sh_wsh_spk) != ISMINE_NO);

Review comment (suggestion):
nit: Might as well use `make_unique` on the lines you modify:
```suggestion
        auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
```

Code after:
        if (watchonly && !m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
            out.watch_descs.emplace_back(desc->ToString(), creation_time);

            // Get the scriptPubKeys without writing this to the wallet
            FlatSigningProvider provider;
            desc->Expand(0, provider, desc_spks, provider);
        } else {
            // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
            WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
            auto desc_spk_man = std::make_unique<DescriptorScriptPubKeyMan>(m_storage, w_desc, /*keypool_size=*/0);
            for (const auto& keyid : privkeyids) {
                CKey key;
                if (!GetKey(keyid, key)) {
                    continue;
                }
                desc_spk_man->AddDescriptorKey(key, key.GetPubKey());
            }
            desc_spk_man->TopUp();
            auto desc_spks_set = desc_spk_man->GetScriptPubKeys();
            desc_spks.insert(desc_spks.end(), desc_spks_set.begin(), desc_spks_set.end());

            out.desc_spkms.push_back(std::move(desc_spk_man));
        }

        // Remove the scriptPubKeys from our current set
        for (const CScript& desc_spk : desc_spks) {
            auto del_it = spks.find(desc_spk);
            assert(del_it != spks.end());
            assert(IsMine(desc_spk) != ISMINE_NO);
            it = spks.erase(del_it);
        }
    }

    // Multisigs are special. They don't show up as ISMINE_SPENDABLE unless they are in a P2SH
    // So we have to check if any of our scripts are a multisig and if so, add the P2SH
    for (const auto& script_pair : mapScripts) {
        const CScript script = script_pair.second;

        // Get birthdate from script meta
        uint64_t creation_time = 0;
        const auto& it = m_script_metadata.find(CScriptID(script));
        if (it != m_script_metadata.end()) {
            creation_time = it->second.nCreateTime;
        }

        std::vector<std::vector<unsigned char>> sols;
        TxoutType type = Solver(script, sols);
        if (type == TxoutType::MULTISIG) {
            CScript sh_spk = GetScriptForDestination(ScriptHash(script));
            CTxDestination witdest = WitnessV0ScriptHash(script);
            CScript witprog = GetScriptForDestination(witdest);