Coverage Report

Created: 2026-05-30 09:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/scriptpubkeyman.cpp
Line
Count
Source
1
// Copyright (c) 2019-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <wallet/scriptpubkeyman.h>
6
7
#include <coins.h>
8
#include <hash.h>
9
#include <key_io.h>
10
#include <node/types.h>
11
#include <outputtype.h>
12
#include <script/descriptor.h>
13
#include <script/script.h>
14
#include <script/sign.h>
15
#include <script/solver.h>
16
#include <util/bip32.h>
17
#include <util/check.h>
18
#include <util/log.h>
19
#include <util/strencodings.h>
20
#include <util/string.h>
21
#include <util/time.h>
22
#include <util/translation.h>
23
24
#include <optional>
25
26
using common::PSBTError;
27
using util::ToString;
28
29
namespace wallet {
30
31
typedef std::vector<unsigned char> valtype;
32
33
// Legacy wallet IsMine(). Used only in migration
34
// DO NOT USE ANYTHING IN THIS NAMESPACE OUTSIDE OF MIGRATION
35
namespace {
36
37
/**
38
 * This is an enum that tracks the execution context of a script, similar to
39
 * SigVersion in script/interpreter. It is separate however because we want to
40
 * distinguish between top-level scriptPubKey execution and P2SH redeemScript
41
 * execution (a distinction that has no impact on consensus rules).
42
 */
43
enum class IsMineSigVersion
44
{
45
    TOP = 0,        //!< scriptPubKey execution
46
    P2SH = 1,       //!< P2SH redeemScript
47
    WITNESS_V0 = 2, //!< P2WSH witness script execution
48
};
49
50
/**
51
 * This is an internal representation of isminetype + invalidity.
52
 * Its order is significant, as we return the max of all explored
53
 * possibilities.
54
 */
55
enum class IsMineResult
56
{
57
    NO = 0,         //!< Not ours
58
    WATCH_ONLY = 1, //!< Included in watch-only balance
59
    SPENDABLE = 2,  //!< Included in all balances
60
    INVALID = 3,    //!< Not spendable by anyone (uncompressed pubkey in segwit, P2SH inside P2SH or witness, witness inside witness)
61
};
62
63
bool PermitsUncompressed(IsMineSigVersion sigversion)
64
1.94k
{
65
1.94k
    return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
66
1.94k
}
67
68
bool HaveKeys(const std::vector<valtype>& pubkeys, const LegacyDataSPKM& keystore)
69
42
{
70
93
    for (const valtype& pubkey : pubkeys) {
71
93
        CKeyID keyID = CPubKey(pubkey).GetID();
72
93
        if (!keystore.HaveKey(keyID)) return false;
73
93
    }
74
9
    return true;
75
42
}
76
77
//! Recursively solve script and return spendable/watchonly/invalid status.
78
//!
79
//! @param keystore            legacy key and script store
80
//! @param scriptPubKey        script to solve
81
//! @param sigversion          script type (top-level / redeemscript / witnessscript)
82
//! @param recurse_scripthash  whether to recurse into nested p2sh and p2wsh
83
//!                            scripts or simply treat any script that has been
84
//!                            stored in the keystore as spendable
85
// NOLINTNEXTLINE(misc-no-recursion)
86
IsMineResult LegacyWalletIsMineInnerDONOTUSE(const LegacyDataSPKM& keystore, const CScript& scriptPubKey, IsMineSigVersion sigversion, bool recurse_scripthash=true)
87
5.40k
{
88
5.40k
    IsMineResult ret = IsMineResult::NO;
89
90
5.40k
    std::vector<valtype> vSolutions;
91
5.40k
    TxoutType whichType = Solver(scriptPubKey, vSolutions);
92
93
5.40k
    CKeyID keyID;
94
5.40k
    switch (whichType) {
95
6
    case TxoutType::NONSTANDARD:
96
6
    case TxoutType::NULL_DATA:
97
6
    case TxoutType::WITNESS_UNKNOWN:
98
24
    case TxoutType::WITNESS_V1_TAPROOT:
99
24
    case TxoutType::ANCHOR:
100
24
        break;
101
424
    case TxoutType::PUBKEY:
102
424
        keyID = CPubKey(vSolutions[0]).GetID();
103
424
        if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
104
0
            return IsMineResult::INVALID;
105
0
        }
106
424
        if (keystore.HaveKey(keyID)) {
107
394
            ret = std::max(ret, IsMineResult::SPENDABLE);
108
394
        }
109
424
        break;
110
1.03k
    case TxoutType::WITNESS_V0_KEYHASH:
111
1.03k
    {
112
1.03k
        if (sigversion == IsMineSigVersion::WITNESS_V0) {
113
            // P2WPKH inside P2WSH is invalid.
114
0
            return IsMineResult::INVALID;
115
0
        }
116
1.03k
        if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
117
            // We do not support bare witness outputs unless the P2SH version of it would be
118
            // acceptable as well. This protects against matching before segwit activates.
119
            // This also applies to the P2WSH case.
120
13
            break;
121
13
        }
122
1.02k
        ret = std::max(ret, LegacyWalletIsMineInnerDONOTUSE(keystore, GetScriptForDestination(PKHash(uint160(vSolutions[0]))), IsMineSigVersion::WITNESS_V0));
123
1.02k
        break;
124
1.03k
    }
125
1.47k
    case TxoutType::PUBKEYHASH:
126
1.47k
        keyID = CKeyID(uint160(vSolutions[0]));
127
1.47k
        if (!PermitsUncompressed(sigversion)) {
128
1.04k
            CPubKey pubkey;
129
1.04k
            if (keystore.GetPubKey(keyID, pubkey) && !pubkey.IsCompressed()) {
130
5
                return IsMineResult::INVALID;
131
5
            }
132
1.04k
        }
133
1.47k
        if (keystore.HaveKey(keyID)) {
134
1.39k
            ret = std::max(ret, IsMineResult::SPENDABLE);
135
1.39k
        }
136
1.47k
        break;
137
1.56k
    case TxoutType::SCRIPTHASH:
138
1.56k
    {
139
1.56k
        if (sigversion != IsMineSigVersion::TOP) {
140
            // P2SH inside P2WSH or P2SH is invalid.
141
10
            return IsMineResult::INVALID;
142
10
        }
143
1.55k
        CScriptID scriptID = CScriptID(uint160(vSolutions[0]));
144
1.55k
        CScript subscript;
145
1.55k
        if (keystore.GetCScript(scriptID, subscript)) {
146
709
            ret = std::max(ret, recurse_scripthash ? LegacyWalletIsMineInnerDONOTUSE(keystore, subscript, IsMineSigVersion::P2SH) : IsMineResult::SPENDABLE);
147
709
        }
148
1.55k
        break;
149
1.56k
    }
150
834
    case TxoutType::WITNESS_V0_SCRIPTHASH:
151
834
    {
152
834
        if (sigversion == IsMineSigVersion::WITNESS_V0) {
153
            // P2WSH inside P2WSH is invalid.
154
5
            return IsMineResult::INVALID;
155
5
        }
156
829
        if (sigversion == IsMineSigVersion::TOP && !keystore.HaveCScript(CScriptID(CScript() << OP_0 << vSolutions[0]))) {
157
758
            break;
158
758
        }
159
71
        CScriptID scriptID{RIPEMD160(vSolutions[0])};
160
71
        CScript subscript;
161
71
        if (keystore.GetCScript(scriptID, subscript)) {
162
64
            ret = std::max(ret, recurse_scripthash ? LegacyWalletIsMineInnerDONOTUSE(keystore, subscript, IsMineSigVersion::WITNESS_V0) : IsMineResult::SPENDABLE);
163
64
        }
164
71
        break;
165
829
    }
166
167
48
    case TxoutType::MULTISIG:
168
48
    {
169
        // Never treat bare multisig outputs as ours (they can still be made watchonly-though)
170
48
        if (sigversion == IsMineSigVersion::TOP) {
171
6
            break;
172
6
        }
173
174
        // Only consider transactions "mine" if we own ALL the
175
        // keys involved. Multi-signature transactions that are
176
        // partially owned (somebody else has a key that can spend
177
        // them) enable spend-out-from-under-you attacks, especially
178
        // in shared-wallet situations.
179
42
        std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
180
42
        if (!PermitsUncompressed(sigversion)) {
181
104
            for (size_t i = 0; i < keys.size(); i++) {
182
75
                if (keys[i].size() != 33) {
183
0
                    return IsMineResult::INVALID;
184
0
                }
185
75
            }
186
29
        }
187
42
        if (HaveKeys(keys, keystore)) {
188
9
            ret = std::max(ret, IsMineResult::SPENDABLE);
189
9
        }
190
42
        break;
191
42
    }
192
5.40k
    } // no default case, so the compiler can warn about missing cases
193
194
5.38k
    if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
195
155
        ret = std::max(ret, IsMineResult::WATCH_ONLY);
196
155
    }
197
5.38k
    return ret;
