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