5 #ifndef BITCOIN_WALLET_SCRIPTPUBKEYMAN_H 6 #define BITCOIN_WALLET_SCRIPTPUBKEYMAN_H 19 #include <boost/signals2/signal.hpp> 21 #include <unordered_map> 115 template<
typename Stream>
118 int nVersion = s.GetVersion();
122 s << nTime << vchPubKey << fInternal << m_pre_split;
125 template<
typename Stream>
128 int nVersion = s.GetVersion();
132 s >> nTime >> vchPubKey;
135 }
catch (std::ios_base::failure&) {
142 }
catch (std::ios_base::failure&) {
157 return id.GetUint64(0);
191 virtual bool TopUp(
unsigned int size = 0) {
return false; }
244 template<
typename...
Params>
260 bool fDecryptionThoroughlyChecked =
true;
267 using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
273 int64_t nTimeFirstKey
GUARDED_BY(cs_KeyStore) = 0;
275 bool AddKeyPubKeyInner(
const CKey& key,
const CPubKey &pubkey);
276 bool AddCryptedKeyInner(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
289 bool AddWatchOnlyInMem(
const CScript &dest);
296 void AddKeypoolPubkeyWithDB(
const CPubKey& pubkey,
const bool internal,
WalletBatch& batch);
311 std::set<int64_t> setInternalKeyPool
GUARDED_BY(cs_KeyStore);
312 std::set<int64_t> setExternalKeyPool
GUARDED_BY(cs_KeyStore);
313 std::set<int64_t> set_pre_split_keypool
GUARDED_BY(cs_KeyStore);
314 int64_t m_max_keypool_index
GUARDED_BY(cs_KeyStore) = 0;
317 std::map<int64_t, CKeyID> m_index_to_reserved_key;
336 bool ReserveKeyFromKeyPool(int64_t& nIndex,
CKeyPool& keypool,
bool fRequestedInternal);
348 bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index,
bool internal);
356 bool CheckDecryptionKey(const
CKeyingMaterial& master_key,
bool accept_no_keys = false) override;
360 void KeepDestination(int64_t index, const
OutputType& type) override;
361 void ReturnDestination(int64_t index,
bool internal, const
CTxDestination&) override;
363 bool TopUp(
unsigned int size = 0) override;
365 void MarkUnusedAddresses(const
CScript& script) override;
368 void UpgradeKeyMetadata();
370 bool IsHDEnabled() const override;
372 bool SetupGeneration(
bool force = false) override;
376 bool HavePrivateKeys() const override;
378 void RewriteDB() override;
380 int64_t GetOldestKeyPoolTime() const override;
381 size_t KeypoolCountExternalKeys() const override;
382 unsigned int GetKeyPoolSize() const override;
384 int64_t GetTimeFirstKey() const override;
388 bool CanGetAddresses(
bool internal = false) const override;
398 uint256 GetID() const override;
400 void SetInternal(
bool internal) override;
409 bool AddKeyPubKey(const
CKey& key, const
CPubKey &pubkey) override;
411 bool LoadKey(const
CKey& key, const
CPubKey &pubkey);
413 bool AddCryptedKey(const
CPubKey &vchPubKey, const
std::vector<
unsigned char> &vchCryptedSecret);
415 bool LoadCryptedKey(const
CPubKey &vchPubKey, const
std::vector<
unsigned char> &vchCryptedSecret,
bool checksum_valid);
418 bool LoadCScript(const
CScript& redeemScript);
420 void LoadKeyMetadata(const CKeyID& keyID, const
CKeyMetadata &metadata);
421 void LoadScriptMetadata(const CScriptID& script_id, const
CKeyMetadata &metadata);
426 void AddHDChain(const
CHDChain& chain);
428 void LoadHDChain(const
CHDChain& chain);
429 const
CHDChain& GetHDChain()
const {
return m_hd_chain; }
430 void AddInactiveHDChain(
const CHDChain& chain);
433 bool LoadWatchOnly(
const CScript &dest);
435 bool HaveWatchOnly(
const CScript &dest)
const;
437 bool HaveWatchOnly()
const;
439 bool RemoveWatchOnly(
const CScript &dest);
443 bool GetWatchPubKey(
const CKeyID &address,
CPubKey &pubkey_out)
const;
447 bool GetKey(
const CKeyID &address,
CKey& keyOut)
const override;
449 bool AddCScript(
const CScript& redeemScript)
override;
453 void LoadKeyPool(int64_t nIndex,
const CKeyPool &keypool);
458 bool ImportPrivKeys(
const std::map<CKeyID, CKey>& privkey_map,
const int64_t timestamp)
EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
459 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);
460 bool ImportScriptPubKeys(
const std::set<CScript>& script_pub_keys,
const bool have_solving_data,
const int64_t timestamp)
EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
463 bool CanGenerateKeys()
const;
475 void SetHDSeed(
const CPubKey& key);
489 void LearnAllRelatedScripts(
const CPubKey& key);
497 std::set<CKeyID> GetKeys()
const override;
523 using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
528 int32_t m_max_cached_index = -1;
530 bool m_internal =
false;
536 bool m_decryption_thoroughly_checked =
false;
552 m_wallet_descriptor(descriptor)
564 bool CheckDecryptionKey(
const CKeyingMaterial& master_key,
bool accept_no_keys =
false)
override;
568 void ReturnDestination(int64_t index,
bool internal,
const CTxDestination& addr)
override;
574 bool TopUp(
unsigned int size = 0)
override;
576 void MarkUnusedAddresses(
const CScript& script)
override;
578 bool IsHDEnabled()
const override;
583 bool HavePrivateKeys()
const override;
585 int64_t GetOldestKeyPoolTime()
const override;
586 size_t KeypoolCountExternalKeys()
const override;
587 unsigned int GetKeyPoolSize()
const override;
589 int64_t GetTimeFirstKey()
const override;
591 std::unique_ptr<CKeyMetadata> GetMetadata(
const CTxDestination& dest)
const override;
593 bool CanGetAddresses(
bool internal =
false)
const override;
595 std::unique_ptr<SigningProvider> GetSolvingProvider(
const CScript& script)
const override;
600 SigningResult SignMessage(
const std::string& message,
const PKHash& pkhash, std::string& str_sig)
const override;
603 uint256 GetID()
const override;
605 void SetInternal(
bool internal)
override;
610 bool AddCryptedKey(
const CKeyID& key_id,
const CPubKey& pubkey,
const std::vector<unsigned char>& crypted_key);
613 void AddDescriptorKey(
const CKey& key,
const CPubKey &pubkey);
614 void WriteDescriptor();
617 const
std::vector<
CScript> GetScriptPubKeys() const;
620 #endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H std::unordered_map< CKeyID, CHDChain, KeyIDHasher > m_inactive_hd_chains
bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const override
void SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, const UniValue &hashType, UniValue &result)
Sign a transaction with the given keystore and previous transactions.
const LegacyScriptPubKeyMan & m_spk_man
virtual uint256 GetID() const
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
virtual int64_t GetTimeFirstKey() const
virtual ~ScriptPubKeyMan()
static void LogPrintf(const char *fmt, const Args &...args)
RecursiveMutex cs_desc_man
virtual const CKeyingMaterial & GetEncryptionKey() const =0
size_t operator()(const CKeyID &id) const
virtual void SetMinVersion(enum WalletFeature, WalletBatch *=nullptr)=0
virtual bool HaveCScript(const CScriptID &hash) const override
bool HaveKey(const CKeyID &address) const override
virtual const std::string GetDisplayName() const =0
bool HaveKey(const SigningProvider &wallet, const CKey &key)
Checks if a CKey is in the given CWallet compressed or otherwise.
std::set< CScript > WatchOnlySet
DescriptorScriptPubKeyMan(WalletStorage &storage, bool internal)
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
boost::signals2::signal< void()> NotifyCanGetAddressesChanged
Keypool has new keys.
virtual void RewriteDB()
The action to do when the DB needs rewrite.
A version of CTransaction with the PSBT format.
virtual bool IsLocked() const =0
boost::signals2::signal< void(bool fHaveWatchOnly)> NotifyWatchonlyChanged
Watch-only address added.
void Unserialize(Stream &s)
WalletFeature
(client) version numbers for particular wallet features
Access to the wallet database.
virtual bool IsHDEnabled() const
virtual void KeepDestination(int64_t index, const OutputType &type)
virtual bool CanProvide(const CScript &script, SignatureData &sigdata)
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that...
bool error(const char *fmt, const Args &...args)
int64_t nTime
The time at which the key was generated. Set in AddKeypoolPubKeyWithDB.
static void AddKey(CWallet &wallet, const CKey &key)
virtual int64_t GetOldestKeyPoolTime() const
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
ScriptPubKeyMan(WalletStorage &storage)
virtual bool Upgrade(int prev_version, int new_version, bilingual_str &error)
Upgrades the wallet to the specified version.
virtual bool CanGetAddresses(bool internal=false) const
virtual isminetype IsMine(const CScript &script) const
virtual ~WalletStorage()=default
static const unsigned int DEFAULT_KEYPOOL_SIZE
Default for -keypool.
virtual bool HasEncryptionKeys() const =0
An encapsulated public key.
std::map< CKeyID, CPubKey > WatchKeyMap
Fillable signing provider that keeps keys in an address->secret map.
virtual void MarkUnusedAddresses(const CScript &script)
Mark unused addresses as being used.
Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr.
LegacySigningProvider(const LegacyScriptPubKeyMan &spk_man)
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::map< CScript, int32_t > ScriptPubKeyMap
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. it decrypts all of the ke...
bool m_pre_split
Whether this key was generated for a keypool before the wallet was upgraded to HD-split.
isminetype
IsMine() return codes.
Descriptor with some wallet metadata.
virtual bool CanSupportFeature(enum WalletFeature) const =0
An outpoint - a combination of a transaction hash and an index n into its vout.
bool GetKey(const CKeyID &address, CKey &key) const override
virtual TransactionError FillPSBT(PartiallySignedTransaction &psbt, int sighash_type=1, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr) const
Adds script and derivation path information to a PSBT, and optionally signs it.
virtual bool IsWalletFlagSet(uint64_t) const =0
const std::map< CKeyID, int64_t > & GetAllReserveKeys() const
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr)
CPubKey vchPubKey
The public key.
bool HaveCScript(const CScriptID &scriptid) const override
#define EXCLUSIVE_LOCKS_REQUIRED(...)
virtual unsigned int GetKeyPoolSize() const
virtual SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const
Sign a message with the given script.
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char >>> CryptedKeyMap
An interface to be implemented by keystores that support signing.
virtual std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
virtual bool TopUp(unsigned int size=0)
Fills internal address pool.
const CChainParams & Params()
Return the currently selected parameters.
Cache for single descriptor's derived extended pubkeys.
Serialized script, used inside transaction inputs and outputs.
std::map< CPubKey, int32_t > PubKeyMap
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
A reference to a CKey: the Hash160 of its serialized public key.
virtual bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch)
virtual void SetInternal(bool internal)
void Serialize(Stream &s) const
void WalletLogPrintf(std::string fmt, Params...parameters) const
Prepends the wallet name in logging output to ease debugging in multi-wallet use cases.
A reference to a CScript: the Hash160 of its serialization (see script.h)
A mutable version of CTransaction.
virtual std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const
std::map< CKeyID, CKey > KeyMap
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char >>> CryptedKeyMap
An encapsulated private key.
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool)
virtual WalletDatabase & GetDatabase() const =0
virtual bool HavePrivateKeys() const
WalletStorage & m_storage
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
virtual bool GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error)
int64_t GetTime()
Return system time (or mocked time, if set)
virtual bool SetupGeneration(bool force=false)
Sets up the key generation stuff, i.e.
bool fInternal
Whether this keypool entry is in the internal keypool (for change outputs)
virtual size_t KeypoolCountExternalKeys() const
virtual bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, std::string > &input_errors) const
Creates new signatures and adds them to the transaction.
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
A key from a CWallet's keypool.
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
An instance of this class represents one database.