198
5.40k
}
199
200
} // namespace
201
202
bool LegacyDataSPKM::IsMine(const CScript& script) const
203
3.08k
{
204
3.08k
    switch (LegacyWalletIsMineInnerDONOTUSE(*this, script, IsMineSigVersion::TOP)) {
205
20
    case IsMineResult::INVALID:
206
1.12k
    case IsMineResult::NO:
207
1.12k
        return false;
208
155
    case IsMineResult::WATCH_ONLY:
209
1.95k
    case IsMineResult::SPENDABLE:
210
1.95k
        return true;
211
3.08k
    }
212
3.08k
    assert(false);
213
0
}
214
215
bool LegacyDataSPKM::CheckDecryptionKey(const CKeyingMaterial& master_key)
216
1
{
217
1
    {
218
1
        LOCK(cs_KeyStore);
219
1
        assert(mapKeys.empty());
220
221
1
        bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
222
1
        bool keyFail = false;
223
1
        CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
224
1
        WalletBatch batch(m_storage.GetDatabase());
225
1
        for (; mi != mapCryptedKeys.end(); ++mi)
226
1
        {
227
1
            const CPubKey &vchPubKey = (*mi).second.first;
228
1
            const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
229
1
            CKey key;
230
1
            if (!DecryptKey(master_key, vchCryptedSecret, vchPubKey, key))
231
0
            {
232
0
                keyFail = true;
233
0
                break;
234
0
            }
235
1
            keyPass = true;
236
1
            if (fDecryptionThoroughlyChecked)
237
1
                break;
238
0
            else {
239
                // Rewrite these encrypted keys with checksums
240
0
                batch.WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
241
0
            }
242
1
        }
243
1
        if (keyPass && keyFail)
244
0
        {
245
0
            LogWarning("The wallet is probably corrupted: Some keys decrypt but not all.");
246
0
            throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
247
0
        }
248
1
        if (keyFail || !keyPass)
249
0
            return false;
250
1
        fDecryptionThoroughlyChecked = true;
251
1
    }
252
0
    return true;
253
1
}
254
255
std::unique_ptr<SigningProvider> LegacyDataSPKM::GetSolvingProvider(const CScript& script) const
256
85
{
257
85
    return std::make_unique<LegacySigningProvider>(*this);
258
85
}
259
260
bool LegacyDataSPKM::CanProvide(const CScript& script, SignatureData& sigdata)
261
555
{
262
555
    IsMineResult ismine = LegacyWalletIsMineInnerDONOTUSE(*this, script, IsMineSigVersion::TOP, /* recurse_scripthash= */ false);
263
555
    if (ismine == IsMineResult::SPENDABLE || ismine == IsMineResult::WATCH_ONLY) {
264
        // If ismine, it means we recognize keys or script ids in the script, or
265
        // are watching the script itself, and we can at least provide metadata
266
        // or solving information, even if not able to sign fully.
267
25
        return true;
268
530
    } else {
269
        // If, given the stuff in sigdata, we could make a valid signature, then we can provide for this script
270
530
        ProduceSignature(*this, DUMMY_SIGNATURE_CREATOR, script, sigdata);
271
530
        if (!sigdata.signatures.empty()) {
272
            // If we could make signatures, make sure we have a private key to actually make a signature
273
1
            bool has_privkeys = false;
274
1
            for (const auto& key_sig_pair : sigdata.signatures) {
275
1
                has_privkeys |= HaveKey(key_sig_pair.first);
276
1
            }
277
1
            return has_privkeys;
278
1
        }
279
529
        return false;
280
530
    }
281
555
}
282
283
bool LegacyDataSPKM::LoadKey(const CKey& key, const CPubKey &pubkey)
284
191
{
285
191
    return AddKeyPubKeyInner(key, pubkey);
286
191
}
287
288
bool LegacyDataSPKM::LoadCScript(const CScript& redeemScript)
289
86
{
290
    /* A sanity check was added in pull #3843 to avoid adding redeemScripts
291
     * that never can be redeemed. However, old wallets may still contain
292
     * these. Do not add them to the wallet and warn. */
293
86
    if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
294
0
    {
295
0
        std::string strAddr = EncodeDestination(ScriptHash(redeemScript));
296
0
        WalletLogPrintf("%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n", __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
297
0
        return true;
298
0
    }
299
300
86
    return FillableSigningProvider::AddCScript(redeemScript);
301
86
}
302
303
void LegacyDataSPKM::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata& meta)
304
219
{
305
219
    LOCK(cs_KeyStore);
306
219
    mapKeyMetadata[keyID] = meta;
307
219
}
308
309
void LegacyDataSPKM::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata& meta)
310
48
{
311
48
    LOCK(cs_KeyStore);
312
48
    m_script_metadata[script_id] = meta;
313
48
}
314
315
bool LegacyDataSPKM::AddKeyPubKeyInner(const CKey& key, const CPubKey& pubkey)
316
191
{
317
191
    LOCK(cs_KeyStore);
318
191
    return FillableSigningProvider::AddKeyPubKey(key, pubkey);
319
191
}
320
321
bool LegacyDataSPKM::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid)
322
24
{
323
    // Set fDecryptionThoroughlyChecked to false when the checksum is invalid
324
24
    if (!checksum_valid) {
325
0
        fDecryptionThoroughlyChecked = false;
326
0
    }
327
328
24
    return AddCryptedKeyInner(vchPubKey, vchCryptedSecret);
329
24
}
330
331
bool LegacyDataSPKM::AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
332
24
{
333
24
    LOCK(cs_KeyStore);
334
24
    assert(mapKeys.empty());
335
336
24
    mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
337
24
    ImplicitlyLearnRelatedKeyScripts(vchPubKey);
338
24
    return true;
339
24
}
340
341
bool LegacyDataSPKM::HaveWatchOnly(const CScript &dest) const
342
1.86k
{
343
1.86k
    LOCK(cs_KeyStore);
344
1.86k
    return setWatchOnly.contains(dest);
345
1.86k
}
346
347
bool LegacyDataSPKM::LoadWatchOnly(const CScript &dest)
348
48
{
349
48
    return AddWatchOnlyInMem(dest);
350
48
}
351
352
static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
353
48
{
354
48
    std::vector<std::vector<unsigned char>> solutions;
355
48
    return Solver(dest, solutions) == TxoutType::PUBKEY &&
356
48
        (pubKeyOut = CPubKey(solutions[0])).IsFullyValid();
357
48
}
358
359
bool LegacyDataSPKM::AddWatchOnlyInMem(const CScript &dest)
360
48
{
361
48
    LOCK(cs_KeyStore);
362
48
    setWatchOnly.insert(dest);
363
48
    CPubKey pubKey;
364
48
    if (ExtractPubKey(dest, pubKey)) {
365
9
        mapWatchKeys[pubKey.GetID()] = pubKey;
366
9
        ImplicitlyLearnRelatedKeyScripts(pubKey);
367
9
    }
368
48
    return true;
369
48
}
370
371
void LegacyDataSPKM::LoadHDChain(const CHDChain& chain)
372
32
{
373
32
    LOCK(cs_KeyStore);
374
32
    m_hd_chain = chain;
375
32
}
376
377
void LegacyDataSPKM::AddInactiveHDChain(const CHDChain& chain)
378
6
{
379
6
    LOCK(cs_KeyStore);
380
6
    assert(!chain.seed_id.IsNull());
381
6
    m_inactive_hd_chains[chain.seed_id] = chain;
382
6
}
383
384
bool LegacyDataSPKM::HaveKey(const CKeyID &address) const
385
1.99k
{
386
1.99k
    LOCK(cs_KeyStore);
387
1.99k
    if (!m_storage.HasEncryptionKeys()) {
388
1.93k
        return FillableSigningProvider::HaveKey(address);
389
1.93k
    }
390
54
    return mapCryptedKeys.contains(address);
391
1.99k
}
392
393
bool LegacyDataSPKM::GetKey(const CKeyID &address, CKey& keyOut) const
394
1.26k
{
395
1.26k
    LOCK(cs_KeyStore);
396
1.26k
    if (!m_storage.HasEncryptionKeys()) {
397
1.25k
        return FillableSigningProvider::GetKey(address, keyOut);
398
1.25k
    }
399
400
7
    CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
401
7
    if (mi != mapCryptedKeys.end())
402
7
    {
403
7
        const CPubKey &vchPubKey = (*mi).second.first;
404
7
        const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
405
7
        return m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
406
7
            return DecryptKey(encryption_key, vchCryptedSecret, vchPubKey, keyOut);
407
7
        });
408
7
    }
409
0
    return false;
