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();
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>
267 using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
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;
363 bool TopUp(
unsigned int size = 0) override;
415 bool LoadCryptedKey(const
CPubKey &vchPubKey, const
std::vector<
unsigned char> &vchCryptedSecret,
bool checksum_valid);
497 std::set<CKeyID>
GetKeys()
const override;
523 using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
552 m_wallet_descriptor(descriptor)
574 bool TopUp(
unsigned int size = 0)
override;
620 #endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
void ReturnDestination(int64_t index, bool internal, const CTxDestination &) override
std::unordered_map< CKeyID, CHDChain, KeyIDHasher > m_inactive_hd_chains
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) ...
bool NewKeyPool()
Mark old keypool keys as used, and generate all new keys.
bool AddKeyPubKeyInner(const CKey &key, const CPubKey &pubkey)
void LoadScriptMetadata(const CScriptID &script_id, const CKeyMetadata &metadata)
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 KeepDestination(int64_t index, const OutputType &type) override
bool AddCryptedKey(const CKeyID &key_id, const CPubKey &pubkey, const std::vector< unsigned char > &crypted_key)
const LegacyScriptPubKeyMan & m_spk_man
virtual uint256 GetID() const
void RewriteDB() override
The action to do when the DB needs rewrite.
bool GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error) override
virtual const CKeyingMaterial & GetEncryptionKey() const =0
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
virtual void UnsetBlankWalletFlag(WalletBatch &)=0
TransactionError FillPSBT(PartiallySignedTransaction &psbt, int sighash_type=1, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
void LearnAllRelatedScripts(const CPubKey &key)
Same as LearnRelatedScripts, but when the OutputType is not known (and could be anything).
bool Upgrade(int prev_version, int new_version, bilingual_str &error) override
Upgrades the wallet to the specified version.
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.
bool ImportScriptPubKeys(const std::set< CScript > &script_pub_keys, const bool have_solving_data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
bool LoadCScript(const CScript &redeemScript)
Adds a CScript to the store.
bool AddKeyOriginWithDB(WalletBatch &batch, const CPubKey &pubkey, const KeyOriginInfo &info)
Add a KeyOriginInfo to the wallet.
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, std::string > &input_errors) const override
Creates new signatures and adds them to the transaction.
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
virtual int64_t GetTimeFirstKey() const
void SetInternal(bool internal) override
virtual const std::string GetDisplayName() const =0
bool CanGenerateKeys() const
isminetype IsMine(const CScript &script) const override
virtual ~ScriptPubKeyMan()
RecursiveMutex cs_KeyStore
uint256 GetID() const override
static void LogPrintf(const char *fmt, const Args &...args)
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. it decrypts all of the ke...
RecursiveMutex cs_desc_man
int64_t GetOldestKeyPoolTime() const override
void AddKeypoolPubkeyWithDB(const CPubKey &pubkey, const bool internal, WalletBatch &batch)
std::unique_ptr< FlatSigningProvider > GetSigningProvider(const CScript &script, bool include_private=false) const
bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const override
const std::vector< CScript > GetScriptPubKeys() const
size_t operator()(const CKeyID &id) const
virtual void SetMinVersion(enum WalletFeature, WalletBatch *=nullptr)=0
bool CanProvide(const CScript &script, SignatureData &sigdata) override
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that...
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.
bool SetupDescriptorGeneration(const CExtKey &master_key, OutputType addr_type)
Setup descriptors based on the given CExtkey.
unsigned int GetKeyPoolSize() const override
bool CanGetAddresses(bool internal=false) const override
virtual void RewriteDB()
The action to do when the DB needs rewrite.
bool HasWalletDescriptor(const WalletDescriptor &desc) const
bool GetKey(const CKeyID &address, CKey &key) const override
uint256 GetID() const override
bool HaveKey(const CKeyID &address) const override
void LearnRelatedScripts(const CPubKey &key, OutputType)
Explicitly make the wallet learn the related scripts for outputs to the given key.
A version of CTransaction with the PSBT format.
void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata)
Load metadata (used by LoadWallet)
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
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)
CPubKey GenerateNewKey(WalletBatch &batch, CHDChain &hd_chain, bool internal=false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Generate a new key.
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool) override
boost::signals2::signal< void(bool fHaveWatchOnly)> NotifyWatchonlyChanged
Watch-only address added.
const WalletDescriptor GetWalletDescriptor() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
virtual WalletDatabase & GetDatabase() const =0
virtual bool IsWalletFlagSet(uint64_t) const =0
bool RemoveWatchOnly(const CScript &dest)
Remove a watch only script from the keystore.
std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const override
KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
void Unserialize(Stream &s)
WalletFeature
(client) version numbers for particular wallet features
bool IsHDEnabled() const override
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Adds a key to the store, and saves it to disk.
void AddInactiveHDChain(const CHDChain &chain)
Access to the wallet database.
TransactionError FillPSBT(PartiallySignedTransaction &psbt, int sighash_type=1, bool sign=true, bool bip32derivs=false, int *n_signed=nullptr) const override
Adds script and derivation path information to a PSBT, and optionally signs it.
void MarkPreSplitKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual bool IsHDEnabled() const
bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch) override
int64_t GetOldestKeyPoolTime() const override
bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual void KeepDestination(int64_t index, const OutputType &type)
CPubKey GenerateNewSeed()
unsigned int GetKeyPoolSize() const override
bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index, bool internal)
Like TopUp() but adds keys for inactive HD chains.
bool IsHDEnabled() const override
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)
size_t KeypoolCountExternalKeys() const override
int64_t nTime
The time at which the key was generated. Set in AddKeypoolPubKeyWithDB.
void UpdateTimeFirstKey(int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Update wallet first key creation time.
virtual int64_t GetOldestKeyPoolTime() const
bool CanProvide(const CScript &script, SignatureData &sigdata) override
Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSolvingProvider) that...
ScriptPubKeyMan(WalletStorage &storage)
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Adds an encrypted key to the store, and saves it to disk.
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
bool AddWatchOnlyInMem(const CScript &dest)
virtual isminetype IsMine(const CScript &script) const
virtual ~WalletStorage()=default
void MarkUnusedAddresses(const CScript &script) override
Mark unused addresses as being used.
static const unsigned int DEFAULT_KEYPOOL_SIZE
Default for -keypool.
bool AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
bool TopUp(unsigned int size=0) override
Fills internal address pool.
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)
void SetHDSeed(const CPubKey &key)
void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr) 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.
bool GetKey(const CKeyID &address, CKey &keyOut) const override
isminetype
IsMine() return codes.
void MarkUnusedAddresses(const CScript &script) override
Mark unused addresses as being used.
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man)
Descriptor with some wallet metadata.
bool AddKey(const CKeyID &key_id, const CKey &key)
An outpoint - a combination of a transaction hash and an index n into its vout.
bool LoadWatchOnly(const CScript &dest)
Adds a watch-only address to the store, without saving it to disk (used by LoadWallet) ...
bool ReserveKeyFromKeyPool(int64_t &nIndex, CKeyPool &keypool, bool fRequestedInternal)
Reserves a key from the keypool and sets nIndex to its index.
bool GetKeyFromPool(CPubKey &key, const OutputType type, bool internal=false)
Fetches a key from the keypool.
virtual bool IsLocked() const =0
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.
bool HavePrivateKeys() const override
std::map< int64_t, CKeyID > m_index_to_reserved_key
bool AddCScript(const CScript &redeemScript) override
int64_t GetTimeFirstKey() const override
const std::map< CKeyID, int64_t > & GetAllReserveKeys() const
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination &addr)
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool) override
CPubKey vchPubKey
The public key.
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Adds a key to the store, without saving it to disk (used by LoadWallet)
virtual bool CanSupportFeature(enum WalletFeature) const =0
const CHDChain & GetHDChain() const
void SetCache(const DescriptorCache &cache)
#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.
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
WalletBatch *encrypted_batch GUARDED_BY(cs_KeyStore)
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.
bool GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error) override
bool HaveKey(const CKeyID &address) const override
Serialized script, used inside transaction inputs and outputs.
std::map< CPubKey, int32_t > PubKeyMap
void AddHDChain(const CHDChain &chain)
bool HaveCScript(const CScriptID &scriptid) const override
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool)
Load a keypool entry.
A reference to a CKey: the Hash160 of its serialized public key.
virtual bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch)
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. it decrypts all of the ke...
virtual void SetInternal(bool internal)
bool CanGetAddresses(bool internal=false) const override
std::unique_ptr< SigningProvider > GetSolvingProvider(const CScript &script) const override
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.
virtual bool HaveCScript(const CScriptID &hash) const override
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...
void UpgradeKeyMetadata()
Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo.
CPubKey DeriveNewSeed(const CKey &key)
int64_t GetTimeFirstKey() const override
A reference to a CScript: the Hash160 of its serialization (see script.h)
bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
Fetches a pubkey from mapWatchKeys if it exists there.
virtual bool HasEncryptionKeys() const =0
A mutable version of CTransaction.
bool SignTransaction(CMutableTransaction &tx, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, std::string > &input_errors) const override
Creates new signatures and adds them to the transaction.
virtual std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const
void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata, CKey &secret, CHDChain &hd_chain, bool internal=false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
bool SetupGeneration(bool force=false) override
Sets up the key generation stuff, i.e.
bool Encrypt(const CKeyingMaterial &master_key, WalletBatch *batch) override
void SetInternal(bool internal) override
bool HavePrivateKeys() const override
std::map< CKeyID, CKey > KeyMap
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char >>> CryptedKeyMap
void AddDescriptorKey(const CKey &key, const CPubKey &pubkey)
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const override
Sign a message with the given script.
An encapsulated private key.
std::unique_ptr< CKeyMetadata > GetMetadata(const CTxDestination &dest) const override
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination &address, int64_t &index, CKeyPool &keypool)
bool AddDescriptorKeyWithDB(WalletBatch &batch, const CKey &key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)
bool TopUp(unsigned int size=0) override
Fills internal address pool.
virtual bool HavePrivateKeys() const
WalletStorage & m_storage
bool m_decryption_thoroughly_checked
keeps track of whether Unlock has run a thorough check before
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
bool HaveWatchOnly() const
Returns whether there are any watch-only things in the wallet.
virtual bool GetNewDestination(const OutputType type, CTxDestination &dest, std::string &error)
bool ImportPrivKeys(const std::map< CKeyID, CKey > &privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
int64_t GetTime()
Return system time (or mocked time, if set)
void LoadHDChain(const CHDChain &chain)
Load a HD chain model (used by LoadWallet)
virtual bool SetupGeneration(bool force=false)
Sets up the key generation stuff, i.e.
int32_t m_max_cached_index
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
bool AddCScriptWithDB(WalletBatch &batch, const CScript &script)
Adds a script to the store and saves it to disk.
size_t KeypoolCountExternalKeys() const override
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
bool fInternal
Whether this keypool entry is in the internal keypool (for change outputs)
std::set< CKeyID > GetKeys() const override
virtual size_t KeypoolCountExternalKeys() const
isminetype IsMine(const CScript &script) const override
bool ImportScripts(const std::set< CScript > scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
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)
std::map< CKeyID, int64_t > m_pool_key_to_index
A key from a CWallet's keypool.
SigningResult SignMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig) const override
Sign a message with the given script.
An instance of this class represents one database.