Bitcoin Core  31.0.0
P2P Digital Currency
scriptpubkeyman.h
Go to the documentation of this file.
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 #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 <logging.h>
13 #include <musig.h>
14 #include <node/types.h>
15 #include <psbt.h>
16 #include <script/descriptor.h>
17 #include <script/script.h>
18 #include <script/signingprovider.h>
19 #include <util/result.h>
20 #include <util/time.h>
21 #include <wallet/crypter.h>
22 #include <wallet/types.h>
23 #include <wallet/walletdb.h>
24 #include <wallet/walletutil.h>
25 
26 #include <boost/signals2/signal.hpp>
27 
28 #include <functional>
29 #include <optional>
30 #include <unordered_map>
31 
32 enum class OutputType;
33 
34 namespace wallet {
35 struct MigrationData;
36 class ScriptPubKeyMan;
37 
38 // Wallet storage things that ScriptPubKeyMans need in order to be able to store things to the wallet database.
39 // It provides access to things that are part of the entire wallet and not specific to a ScriptPubKeyMan such as
40 // wallet flags, wallet version, encryption keys, encryption status, and the database itself. This allows a
41 // ScriptPubKeyMan to have callbacks into CWallet without causing a circular dependency.
42 // WalletStorage should be the same for all ScriptPubKeyMans of a wallet.
44 {
45 public:
46  virtual ~WalletStorage() = default;
47  virtual std::string LogName() const = 0;
48  virtual WalletDatabase& GetDatabase() const = 0;
49  virtual bool IsWalletFlagSet(uint64_t) const = 0;
50  virtual void UnsetBlankWalletFlag(WalletBatch&) = 0;
52  virtual bool WithEncryptionKey(std::function<bool (const CKeyingMaterial&)> cb) const = 0;
53  virtual bool HasEncryptionKeys() const = 0;
54  virtual bool IsLocked() const = 0;
56  virtual void TopUpCallback(const std::set<CScript>&, ScriptPubKeyMan*) = 0;
57 };
58 
60 static constexpr int64_t UNKNOWN_TIME = std::numeric_limits<int64_t>::max();
61 
63 static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;
64 
65 std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& provider);
66 
68 {
70  std::optional<bool> internal;
71 };
72 
73 /*
74  * A class implementing ScriptPubKeyMan manages some (or all) scriptPubKeys used in a wallet.
75  * It contains the scripts and keys related to the scriptPubKeys it manages.
76  * A ScriptPubKeyMan will be able to give out scriptPubKeys to be used, as well as marking
77  * when a scriptPubKey has been used. It also handles when and how to store a scriptPubKey
78  * and its related scripts and keys, including encryption.
79  */
81 {
82 protected:
84 
85 public:
86  explicit ScriptPubKeyMan(WalletStorage& storage) : m_storage(storage) {}
87  virtual ~ScriptPubKeyMan() = default;
88  virtual util::Result<CTxDestination> GetNewDestination(const OutputType type) { return util::Error{Untranslated("Not supported")}; }
89  virtual bool IsMine(const CScript& script) const { return false; }
90 
92  virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key) { return false; }
93  virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }
94 
95  virtual util::Result<CTxDestination> GetReservedDestination(const OutputType type, bool internal, int64_t& index) { return util::Error{Untranslated("Not supported")}; }
96  virtual void KeepDestination(int64_t index, const OutputType& type) {}
97  virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {}
98 
103  virtual bool TopUp(unsigned int size = 0) { return false; }
104 
112  virtual std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) { return {}; }
113 
114  /* Returns true if HD is enabled */
115  virtual bool IsHDEnabled() const { return false; }
116 
117  /* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
118  virtual bool CanGetAddresses(bool internal = false) const { return false; }
119 
120  virtual bool HavePrivateKeys() const { return false; }
121  virtual bool HaveCryptedKeys() const { return false; }
122 
124  virtual void RewriteDB() {}
125 
126  virtual unsigned int GetKeyPoolSize() const { return 0; }
127 
128  virtual int64_t GetTimeFirstKey() const { return 0; }
129 
130  virtual std::unique_ptr<CKeyMetadata> GetMetadata(const CTxDestination& dest) const { return nullptr; }
131 
132  virtual std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const { return nullptr; }
133 
137  virtual bool CanProvide(const CScript& script, SignatureData& sigdata) { return false; }
138 
140  virtual bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const { return false; }
142  virtual SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const { return SigningResult::SIGNING_FAILED; };
144  virtual std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, std::optional<int> sighash_type = std::nullopt, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const { return common::PSBTError::UNSUPPORTED; }
145 
146  virtual uint256 GetID() const { return uint256(); }
147 
149  virtual std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const { return {}; };
150 
152  template <typename... Params>
153  void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
154  {
155  LogInfo("[%s] %s", m_storage.LogName(), tfm::format(wallet_fmt, params...));
156  };
157 
159  boost::signals2::signal<void ()> NotifyCanGetAddressesChanged;
160 
162  boost::signals2::signal<void (const ScriptPubKeyMan* spkm, int64_t new_birth_time)> NotifyFirstKeyTimeChanged;
163 };
164 
166 static const std::unordered_set<OutputType> LEGACY_OUTPUT_TYPES {
170 };
171 
172 // Manages the data for a LegacyScriptPubKeyMan.
173 // This is the minimum necessary to load a legacy wallet so that it can be migrated.
175 {
176 private:
177  using WatchOnlySet = std::set<CScript>;
178  using WatchKeyMap = std::map<CKeyID, CPubKey>;
179  using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
180 
181  CryptedKeyMap mapCryptedKeys GUARDED_BY(cs_KeyStore);
182  WatchOnlySet setWatchOnly GUARDED_BY(cs_KeyStore);
183  WatchKeyMap mapWatchKeys GUARDED_BY(cs_KeyStore);
184 
185  /* the HD chain data model (external chain counters) */
187  std::unordered_map<CKeyID, CHDChain, SaltedSipHasher> m_inactive_hd_chains;
188 
191 
192  bool AddWatchOnlyInMem(const CScript &dest);
193  virtual bool AddKeyPubKeyInner(const CKey& key, const CPubKey &pubkey);
194  bool AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
195 
196  // Helper function to retrieve a conservative superset of all output scripts that may be relevant to this LegacyDataSPKM.
197  // It may include scripts that are invalid or not actually watched by this LegacyDataSPKM.
198  // Used only in migration.
199  std::unordered_set<CScript, SaltedSipHasher> GetCandidateScriptPubKeys() const;
200 
201  bool IsMine(const CScript& script) const override;
202  bool CanProvide(const CScript& script, SignatureData& sigdata) override;
203 public:
205 
206  // Map from Key ID to key metadata.
207  std::map<CKeyID, CKeyMetadata> mapKeyMetadata GUARDED_BY(cs_KeyStore);
208 
209  // Map from Script ID to key metadata (for watch-only keys).
210  std::map<CScriptID, CKeyMetadata> m_script_metadata GUARDED_BY(cs_KeyStore);
211 
212  // ScriptPubKeyMan overrides
213  bool CheckDecryptionKey(const CKeyingMaterial& master_key) override;
214  std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const override;
215  std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const override;
216  uint256 GetID() const override { return uint256::ONE; }
217 
218  // FillableSigningProvider overrides
219  bool HaveKey(const CKeyID &address) const override;
220  bool GetKey(const CKeyID &address, CKey& keyOut) const override;
221  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
222  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
223 
225  virtual void LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata &metadata);
226  virtual void LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata &metadata);
227 
229  bool LoadWatchOnly(const CScript &dest);
231  bool HaveWatchOnly(const CScript &dest) const;
233  bool LoadKey(const CKey& key, const CPubKey &pubkey);
235  bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid);
237  bool LoadCScript(const CScript& redeemScript);
239  void LoadHDChain(const CHDChain& chain);
240  void AddInactiveHDChain(const CHDChain& chain);
241  const CHDChain& GetHDChain() const { return m_hd_chain; }
242 
244  bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const;
245 
250  std::unordered_set<CScript, SaltedSipHasher> GetNotMineScriptPubKeys() const;
251 
254  std::optional<MigrationData> MigrateToDescriptor();
256  bool DeleteRecordsWithDB(WalletBatch& batch);
257 };
258 
261 {
262 private:
264 public:
265  explicit LegacySigningProvider(const LegacyDataSPKM& spk_man) : m_spk_man(spk_man) {}
266 
267  bool GetCScript(const CScriptID &scriptid, CScript& script) const override { return m_spk_man.GetCScript(scriptid, script); }
268  bool HaveCScript(const CScriptID &scriptid) const override { return m_spk_man.HaveCScript(scriptid); }
269  bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const override { return m_spk_man.GetPubKey(address, pubkey); }
270  bool GetKey(const CKeyID &address, CKey& key) const override { return false; }
271  bool HaveKey(const CKeyID &address) const override { return false; }
272  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override { return m_spk_man.GetKeyOrigin(keyid, info); }
273 };
274 
276 {
277  friend class LegacyDataSPKM;
278 private:
279  using ScriptPubKeyMap = std::map<CScript, int32_t>; // Map of scripts to descriptor range index
280  using PubKeyMap = std::map<CPubKey, int32_t>; // Map of pubkeys involved in scripts to descriptor range index
281  using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
282  using KeyMap = std::map<CKeyID, CKey>;
283 
284  ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man);
285  PubKeyMap m_map_pubkeys GUARDED_BY(cs_desc_man);
286  int32_t m_max_cached_index = -1;
287 
288  KeyMap m_map_keys GUARDED_BY(cs_desc_man);
289  CryptedKeyMap m_map_crypted_keys GUARDED_BY(cs_desc_man);
290 
293 
295  int64_t m_keypool_size GUARDED_BY(cs_desc_man){DEFAULT_KEYPOOL_SIZE};
296 
308  mutable std::map<uint256, MuSig2SecNonce> m_musig2_secnonces;
309 
310  bool AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
311 
313 
314  // Cached FlatSigningProviders to avoid regenerating them each time they are needed.
316  // Fetch the SigningProvider for the given script and optionally include private keys
317  std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const;
318  // Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions.
319  std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
320 
321 protected:
322  WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
323 
325  bool TopUpWithDB(WalletBatch& batch, unsigned int size = 0);
326 
327 public:
328  DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)
329  : ScriptPubKeyMan(storage),
330  m_keypool_size(keypool_size),
331  m_wallet_descriptor(descriptor)
332  {}
333  DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
334  : ScriptPubKeyMan(storage),
335  m_keypool_size(keypool_size)
336  {}
337 
339 
341  bool IsMine(const CScript& script) const override;
342 
343  bool CheckDecryptionKey(const CKeyingMaterial& master_key) override;
344  bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
345 
346  util::Result<CTxDestination> GetReservedDestination(OutputType type, bool internal, int64_t& index) override;
347  void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override;
348 
349  // Tops up the descriptor cache and m_map_script_pub_keys. The cache is stored in the wallet file
350  // and is used to expand the descriptor in GetNewDestination. DescriptorScriptPubKeyMan relies
351  // more on ephemeral data than LegacyScriptPubKeyMan. For wallets using unhardened derivation
352  // (with or without private keys), the "keypool" is a single xpub.
353  bool TopUp(unsigned int size = 0) override;
354 
355  std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) override;
356 
357  bool IsHDEnabled() const override;
358 
360  bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);
361 
362  bool HavePrivateKeys() const override;
363  bool HasPrivKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
365  std::optional<CKey> GetKey(const CKeyID& keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
366  bool HaveCryptedKeys() const override;
367 
368  unsigned int GetKeyPoolSize() const override;
369 
370  int64_t GetTimeFirstKey() const override;
371 
372  std::unique_ptr<CKeyMetadata> GetMetadata(const CTxDestination& dest) const override;
373 
374  bool CanGetAddresses(bool internal = false) const override;
375 
376  std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const override;
377 
378  bool CanProvide(const CScript& script, SignatureData& sigdata) override;
379 
380  // Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.
381  std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;
382 
383  bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;
384  SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
385  std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, std::optional<int> sighash_type = std::nullopt, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
386 
387  uint256 GetID() const override;
388 
389  void SetCache(const DescriptorCache& cache);
390 
391  bool AddKey(const CKeyID& key_id, const CKey& key);
392  bool AddCryptedKey(const CKeyID& key_id, const CPubKey& pubkey, const std::vector<unsigned char>& crypted_key);
393 
394  bool HasWalletDescriptor(const WalletDescriptor& desc) const;
396  bool CanUpdateToWalletDescriptor(const WalletDescriptor& descriptor, std::string& error);
397  void AddDescriptorKey(const CKey& key, const CPubKey &pubkey);
398  void WriteDescriptor();
399 
401  std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys() const override;
402  std::unordered_set<CScript, SaltedSipHasher> GetScriptPubKeys(int32_t minimum_index) const;
403  int32_t GetEndRange() const;
404 
405  [[nodiscard]] bool GetDescriptorString(std::string& out, bool priv) const;
406 
407  void UpgradeDescriptorCache();
408 };
409 
412 {
414  std::vector<std::pair<std::string, int64_t>> watch_descs;
415  std::vector<std::pair<std::string, int64_t>> solvable_descs;
416  std::vector<std::unique_ptr<DescriptorScriptPubKeyMan>> desc_spkms;
417  std::shared_ptr<CWallet> watchonly_wallet{nullptr};
418  std::shared_ptr<CWallet> solvable_wallet{nullptr};
419 };
420 
421 } // namespace wallet
422 
423 #endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
int64_t GetTimeFirstKey() const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
virtual SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const
Sign a message with the given script.
bool CheckDecryptionKey(const CKeyingMaterial &master_key) override
Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the ke...
bool CanProvide(const CScript &script, SignatureData &sigdata) override
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that...
bool HaveWatchOnly(const CScript &dest) const
Returns whether the watch-only script is in the wallet.
std::optional< CKey > GetKey(const CKeyID &keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
Retrieve the particular key if it is available. Returns nullopt if the key is not in the wallet...
virtual uint256 GetID() const
bool GetKey(const CKeyID &address, CKey &key) const override
static const uint256 ONE
Definition: uint256.h:204
bool CheckDecryptionKey(const CKeyingMaterial &master_key) override
Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the ke...
std::vector< WalletDestination > MarkUnusedAddresses(const CScript &script) override
Mark unused addresses as being used Affects all keys up to and including the one determined by provid...
bool SetupDescriptorGeneration(WalletBatch &batch, const CExtKey &master_key, OutputType addr_type, bool internal)
Setup descriptors based on the given CExtkey.
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
virtual bool IsWalletFlagSet(uint64_t) const =0
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
virtual WalletDatabase & GetDatabase() const =0
std::unordered_set< CScript, SaltedSipHasher > GetCandidateScriptPubKeys() const
virtual std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, std::optional< int > sighash_type=std::nullopt, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr, bool finalize=true) const
Adds script and derivation path information to a PSBT, and optionally signs it.
is a home for simple string functions returning descriptive messages that are used in RPC and GUI int...
virtual bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch)
RecursiveMutex cs_KeyStore
bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const override
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
bool DeleteRecordsWithDB(WalletBatch &batch)
Delete all the records of this LegacyScriptPubKeyMan from disk.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const override
Sign a message with the given script.
Definition: key.h:231
struct containing information needed for migrating legacy wallets to descriptor wallets ...
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, without saving it to disk (used by LoadWallet)
std::vector< std::pair< std::string, int64_t > > solvable_descs
std::map< CKeyID, CKey > KeyMap
bool CanUpdateToWalletDescriptor(const WalletDescriptor &descriptor, std::string &error)
DescriptorScriptPubKeyMan(WalletStorage &storage, int64_t keypool_size)
boost::signals2::signal< void()> NotifyCanGetAddressesChanged
Keypool has new keys.
virtual bool CheckDecryptionKey(const CKeyingMaterial &master_key)
Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the ke...
static const unsigned int DEFAULT_KEYPOOL_SIZE
Default for -keypool.
void WalletLogPrintf(util::ConstevalFormatString< sizeof...(Params)> wallet_fmt, const Params &... params) const
Prepends the wallet name in logging output to ease debugging in multi-wallet use cases.
std::vector< std::pair< std::string, int64_t > > watch_descs
void AddInactiveHDChain(const CHDChain &chain)
Definition: common.h:29
A version of CTransaction with the PSBT format.
Definition: psbt.h:1138
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
SigningResult
Definition: signmessage.h:43
void LoadHDChain(const CHDChain &chain)
Load a HD chain model (used by LoadWallet)
Access to the wallet database.
Definition: walletdb.h:192
boost::signals2::signal< void(const ScriptPubKeyMan *spkm, int64_t new_birth_time)> NotifyFirstKeyTimeChanged
Birth time changed.
bool AddKey(const CKeyID &key_id, const CKey &key)
virtual bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, bilingual_str > &input_errors) const
Creates new signatures and adds them to the transaction.
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
virtual bool IsLocked() const =0
virtual bool CanProvide(const CScript &script, SignatureData &sigdata)
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that...
unsigned int GetKeyPoolSize() const override
util::Result< CTxDestination > GetNewDestination(OutputType type) override
std::map< uint256, MuSig2SecNonce > m_musig2_secnonces
Map of a session id to MuSig2 secnonce.
std::map< CPubKey, int32_t > PubKeyMap
OutputType
Definition: outputtype.h:18
bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
Fetches a pubkey from mapWatchKeys if it exists there.
CryptedKeyMap mapCryptedKeys GUARDED_BY(cs_KeyStore)
bool CanGetAddresses(bool internal=false) const override
bool GetDescriptorString(std::string &out, bool priv) const
bool AddWatchOnlyInMem(const CScript &dest)
bool LoadWatchOnly(const CScript &dest)
Adds a watch-only address to the store, without saving it to disk (used by LoadWallet) ...
bool AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
void SetCache(const DescriptorCache &cache)
bool AddCryptedKey(const CKeyID &key_id, const CPubKey &pubkey, const std::vector< unsigned char > &crypted_key)
WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
virtual std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
std::map< CKeyID, CPubKey > WatchKeyMap
ScriptPubKeyMan(WalletStorage &storage)
bool GetKey(const CKeyID &address, CKey &keyOut) const override
virtual ~ScriptPubKeyMan()=default
static constexpr int64_t UNKNOWN_TIME
Constant representing an unknown spkm creation time.
bool CanProvide(const CScript &script, SignatureData &sigdata) override
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that...
void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr) override
virtual bool HasEncryptionKeys() const =0
bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch) override
Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr.
#define LogInfo(...)
Definition: log.h:95
bool HaveKey(const CKeyID &address) const override
virtual std::vector< WalletDestination > MarkUnusedAddresses(const CScript &script)
Mark unused addresses as being used Affects all keys up to and including the one determined by provid...
std::optional< common::PSBTError > FillPSBT(PartiallySignedTransaction &psbt, const PrecomputedTransactionData &txdata, std::optional< int > sighash_type=std::nullopt, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr, bool finalize=true) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
An encapsulated public key.
Definition: pubkey.h:33
Fillable signing provider that keeps keys in an address->secret map.
virtual bool IsMine(const CScript &script) const
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, bool checksum_valid)
Adds an encrypted key to the store, without saving it to disk (used by LoadWallet) ...
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
is a home for simple enum and struct type definitions that can be used internally by functions in the...
ScriptPubKeyMap m_map_script_pub_keys GUARDED_BY(cs_desc_man)
std::map< CScript, int32_t > ScriptPubKeyMap
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::unordered_set< CScript, SaltedSipHasher > GetScriptPubKeys() const override
Returns a set of all the scriptPubKeys that this ScriptPubKeyMan watches.
std::optional< MigrationData > MigrateToDescriptor()
Get the DescriptorScriptPubKeyMans (with private keys) that have the same scriptPubKeys as this Legac...
virtual bool IsHDEnabled() const
util::Result< CTxDestination > GetReservedDestination(OutputType type, bool internal, int64_t &index) override
virtual void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata)
Load metadata (used by LoadWallet)
A wrapper for a compile-time partially validated format string.
Definition: base.h:41
virtual std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const
static const std::unordered_set< OutputType > LEGACY_OUTPUT_TYPES
OutputTypes supported by the LegacyScriptPubKeyMan.
is a home for public enum and struct type definitions that are used by internally by wallet code...
virtual void TopUpCallback(const std::set< CScript > &, ScriptPubKeyMan *)=0
Callback function for after TopUp completes containing any scripts that were added by a SPKMan...
Descriptor with some wallet metadata.
Definition: walletutil.h:63
virtual bool HavePrivateKeys() const
bool HaveCScript(const CScriptID &scriptid) const override
virtual void KeepDestination(int64_t index, const OutputType &type)
virtual unsigned int GetKeyPoolSize() const
virtual bool AddKeyPubKeyInner(const CKey &key, const CPubKey &pubkey)
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
virtual void RewriteDB()
The action to do when the DB needs rewrite.
static int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
Definition: musig.c:106
uint256 GetID() const override
virtual void LoadScriptMetadata(const CScriptID &script_id, const CKeyMetadata &metadata)
virtual bool CanGetAddresses(bool internal=false) const
256-bit opaque blob.
Definition: uint256.h:195
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
virtual bool WithEncryptionKey(std::function< bool(const CKeyingMaterial &)> cb) const =0
Pass the encryption key to cb().
An interface to be implemented by keystores that support signing.
util::Result< void > UpdateWalletDescriptor(WalletDescriptor &descriptor)
virtual bool HaveCryptedKeys() const
bool IsMine(const CScript &script) const override
const CChainParams & Params()
Return the currently selected parameters.
Cache for single descriptor&#39;s derived extended pubkeys.
Definition: descriptor.h:19
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > >> CryptedKeyMap
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > >> CryptedKeyMap
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
std::map< int32_t, FlatSigningProvider > m_map_signing_providers
std::set< CScript > WatchOnlySet
bool m_decryption_thoroughly_checked
keeps track of whether Unlock has run a thorough check before
virtual std::string LogName() const =0
virtual bool HaveCScript(const CScriptID &hash) const override
void AddDescriptorKey(const CKey &key, const CPubKey &pubkey)
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:593
A mutable version of CTransaction.
Definition: transaction.h:357
bool AddDescriptorKeyWithDB(WalletBatch &batch, const CKey &key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const override
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr)
An encapsulated private key.
Definition: key.h:35
virtual int64_t GetTimeFirstKey() const
bool HasPrivKey(const CKeyID &keyid) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
std::unordered_map< CKeyID, CHDChain, SaltedSipHasher > m_inactive_hd_chains
virtual bool TopUp(unsigned int size=0)
Fills internal address pool.
virtual std::unordered_set< CScript, SaltedSipHasher > GetScriptPubKeys() const
Returns a set of all the scriptPubKeys that this ScriptPubKeyMan watches.
is a home for public enum and struct type definitions that are used internally by node code...
bool HaveKey(const CKeyID &address) const override
virtual util::Result< CTxDestination > GetNewDestination(const OutputType type)
bool LoadCScript(const CScript &redeemScript)
Adds a CScript to the store.
std::unordered_set< CScript, SaltedSipHasher > GetNotMineScriptPubKeys() const
Retrieves scripts that were imported by bugs into the legacy spkm and are simply invalid, such as a sh(sh(pkh())) script, or not watched.
std::unordered_set< CScript, SaltedSipHasher > GetScriptPubKeys() const override
Returns a set of all the scriptPubKeys that this ScriptPubKeyMan watches.
virtual ~WalletStorage()=default
const CHDChain & GetHDChain() const
LegacySigningProvider(const LegacyDataSPKM &spk_man)
bool IsMine(const CScript &script) const override
bool TopUpWithDB(WalletBatch &batch, unsigned int size=0)
Same as &#39;TopUp&#39; but designed for use within a batch transaction context.
bool HasWalletDescriptor(const WalletDescriptor &desc) const
An instance of this class represents one database.
Definition: db.h:129
bool TopUp(unsigned int size=0) override
Fills internal address pool.
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:63
std::vector< std::unique_ptr< DescriptorScriptPubKeyMan > > desc_spkms
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, bilingual_str > &input_errors) const override
Creates new signatures and adds them to the transaction.
virtual util::Result< CTxDestination > GetReservedDestination(const OutputType type, bool internal, int64_t &index)
const LegacyDataSPKM & m_spk_man
std::unique_ptr< FlatSigningProvider > GetSigningProvider(const CScript &script, bool include_private=false) const
WalletStorage & m_storage