410
7
}
411
412
bool LegacyDataSPKM::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& info) const
413
135
{
414
135
    CKeyMetadata meta;
415
135
    {
416
135
        LOCK(cs_KeyStore);
417
135
        auto it = mapKeyMetadata.find(keyID);
418
135
        if (it == mapKeyMetadata.end()) {
419
51
            return false;
420
51
        }
421
84
        meta = it->second;
422
84
    }
423
84
    if (meta.has_key_origin) {
424
42
        std::copy(meta.key_origin.fingerprint, meta.key_origin.fingerprint + 4, info.fingerprint);
425
42
        info.path = meta.key_origin.path;
426
42
    } else { // Single pubkeys get the master fingerprint of themselves
427
42
        std::copy(keyID.begin(), keyID.begin() + 4, info.fingerprint);
428
42
    }
429
84
    return true;
430
135
}
431
432
bool LegacyDataSPKM::GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
433
76
{
434
76
    LOCK(cs_KeyStore);
435
76
    WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
436
76
    if (it != mapWatchKeys.end()) {
437
64
        pubkey_out = it->second;
438
64
        return true;
439
64
    }
440
12
    return false;
441
76
}
442
443
bool LegacyDataSPKM::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
444
1.08k
{
445
1.08k
    LOCK(cs_KeyStore);
446
1.08k
    if (!m_storage.HasEncryptionKeys()) {
447
1.05k
        if (!FillableSigningProvider::GetPubKey(address, vchPubKeyOut)) {
448
76
            return GetWatchPubKey(address, vchPubKeyOut);
449
76
        }
450
979
        return true;
451
1.05k
    }
452
453
30
    CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
454
30
    if (mi != mapCryptedKeys.end())
455
30
    {
456
30
        vchPubKeyOut = (*mi).second.first;
457
30
        return true;
458
30
    }
459
    // Check for watch-only pubkeys
460
0
    return GetWatchPubKey(address, vchPubKeyOut);
461
30
}
462
463
std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetCandidateScriptPubKeys() const
464
76
{
465
76
    LOCK(cs_KeyStore);
466
76
    std::unordered_set<CScript, SaltedSipHasher> candidate_spks;
467
468
    // For every private key in the wallet, there should be a P2PK, P2PKH, P2WPKH, and P2SH-P2WPKH
469
394
    const auto& add_pubkey = [&candidate_spks](const CPubKey& pub) -> void {
470
394
        candidate_spks.insert(GetScriptForRawPubKey(pub));
471
394
        candidate_spks.insert(GetScriptForDestination(PKHash(pub)));
472
473
394
        CScript wpkh = GetScriptForDestination(WitnessV0KeyHash(pub));
474
394
        candidate_spks.insert(wpkh);
475
394
        candidate_spks.insert(GetScriptForDestination(ScriptHash(wpkh)));
476
394
    };
477
382
    for (const auto& [_, key] : mapKeys) {
478
382
        add_pubkey(key.GetPubKey());
479
382
    }
480
76
    for (const auto& [_, ckeypair] : mapCryptedKeys) {
481
12
        add_pubkey(ckeypair.first);
482
12
    }
483
484
    // mapScripts contains all redeemScripts and witnessScripts. Therefore each script in it has
485
    // itself, P2SH, P2WSH, and P2SH-P2WSH as a candidate.
486
    // Invalid scripts such as P2SH-P2SH and P2WSH-P2SH, among others, will be added as candidates.
487
    // Callers of this function will need to remove such scripts.
488
564
    const auto& add_script = [&candidate_spks](const CScript& script) -> void {
489
564
        candidate_spks.insert(script);
490
564
        candidate_spks.insert(GetScriptForDestination(ScriptHash(script)));
491
492
564
        CScript wsh = GetScriptForDestination(WitnessV0ScriptHash(script));
493
564
        candidate_spks.insert(wsh);
494
564
        candidate_spks.insert(GetScriptForDestination(ScriptHash(wsh)));
495
564
    };
496
468
    for (const auto& [_, script] : mapScripts) {
497
468
        add_script(script);
498
468
    }
499
500
    // Although setWatchOnly should only contain output scripts, we will also include each script's
501
    // P2SH, P2WSH, and P2SH-P2WSH as a precaution.
502
96
    for (const auto& script : setWatchOnly) {
503
96
        add_script(script);
504
96
    }
505
506
76
    return candidate_spks;
507
76
}
508
509
std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetScriptPubKeys() const
510
38
{
511
    // Run IsMine() on each candidate output script. Any script that IsMine is an output
512
    // script to return.
513
    // This both filters out things that are not watched by the wallet, and things that are invalid.
514
38
    std::unordered_set<CScript, SaltedSipHasher> spks;
515
1.41k
    for (const CScript& script : GetCandidateScriptPubKeys()) {
516
1.41k
        if (IsMine(script)) {
517
846
            spks.insert(script);
518
846
        }
519
1.41k
    }
520
521
38
    return spks;
522
38
}
523
524
std::unordered_set<CScript, SaltedSipHasher> LegacyDataSPKM::GetNotMineScriptPubKeys() const
525
34
{
526
34
    LOCK(cs_KeyStore);
527
34
    std::unordered_set<CScript, SaltedSipHasher> spks;
528
44
    for (const CScript& script : setWatchOnly) {
529
44
        if (!IsMine(script)) spks.insert(script);
530
44
    }
531
34
    return spks;
532
34
}
533
534
std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
535
38
{
536
38
    LOCK(cs_KeyStore);
537
38
    if (m_storage.IsLocked()) {
538
0
        return std::nullopt;
539
0
    }
540
541
38
    MigrationData out;
542
543
38
    std::unordered_set<CScript, SaltedSipHasher> spks{GetScriptPubKeys()};
544
545
    // Get all key ids
546
38
    std::set<CKeyID> keyids;
547
191
    for (const auto& key_pair : mapKeys) {
548
191
        keyids.insert(key_pair.first);
549
191
    }
550
38
    for (const auto& key_pair : mapCryptedKeys) {
551
6
        keyids.insert(key_pair.first);
552
6
    }
553
554
    // Get key metadata and figure out which keys don't have a seed
555
    // Note that we do not ignore the seeds themselves because they are considered IsMine!
556
235
    for (auto keyid_it = keyids.begin(); keyid_it != keyids.end();) {
557
197
        const CKeyID& keyid = *keyid_it;
558
197
        const auto& it = mapKeyMetadata.find(keyid);
559
197
        if (it != mapKeyMetadata.end()) {
560
197
            const CKeyMetadata& meta = it->second;
561
197
            if (meta.hdKeypath == "s" || meta.hdKeypath == "m") {
562
33
                keyid_it++;
563
33
                continue;
564
33
            }
565
164
            if (!meta.hd_seed_id.IsNull() && (m_hd_chain.seed_id == meta.hd_seed_id || m_inactive_hd_chains.contains(meta.hd_seed_id))) {
566
160
                keyid_it = keyids.erase(keyid_it);
567
160
                continue;
568
160
            }
569
164
        }
570
4
        keyid_it++;
571
4
    }
572
573
38
    WalletBatch batch(m_storage.GetDatabase());
574
38
    if (!batch.TxnBegin()) {
575
0
        LogWarning("Error generating descriptors for migration, cannot initialize db transaction");
576
0
        return std::nullopt;
577
0
    }
578
579
    // keyids is now all non-HD keys. Each key will have its own combo descriptor
580
38
    for (const CKeyID& keyid : keyids) {
581
37
        CKey key;
582
37
        if (!GetKey(keyid, key)) {
583
0
            assert(false);
584
0
        }
585
586
        // Get birthdate from key meta
587
37
        uint64_t creation_time = 0;
588
37
        const auto& it = mapKeyMetadata.find(keyid);
589
37
        if (it != mapKeyMetadata.end()) {
590
37
            creation_time = it->second.nCreateTime;
591
37
        }
592
593
        // Get the key origin
594
        // Maybe this doesn't matter because floating keys here shouldn't have origins
595
37
        KeyOriginInfo info;
596
37
        bool has_info = GetKeyOrigin(keyid, info);
597
37
        std::string origin_str = has_info ? "[" + HexStr(info.fingerprint) + FormatHDKeypath(info.path) + "]" : "";
598
599
        // Construct the combo descriptor
600
37
        std::string desc_str = "combo(" + origin_str + HexStr(key.GetPubKey()) + ")";
601
37
        FlatSigningProvider provider;
602
37
        std::string error;
603
37
        std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_str, provider, error, false);
604
37
        CHECK_NONFATAL(descs.size() == 1); // It shouldn't be possible to have an invalid or multipath descriptor
605
37
        WalletDescriptor w_desc(std::move(descs.at(0)), creation_time, 0, 0, 0);
606
607
        // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
608
37
        provider.keys.emplace(key.GetPubKey().GetID(), key);
609
37
        auto desc_spk_man = DescriptorScriptPubKeyMan::CreateFromMigration(m_storage, batch, w_desc, /*keypool_size=*/0, provider);
610
37
        auto desc_spks = desc_spk_man->GetScriptPubKeys();
611
612
        // Remove the scriptPubKeys from our current set
613
146
        for (const CScript& spk : desc_spks) {
614
146
            size_t erased = spks.erase(spk);
615
146
            assert(erased == 1);
616
146
            assert(IsMine(spk));
617
146
        }
618
619
37
        out.desc_spkms.push_back(std::move(desc_spk_man));
620
37
    }
621
622
    // Handle HD keys by using the CHDChains
623
38
    std::set<CHDChain> chains;
624
38
    chains.insert(m_hd_chain);
625
38
    for (const auto& chain_pair : m_inactive_hd_chains) {
626
3
        chains.insert(chain_pair.second);
627
3
    }
628
629
38
    bool can_support_hd_split_feature = m_hd_chain.nVersion >= CHDChain::VERSION_HD_CHAIN_SPLIT;
