Bitcoin ABC  0.28.12
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 <psbt.h>
9 #include <script/descriptor.h>
10 #include <script/signingprovider.h>
11 #include <script/standard.h>
12 #include <util/error.h>
13 #include <util/message.h>
14 #include <wallet/crypter.h>
15 #include <wallet/ismine.h>
16 #include <wallet/walletdb.h>
17 #include <wallet/walletutil.h>
18 
19 #include <boost/signals2/signal.hpp>
20 
21 #include <unordered_map>
22 
23 enum class OutputType;
24 class CChainParams;
25 struct bilingual_str;
26 
27 // Wallet storage things that ScriptPubKeyMans need in order to be able to store
28 // things to the wallet database. It provides access to things that are part of
29 // the entire wallet and not specific to a ScriptPubKeyMan such as wallet flags,
30 // wallet version, encryption keys, encryption status, and the database itself.
31 // This allows a ScriptPubKeyMan to have callbacks into CWallet without causing
32 // a circular dependency. WalletStorage should be the same for all
33 // ScriptPubKeyMans of a wallet.
35 public:
36  virtual ~WalletStorage() = default;
37  virtual const std::string GetDisplayName() const = 0;
38  virtual WalletDatabase &GetDatabase() = 0;
39  virtual const CChainParams &GetChainParams() const = 0;
40  virtual bool IsWalletFlagSet(uint64_t) const = 0;
41  virtual void UnsetBlankWalletFlag(WalletBatch &) = 0;
42  virtual bool CanSupportFeature(enum WalletFeature) const = 0;
43  virtual void SetMinVersion(enum WalletFeature, WalletBatch * = nullptr,
44  bool = false) = 0;
45  virtual const CKeyingMaterial &GetEncryptionKey() const = 0;
46  virtual bool HasEncryptionKeys() const = 0;
47  virtual bool IsLocked() const = 0;
48 };
49 
51 static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
52 
53 std::vector<CKeyID> GetAffectedKeys(const CScript &spk,
54  const SigningProvider &provider);
55 
106 class CKeyPool {
107 public:
109  int64_t nTime;
114  bool fInternal;
118 
119  CKeyPool();
120  CKeyPool(const CPubKey &vchPubKeyIn, bool internalIn);
121 
122  template <typename Stream> void Serialize(Stream &s) const {
123  int nVersion = s.GetVersion();
124  if (!(s.GetType() & SER_GETHASH)) {
125  s << nVersion;
126  }
127  s << nTime << vchPubKey << fInternal << m_pre_split;
128  }
129 
130  template <typename Stream> void Unserialize(Stream &s) {
131  int nVersion = s.GetVersion();
132  if (!(s.GetType() & SER_GETHASH)) {
133  s >> nVersion;
134  }
135  s >> nTime >> vchPubKey;
136  try {
137  s >> fInternal;
138  } catch (std::ios_base::failure &) {
144  fInternal = false;
145  }
146  try {
147  s >> m_pre_split;
148  } catch (std::ios_base::failure &) {
154  m_pre_split = false;
155  }
156  }
157 };
158 
168 protected:
170 
171 public:
172  explicit ScriptPubKeyMan(WalletStorage &storage) : m_storage(storage) {}
173  virtual ~ScriptPubKeyMan(){};
174  virtual bool GetNewDestination(const OutputType type, CTxDestination &dest,
175  std::string &error) {
176  return false;
177  }
178  virtual isminetype IsMine(const CScript &script) const { return ISMINE_NO; }
179 
182  virtual bool CheckDecryptionKey(const CKeyingMaterial &master_key,
183  bool accept_no_keys = false) {
184  return false;
185  }
186  virtual bool Encrypt(const CKeyingMaterial &master_key,
187  WalletBatch *batch) {
188  return false;
189  }
190 
191  virtual bool GetReservedDestination(const OutputType type, bool internal,
192  CTxDestination &address, int64_t &index,
193  CKeyPool &keypool) {
194  return false;
195  }
196  virtual void KeepDestination(int64_t index, const OutputType &type) {}
197  virtual void ReturnDestination(int64_t index, bool internal,
198  const CTxDestination &addr) {}
199 
207  virtual bool TopUp(unsigned int size = 0) { return false; }
208 
210  virtual void MarkUnusedAddresses(const CScript &script) {}
211 
218  virtual bool SetupGeneration(bool force = false) { return false; }
219 
220  /* Returns true if HD is enabled */
221  virtual bool IsHDEnabled() const { return false; }
222 
227  virtual bool CanGetAddresses(bool internal = false) const { return false; }
228 
230  virtual bool Upgrade(int prev_version, bilingual_str &error) {
231  return false;
232  }
233 
234  virtual bool HavePrivateKeys() const { return false; }
235 
237  virtual void RewriteDB() {}
238 
239  virtual int64_t GetOldestKeyPoolTime() const { return GetTime(); }
240 
241  virtual size_t KeypoolCountExternalKeys() const { return 0; }
242  virtual unsigned int GetKeyPoolSize() const { return 0; }
243 
244  virtual int64_t GetTimeFirstKey() const { return 0; }
245 
246  virtual std::unique_ptr<CKeyMetadata>
247  GetMetadata(const CTxDestination &dest) const {
248  return nullptr;
249  }
250 
251  virtual std::unique_ptr<SigningProvider>
252  GetSolvingProvider(const CScript &script) const {
253  return nullptr;
254  }
255 
261  virtual bool CanProvide(const CScript &script, SignatureData &sigdata) {
262  return false;
263  }
264 
269  virtual bool
271  const std::map<COutPoint, Coin> &coins, SigHashType sighash,
272  std::map<int, std::string> &input_errors) const {
273  return false;
274  }
276  virtual SigningResult SignMessage(const std::string &message,
277  const PKHash &pkhash,
278  std::string &str_sig) const {
280  };
285  virtual TransactionError
287  SigHashType sighash_type = SigHashType().withForkId(),
288  bool sign = true, bool bip32derivs = false) const {
290  }
291 
292  virtual uint256 GetID() const { return uint256(); }
293 
294  virtual void SetInternal(bool internal) {}
295 
300  template <typename... Params>
301  void WalletLogPrintf(std::string fmt, Params... parameters) const {
302  LogPrintf(("%s " + fmt).c_str(), m_storage.GetDisplayName(),
303  parameters...);
304  };
305 
307  boost::signals2::signal<void(bool fHaveWatchOnly)> NotifyWatchonlyChanged;
308 
310  boost::signals2::signal<void()> NotifyCanGetAddressesChanged;
311 };
312 
314  public FillableSigningProvider {
315 private:
318 
319  using WatchOnlySet = std::set<CScript>;
320  using WatchKeyMap = std::map<CKeyID, CPubKey>;
321 
322  WalletBatch *encrypted_batch GUARDED_BY(cs_KeyStore) = nullptr;
323 
325  std::map<CKeyID, std::pair<CPubKey, std::vector<uint8_t>>>;
326 
330 
331  int64_t nTimeFirstKey GUARDED_BY(cs_KeyStore) = 0;
332 
333  bool AddKeyPubKeyInner(const CKey &key, const CPubKey &pubkey);
334  bool AddCryptedKeyInner(const CPubKey &vchPubKey,
335  const std::vector<uint8_t> &vchCryptedSecret);
336 
346  bool AddWatchOnly(const CScript &dest)
348  bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest)
350  bool AddWatchOnlyInMem(const CScript &dest);
352  bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest,
353  int64_t create_time)
355 
357  bool AddKeyPubKeyWithDB(WalletBatch &batch, const CKey &key,
358  const CPubKey &pubkey)
360 
361  void AddKeypoolPubkeyWithDB(const CPubKey &pubkey, const bool internal,
362  WalletBatch &batch);
363 
365  bool AddCScriptWithDB(WalletBatch &batch, const CScript &script);
366 
368  bool AddKeyOriginWithDB(WalletBatch &batch, const CPubKey &pubkey,
369  const KeyOriginInfo &info);
370 
371  /* the HD chain data model (external chain counters) */
373  std::unordered_map<CKeyID, CHDChain, SaltedSipHasher> m_inactive_hd_chains;
374 
375  /* HD derive new child key (on internal or external chain) */
376  void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata,
377  CKey &secret, CHDChain &hd_chain,
378  bool internal = false)
380 
381  std::set<int64_t> setInternalKeyPool GUARDED_BY(cs_KeyStore);
382  std::set<int64_t> setExternalKeyPool GUARDED_BY(cs_KeyStore);
383  std::set<int64_t> set_pre_split_keypool GUARDED_BY(cs_KeyStore);
384  int64_t m_max_keypool_index GUARDED_BY(cs_KeyStore) = 0;
386  // Tracks keypool indexes to CKeyIDs of keys that have been taken out of the
387  // keypool but may be returned to it
389 
391  bool GetKeyFromPool(CPubKey &key, const OutputType type,
392  bool internal = false);
393 
408  bool ReserveKeyFromKeyPool(int64_t &nIndex, CKeyPool &keypool,
409  bool fRequestedInternal);
410 
424  bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index,
425  bool internal);
426 
427 public:
429 
430  bool GetNewDestination(const OutputType type, CTxDestination &dest,
431  std::string &error) override;
432  isminetype IsMine(const CScript &script) const override;
433 
434  bool CheckDecryptionKey(const CKeyingMaterial &master_key,
435  bool accept_no_keys = false) override;
436  bool Encrypt(const CKeyingMaterial &master_key,
437  WalletBatch *batch) override;
438 
439  bool GetReservedDestination(const OutputType type, bool internal,
440  CTxDestination &address, int64_t &index,
441  CKeyPool &keypool) override;
442  void KeepDestination(int64_t index, const OutputType &type) override;
443  void ReturnDestination(int64_t index, bool internal,
444  const CTxDestination &) override;
445 
446  bool TopUp(unsigned int size = 0) override;
447 
448  void MarkUnusedAddresses(const CScript &script) override;
449 
452  void UpgradeKeyMetadata();
453 
454  bool IsHDEnabled() const override;
455 
456  bool SetupGeneration(bool force = false) override;
457 
458  bool Upgrade(int prev_version, bilingual_str &error) override;
459 
460  bool HavePrivateKeys() const override;
461 
462  void RewriteDB() override;
463 
464  int64_t GetOldestKeyPoolTime() const override;
465  size_t KeypoolCountExternalKeys() const override;
466  unsigned int GetKeyPoolSize() const override;
467 
468  int64_t GetTimeFirstKey() const override;
469 
470  std::unique_ptr<CKeyMetadata>
471  GetMetadata(const CTxDestination &dest) const override;
472 
473  bool CanGetAddresses(bool internal = false) const override;
474 
475  std::unique_ptr<SigningProvider>
476  GetSolvingProvider(const CScript &script) const override;
477 
478  bool CanProvide(const CScript &script, SignatureData &sigdata) override;
479 
480  bool
482  const std::map<COutPoint, Coin> &coins, SigHashType sighash,
483  std::map<int, std::string> &input_errors) const override;
484  SigningResult SignMessage(const std::string &message, const PKHash &pkhash,
485  std::string &str_sig) const override;
488  SigHashType sighash_type = SigHashType().withForkId(),
489  bool sign = true, bool bip32derivs = false) const override;
490 
491  uint256 GetID() const override;
492 
493  void SetInternal(bool internal) override;
494 
495  // Map from Key ID to key metadata.
496  std::map<CKeyID, CKeyMetadata> mapKeyMetadata GUARDED_BY(cs_KeyStore);
497 
498  // Map from Script ID to key metadata (for watch-only keys).
499  std::map<CScriptID, CKeyMetadata> m_script_metadata GUARDED_BY(cs_KeyStore);
500 
502  bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override;
504  bool LoadKey(const CKey &key, const CPubKey &pubkey);
506  bool AddCryptedKey(const CPubKey &vchPubKey,
507  const std::vector<uint8_t> &vchCryptedSecret);
510  bool LoadCryptedKey(const CPubKey &vchPubKey,
511  const std::vector<uint8_t> &vchCryptedSecret,
512  bool checksum_valid);
513  void UpdateTimeFirstKey(int64_t nCreateTime)
516  bool LoadCScript(const CScript &redeemScript);
518  void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata);
519  void LoadScriptMetadata(const CScriptID &script_id,
520  const CKeyMetadata &metadata);
522  CPubKey GenerateNewKey(WalletBatch &batch, CHDChain &hd_chain,
523  bool internal = false)
525 
530  void AddHDChain(const CHDChain &chain);
532  void LoadHDChain(const CHDChain &chain);
533  const CHDChain &GetHDChain() const { return m_hd_chain; }
534  void AddInactiveHDChain(const CHDChain &chain);
535 
538  bool LoadWatchOnly(const CScript &dest);
540  bool HaveWatchOnly(const CScript &dest) const;
542  bool HaveWatchOnly() const;
544  bool RemoveWatchOnly(const CScript &dest);
545  bool AddWatchOnly(const CScript &dest, int64_t nCreateTime)
547 
549  bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const;
550 
551  /* SigningProvider overrides */
552  bool HaveKey(const CKeyID &address) const override;
553  bool GetKey(const CKeyID &address, CKey &keyOut) const override;
554  bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override;
555  bool AddCScript(const CScript &redeemScript) override;
556  bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override;
557 
559  void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool);
560  bool NewKeyPool();
562 
563  bool ImportScripts(const std::set<CScript> scripts, int64_t timestamp)
565  bool ImportPrivKeys(const std::map<CKeyID, CKey> &privkey_map,
566  const int64_t timestamp)
568  bool ImportPubKeys(
569  const std::vector<CKeyID> &ordered_pubkeys,
570  const std::map<CKeyID, CPubKey> &pubkey_map,
571  const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> &key_origins,
572  const bool add_keypool, const bool internal, const int64_t timestamp)
574  bool ImportScriptPubKeys(const std::set<CScript> &script_pub_keys,
575  const bool have_solving_data,
576  const int64_t timestamp)
578 
579  /* Returns true if the wallet can generate new keys */
580  bool CanGenerateKeys() const;
581 
582  /* Generates a new HD seed (will not be activated) */
584 
585  /* Derives a new HD seed (will not be activated) */
586  CPubKey DeriveNewSeed(const CKey &key);
587 
588  /* Set the current HD seed (will reset the chain child index counters)
589  Sets the seed's version based on the current wallet version (so the
590  caller must ensure the current wallet version is correct before calling
591  this function). */
592  void SetHDSeed(const CPubKey &key);
593 
600  void LearnRelatedScripts(const CPubKey &key, OutputType);
601 
606  void LearnAllRelatedScripts(const CPubKey &key);
607 
611  void MarkReserveKeysAsUsed(int64_t keypool_id)
613  const std::map<CKeyID, int64_t> &GetAllReserveKeys() const {
614  return m_pool_key_to_index;
615  }
616 
617  std::set<CKeyID> GetKeys() const override;
618 };
619 
625 private:
627 
628 public:
630  : m_spk_man(spk_man) {}
631 
632  bool GetCScript(const CScriptID &scriptid, CScript &script) const override {
633  return m_spk_man.GetCScript(scriptid, script);
634  }
635  bool HaveCScript(const CScriptID &scriptid) const override {
636  return m_spk_man.HaveCScript(scriptid);
637  }
638  bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const override {
639  return m_spk_man.GetPubKey(address, pubkey);
640  }
641  bool GetKey(const CKeyID &address, CKey &key) const override {
642  return false;
643  }
644  bool HaveKey(const CKeyID &address) const override { return false; }
645  bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override {
646  return m_spk_man.GetKeyOrigin(keyid, info);
647  }
648 };
649 
651 private:
653 
654  // Map of scripts to descriptor range index
655  using ScriptPubKeyMap = std::map<CScript, int32_t>;
656  // Map of pubkeys involved in scripts to descriptor range index
657  using PubKeyMap = std::map<CPubKey, int32_t>;
659  std::map<CKeyID, std::pair<CPubKey, std::vector<uint8_t>>>;
660  using KeyMap = std::map<CKeyID, CKey>;
661 
662  ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man);
664  int32_t m_max_cached_index = -1;
665 
666  bool m_internal = false;
667 
669  CryptedKeyMap m_map_crypted_keys GUARDED_BY(cs_desc_man);
670 
673 
674  bool AddDescriptorKeyWithDB(WalletBatch &batch, const CKey &key,
675  const CPubKey &pubkey)
677 
679 
680  // Fetch the SigningProvider for the given script and optionally include
681  // private keys
682  std::unique_ptr<FlatSigningProvider>
683  GetSigningProvider(const CScript &script,
684  bool include_private = false) const;
685  // Fetch the SigningProvider for the given pubkey and always include private
686  // keys. This should only be called by signing code.
687  std::unique_ptr<FlatSigningProvider>
688  GetSigningProvider(const CPubKey &pubkey) const;
689  // Fetch the SigningProvider for a given index and optionally include
690  // private keys. Called by the above functions.
691  std::unique_ptr<FlatSigningProvider>
692  GetSigningProvider(int32_t index, bool include_private = false) const
694 
695 public:
697  WalletDescriptor &descriptor)
698  : ScriptPubKeyMan(storage), m_wallet_descriptor(descriptor) {}
699  DescriptorScriptPubKeyMan(WalletStorage &storage, bool internal)
700  : ScriptPubKeyMan(storage), m_internal(internal) {}
701 
703 
704  bool GetNewDestination(const OutputType type, CTxDestination &dest,
705  std::string &error) override;
706  isminetype IsMine(const CScript &script) const override;
707 
708  bool CheckDecryptionKey(const CKeyingMaterial &master_key,
709  bool accept_no_keys = false) override;
710  bool Encrypt(const CKeyingMaterial &master_key,
711  WalletBatch *batch) override;
712 
713  bool GetReservedDestination(const OutputType type, bool internal,
714  CTxDestination &address, int64_t &index,
715  CKeyPool &keypool) override;
716  void ReturnDestination(int64_t index, bool internal,
717  const CTxDestination &addr) override;
718 
719  // Tops up the descriptor cache and m_map_script_pub_keys. The cache is
720  // stored in the wallet file and is used to expand the descriptor in
721  // GetNewDestination. DescriptorScriptPubKeyMan relies more on ephemeral
722  // data than LegacyScriptPubKeyMan. For wallets using unhardened derivation
723  // (with or without private keys), the "keypool" is a single xpub.
724  bool TopUp(unsigned int size = 0) override;
725 
726  void MarkUnusedAddresses(const CScript &script) override;
727 
728  bool IsHDEnabled() const override;
729 
731  bool SetupDescriptorGeneration(const CExtKey &master_key,
732  OutputType addr_type);
733 
734  bool HavePrivateKeys() const override;
735 
736  int64_t GetOldestKeyPoolTime() const override;
737  size_t KeypoolCountExternalKeys() const override;
738  unsigned int GetKeyPoolSize() const override;
739 
740  int64_t GetTimeFirstKey() const override;
741 
742  std::unique_ptr<CKeyMetadata>
743  GetMetadata(const CTxDestination &dest) const override;
744 
745  bool CanGetAddresses(bool internal = false) const override;
746 
747  std::unique_ptr<SigningProvider>
748  GetSolvingProvider(const CScript &script) const override;
749 
750  bool CanProvide(const CScript &script, SignatureData &sigdata) override;
751 
752  bool
754  const std::map<COutPoint, Coin> &coins, SigHashType sighash,
755  std::map<int, std::string> &input_errors) const override;
756  SigningResult SignMessage(const std::string &message, const PKHash &pkhash,
757  std::string &str_sig) const override;
760  SigHashType sighash_type = SigHashType().withForkId(),
761  bool sign = true, bool bip32derivs = false) const override;
762 
763  uint256 GetID() const override;
764 
765  void SetInternal(bool internal) override;
766 
767  void SetCache(const DescriptorCache &cache);
768 
769  bool AddKey(const CKeyID &key_id, const CKey &key);
770  bool AddCryptedKey(const CKeyID &key_id, const CPubKey &pubkey,
771  const std::vector<uint8_t> &crypted_key);
772 
773  bool HasWalletDescriptor(const WalletDescriptor &desc) const;
774  void UpdateWalletDescriptor(WalletDescriptor &descriptor);
775  bool CanUpdateToWalletDescriptor(const WalletDescriptor &descriptor,
776  std::string &error);
777  void AddDescriptorKey(const CKey &key, const CPubKey &pubkey);
778  void WriteDescriptor();
779 
782  const std::vector<CScript> GetScriptPubKeys() const;
783 };
784 
785 #endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
const CChainParams & Params()
Return the currently selected parameters.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
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:3099
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 outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
An encapsulated public key.
Definition: pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:24
A UTXO entry.
Definition: coins.h:27
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)
TransactionError FillPSBT(PartiallySignedTransaction &psbt, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=false) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
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
bool GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error) 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
const std::vector< CScript > GetScriptPubKeys() 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
const WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
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.
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
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)
WalletBatch *encrypted_batch 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 GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error) override
bool AddWatchOnlyInMem(const CScript &dest)
const CHDChain & GetHDChain() const
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)
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)
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
bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
WatchOnlySet setWatchOnly GUARDED_BY(cs_KeyStore)
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, without saving it to disk (used by LoadWallet)
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.
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.
TransactionError FillPSBT(PartiallySignedTransaction &psbt, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=false) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
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.
const std::map< CKeyID, int64_t > & GetAllReserveKeys() const
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 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 GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error)
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool)
virtual TransactionError FillPSBT(PartiallySignedTransaction &psbt, SigHashType sighash_type=SigHashType().withForkId(), bool sign=true, bool bip32derivs=false) const
Adds script and derivation path information to a PSBT, and optionally signs it.
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 std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
WalletStorage & m_storage
virtual std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const
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 ~ScriptPubKeyMan()
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
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 WalletDatabase & GetDatabase()=0
virtual bool IsLocked() const =0
virtual const CKeyingMaterial & GetEncryptionKey() const =0
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
virtual void SetMinVersion(enum WalletFeature, WalletBatch *=nullptr, bool=false)=0
virtual const std::string GetDisplayName() const =0
virtual ~WalletStorage()=default
virtual bool CanSupportFeature(enum WalletFeature) const =0
virtual const CChainParams & GetChainParams() const =0
256-bit opaque blob.
Definition: uint256.h:127
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
Definition: crypter.h:57
TransactionError
Definition: error.h:22
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_NO
Definition: ismine.h:19
#define LogPrintf(...)
Definition: logging.h:206
SigningResult
Definition: message.h:47
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
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)
@ SER_GETHASH
Definition: serialize.h:168
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
Definition: key.h:164
A version of CTransaction with the PSBT format.
Definition: psbt.h:334
Bilingual messages:
Definition: translation.h:17
bool error(const char *fmt, const Args &...args)
Definition: system.h:45
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetTime()
Definition: time.cpp:109
WalletFeature
(client) version numbers for particular wallet features
Definition: walletutil.h:14