5#ifndef BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
6#define BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
20#include <boost/signals2/signal.hpp>
23#include <unordered_map>
126 template <
typename Stream>
void Serialize(Stream &s)
const {
127 int nVersion = s.GetVersion();
135 int nVersion = s.GetVersion();
142 }
catch (std::ios_base::failure &) {
152 }
catch (std::ios_base::failure &) {
179 std::string &
error) {
187 bool accept_no_keys =
false) {
211 virtual bool TopUp(
unsigned int size = 0) {
return false; }
250 virtual std::unique_ptr<CKeyMetadata>
255 virtual std::unique_ptr<SigningProvider>
275 const std::map<COutPoint, Coin> &coins,
SigHashType sighash,
276 std::map<int, std::string> &input_errors)
const {
282 std::string &str_sig)
const {
292 bool sign =
true,
bool bip32derivs =
false)
const {
304 template <
typename...
Params>
329 std::map<CKeyID, std::pair<CPubKey, std::vector<uint8_t>>>;
339 const std::vector<uint8_t> &vchCryptedSecret);
382 bool internal =
false)
396 bool internal = false);
413 bool fRequestedInternal);
439 bool accept_no_keys = false) override;
450 bool TopUp(
unsigned int size = 0) override;
487 std::map<
int,
std::
string> &input_errors) const override;
489 std::
string &str_sig) const override;
493 bool sign = true,
bool bip32derivs = false) const override;
511 const
std::vector<uint8_t> &vchCryptedSecret);
515 const
std::vector<uint8_t> &vchCryptedSecret,
516 bool checksum_valid);
527 bool internal = false)
549 bool AddWatchOnly(
const CScript &dest, int64_t nCreateTime)
559 bool AddCScript(
const CScript &redeemScript)
override;
570 const int64_t timestamp)
573 const
std::vector<
CKeyID> &ordered_pubkeys,
576 const
bool add_keypool, const
bool internal, const int64_t timestamp)
579 const
bool have_solving_data,
580 const int64_t timestamp)
621 std::set<CKeyID>
GetKeys()
const override;
663 std::map<CKeyID, std::pair<CPubKey, std::vector<uint8_t>>>;
688 bool include_private = false) const;
709 std::string &
error)
override;
713 bool accept_no_keys =
false)
override;
728 bool TopUp(
unsigned int size = 0)
override;
746 std::unique_ptr<CKeyMetadata>
751 std::unique_ptr<SigningProvider>
758 const std::map<COutPoint, Coin> &coins,
SigHashType sighash,
759 std::map<int, std::string> &input_errors)
const override;
761 std::string &str_sig)
const override;
765 bool sign =
true,
bool bip32derivs =
false)
const override;
775 const std::vector<uint8_t> &crypted_key);
const CChainParams & Params()
Return the currently selected parameters.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
An encapsulated secp256k1 private key.
A reference to a CKey: the Hash160 of its serialized public key.
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.
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.
An encapsulated public key.
A reference to a CScript: the Hash160 of its serialization (see script.h)
Cache for single descriptor's derived extended pubkeys.
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
RecursiveMutex cs_desc_man
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
int32_t m_max_cached_index
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
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 GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error) override
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.
bool CanGenerateKeys() const
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)
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.
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.
CPubKey GenerateNewSeed()
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 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 std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const
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.
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 ~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.
An interface to be implemented by keystores that support signing.
Access to the wallet database.
An instance of this class represents one database.
Descriptor with some wallet metadata.
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 bool IsLocked() 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 const CChainParams & GetChainParams() const =0
virtual bool CanSupportFeature(enum WalletFeature) const =0
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
isminetype
IsMine() return codes.
bool error(const char *fmt, const Args &...args)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
static const unsigned int DEFAULT_KEYPOOL_SIZE
Default for -keypool.
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
A version of CTransaction with the PSBT format.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
WalletFeature
(client) version numbers for particular wallet features