630
631
41
    for (const CHDChain& chain : chains) {
632
123
        for (int i = 0; i < 2; ++i) {
633
            // Skip if doing internal chain and split chain is not supported
634
82
            if (chain.seed_id.IsNull() || (i == 1 && !can_support_hd_split_feature)) {
635
18
                continue;
636
18
            }
637
            // Get the master xprv
638
64
            CKey seed_key;
639
64
            if (!GetKey(chain.seed_id, seed_key)) {
640
0
                assert(false);
641
0
            }
642
64
            CExtKey master_key;
643
64
            master_key.SetSeed(seed_key);
644
645
            // Make the combo descriptor
646
64
            std::string xpub = EncodeExtPubKey(master_key.Neuter());
647
64
            std::string desc_str = "combo(" + xpub + "/0h/" + ToString(i) + "h/*h)";
648
64
            FlatSigningProvider provider;
649
64
            std::string error;
650
64
            std::vector<std::unique_ptr<Descriptor>> descs = Parse(desc_str, provider, error, false);
651
64
            CHECK_NONFATAL(descs.size() == 1); // It shouldn't be possible to have an invalid or multipath descriptor
652
64
            uint32_t chain_counter = std::max((i == 1 ? chain.nInternalChainCounter : chain.nExternalChainCounter), (uint32_t)0);
653
64
            WalletDescriptor w_desc(std::move(descs.at(0)), 0, 0, chain_counter, 0);
654
655
            // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
656
64
            provider.keys.emplace(master_key.key.GetPubKey().GetID(), master_key.key);
657
64
            auto desc_spk_man = DescriptorScriptPubKeyMan::CreateFromMigration(m_storage, batch, w_desc, /*keypool_size=*/0, provider);
658
64
            auto desc_spks = desc_spk_man->GetScriptPubKeys();
659
660
            // Remove the scriptPubKeys from our current set
661
640
            for (const CScript& spk : desc_spks) {
662
640
                size_t erased = spks.erase(spk);
663
640
                assert(erased == 1);
664
640
                assert(IsMine(spk));
665
640
            }
666
667
64
            out.desc_spkms.push_back(std::move(desc_spk_man));
668
64
        }
669
41
    }
670
    // Add the current master seed to the migration data
671
38
    if (!m_hd_chain.seed_id.IsNull()) {
672
29
        CKey seed_key;
673
29
        if (!GetKey(m_hd_chain.seed_id, seed_key)) {
674
0
            assert(false);
675
0
        }
676
29
        out.master_key.SetSeed(seed_key);
677
29
    }
678
679
    // Handle the rest of the scriptPubKeys which must be imports and may not have all info
680
98
    for (auto it = spks.begin(); it != spks.end();) {
681
60
        const CScript& spk = *it;
682
683
        // Get birthdate from script meta
684
60
        uint64_t creation_time = 0;
685
60
        const auto& mit = m_script_metadata.find(CScriptID(spk));
686
60
        if (mit != m_script_metadata.end()) {
687
43
            creation_time = mit->second.nCreateTime;
688
43
        }
689
690
        // InferDescriptor as that will get us all the solving info if it is there
691
60
        std::unique_ptr<Descriptor> desc = InferDescriptor(spk, *GetSolvingProvider(spk));
692
693
        // Past bugs in InferDescriptor have caused it to create descriptors which cannot be re-parsed.
694
        // Re-parse the descriptors to detect that, and skip any that do not parse.
695
60
        {
696
60
            std::string desc_str = desc->ToString();
697
60
            FlatSigningProvider parsed_keys;
698
60
            std::string parse_error;
699
60
            std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str, parsed_keys, parse_error);
700
60
            if (parsed_descs.empty()) {
701
                // Remove this scriptPubKey from the set
702
0
                it = spks.erase(it);
703
0
                continue;
704
0
            }
705
60
        }
706
707
        // Get the private keys for this descriptor
708
60
        std::vector<CScript> scripts;
709
60
        FlatSigningProvider keys;
710
60
        if (!desc->Expand(0, DUMMY_SIGNING_PROVIDER, scripts, keys)) {
711
0
            assert(false);
712
0
        }
713
60
        std::set<CKeyID> privkeyids;
714
60
        for (const auto& key_orig_pair : keys.origins) {
715
52
            privkeyids.insert(key_orig_pair.first);
716
52
        }
717
718
60
        std::vector<CScript> desc_spks;
719
720
        // If we can't provide all private keys for this inferred descriptor,
721
        // but this wallet is not watch-only, migrate it to the watch-only wallet.
722
60
        if (!desc->HavePrivateKeys(*this) && !m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
723
39
            out.watch_descs.emplace_back(desc->ToString(), creation_time);
724
725
            // Get the scriptPubKeys without writing this to the wallet
726
39
            FlatSigningProvider provider;
727
39
            desc->Expand(0, provider, desc_spks, provider);
728
39
        } else {
729
            // Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
730
27
            for (const auto& keyid : privkeyids) {
731
27
                CKey key;
732
27
                if (!GetKey(keyid, key)) {
733
9
                    continue;
734
9
                }
735
18
                keys.keys.emplace(key.GetPubKey().GetID(), key);
736
18
            }
737
21
            WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
738
21
            auto desc_spk_man = DescriptorScriptPubKeyMan::CreateFromMigration(m_storage, batch, w_desc, /*keypool_size=*/0, keys);
739
21
            auto desc_spks_set = desc_spk_man->GetScriptPubKeys();
740
21
            desc_spks.insert(desc_spks.end(), desc_spks_set.begin(), desc_spks_set.end());
741
742
21
            out.desc_spkms.push_back(std::move(desc_spk_man));
743
21
        }
744
745
        // Remove the scriptPubKeys from our current set
746
60
        for (const CScript& desc_spk : desc_spks) {
747
60
            auto del_it = spks.find(desc_spk);
748
60
            assert(del_it != spks.end());
749
60
            assert(IsMine(desc_spk));
750
60
            it = spks.erase(del_it);
751
60
        }
752
60
    }
753
754
    // Make sure that we have accounted for all scriptPubKeys
755
38
    if (!Assume(spks.empty())) {
756
0
        LogError("%s", STR_INTERNAL_BUG("Error: Some output scripts were not migrated."));
757
0
        return std::nullopt;
758
0
    }
759
760
    // Legacy wallets can also contain scripts whose P2SH, P2WSH, or P2SH-P2WSH it is not watching for
761
    // but can provide script data to a PSBT spending them. These "solvable" output scripts will need to
762
    // be put into the separate "solvables" wallet.
763
    // These can be detected by going through the entire candidate output scripts, finding the not IsMine scripts,
764
    // and checking CanProvide() which will dummy sign.
765
1.41k
    for (const CScript& script : GetCandidateScriptPubKeys()) {
766
        // Since we only care about P2SH, P2WSH, and P2SH-P2WSH, filter out any scripts that are not those
767
1.41k
        if (!script.IsPayToScriptHash() && !script.IsPayToWitnessScriptHash()) {
768
633
            continue;
769
633
        }
770
779
        if (IsMine(script)) {
771
224
            continue;
772
224
        }
773
555
        SignatureData dummy_sigdata;
774
555
        if (!CanProvide(script, dummy_sigdata)) {
775
530
            continue;
776
530
        }
777
778
        // Get birthdate from script meta
779
25
        uint64_t creation_time = 0;
780
25
        const auto& it = m_script_metadata.find(CScriptID(script));
781
25
        if (it != m_script_metadata.end()) {
782
4
            creation_time = it->second.nCreateTime;
783
4
        }
784
785
        // InferDescriptor as that will get us all the solving info if it is there
786
25
        std::unique_ptr<Descriptor> desc = InferDescriptor(script, *GetSolvingProvider(script));
787
25
        if (!desc->IsSolvable()) {
788
            // The wallet was able to provide some information, but not enough to make a descriptor that actually
789
            // contains anything useful. This is probably because the script itself is actually unsignable (e.g. P2WSH-P2WSH).
790
10
            continue;
791
10
        }
792
793
        // Past bugs in InferDescriptor have caused it to create descriptors which cannot be re-parsed
794
        // Re-parse the descriptors to detect that, and skip any that do not parse.
795
15
        {
796
15
            std::string desc_str = desc->ToString();
797
15
            FlatSigningProvider parsed_keys;
798
15
            std::string parse_error;
799
15
            std::vector<std::unique_ptr<Descriptor>> parsed_descs = Parse(desc_str, parsed_keys, parse_error, false);
800
15
            if (parsed_descs.empty()) {
801
0
                continue;
802
0
            }
803
15
        }
804
805
15
        out.solvable_descs.emplace_back(desc->ToString(), creation_time);
806
15
    }
807
808
    // Finalize transaction
809
38
    if (!batch.TxnCommit()) {
810
0
        LogWarning("Error generating descriptors for migration, cannot commit db transaction");
811
0
        return std::nullopt;
812
0
    }
813
814
38
    return out;
815
38
}
816
817
bool LegacyDataSPKM::DeleteRecordsWithDB(WalletBatch& batch)
818
34
{
819
34
    LOCK(cs_KeyStore);
820
34
    return batch.EraseRecords(DBKeys::LEGACY_TYPES);
821
34
}
822
823
std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::CreateFromImport(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider)
824
830
{
825
830
    auto spkm = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, descriptor, keypool_size));
826
830
    LOCK(spkm->cs_desc_man);
