Bitcoin ABC 0.34.2
P2P Digital Currency
scriptpubkeyman.h
Go to the documentation of this file.
1// Copyright (c) 2019 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#ifndef BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
6#define BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
7
8#include <addresstype.h>
9#include <common/messages.h>
10#include <common/signmessage.h>
11#include <common/types.h>
12#include <psbt.h>
13#include <script/descriptor.h>
14#include <script/script.h>
16#include <util/result.h>
17#include <util/time.h>
18#include <wallet/crypter.h>
19#include <wallet/ismine.h>
20#include <wallet/walletdb.h>
21#include <wallet/walletutil.h>
22
23#include <boost/signals2/signal.hpp>
24
25#include <functional>
26#include <unordered_map>
27
28enum class OutputType;
29class CChainParams;
30struct bilingual_str;
31
32// Wallet storage things that ScriptPubKeyMans need in order to be able to store
33// things to the wallet database. It provides access to things that are part of
34// the entire wallet and not specific to a ScriptPubKeyMan such as wallet flags,
35// wallet version, encryption keys, encryption status, and the database itself.
36// This allows a ScriptPubKeyMan to have callbacks into CWallet without causing
37// a circular dependency. WalletStorage should be the same for all
38// ScriptPubKeyMans of a wallet.
40public:
41 virtual ~WalletStorage() = default;
42 virtual std::string GetDisplayName() const = 0;
44 virtual const CChainParams &GetChainParams() const = 0;
45 virtual bool IsWalletFlagSet(uint64_t) const = 0;
46 virtual void UnsetBlankWalletFlag(WalletBatch &) = 0;
47 virtual bool CanSupportFeature(enum WalletFeature) const = 0;
48 virtual void SetMinVersion(enum WalletFeature, WalletBatch * = nullptr,
49 bool = false) = 0;
51 virtual bool WithEncryptionKey(
52 const std::function<bool(const CKeyingMaterial &)> &cb) const = 0;
53 virtual bool HasEncryptionKeys() const = 0;
54 virtual bool IsLocked() const = 0;
55};
56
58static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
59
60std::vector<CKeyID> GetAffectedKeys(const CScript &spk,
61 const SigningProvider &provider);
62
113class CKeyPool {
114public:
116 int64_t nTime;
125
126 CKeyPool();
127 CKeyPool(const CPubKey &vchPubKeyIn, bool internalIn);
128
129 template <typename Stream> void Serialize(Stream &s) const {
130 // Unused field, writes the highest client version ever written
131 s << int{320400};
132 s << nTime << vchPubKey << fInternal << m_pre_split;
133 }
134
135 template <typename Stream> void Unserialize(Stream &s) {
136 // Discard unused field
137 s >> int{};
138 s >> nTime >> vchPubKey;
139 try {
140 s >> fInternal;
141 } catch (std::ios_base::failure &) {
147 fInternal = false;
148 }
149 try {
150 s >> m_pre_split;
151 } catch (std::ios_base::failure &) {
157 m_pre_split = false;
158 }
159 }
160};
161
171protected:
173
174public:
175 explicit ScriptPubKeyMan(WalletStorage &storage) : m_storage(storage) {}
176 virtual ~ScriptPubKeyMan() = default;
179 return util::Error{Untranslated("Not supported")};
180 }
181 virtual isminetype IsMine(const CScript &script) const { return ISMINE_NO; }
182
185 virtual bool CheckDecryptionKey(const CKeyingMaterial &master_key,
186 bool accept_no_keys = false) {
187 return false;
188 }
189 virtual bool Encrypt(const CKeyingMaterial &master_key,
190 WalletBatch *batch) {
191 return false;
192 }
193
194 virtual bool GetReservedDestination(const OutputType type, bool internal,
195 CTxDestination &address, int64_t &index,
196 CKeyPool &keypool) {
197 return false;
198 }
199 virtual void KeepDestination(int64_t index, const OutputType &type) {}
200 virtual void ReturnDestination(int64_t index, bool internal,
201 const CTxDestination &addr) {}
202
210 virtual bool TopUp(unsigned int size = 0) { return false; }
211
213 virtual void MarkUnusedAddresses(const CScript &script) {}
214
221 virtual bool SetupGeneration(bool force = false) { return false; }
222
223 /* Returns true if HD is enabled */
224 virtual bool IsHDEnabled() const { return false; }
225
230 virtual bool CanGetAddresses(bool internal = false) const { return false; }
231
233 virtual bool Upgrade(int prev_version, bilingual_str &error) {
234 return false;
235 }
236
237 virtual bool HavePrivateKeys() const { return false; }
238
240 virtual void RewriteDB() {}
241
242 virtual int64_t GetOldestKeyPoolTime() const { return GetTime(); }
243
244 virtual size_t KeypoolCountExternalKeys() const { return 0; }
245 virtual unsigned int GetKeyPoolSize() const { return 0; }
246
247 virtual int64_t GetTimeFirstKey() const { return 0; }
248
249 virtual std::unique_ptr<CKeyMetadata>
250 GetMetadata(const CTxDestination &dest) const {
251 return nullptr;
252 }
253
254 virtual std::unique_ptr<SigningProvider>
255 GetSolvingProvider(const CScript &script) const {
256 return nullptr;
257 }
258
264 virtual bool CanProvide(const CScript &script, SignatureData &sigdata) {
265 return false;
266 }
267
272 virtual bool
274 const std::map<COutPoint, Coin> &coins, SigHashType sighash,
275 std::map<int, std::string> &input_errors) const {
276 return false;
277 }
279 virtual SigningResult SignMessage(const std::string &message,
280 const PKHash &pkhash,
281 std::string &str_sig) const {
283 };
288 virtual std::optional<common::PSBTError>
290 SigHashType sighash_type = SigHashType().withForkId(),
291 bool sign = true, bool bip32derivs = false,
292 bool finalize = true) const {
294 }
295
296 virtual uint256 GetID() const { return uint256(); }
297
298 virtual void SetInternal(bool internal) {}
299
304 template <typename... Params>
305 void WalletLogPrintf(std::string fmt, Params... parameters) const {
306 LogPrintf(("%s " + fmt).c_str(), m_storage.GetDisplayName(),
307 parameters...);
308 };
309
311 boost::signals2::signal<void(bool fHaveWatchOnly)> NotifyWatchonlyChanged;
312
314 boost::signals2::signal<void()> NotifyCanGetAddressesChanged;
315};
316
319private:
322
323 using WatchOnlySet = std::set<CScript>;
324 using WatchKeyMap = std::map<CKeyID, CPubKey>;
325
326 WalletBatch *encrypted_batch GUARDED_BY(cs_KeyStore) = nullptr;
327
329 std::map<CKeyID, std::pair<CPubKey, std::vector<uint8_t>>>;
330
334
335 int64_t nTimeFirstKey GUARDED_BY(cs_KeyStore) = 0;
336
337 bool AddKeyPubKeyInner(const CKey &key, const CPubKey &pubkey);
338 bool AddCryptedKeyInner(const CPubKey &vchPubKey,
339 const std::vector<uint8_t> &vchCryptedSecret);
340
350 bool AddWatchOnly(const CScript &dest)
352 bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest)
354 bool AddWatchOnlyInMem(const CScript &dest);
356 bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest,
357 int64_t create_time)
359
361 bool AddKeyPubKeyWithDB(WalletBatch &batch, const CKey &key,
362 const CPubKey &pubkey)
364
365 void AddKeypoolPubkeyWithDB(const CPubKey &pubkey, const bool internal,
366 WalletBatch &batch);
367
369 bool AddCScriptWithDB(WalletBatch &batch, const CScript &script);
370
372 bool AddKeyOriginWithDB(WalletBatch &batch, const CPubKey &pubkey,
373 const KeyOriginInfo &info);
374
375 /* the HD chain data model (external chain counters) */
377 std::unordered_map<CKeyID, CHDChain, SaltedSipHasher> m_inactive_hd_chains;
378
379 /* HD derive new child key (on internal or external chain) */
380 void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata,
381 CKey &secret, CHDChain &hd_chain,
382 bool internal = false)
384
385 std::set<int64_t> setInternalKeyPool GUARDED_BY(cs_KeyStore);
386 std::set<int64_t> setExternalKeyPool GUARDED_BY(cs_KeyStore);
387 std::set<int64_t> set_pre_split_keypool GUARDED_BY(cs_KeyStore);
388 int64_t m_max_keypool_index GUARDED_BY(cs_KeyStore) = 0;
390 // Tracks keypool indexes to CKeyIDs of keys that have been taken out of the
391 // keypool but may be returned to it
393
395 bool GetKeyFromPool(CPubKey &key, const OutputType type,
396 bool internal = false);
397
412 bool ReserveKeyFromKeyPool(int64_t &nIndex, CKeyPool &keypool,
413 bool fRequestedInternal);
414
428 bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index,
429 bool internal);
430
431public:
433
434 util::Result<CTxDestination>
435 GetNewDestination(const OutputType type) override;
436 isminetype IsMine(const CScript &script) const override;
437
438 bool CheckDecryptionKey(const CKeyingMaterial &master_key,
439 bool accept_no_keys = false) override;
440 bool Encrypt(const CKeyingMaterial &master_key,
441 WalletBatch *batch) override;
442
443 bool GetReservedDestination(const OutputType type, bool internal,
444 CTxDestination &address, int64_t &index,
445 CKeyPool &keypool) override;
446 void KeepDestination(int64_t index, const OutputType &type) override;
447 void ReturnDestination(int64_t index, bool internal,
448 const CTxDestination &) override;
449
450 bool TopUp(unsigned int size = 0) override;
451
452 void MarkUnusedAddresses(const CScript &script) override;
453
456 void UpgradeKeyMetadata();
457
458 bool IsHDEnabled() const override;
459
460 bool SetupGeneration(bool force = false) override;
461
462 bool Upgrade(int prev_version, bilingual_str &error) override;
463
464 bool HavePrivateKeys() const override;
465
466 void RewriteDB() override;
467
468 int64_t GetOldestKeyPoolTime() const override;
469 size_t KeypoolCountExternalKeys() const override;
470 unsigned int GetKeyPoolSize() const override;
471
472 int64_t GetTimeFirstKey() const override;
473
474 std::unique_ptr<CKeyMetadata>
475 GetMetadata(const CTxDestination &dest) const override;
476
477 bool CanGetAddresses(bool internal = false) const override;
478
479 std::unique_ptr<SigningProvider>
480 GetSolvingProvider(const CScript &script) const override;
481
482 bool CanProvide(const CScript &script, SignatureData &sigdata) override;
483
484 bool
486 const std::map<COutPoint, Coin> &coins, SigHashType sighash,
487 std::map<int, std::string> &input_errors) const override;
488 SigningResult SignMessage(const std::string &message, const PKHash &pkhash,
489 std::string &str_sig) const override;
490 std::optional<common::PSBTError>
492 SigHashType sighash_type = SigHashType().withForkId(),
493 bool sign = true, bool bip32derivs = false,
494 bool finalize = true) const override;
495
496 uint256 GetID() const override;
497
498 void SetInternal(bool internal) override;
499
500 // Map from Key ID to key metadata.
502
503 // Map from Script ID to key metadata (for watch-only keys).
505
507 bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override;
509 bool LoadKey(const CKey &key, const CPubKey &pubkey);
511 bool AddCryptedKey(const CPubKey &vchPubKey,
512 const std::vector<uint8_t> &vchCryptedSecret);
515 bool LoadCryptedKey(const CPubKey &vchPubKey,
516 const std::vector<uint8_t> &vchCryptedSecret,
517 bool checksum_valid);
518 void UpdateTimeFirstKey(int64_t nCreateTime)
521 bool LoadCScript(const CScript &redeemScript);
523 void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata);
524 void LoadScriptMetadata(const CScriptID &script_id,
525 const CKeyMetadata &metadata);
527 CPubKey GenerateNewKey(WalletBatch &batch, CHDChain &hd_chain,
528 bool internal = false)
530
535 void AddHDChain(const CHDChain &chain);
537 void LoadHDChain(const CHDChain &chain);
538 const CHDChain &GetHDChain() const { return m_hd_chain; }
539 void AddInactiveHDChain(const CHDChain &chain);
540
543 bool LoadWatchOnly(const CScript &dest);
545 bool HaveWatchOnly(const CScript &dest) const;
547 bool HaveWatchOnly() const;
549 bool RemoveWatchOnly(const CScript &dest);
550 bool AddWatchOnly(const CScript &dest, int64_t nCreateTime)
552
554 bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const;
555
556 /* SigningProvider overrides */
557 bool HaveKey(const CKeyID &address) const override;
558 bool GetKey(const CKeyID &address, CKey &keyOut) const override;
559 bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override;
560 bool AddCScript(const CScript &redeemScript) override;
561 bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override;
562
564 void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool);
565 bool NewKeyPool();
567
568 bool ImportScripts(const std::set<CScript> scripts, int64_t timestamp)
570 bool ImportPrivKeys(const std::map<CKeyID, CKey> &privkey_map,
571 const int64_t timestamp)
573 bool ImportPubKeys(
574 const std::vector<CKeyID> &ordered_pubkeys,
575 const std::map<CKeyID, CPubKey> &pubkey_map,
576 const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> &key_origins,
577 const bool add_keypool, const bool internal, const int64_t timestamp)
579 bool ImportScriptPubKeys(const std::set<CScript> &script_pub_keys,
580 const bool have_solving_data,
581 const int64_t timestamp)
583
584 /* Returns true if the wallet can generate new keys */
585 bool CanGenerateKeys() const;
586
587 /* Generates a new HD seed (will not be activated) */
589
590 /* Derives a new HD seed (will not be activated) */
591 CPubKey DeriveNewSeed(const CKey &key);
592
593 /* Set the current HD seed (will reset the chain child index counters)
594 Sets the seed's version based on the current wallet version (so the
595 caller must ensure the current wallet version is correct before calling
596 this function). */
597 void SetHDSeed(const CPubKey &key);
598
605 void LearnRelatedScripts(const CPubKey &key, OutputType);
606
611 void LearnAllRelatedScripts(const CPubKey &key);
612
616 void MarkReserveKeysAsUsed(int64_t keypool_id)
618 const std::map<CKeyID, int64_t> &GetAllReserveKeys() const {
619 return m_pool_key_to_index;
620 }
621
622 std::set<CKeyID> GetKeys() const override;
623};
624
630private:
632
633public:
635 : m_spk_man(spk_man) {}
636
637 bool GetCScript(const CScriptID &scriptid, CScript &script) const override {
638 return m_spk_man.GetCScript(scriptid, script);
639 }
640 bool HaveCScript(const CScriptID &scriptid) const override {
641 return m_spk_man.HaveCScript(scriptid);
642 }
643 bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const override {
644 return m_spk_man.GetPubKey(address, pubkey);
645 }
646 bool GetKey(const CKeyID &address, CKey &key) const override {
647 return false;
648 }
649 bool HaveKey(const CKeyID &address) const override { return false; }
650 bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override {
651 return m_spk_man.GetKeyOrigin(keyid, info);
652 }
653};
654
656private:
658
659 // Map of scripts to descriptor range index
660 using ScriptPubKeyMap = std::map<CScript, int32_t>;
661 // Map of pubkeys involved in scripts to descriptor range index
662 using PubKeyMap = std::map<CPubKey, int32_t>;
664 std::map<CKeyID, std::pair<CPubKey, std::vector<uint8_t>>>;
665 using KeyMap = std::map<CKeyID, CKey>;
666
667 ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man);
669 int32_t m_max_cached_index = -1;
670
671 bool m_internal = false;
672
675
678
679 bool AddDescriptorKeyWithDB(WalletBatch &batch, const CKey &key,
680 const CPubKey &pubkey)
682
684
685 // Fetch the SigningProvider for the given script and optionally include
686 // private keys
687 std::unique_ptr<FlatSigningProvider>
688 GetSigningProvider(const CScript &script,
689 bool include_private = false) const;
690 // Fetch the SigningProvider for the given pubkey and always include private
691 // keys. This should only be called by signing code.
692 std::unique_ptr<FlatSigningProvider>
693 GetSigningProvider(const CPubKey &pubkey) const;
694 // Fetch the SigningProvider for a given index and optionally include
695 // private keys. Called by the above functions.
696 std::unique_ptr<FlatSigningProvider>
697 GetSigningProvider(int32_t index, bool include_private = false) const
699
700public:
702 WalletDescriptor &descriptor)
703 : ScriptPubKeyMan(storage), m_wallet_descriptor(descriptor) {}
705 : ScriptPubKeyMan(storage), m_internal(internal) {}
706
708
710 GetNewDestination(const OutputType type) override;
711 isminetype IsMine(const CScript &script) const override;
712
713 bool CheckDecryptionKey(const CKeyingMaterial &master_key,
714 bool accept_no_keys = false) override;
715 bool Encrypt(const CKeyingMaterial &master_key,
716 WalletBatch *batch) override;
717
718 bool GetReservedDestination(const OutputType type, bool internal,
719 CTxDestination &address, int64_t &index,
720 CKeyPool &keypool) override;
721 void ReturnDestination(int64_t index, bool internal,
722 const CTxDestination &addr) override;
723
724 // Tops up the descriptor cache and m_map_script_pub_keys. The cache is
725 // stored in the wallet file and is used to expand the descriptor in
726 // GetNewDestination. DescriptorScriptPubKeyMan relies more on ephemeral
727 // data than LegacyScriptPubKeyMan. For wallets using unhardened derivation
728 // (with or without private keys), the "keypool" is a single xpub.
729 bool TopUp(unsigned int size = 0) override;
730
731 void MarkUnusedAddresses(const CScript &script) override;
732
733 bool IsHDEnabled() const override;
734
736 bool SetupDescriptorGeneration(const CExtKey &master_key,
737 OutputType addr_type);
738
739 bool HavePrivateKeys() const override;
740
741 int64_t GetOldestKeyPoolTime() const override;
742 size_t KeypoolCountExternalKeys() const override;
743 unsigned int GetKeyPoolSize() const override;
744
745 int64_t GetTimeFirstKey() const override;
746
747 std::unique_ptr<CKeyMetadata>
748 GetMetadata(const CTxDestination &dest) const override;
749
750 bool CanGetAddresses(bool internal = false) const override;
751
752 std::unique_ptr<SigningProvider>
753 GetSolvingProvider(const CScript &script) const override;
754
755 bool CanProvide(const CScript &script, SignatureData &sigdata) override;
756
757 bool
759 const std::map<COutPoint, Coin> &coins, SigHashType sighash,
760 std::map<int, std::string> &input_errors) const override;
761 SigningResult SignMessage(const std::string &message, const PKHash &pkhash,
762 std::string &str_sig) const override;
763 std::optional<common::PSBTError>
765 SigHashType sighash_type = SigHashType().withForkId(),
766 bool sign = true, bool bip32derivs = false,
767 bool finalize = true) const override;
768
769 uint256 GetID() const override;
770
771 void SetInternal(bool internal) override;
772
773 void SetCache(const DescriptorCache &cache);
774
775 bool AddKey(const CKeyID &key_id, const CKey &key);
776 bool AddCryptedKey(const CKeyID &key_id, const CPubKey &pubkey,
777 const std::vector<uint8_t> &crypted_key);
778
779 bool HasWalletDescriptor(const WalletDescriptor &desc) const;
781 bool CanUpdateToWalletDescriptor(const WalletDescriptor &descriptor,
782 std::string &error);
783 void AddDescriptorKey(const CKey &key, const CPubKey &pubkey);
784 void WriteDescriptor();
785
788 std::vector<CScript> GetScriptPubKeys() const;
789};
790
791#endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: addresstype.h:54
const CScript redeemScript
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
An encapsulated secp256k1 private key.
Definition: key.h:28
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
A key from a CWallet's keypool.
bool fInternal
Whether this keypool entry is in the internal keypool (for change outputs)
void Unserialize(Stream &s)
CPubKey vchPubKey
The public key.
int64_t nTime
The time at which the key was generated. Set in AddKeypoolPubKeyWithDB.
CKeyPool()
Definition: wallet.cpp:3238
bool m_pre_split
Whether this key was generated for a keypool before the wallet was upgraded to HD-split.
void Serialize(Stream &s) const
A mutable version of CTransaction.
Definition: transaction.h:274
An encapsulated public key.
Definition: pubkey.h:31
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:564
A UTXO entry.
Definition: coins.h:31
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
std::map< CPubKey, int32_t > PubKeyMap
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man)
int64_t GetOldestKeyPoolTime() const override
DescriptorScriptPubKeyMan(WalletStorage &storage, bool internal)
void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr) override
bool AddKey(const CKeyID &key_id, const CKey &key)
void MarkUnusedAddresses(const CScript &script) override
Mark unused addresses as being used.
bool HavePrivateKeys() const override
bool AddCryptedKey(const CKeyID &key_id, const CPubKey &pubkey, const std::vector< uint8_t > &crypted_key)
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const override
Sign a message with the given script.
bool CanUpdateToWalletDescriptor(const WalletDescriptor &descriptor, std::string &error)
ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man)
bool TopUp(unsigned int size=0) override
Fills internal address pool.
void SetInternal(bool internal) override
std::map< CKeyID, std::pair< CPubKey, std::vector< uint8_t > > > CryptedKeyMap
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
std::map< CScript, int32_t > ScriptPubKeyMap
bool HasWalletDescriptor(const WalletDescriptor &desc) const
int64_t GetTimeFirstKey() const override
bool CanProvide(const CScript &script, SignatureData &sigdata) override
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that,...
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool) override
util::Result< CTxDestination > GetNewDestination(const OutputType type) override
unsigned int GetKeyPoolSize() const override
bool SetupDescriptorGeneration(const CExtKey &master_key, OutputType addr_type)
Setup descriptors based on the given CExtkey.
void AddDescriptorKey(const CKey &key, const CPubKey &pubkey)
CryptedKeyMap m_map_crypted_keys GUARDED_BY(cs_desc_man)
std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const override
size_t KeypoolCountExternalKeys() const override
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, SigHashType sighash, std::map< int, std::string > &input_errors) const override
Creates new signatures and adds them to the transaction.
bool m_decryption_thoroughly_checked
keeps track of whether Unlock has run a thorough check before
KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
std::unique_ptr< FlatSigningProvider > GetSigningProvider(const CScript &script, bool include_private=false) const
PubKeyMap m_map_pubkeys GUARDED_BY(cs_desc_man)
std::map< CKeyID, CKey > KeyMap
bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch) override
bool AddDescriptorKeyWithDB(WalletBatch &batch, const CKey &key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
void UpdateWalletDescriptor(WalletDescriptor &descriptor)
void SetCache(const DescriptorCache &cache)
KeyMap m_map_keys GUARDED_BY(cs_desc_man)
uint256 GetID() const override
std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=false, bool finalize=true) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
std::vector< CScript > GetScriptPubKeys() const
isminetype IsMine(const CScript &script) const override
bool CheckDecryptionKey(const CKeyingMaterial &master_key, bool accept_no_keys=false) override
Check that the given decryption key is valid for this ScriptPubKeyMan, i.e.
WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
bool IsHDEnabled() const override
bool CanGetAddresses(bool internal=false) const override
Returns true if the wallet can give out new addresses.
Fillable signing provider that keeps keys in an address->secret map.
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
virtual bool HaveCScript(const CScriptID &hash) const override
RecursiveMutex cs_KeyStore
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
const CHDChain & GetHDChain() const
std::map< int64_t, CKeyID > m_index_to_reserved_key
void UpgradeKeyMetadata()
Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo.
bool AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector< uint8_t > &vchCryptedSecret)
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
int64_t GetOldestKeyPoolTime() const override
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool) override
uint256 GetID() const override
bool LoadWatchOnly(const CScript &dest)
Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
CryptedKeyMap mapCryptedKeys GUARDED_BY(cs_KeyStore)
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
void MarkUnusedAddresses(const CScript &script) override
Mark unused addresses as being used.
bool HaveWatchOnly() const
Returns whether there are any watch-only things in the wallet.
bool RemoveWatchOnly(const CScript &dest)
Remove a watch only script from the keystore.
bool AddWatchOnlyInMem(const CScript &dest)
void UpdateTimeFirstKey(int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Update wallet first key creation time.
void AddKeypoolPubkeyWithDB(const CPubKey &pubkey, const bool internal, WalletBatch &batch)
size_t KeypoolCountExternalKeys() const override
void ReturnDestination(int64_t index, bool internal, const CTxDestination &) override
void SetHDSeed(const CPubKey &key)
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector< uint8_t > &vchCryptedSecret, bool checksum_valid)
Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
std::unordered_map< CKeyID, CHDChain, SaltedSipHasher > m_inactive_hd_chains
bool GetKey(const CKeyID &address, CKey &keyOut) const override
WatchKeyMap mapWatchKeys GUARDED_BY(cs_KeyStore)
void LearnAllRelatedScripts(const CPubKey &key)
Same as LearnRelatedScripts, but when the OutputType is not known (and could be anything).
bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch) override
isminetype IsMine(const CScript &script) const override
bool ImportScripts(const std::set< CScript > scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
int64_t nTimeFirstKey GUARDED_BY(cs_KeyStore)=0
std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const override
bool HaveKey(const CKeyID &address) const override
bool ImportPrivKeys(const std::map< CKeyID, CKey > &privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index, bool internal)
Like TopUp() but adds keys for inactive HD chains.
bool CanGetAddresses(bool internal=false) const override
Returns true if the wallet can give out new addresses.
bool AddKeyPubKeyInner(const CKey &key, const CPubKey &pubkey)
void AddHDChain(const CHDChain &chain)
Set the HD chain model (chain child index counters) and writes it to the database.
bool CheckDecryptionKey(const CKeyingMaterial &master_key, bool accept_no_keys=false) override
Check that the given decryption key is valid for this ScriptPubKeyMan, i.e.
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool)
Load a keypool entry.
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< uint8_t > &vchCryptedSecret)
Adds an encrypted key to the store, and saves it to disk.
std::map< CKeyID, CPubKey > WatchKeyMap
std::set< CScript > WatchOnlySet
bool Upgrade(int prev_version, bilingual_str &error) override
Upgrades the wallet to the specified version.
bool IsHDEnabled() const override
util::Result< CTxDestination > GetNewDestination(const OutputType type) override
bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
WatchOnlySet setWatchOnly GUARDED_BY(cs_KeyStore)
std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=false, bool finalize=true) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, without saving it to disk (used by LoadWallet)
WalletBatch *encrypted_batch GUARDED_BY(cs_KeyStore)
bool ImportPubKeys(const std::vector< CKeyID > &ordered_pubkeys, const std::map< CKeyID, CPubKey > &pubkey_map, const std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > &key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
bool AddKeyOriginWithDB(WalletBatch &batch, const CPubKey &pubkey, const KeyOriginInfo &info)
Add a KeyOriginInfo to the wallet.
bool AddWatchOnly(const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Private version of AddWatchOnly method which does not accept a timestamp, and which will reset the wa...
bool TopUp(unsigned int size=0) override
Fills internal address pool.
const std::map< CKeyID, int64_t > & GetAllReserveKeys() const
void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata)
Load metadata (used by LoadWallet)
std::map< CKeyID, std::pair< CPubKey, std::vector< uint8_t > > > CryptedKeyMap
void MarkReserveKeysAsUsed(int64_t keypool_id) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Marks all keys in the keypool up to and including reserve_key as used.
void LoadHDChain(const CHDChain &chain)
Load a HD chain model (used by LoadWallet)
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
void AddInactiveHDChain(const CHDChain &chain)
bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
Fetches a pubkey from mapWatchKeys if it exists there.
void LearnRelatedScripts(const CPubKey &key, OutputType)
Explicitly make the wallet learn the related scripts for outputs to the given key.
bool NewKeyPool()
Mark old keypool keys as used, and generate all new keys.
bool ReserveKeyFromKeyPool(int64_t &nIndex, CKeyPool &keypool, bool fRequestedInternal)
Reserves a key from the keypool and sets nIndex to its index.
std::set< CKeyID > GetKeys() const override
void KeepDestination(int64_t index, const OutputType &type) override
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Adds a key to the store, and saves it to disk.
CPubKey GenerateNewKey(WalletBatch &batch, CHDChain &hd_chain, bool internal=false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Generate a new key.
void MarkPreSplitKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
unsigned int GetKeyPoolSize() const override
bool HavePrivateKeys() const override
void SetInternal(bool internal) override
bool SetupGeneration(bool force=false) override
Sets up the key generation stuff, i.e.
bool ImportScriptPubKeys(const std::set< CScript > &script_pub_keys, const bool have_solving_data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
bool AddKeyPubKeyWithDB(WalletBatch &batch, const CKey &key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Adds a key to the store, and saves it to disk.
void RewriteDB() override
The action to do when the DB needs rewrite.
CPubKey DeriveNewSeed(const CKey &key)
bool CanProvide(const CScript &script, SignatureData &sigdata) override
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that,...
int64_t GetTimeFirstKey() const override
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, SigHashType sighash, std::map< int, std::string > &input_errors) const override
Creates new signatures and adds them to the transaction.
void LoadScriptMetadata(const CScriptID &script_id, const CKeyMetadata &metadata)
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const override
Sign a message with the given script.
bool LoadCScript(const CScript &redeemScript)
Adds a CScript to the store.
bool AddCScript(const CScript &redeemScript) override
bool AddCScriptWithDB(WalletBatch &batch, const CScript &script)
Adds a script to the store and saves it to disk.
std::map< CKeyID, int64_t > m_pool_key_to_index
bool GetKeyFromPool(CPubKey &key, const OutputType type, bool internal=false)
Fetches a key from the keypool.
void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata, CKey &secret, CHDChain &hd_chain, bool internal=false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr.
bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const override
bool HaveCScript(const CScriptID &scriptid) const override
bool GetKey(const CKeyID &address, CKey &key) const override
bool HaveKey(const CKeyID &address) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
LegacySigningProvider(const LegacyScriptPubKeyMan &spk_man)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
const LegacyScriptPubKeyMan & m_spk_man
A class implementing ScriptPubKeyMan manages some (or all) scriptPubKeys used in a wallet.
virtual bool TopUp(unsigned int size=0)
Fills internal address pool.
virtual bool Upgrade(int prev_version, bilingual_str &error)
Upgrades the wallet to the specified version.
virtual std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const
virtual std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=false, bool finalize=true) const
Adds script and derivation path information to a PSBT, and optionally signs it.
virtual bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, SigHashType sighash, std::map< int, std::string > &input_errors) const
Creates new signatures and adds them to the transaction.
virtual void SetInternal(bool internal)
virtual bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch)
virtual void MarkUnusedAddresses(const CScript &script)
Mark unused addresses as being used.
virtual int64_t GetOldestKeyPoolTime() const
virtual size_t KeypoolCountExternalKeys() const
virtual SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const
Sign a message with the given script.
virtual unsigned int GetKeyPoolSize() const
virtual bool SetupGeneration(bool force=false)
Sets up the key generation stuff, i.e.
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool)
virtual std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
virtual isminetype IsMine(const CScript &script) const
virtual bool IsHDEnabled() const
virtual bool CheckDecryptionKey(const CKeyingMaterial &master_key, bool accept_no_keys=false)
Check that the given decryption key is valid for this ScriptPubKeyMan, i.e.
virtual ~ScriptPubKeyMan()=default
WalletStorage & m_storage
virtual void KeepDestination(int64_t index, const OutputType &type)
virtual bool CanGetAddresses(bool internal=false) const
Returns true if the wallet can give out new addresses.
virtual uint256 GetID() const
virtual bool CanProvide(const CScript &script, SignatureData &sigdata)
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that,...
virtual void RewriteDB()
The action to do when the DB needs rewrite.
virtual bool HavePrivateKeys() const
virtual int64_t GetTimeFirstKey() const
virtual util::Result< CTxDestination > GetNewDestination(const OutputType type)
ScriptPubKeyMan(WalletStorage &storage)
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr)
boost::signals2::signal< void()> NotifyCanGetAddressesChanged
Keypool has new keys.
boost::signals2::signal< void(bool fHaveWatchOnly)> NotifyWatchonlyChanged
Watch-only address added.
void WalletLogPrintf(std::string fmt, Params... parameters) const
Prepends the wallet name in logging output to ease debugging in multi-wallet use cases.
Signature hash type wrapper class.
Definition: sighashtype.h:37
An interface to be implemented by keystores that support signing.
Access to the wallet database.
Definition: walletdb.h:175
An instance of this class represents one database.
Definition: db.h:100
Descriptor with some wallet metadata.
Definition: walletutil.h:80
virtual bool IsWalletFlagSet(uint64_t) const =0
virtual bool HasEncryptionKeys() const =0
virtual bool WithEncryptionKey(const std::function< bool(const CKeyingMaterial &)> &cb) const =0
Pass the encryption key to cb().
virtual WalletDatabase & GetDatabase()=0
virtual std::string GetDisplayName() const =0
virtual bool IsLocked() const =0
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
virtual void SetMinVersion(enum WalletFeature, WalletBatch *=nullptr, bool=false)=0
virtual ~WalletStorage()=default
virtual const CChainParams & GetChainParams() const =0
virtual bool CanSupportFeature(enum WalletFeature) const =0
256-bit opaque blob.
Definition: uint256.h:199
is a home for simple enum and struct type definitions that can be used internally by functions in the...
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
Definition: crypter.h:57
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_NO
Definition: ismine.h:19
#define LogPrintf(...)
Definition: logging.h:424
Definition: args.cpp:864
PSBTError
Definition: types.h:17
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
OutputType
Definition: outputtype.h:16
static const unsigned int DEFAULT_KEYPOOL_SIZE
Default for -keypool.
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
SigningResult
Definition: signmessage.h:47
Definition: key.h:167
A version of CTransaction with the PSBT format.
Definition: psbt.h:336
Bilingual messages:
Definition: translation.h:17
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:80
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
WalletFeature
(client) version numbers for particular wallet features
Definition: walletutil.h:14