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