827
830
    WalletBatch batch(storage.GetDatabase());
828
830
    spkm->UpdateWithSigningProvider(batch, provider);
829
830
    return spkm;
830
830
}
831
832
std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::CreateFromMigration(WalletStorage& storage, WalletBatch& batch, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider)
833
122
{
834
122
    auto spkm = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, descriptor, keypool_size));
835
122
    LOCK(spkm->cs_desc_man);
836
122
    spkm->UpdateWithSigningProvider(batch, provider);
837
122
    return spkm;
838
122
}
839
840
DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
841
2.52k
    : ScriptPubKeyMan(storage),
842
2.52k
    m_map_keys(keys),
843
2.52k
    m_map_crypted_keys(ckeys),
844
2.52k
    m_keypool_size(keypool_size),
845
2.52k
    m_wallet_descriptor(descriptor)
846
2.52k
{
847
2.52k
    if (!keys.empty() && !ckeys.empty()) {
848
1
        throw std::runtime_error("Wallet contains both unencrypted and encrypted keys");
849
1
    }
850
2.52k
    Load();
851
2.52k
}
852
853
std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::LoadFromStorage(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
854
2.51k
{
855
2.51k
    return std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, descriptor, keypool_size, keys, ckeys));
856
2.51k
}
857
858
std::unique_ptr<DescriptorScriptPubKeyMan> DescriptorScriptPubKeyMan::GenerateNewSingleSig(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal)
859
3.69k
{
860
3.69k
    auto spkm = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(storage, keypool_size));
861
3.69k
    spkm->SetupDescriptorGeneration(batch, master_key, addr_type, internal);
862
3.69k
    return spkm;
863
3.69k
}
864
865
util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
866
19.2k
{
867
    // Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
868
19.2k
    if (!CanGetAddresses()) {
869
0
        return util::Error{_("No addresses available")};
870
0
    }
871
19.2k
    {
872
19.2k
        LOCK(cs_desc_man);
873
19.2k
        assert(m_wallet_descriptor.descriptor->IsSingleType()); // This is a combo descriptor which should not be an active descriptor
874
19.2k
        std::optional<OutputType> desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType();
875
19.2k
        assert(desc_addr_type);
876
19.2k
        if (type != *desc_addr_type) {
877
0
            throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
878
0
        }
879
880
19.2k
        TopUp();
881
882
        // Get the scriptPubKey from the descriptor
883
19.2k
        FlatSigningProvider out_keys;
884
19.2k
        std::vector<CScript> scripts_temp;
885
19.2k
        if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
886
            // We can't generate anymore keys
887
0
            return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
888
0
        }
889
19.2k
        if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
890
            // We can't generate anymore keys
891
8
            return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
892
8
        }
893
894
19.2k
        CTxDestination dest;
895
19.2k
        if (!ExtractDestination(scripts_temp[0], dest)) {
896
0
            return util::Error{_("Error: Cannot extract destination from the generated scriptpubkey")}; // shouldn't happen
897
0
        }
898
19.2k
        m_wallet_descriptor.next_index++;
899
19.2k
        WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
900
19.2k
        return dest;
901
19.2k
    }
902
19.2k
}
903
904
bool DescriptorScriptPubKeyMan::IsMine(const CScript& script) const
905
618k
{
906
618k
    LOCK(cs_desc_man);
907
618k
    return m_map_script_pub_keys.contains(script);
908
618k
}
909
910
bool DescriptorScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key)
911
719
{
912
719
    LOCK(cs_desc_man);
913
719
    if (!m_map_keys.empty()) {
914
0
        return false;
915
0
    }
916
917
719
    bool keyPass = m_map_crypted_keys.empty(); // Always pass when there are no encrypted keys
918
719
    bool keyFail = false;
919
719
    for (const auto& mi : m_map_crypted_keys) {
920
719
        const CPubKey &pubkey = mi.second.first;
921
719
        const std::vector<unsigned char> &crypted_secret = mi.second.second;
922
719
        CKey key;
923
719
        if (!DecryptKey(master_key, crypted_secret, pubkey, key)) {
924
0
            keyFail = true;
925
0
            break;
926
0
        }
927
719
        keyPass = true;
928
719
        if (m_decryption_thoroughly_checked)
929
521
            break;
930
719
    }
931
719
    if (keyPass && keyFail) {
932
0
        LogWarning("The wallet is probably corrupted: Some keys decrypt but not all.");
933
0
        throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
934
0
    }
935
719
    if (keyFail || !keyPass) {
936
0
        return false;
937
0
    }
938
719
    m_decryption_thoroughly_checked = true;
939
719
    return true;
940
719
}
941
942
bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch)
943
103
{
944
103
    LOCK(cs_desc_man);
945
103
    if (!m_map_crypted_keys.empty()) {
946
0
        return false;
947
0
    }
948
949
103
    for (const KeyMap::value_type& key_in : m_map_keys)
950
103
    {
951
103
        const CKey &key = key_in.second;
952
103
        CPubKey pubkey = key.GetPubKey();
953
103
        CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
954
103
        std::vector<unsigned char> crypted_secret;
955
103
        if (!EncryptSecret(master_key, secret, pubkey.GetHash(), crypted_secret)) {
956
0
            return false;
957
0
        }
958
103
        m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
959
103
        batch->WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
960
103
    }
961
103
    m_map_keys.clear();
962
103
    return true;
963
103
}
964
965
util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, int64_t& index)
966
2.10k
{
967
2.10k
    LOCK(cs_desc_man);
968
2.10k
    auto op_dest = GetNewDestination(type);
969
2.10k
    index = m_wallet_descriptor.next_index - 1;
970
2.10k
    return op_dest;
971
2.10k
}
972
973
void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr)
974
105
{
975
105
    LOCK(cs_desc_man);
976
    // Only return when the index was the most recent
977
105
    if (m_wallet_descriptor.next_index - 1 == index) {
978
105
        m_wallet_descriptor.next_index--;
979
105
    }
980
105
    WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
981
105
    NotifyCanGetAddressesChanged();
982
105
}
983
984
std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
985
91.8k
{
986
91.8k
    AssertLockHeld(cs_desc_man);
987
91.8k
    if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
988
2.28k
        KeyMap keys;
989
2.28k
        for (const auto& key_pair : m_map_crypted_keys) {
990
2.28k
            const CPubKey& pubkey = key_pair.second.first;
991
2.28k
            const std::vector<unsigned char>& crypted_secret = key_pair.second.second;
992
2.28k
            CKey key;
993
2.28k
            m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
994
2.28k
                return DecryptKey(encryption_key, crypted_secret, pubkey, key);
995
2.28k
            });
996
2.28k
            keys[pubkey.GetID()] = key;
997
2.28k
        }
998
2.28k
        return keys;
999
2.28k
    }
1000
89.5k
    return m_map_keys;
1001
91.8k
}
1002
1003
bool DescriptorScriptPubKeyMan::HasPrivKey(const CKeyID& keyid) const
1004
271
{
1005
271
    AssertLockHeld(cs_desc_man);
1006
271
    return m_map_keys.contains(keyid) || m_map_crypted_keys.contains(keyid);
1007
271
}
1008
1009
std::optional<CKey> DescriptorScriptPubKeyMan::GetKey(const CKeyID& keyid) const
1010
99
{
1011
99
    AssertLockHeld(cs_desc_man);
1012
99
    if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
1013
9
        const auto& it = m_map_crypted_keys.find(keyid);
1014
9
        if (it == m_map_crypted_keys.end()) {
1015
0
            return std::nullopt;
1016
0
        }
1017
9
        const std::vector<unsigned char>& crypted_secret = it->second.second;
1018
9
        CKey key;
1019
9
        if (!Assume(m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
1020
9
            return DecryptKey(encryption_key, crypted_secret, it->second.first, key);
1021
9
        }))) {
1022
0
            return std::nullopt;
1023
0
        }
1024
9
        return key;
1025
9
    }
1026
90
    const auto& it = m_map_keys.find(keyid);
1027
90
    if (it == m_map_keys.end()) {
1028
2
        return std::nullopt;
1029
2
    }
1030
88
    return it->second;
1031
90
}
1032
1033
bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
1034
72.5k
{
1035
72.5k
    WalletBatch batch(m_storage.GetDatabase());
1036
72.5k
    if (!batch.TxnBegin()) return false;
1037
72.5k
    bool res = TopUpWithDB(batch, size);
1038
72.5k
    if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during descriptors keypool top up. Cannot commit changes for wallet [%s]", m_storage.LogName()));
1039
72.5k
    return res;
1040
72.5k
}
1041
1042
bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
1043
77.2k
{
1044
77.2k
    LOCK(cs_desc_man);
1045
77.2k
    std::set<CScript> new_spks;
1046
77.2k
    unsigned int target_size;
1047
77.2k
    if (size > 0) {
1048
72
        target_size = size;
1049
77.1k
    } else {
1050
77.1k
        target_size = m_keypool_size;
1051
77.1k
    }
1052
1053
    // Calculate the new range_end
1054
77.2k
    int32_t new_range_end = std::max(m_wallet_descriptor.next_index + (int32_t)target_size, m_wallet_descriptor.range_end);
1055
1056
    // If the descriptor is not ranged, we actually just want to fill the first cache item
1057
77.2k
    if (!m_wallet_descriptor.descriptor->IsRange()) {
1058
11.6k
        new_range_end = 1;
1059
11.6k
        m_wallet_descriptor.range_end = 1;
1060
11.6k
        m_wallet_descriptor.range_start = 0;
1061
11.6k
    }
1062
1063
77.2k
    FlatSigningProvider provider;
1064
77.2k
    provider.keys = GetKeys();
1065
1066
77.2k
    uint256 id = GetID();
1067
490k
    for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
1068
413k
        FlatSigningProvider out_keys;
1069
413k
        std::vector<CScript> scripts_temp;
1070
413k
        DescriptorCache temp_cache;
1071
        // Maybe we have a cached xpub and we can expand from the cache first
1072
413k
        if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
1073
14.3k
            if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false;
1074
14.3k
        }
1075
        // Add all of the scriptPubKeys to the scriptPubKey set
1076
413k
        new_spks.insert(scripts_temp.begin(), scripts_temp.end());
1077
414k
        for (const CScript& script : scripts_temp) {
1078
414k
            m_map_script_pub_keys[script] = i;
1079
414k
        }
1080
461k
        for (const auto& pk_pair : out_keys.pubkeys) {
1081
461k
            const CPubKey& pubkey = pk_pair.second;
1082
461k
            if (m_map_pubkeys.contains(pubkey)) {
1083
                // We don't need to give an error here.
1084
                // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key
1085
10.5k
                continue;
1086
10.5k
            }
1087
450k
            m_map_pubkeys[pubkey] = i;
1088
450k
        }
1089
        // Merge and write the cache
1090
413k
        DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
1091
413k
        if (!batch.WriteDescriptorCacheItems(id, new_items)) {
1092
0
            throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
1093
0
        }
1094
413k
        m_max_cached_index++;
1095
413k
    }
1096
77.1k
    m_wallet_descriptor.range_end = new_range_end;
1097
77.1k
    batch.WriteDescriptor(GetID(), m_wallet_descriptor);
1098
1099
    // By this point, the cache size should be the size of the entire range
1100
77.1k
    assert(m_wallet_descriptor.range_end - 1 == m_max_cached_index);
1101
1102
77.1k
    m_storage.TopUpCallback(new_spks, this);
1103
77.1k
    NotifyCanGetAddressesChanged();
1104
77.1k
    return true;
1105
77.1k
}
1106
1107
std::vector<WalletDestination> DescriptorScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
1108
47.7k
{
1109
47.7k
    LOCK(cs_desc_man);
1110
47.7k
    std::vector<WalletDestination> result;
1111
47.7k
    if (IsMine(script)) {
1112
47.7k
        int32_t index = m_map_script_pub_keys[script];
1113
47.7k
        if (index >= m_wallet_descriptor.next_index) {
1114
496
            WalletLogPrintf("%s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n", __func__, index);
1115
496
            auto out_keys = std::make_unique<FlatSigningProvider>();
1116
496
            std::vector<CScript> scripts_temp;
1117
24.9k
            while (index >= m_wallet_descriptor.next_index) {
1118
24.4k
                if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) {
1119
0
                    throw std::runtime_error(std::string(__func__) + ": Unable to expand descriptor from cache");
1120
0
                }
1121
24.4k
                CTxDestination dest;
1122
24.4k
                ExtractDestination(scripts_temp[0], dest);
1123
24.4k
                result.push_back({dest, std::nullopt});
1124
24.4k
                m_wallet_descriptor.next_index++;
1125
24.4k
            }
1126
496
        }
1127
47.7k
        if (!TopUp()) {
1128
0
            WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
1129
0
        }
1130
47.7k
    }
1131
1132
47.7k
    return result;
1133
47.7k
}
1134
1135
void DescriptorScriptPubKeyMan::AddDescriptorKey(const CKey& key, const CPubKey &pubkey)
1136
0
{
1137
0
    LOCK(cs_desc_man);
1138
0
    WalletBatch batch(m_storage.GetDatabase());
1139
0
    if (!AddDescriptorKeyWithDB(batch, key, pubkey)) {
1140
0
        throw std::runtime_error(std::string(__func__) + ": writing descriptor private key failed");
1141
0
    }
1142
0
}
1143
1144
bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey)
1145
4.46k
{
1146
4.46k
    AssertLockHeld(cs_desc_man);
1147
4.46k
    assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
1148
1149
    // Check if provided key already exists
1150
4.46k
    if (m_map_keys.contains(pubkey.GetID()) ||
1151
4.46k
        m_map_crypted_keys.contains(pubkey.GetID())) {
1152
8
        return true;
1153
8
    }
1154
1155
4.45k
    if (m_storage.HasEncryptionKeys()) {
1156
162
        if (m_storage.IsLocked()) {
1157
0
            return false;
1158
0
        }
1159
1160
162
        std::vector<unsigned char> crypted_secret;
1161
162
        CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
1162
162
        if (!m_storage.WithEncryptionKey([&](const CKeyingMaterial& encryption_key) {
1163
162
                return EncryptSecret(encryption_key, secret, pubkey.GetHash(), crypted_secret);
1164
162
            })) {
1165
0
            return false;
1166
0
        }
1167
1168
162
        m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
1169
162
        return batch.WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
1170
4.29k
    } else {
1171
4.29k
        m_map_keys[pubkey.GetID()] = key;
1172
4.29k
        return batch.WriteDescriptorKey(GetID(), pubkey, key.GetPrivKey());
1173
4.29k
    }
1174
4.45k
}
1175
1176
void DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal)
1177
3.69k
{
1178
3.69k
    LOCK(cs_desc_man);
1179
3.69k
    Assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
1180
3.69k
    Assert(!m_wallet_descriptor.descriptor);
1181
1182
3.69k
    m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal);
1183
1184
    // Store the master private key, and descriptor
1185
3.69k
    if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
1186
0
        throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
1187
0
    }
1188
3.69k
    if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
1189
0
        throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
1190
0
    }
1191
1192
    // Set m_decryption_thoroughly_checked for encrypted wallets
1193
3.69k
    if (m_storage.HasEncryptionKeys()) {
1194
138
        m_decryption_thoroughly_checked = true;
1195
138
    }
1196
1197
    // TopUp
1198
3.69k
    TopUpWithDB(batch);
1199
1200
3.69k
    m_storage.UnsetBlankWalletFlag(batch);
1201
3.69k
}
1202
1203
bool DescriptorScriptPubKeyMan::IsHDEnabled() const
1204
68
{
1205
68
    LOCK(cs_desc_man);
1206
68
    return m_wallet_descriptor.descriptor->IsRange();
1207
68
}
1208
1209
bool DescriptorScriptPubKeyMan::CanGetAddresses(bool internal) const
1210
30.6k
{
1211
    // We can only give out addresses from descriptors that are single type (not combo), ranged,
1212
    // and either have cached keys or can generate more keys (ignoring encryption)
1213
30.6k
    LOCK(cs_desc_man);
1214
30.6k
    return m_wallet_descriptor.descriptor->IsSingleType() &&
1215
30.6k
           m_wallet_descriptor.descriptor->IsRange() &&
1216
30.6k
           (HavePrivateKeys() || m_wallet_descriptor.next_index < m_wallet_descriptor.range_end);
1217
30.6k
}
1218
1219
bool DescriptorScriptPubKeyMan::HavePrivateKeys() const
1220
337k
{
1221
337k
    LOCK(cs_desc_man);
1222
337k
    return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0;
1223
337k
}
1224
1225
bool DescriptorScriptPubKeyMan::HaveCryptedKeys() const
1226
0
{
1227
0
    LOCK(cs_desc_man);
1228
0
    return !m_map_crypted_keys.empty();
1229
0
}
1230
1231
unsigned int DescriptorScriptPubKeyMan::GetKeyPoolSize() const
1232
9.27k
{
1233
9.27k
    LOCK(cs_desc_man);
1234
9.27k
    return m_wallet_descriptor.range_end - m_wallet_descriptor.next_index;
1235
9.27k
}
1236
1237
int64_t DescriptorScriptPubKeyMan::GetTimeFirstKey() const
1238
7.17k
{
1239
7.17k
    LOCK(cs_desc_man);
1240
7.17k
    return m_wallet_descriptor.creation_time;
1241
7.17k
}
1242
1243
std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(const CScript& script, bool include_private) const
1244
433k
{
1245
433k
    LOCK(cs_desc_man);
1246
1247
    // Find the index of the script
1248
433k
    auto it = m_map_script_pub_keys.find(script);
1249
433k
    if (it == m_map_script_pub_keys.end()) {
1250
127k
        return nullptr;
1251
127k
    }
1252
305k
    int32_t index = it->second;
1253
1254
305k
    return GetSigningProvider(index, include_private);
1255
433k
}
1256
1257
std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(const CPubKey& pubkey) const
1258
35.9k
{
1259
35.9k
    LOCK(cs_desc_man);
1260
1261
    // Find index of the pubkey
1262
35.9k
    auto it = m_map_pubkeys.find(pubkey);
1263
35.9k
    if (it == m_map_pubkeys.end()) {
1264
35.0k
        return nullptr;
1265
35.0k
    }
1266
949
    int32_t index = it->second;
1267
1268
    // Always try to get the signing provider with private keys. This function should only be called during signing anyways
1269
949
    std::unique_ptr<FlatSigningProvider> out = GetSigningProvider(index, true);
1270
949
    if (!out->HaveKey(pubkey.GetID())) {
1271
561
        return nullptr;
1272
561
    }
1273
388
    return out;
1274
949
}
1275
1276
std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(int32_t index, bool include_private) const
1277
306k
{
1278
306k
    AssertLockHeld(cs_desc_man);
1279
1280
306k
    std::unique_ptr<FlatSigningProvider> out_keys = std::make_unique<FlatSigningProvider>();
1281
1282
    // Fetch SigningProvider from cache to avoid re-deriving
1283
306k
    auto it = m_map_signing_providers.find(index);
1284
306k
    if (it != m_map_signing_providers.end()) {
1285
290k
        out_keys->Merge(FlatSigningProvider{it->second});
1286
290k
    } else {
1287
        // Get the scripts, keys, and key origins for this script
1288
16.2k
        std::vector<CScript> scripts_temp;
1289
16.2k
        if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr;
1290
1291
        // Cache SigningProvider so we don't need to re-derive if we need this SigningProvider again
1292
16.2k
        m_map_signing_providers[index] = *out_keys;
1293
16.2k
    }
1294
1295
306k
    if (HavePrivateKeys() && include_private) {
1296
12.2k
        FlatSigningProvider master_provider;
1297
12.2k
        master_provider.keys = GetKeys();
1298
12.2k
        m_wallet_descriptor.descriptor->ExpandPrivate(index, master_provider, *out_keys);
1299
1300
        // Always include musig_secnonces as this descriptor may have a participant private key
1301
        // but not a musig() descriptor
1302
12.2k
        out_keys->musig2_secnonces = &m_musig2_secnonces;
1303
12.2k
    }
1304
1305
306k
    return out_keys;
1306
306k
}
1307
1308
std::unique_ptr<SigningProvider> DescriptorScriptPubKeyMan::GetSolvingProvider(const CScript& script) const
1309
366k
{
1310
366k
    return GetSigningProvider(script, false);
1311
366k
}
1312
1313
bool DescriptorScriptPubKeyMan::CanProvide(const CScript& script, SignatureData& sigdata)
1314
376k
{
1315
376k
    return IsMine(script);
1316
376k
}
1317
1318
bool DescriptorScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const
1319
17.2k
{
1320
17.2k
    std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
1321
43.0k
    for (const auto& coin_pair : coins) {
1322
43.0k
        std::unique_ptr<FlatSigningProvider> coin_keys = GetSigningProvider(coin_pair.second.out.scriptPubKey, true);
1323
43.0k
        if (!coin_keys) {
1324
33.4k
            continue;
1325
33.4k
        }
1326
9.62k
        keys->Merge(std::move(*coin_keys));
1327
9.62k
    }
1328
1329
17.2k
    return ::SignTransaction(tx, keys.get(), coins, {.sighash_type = sighash}, input_errors);
1330
17.2k
}
1331
1332
SigningResult DescriptorScriptPubKeyMan::SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const
1333
9
{
1334
9
    std::unique_ptr<FlatSigningProvider> keys = GetSigningProvider(GetScriptForDestination(pkhash), true);
1335
9
    if (!keys) {
1336
0
        return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
1337
0
    }
1338
1339
9
    CKey key;
1340
9
    if (!keys->GetKey(ToKeyID(pkhash), key)) {
1341
0
        return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
1342
0
    }
1343
1344
9
    if (!MessageSign(key, message, str_sig)) {
1345
0
        return SigningResult::SIGNING_FAILED;
1346
0
    }
1347
9
    return SigningResult::OK;
1348
9
}
1349
1350
std::optional<PSBTError> DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, const PrecomputedTransactionData& txdata, const common::PSBTFillOptions& options, int* n_signed) const
1351
8.30k
{
1352
8.30k
    if (n_signed) {
1353
8.30k
        *n_signed = 0;
1354
8.30k
    }
1355
38.7k
    for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
1356
30.4k
        PSBTInput& input = psbtx.inputs.at(i);
1357
1358
30.4k
        if (PSBTInputSigned(input)) {
1359
6.87k
            continue;
1360
6.87k
        }
1361
1362
        // Get the scriptPubKey to know which SigningProvider to use
1363
23.5k
        CScript script;
1364
23.5k
        if (!input.witness_utxo.IsNull()) {
1365
17.3k
            script = input.witness_utxo.scriptPubKey;
1366
17.3k
        } else if (input.non_witness_utxo) {
1367
6.03k
            if (input.prev_out >= input.non_witness_utxo->vout.size()) {
1368
1
                return PSBTError::MISSING_INPUTS;
1369
1
            }
1370
6.02k
            script = input.non_witness_utxo->vout[input.prev_out].scriptPubKey;
1371
6.02k
        } else {
1372
            // There's no UTXO so we can just skip this now
1373
237
            continue;
1374
237
        }
1375
1376
23.3k
        std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
1377
23.3k
        std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, /*include_private=*/options.sign);
1378
23.3k
        if (script_keys) {
1379
3.50k
            keys->Merge(std::move(*script_keys));
1380
19.8k
        } else {
1381
            // Maybe there are pubkeys listed that we can sign for
1382
19.8k
            std::vector<CPubKey> pubkeys;
1383
19.8k
            pubkeys.reserve(input.hd_keypaths.size() + 2);
1384
1385
            // ECDSA Pubkeys
1386
19.8k
            for (const auto& [pk, _] : input.hd_keypaths) {
1387
12.6k
                pubkeys.push_back(pk);
1388
12.6k
            }
1389
1390
            // Taproot output pubkey
1391
19.8k
            std::vector<std::vector<unsigned char>> sols;
1392
19.8k
            if (Solver(script, sols) == TxoutType::WITNESS_V1_TAPROOT) {
1393
2.54k
                sols[0].insert(sols[0].begin(), 0x02);
1394
2.54k
                pubkeys.emplace_back(sols[0]);
1395
2.54k
                sols[0][0] = 0x03;
1396
2.54k
                pubkeys.emplace_back(sols[0]);
1397
2.54k
            }
1398
1399
            // Taproot pubkeys
1400
19.8k
            for (const auto& pk_pair : input.m_tap_bip32_paths) {
1401
9.11k
                const XOnlyPubKey& pubkey = pk_pair.first;
1402
18.2k
                for (unsigned char prefix : {0x02, 0x03}) {
1403
18.2k
                    unsigned char b[33] = {prefix};
1404
18.2k
                    std::copy(pubkey.begin(), pubkey.end(), b + 1);
1405
18.2k
                    CPubKey fullpubkey;
1406
18.2k
                    fullpubkey.Set(b, b + 33);
1407
18.2k
                    pubkeys.push_back(fullpubkey);
1408
18.2k
                }
1409
9.11k
            }
1410
1411
35.9k
            for (const auto& pubkey : pubkeys) {
1412
35.9k
                std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey);
1413
35.9k
                if (pk_keys) {
1414
387
                    keys->Merge(std::move(*pk_keys));
1415
387
                }
1416
35.9k
            }
1417
19.8k
        }
1418
1419
23.3k
        PSBTError res = SignPSBTInput(HidingSigningProvider(keys.get(), /*hide_secret=*/!options.sign, /*hide_origin=*/!options.bip32_derivs), psbtx, i, &txdata, options, /*out_sigdata=*/nullptr);
1420
23.3k
        if (res != PSBTError::OK && res != PSBTError::INCOMPLETE) {
1421
7
            return res;
1422
7
        }
1423
1424
23.3k
        bool signed_one = PSBTInputSigned(input);
1425
23.3k
        if (n_signed && (signed_one || !options.sign)) {
1426
            // If sign is false, we assume that we _could_ sign if we get here. This
1427
            // will never have false negatives; it is hard to tell under what i
1428
            // circumstances it could have false positives.
1429
15.8k
            (*n_signed)++;
1430
15.8k
        }
1431
23.3k
    }
1432
1433
    // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
1434
83.2k
    for (unsigned int i = 0; i < psbtx.outputs.size(); ++i) {
1435
74.9k
        std::unique_ptr<SigningProvider> keys = GetSolvingProvider(psbtx.outputs.at(i).script);
1436
74.9k
        if (!keys) {
1437
74.0k
            continue;
1438
74.0k
        }
1439
945
        UpdatePSBTOutput(HidingSigningProvider(keys.get(), /*hide_secret=*/true, /*hide_origin=*/!options.bip32_derivs), psbtx, i);
1440
945
    }
1441
1442
8.29k
    return {};
1443
8.30k
}
1444
1445
std::unique_ptr<CKeyMetadata> DescriptorScriptPubKeyMan::GetMetadata(const CTxDestination& dest) const
1446
659
{
1447
659
    std::unique_ptr<SigningProvider> provider = GetSigningProvider(GetScriptForDestination(dest));
1448
659
    if (provider) {
1449
659
        KeyOriginInfo orig;
1450
659
        CKeyID key_id = GetKeyForDestination(*provider, dest);
1451
659
        if (provider->GetKeyOrigin(key_id, orig)) {
1452
573
            LOCK(cs_desc_man);
1453
573
            std::unique_ptr<CKeyMetadata> meta = std::make_unique<CKeyMetadata>();
1454
573
            meta->key_origin = orig;
1455
573
            meta->has_key_origin = true;
1456
573
            meta->nCreateTime = m_wallet_descriptor.creation_time;
1457
573
            return meta;
1458
573
        }
1459
659
    }
1460
86
    return nullptr;
1461
659
}
1462
1463
uint256 DescriptorScriptPubKeyMan::GetID() const
1464
188k
{
1465
188k
    LOCK(cs_desc_man);
1466
188k
    return m_wallet_descriptor.id;
1467
188k
}
1468
1469
void DescriptorScriptPubKeyMan::Load()
1470
2.52k
{
1471
2.52k
    LOCK(cs_desc_man);
1472
2.52k
    std::set<CScript> new_spks;
1473
62.9k
    for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
1474
60.4k
        FlatSigningProvider out_keys;
1475
60.4k
        std::vector<CScript> scripts_temp;
1476
60.4k
        if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
1477
0
            throw std::runtime_error("Error: Unable to expand wallet descriptor from cache");
1478
0
        }
1479
        // Add all of the scriptPubKeys to the scriptPubKey set
1480
60.4k
        new_spks.insert(scripts_temp.begin(), scripts_temp.end());
1481
61.4k
        for (const CScript& script : scripts_temp) {
1482
61.4k
            if (m_map_script_pub_keys.contains(script)) {
1483
0
                throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
1484
0
            }
1485
61.4k
            m_map_script_pub_keys[script] = i;
1486
61.4k
        }
1487
64.4k
        for (const auto& pk_pair : out_keys.pubkeys) {
1488
64.4k
            const CPubKey& pubkey = pk_pair.second;
1489
64.4k
            if (m_map_pubkeys.contains(pubkey)) {
1490
                // We don't need to give an error here.
1491
                // It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and its private key
1492
0
                continue;
1493
0
            }
1494
64.4k
            m_map_pubkeys[pubkey] = i;
1495
64.4k
        }
1496
60.4k
        m_max_cached_index++;
1497
60.4k
    }
1498
    // Make sure the wallet knows about our new spks
1499
2.52k
    m_storage.TopUpCallback(new_spks, this);
1500
2.52k
}
1501
1502
bool DescriptorScriptPubKeyMan::HasWalletDescriptor(const WalletDescriptor& desc) const
1503
43
{
1504
43
    LOCK(cs_desc_man);
1505
43
    return !m_wallet_descriptor.id.IsNull() && !desc.id.IsNull() && m_wallet_descriptor.id == desc.id;
1506
43
}
1507
1508
void DescriptorScriptPubKeyMan::WriteDescriptor()
1509
847
{
1510
847
    LOCK(cs_desc_man);
1511
847
    WalletBatch batch(m_storage.GetDatabase());
1512
847
    if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
1513
0
        throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
1514
0
    }
1515
847
}
1516
1517
WalletDescriptor DescriptorScriptPubKeyMan::GetWalletDescriptor() const
1518
55.6k
{
1519
55.6k
    return m_wallet_descriptor;
1520
55.6k
}
1521
1522
std::unordered_set<CScript, SaltedSipHasher> DescriptorScriptPubKeyMan::GetScriptPubKeys() const
1523
451
{
1524
451
    return GetScriptPubKeys(0);
1525
451
}
1526
1527
std::unordered_set<CScript, SaltedSipHasher> DescriptorScriptPubKeyMan::GetScriptPubKeys(int32_t minimum_index) const
1528
551
{
1529
551
    LOCK(cs_desc_man);
1530
551
    std::unordered_set<CScript, SaltedSipHasher> script_pub_keys;
1531
551
    script_pub_keys.reserve(m_map_script_pub_keys.size());
1532
1533
27.2k
    for (auto const& [script_pub_key, index] : m_map_script_pub_keys) {
1534
27.2k
        if (index >= minimum_index) script_pub_keys.insert(script_pub_key);
1535
27.2k
    }
1536
551
    return script_pub_keys;
1537
551
}
1538
1539
int32_t DescriptorScriptPubKeyMan::GetEndRange() const
1540
4.95k
{
1541
4.95k
    return m_max_cached_index + 1;
1542
4.95k
}
1543
1544
bool DescriptorScriptPubKeyMan::GetDescriptorString(std::string& out, const bool priv) const
1545
2.33k
{
1546
2.33k
    LOCK(cs_desc_man);
1547
1548
2.33k
    FlatSigningProvider provider;
1549
2.33k
    provider.keys = GetKeys();
1550
1551
2.33k
    if (priv) {
1552
        // For the private version, always return the master key to avoid
1553
        // exposing child private keys. The risk implications of exposing child
1554
        // private keys together with the parent xpub may be non-obvious for users.
1555
637
        return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
1556
637
    }
1557
1558
1.70k
    return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
1559
2.33k
}
1560
1561
void DescriptorScriptPubKeyMan::UpgradeDescriptorCache()
1562
44
{
1563
44
    LOCK(cs_desc_man);
1564
44
    if (m_storage.IsLocked() || m_storage.IsWalletFlagSet(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED)) {
1565
0
        return;
1566
0
    }
1567
1568
    // Skip if we have the last hardened xpub cache
1569
44
    if (m_wallet_descriptor.cache.GetCachedLastHardenedExtPubKeys().size() > 0) {
1570
38
        return;
1571
38
    }
1572
1573
    // Expand the descriptor
1574
6
    FlatSigningProvider provider;
1575
6
    provider.keys = GetKeys();
1576
6
    FlatSigningProvider out_keys;
1577
6
    std::vector<CScript> scripts_temp;
1578
6
    DescriptorCache temp_cache;
1579
6
    if (!m_wallet_descriptor.descriptor->Expand(0, provider, scripts_temp, out_keys, &temp_cache)){
1580
0
        throw std::runtime_error("Unable to expand descriptor");
1581
0
    }
1582
1583
    // Cache the last hardened xpubs
1584
6
    DescriptorCache diff = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
1585
6
    if (!WalletBatch(m_storage.GetDatabase()).WriteDescriptorCacheItems(GetID(), diff)) {
1586
0
        throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
1587
0
    }
1588
6
}
1589
1590
util::Result<void> DescriptorScriptPubKeyMan::UpdateWalletDescriptor(WalletDescriptor& descriptor, const FlatSigningProvider& provider)
1591
21
{
1592
21
    LOCK(cs_desc_man);
1593
21
    std::string error;
1594
21
    if (!CanUpdateToWalletDescriptor(descriptor, error)) {
1595
3
        return util::Error{Untranslated(std::move(error))};
1596
3
    }
1597
1598
18
    m_map_pubkeys.clear();
1599
18
    m_map_script_pub_keys.clear();
1600
18
    m_max_cached_index = -1;
1601
18
    m_wallet_descriptor = descriptor;
1602
1603
18
    WalletBatch batch(m_storage.GetDatabase());
1604
18
    UpdateWithSigningProvider(batch, provider);
1605
18
    NotifyFirstKeyTimeChanged(this, m_wallet_descriptor.creation_time);
1606
18
    return {};
1607
21
}
1608
1609
void DescriptorScriptPubKeyMan::UpdateWithSigningProvider(WalletBatch& batch, const FlatSigningProvider& signing_provider)
1610
970
{
1611
970
    AssertLockHeld(cs_desc_man);
1612
    // Add the private keys to the descriptor
1613
970
    for (const auto& entry : signing_provider.keys) {
1614
771
        const CKey& key = entry.second;
1615
771
        if (!AddDescriptorKeyWithDB(batch, key, key.GetPubKey())) {
1616
0
            throw std::runtime_error(std::string(__func__) + ": writing descriptor private key failed");
1617
0
        }
1618
771
    }
1619
1620
    // Top up key pool, to generate scriptPubKeys
1621
970
    if (!TopUpWithDB(batch)) {
1622
1
        throw std::runtime_error("Could not top up scriptPubKeys");
1623
1
    }
1624
970
}
1625
1626
bool DescriptorScriptPubKeyMan::CanUpdateToWalletDescriptor(const WalletDescriptor& descriptor, std::string& error)
1627
21
{
1628
21
    LOCK(cs_desc_man);
1629
21
    if (!HasWalletDescriptor(descriptor)) {
1630
0
        error = "can only update matching descriptor";
1631
0
        return false;
1632
0
    }
1633
1634
21
    if (!descriptor.descriptor->IsRange()) {
1635
        // Skip range check for non-range descriptors
1636
6
        return true;
1637
6
    }
1638
1639
15
    if (descriptor.range_start > m_wallet_descriptor.range_start ||
1640
15
        descriptor.range_end < m_wallet_descriptor.range_end) {
1641
        // Use inclusive range for error
1642
3
        error = strprintf("new range must include current range = [%d,%d]",
1643
3
                          m_wallet_descriptor.range_start,
1644
3
                          m_wallet_descriptor.range_end - 1);
1645
3
        return false;
1646
3
    }
1647
1648
12
    return true;
1649
15
}
1650
} // namespace wallet