@@ -150,36 +150,22 @@ class ScriptPubKeyMan
150150class LegacyScriptPubKeyMan : public ScriptPubKeyMan , public FillableSigningProvider
151151{
152152private:
153- using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char >>>;
154153 using WatchOnlySet = std::set<CScript>;
155154 using WatchKeyMap = std::map<CKeyID, CPubKey>;
156155
157- // ! will encrypt previously unencrypted keys
158- bool EncryptKeys (CKeyingMaterial& vMasterKeyIn);
156+ WalletBatch *encrypted_batch GUARDED_BY (cs_wallet) = nullptr;
157+
158+ using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char >>>;
159159
160160 CryptedKeyMap mapCryptedKeys GUARDED_BY (cs_KeyStore);
161161 WatchOnlySet setWatchOnly GUARDED_BY (cs_KeyStore);
162162 WatchKeyMap mapWatchKeys GUARDED_BY (cs_KeyStore);
163163
164- bool AddCryptedKeyInner (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret);
165- bool AddKeyPubKeyInner (const CKey& key, const CPubKey &pubkey);
166-
167- WalletBatch *encrypted_batch GUARDED_BY (cs_wallet) = nullptr;
168-
169- /* the HD chain data model (external chain counters) */
170- CHDChain hdChain;
171-
172- /* HD derive new child key (on internal or external chain) */
173- void DeriveNewChildKey (WalletBatch& batch, CKeyMetadata& metadata, CKey& secret, bool internal = false ) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
174-
175- std::set<int64_t > setInternalKeyPool GUARDED_BY (cs_wallet);
176- std::set<int64_t > setExternalKeyPool GUARDED_BY (cs_wallet);
177- std::set<int64_t > set_pre_split_keypool GUARDED_BY (cs_wallet);
178- int64_t m_max_keypool_index GUARDED_BY (cs_wallet) = 0;
179- std::map<CKeyID, int64_t > m_pool_key_to_index;
180-
181164 int64_t nTimeFirstKey GUARDED_BY (cs_wallet) = 0;
182165
166+ bool AddKeyPubKeyInner (const CKey& key, const CPubKey &pubkey);
167+ bool AddCryptedKeyInner (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret);
168+
183169 /* *
184170 * Private version of AddWatchOnly method which does not accept a
185171 * timestamp, and which will reset the wallet's nTimeFirstKey value to 1 if
@@ -192,121 +178,130 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
192178 bool AddWatchOnly (const CScript& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
193179 bool AddWatchOnlyWithDB (WalletBatch &batch, const CScript& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
194180 bool AddWatchOnlyInMem (const CScript &dest);
195-
196- /* * Add a KeyOriginInfo to the wallet */
197- bool AddKeyOriginWithDB (WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info);
181+ // ! Adds a watch-only address to the store, and saves it to disk.
182+ bool AddWatchOnlyWithDB (WalletBatch &batch, const CScript& dest, int64_t create_time) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
198183
199184 // ! Adds a key to the store, and saves it to disk.
200185 bool AddKeyPubKeyWithDB (WalletBatch &batch,const CKey& key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
201186
202- // ! Adds a watch-only address to the store, and saves it to disk.
203- bool AddWatchOnlyWithDB (WalletBatch &batch, const CScript& dest, int64_t create_time) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
204-
205187 void AddKeypoolPubkeyWithDB (const CPubKey& pubkey, const bool internal, WalletBatch& batch);
206188
207189 // ! Adds a script to the store and saves it to disk
208190 bool AddCScriptWithDB (WalletBatch& batch, const CScript& script);
209191
210- public:
192+ /* * Add a KeyOriginInfo to the wallet */
193+ bool AddKeyOriginWithDB (WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info);
194+
195+ /* the HD chain data model (external chain counters) */
196+ CHDChain hdChain;
197+
198+ /* HD derive new child key (on internal or external chain) */
199+ void DeriveNewChildKey (WalletBatch& batch, CKeyMetadata& metadata, CKey& secret, bool internal = false ) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
200+
201+ std::set<int64_t > setInternalKeyPool GUARDED_BY (cs_wallet);
202+ std::set<int64_t > setExternalKeyPool GUARDED_BY (cs_wallet);
203+ std::set<int64_t > set_pre_split_keypool GUARDED_BY (cs_wallet);
204+ int64_t m_max_keypool_index GUARDED_BY (cs_wallet) = 0;
205+ std::map<CKeyID, int64_t > m_pool_key_to_index;
206+
211207 // ! Fetches a key from the keypool
212208 bool GetKeyFromPool (CPubKey &key, bool internal = false );
213- void LoadKeyPool (int64_t nIndex, const CKeyPool &keypool) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
214- void MarkPreSplitKeys () EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
209+
210+ /* *
211+ * Reserves a key from the keypool and sets nIndex to its index
212+ *
213+ * @param[out] nIndex the index of the key in keypool
214+ * @param[out] keypool the keypool the key was drawn from, which could be the
215+ * the pre-split pool if present, or the internal or external pool
216+ * @param fRequestedInternal true if the caller would like the key drawn
217+ * from the internal keypool, false if external is preferred
218+ *
219+ * @return true if succeeded, false if failed due to empty keypool
220+ * @throws std::runtime_error if keypool read failed, key was invalid,
221+ * was not found in the wallet, or was misclassified in the internal
222+ * or external keypool
223+ */
224+ bool ReserveKeyFromKeyPool (int64_t & nIndex, CKeyPool& keypool, bool fRequestedInternal );
225+
226+ void KeepKey (int64_t nIndex);
227+ void ReturnKey (int64_t nIndex, bool fInternal , const CPubKey& pubkey);
228+
229+ public:
230+ bool GetNewDestination (const OutputType type, const std::string label, CTxDestination& dest, std::string& error);
231+ isminetype IsMine (const CScript& script) const ;
232+
233+ // ! will encrypt previously unencrypted keys
234+ bool EncryptKeys (CKeyingMaterial& vMasterKeyIn);
235+ void UpgradeKeyMetadata () EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
236+
237+ bool IsHDEnabled () const ;
238+
239+ int64_t GetOldestKeyPoolTime ();
240+ size_t KeypoolCountExternalKeys () EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
241+
242+ /* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
243+ bool CanGetAddresses (bool internal = false );
215244
216245 // Map from Key ID to key metadata.
217246 std::map<CKeyID, CKeyMetadata> mapKeyMetadata GUARDED_BY (cs_wallet);
218247
219248 // Map from Script ID to key metadata (for watch-only keys).
220249 std::map<CScriptID, CKeyMetadata> m_script_metadata GUARDED_BY (cs_wallet);
221250
222- /* *
223- * keystore implementation
224- * Generate a new key
225- */
226- CPubKey GenerateNewKey (WalletBatch& batch, bool internal = false ) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
227251 // ! Adds a key to the store, and saves it to disk.
228252 bool AddKeyPubKey (const CKey& key, const CPubKey &pubkey) override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
229253 // ! Adds a key to the store, without saving it to disk (used by LoadWallet)
230254 bool LoadKey (const CKey& key, const CPubKey &pubkey) { return AddKeyPubKeyInner (key, pubkey); }
231- // ! Load metadata (used by LoadWallet)
232- void LoadKeyMetadata (const CKeyID& keyID, const CKeyMetadata &metadata) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
233- void LoadScriptMetadata (const CScriptID& script_id, const CKeyMetadata &metadata) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
234- // ! Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo
235- void UpgradeKeyMetadata () EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
236- void UpdateTimeFirstKey (int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
237-
238255 // ! Adds an encrypted key to the store, and saves it to disk.
239256 bool AddCryptedKey (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret);
240257 // ! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
241258 bool LoadCryptedKey (const CPubKey &vchPubKey, const std::vector<unsigned char > &vchCryptedSecret);
242- bool GetKey (const CKeyID &address, CKey& keyOut) const override ;
243- bool GetPubKey (const CKeyID &address, CPubKey& vchPubKeyOut) const override ;
244- bool HaveKey (const CKeyID &address) const override ;
245- std::set<CKeyID> GetKeys () const override ;
246- bool AddCScript (const CScript& redeemScript) override ;
259+ void UpdateTimeFirstKey (int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
260+ // ! Adds a CScript to the store
247261 bool LoadCScript (const CScript& redeemScript);
262+ // ! Load metadata (used by LoadWallet)
263+ void LoadKeyMetadata (const CKeyID& keyID, const CKeyMetadata &metadata) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
264+ void LoadScriptMetadata (const CScriptID& script_id, const CKeyMetadata &metadata) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
265+ // ! Generate a new key
266+ CPubKey GenerateNewKey (WalletBatch& batch, bool internal = false ) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
267+
268+ /* Set the HD chain model (chain child index counters) */
269+ void SetHDChain (const CHDChain& chain, bool memonly);
270+ const CHDChain& GetHDChain () const { return hdChain; }
248271
249- // ! Adds a watch-only address to the store, and saves it to disk.
250- bool AddWatchOnly (const CScript& dest, int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
251- bool RemoveWatchOnly (const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
252272 // ! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
253273 bool LoadWatchOnly (const CScript &dest);
254274 // ! Returns whether the watch-only script is in the wallet
255275 bool HaveWatchOnly (const CScript &dest) const ;
256276 // ! Returns whether there are any watch-only things in the wallet
257277 bool HaveWatchOnly () const ;
278+ // ! Remove a watch only script from the keystore
279+ bool RemoveWatchOnly (const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
280+ bool AddWatchOnly (const CScript& dest, int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
281+
258282 // ! Fetches a pubkey from mapWatchKeys if it exists there
259283 bool GetWatchPubKey (const CKeyID &address, CPubKey &pubkey_out) const ;
260284
285+ bool HaveKey (const CKeyID &address) const override ;
286+ bool GetKey (const CKeyID &address, CKey& keyOut) const override ;
287+ bool GetPubKey (const CKeyID &address, CPubKey& vchPubKeyOut) const override ;
288+ bool AddCScript (const CScript& redeemScript) override ;
289+ bool GetKeyOrigin (const CKeyID& keyid, KeyOriginInfo& info) const override ;
290+
291+ // ! Load a keypool entry
292+ void LoadKeyPool (int64_t nIndex, const CKeyPool &keypool) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
293+ bool TopUpKeyPool (unsigned int kpSize = 0 );
294+ bool NewKeyPool ();
295+ void MarkPreSplitKeys () EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
296+
261297 bool ImportScripts (const std::set<CScript> scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
262298 bool ImportPrivKeys (const std::map<CKeyID, CKey>& privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
263299 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_wallet);
264300 bool ImportScriptPubKeys (const std::string& label, const std::set<CScript>& script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
265301
266- bool NewKeyPool ();
267- size_t KeypoolCountExternalKeys () EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
268- bool TopUpKeyPool (unsigned int kpSize = 0 );
269-
270- /* *
271- * Reserves a key from the keypool and sets nIndex to its index
272- *
273- * @param[out] nIndex the index of the key in keypool
274- * @param[out] keypool the keypool the key was drawn from, which could be the
275- * the pre-split pool if present, or the internal or external pool
276- * @param fRequestedInternal true if the caller would like the key drawn
277- * from the internal keypool, false if external is preferred
278- *
279- * @return true if succeeded, false if failed due to empty keypool
280- * @throws std::runtime_error if keypool read failed, key was invalid,
281- * was not found in the wallet, or was misclassified in the internal
282- * or external keypool
283- */
284- bool ReserveKeyFromKeyPool (int64_t & nIndex, CKeyPool& keypool, bool fRequestedInternal );
285- void KeepKey (int64_t nIndex);
286- void ReturnKey (int64_t nIndex, bool fInternal , const CPubKey& pubkey);
287- int64_t GetOldestKeyPoolTime ();
288- /* *
289- * Marks all keys in the keypool up to and including reserve_key as used.
290- */
291- void MarkReserveKeysAsUsed (int64_t keypool_id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
292- const std::map<CKeyID, int64_t >& GetAllReserveKeys () const { return m_pool_key_to_index; }
293- bool GetNewDestination (const OutputType type, const std::string label, CTxDestination& dest, std::string& error);
294-
295- isminetype IsMine (const CScript& script) const ;
296-
297- /* Set the HD chain model (chain child index counters) */
298- void SetHDChain (const CHDChain& chain, bool memonly);
299- const CHDChain& GetHDChain () const { return hdChain; }
300-
301- /* Returns true if HD is enabled */
302- bool IsHDEnabled () const ;
303-
304302 /* Returns true if the wallet can generate new keys */
305303 bool CanGenerateKeys ();
306304
307- /* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
308- bool CanGetAddresses (bool internal = false );
309-
310305 /* Generates a new HD seed (will not be activated) */
311306 CPubKey GenerateNewSeed ();
312307
@@ -333,9 +328,13 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
333328 */
334329 void LearnAllRelatedScripts (const CPubKey& key);
335330
336- /* * Implement lookup of key origin information through wallet key metadata. */
337- bool GetKeyOrigin (const CKeyID& keyid, KeyOriginInfo& info) const override ;
331+ /* *
332+ * Marks all keys in the keypool up to and including reserve_key as used.
333+ */
334+ void MarkReserveKeysAsUsed (int64_t keypool_id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
335+ const std::map<CKeyID, int64_t >& GetAllReserveKeys () const { return m_pool_key_to_index; }
338336
337+ std::set<CKeyID> GetKeys () const override ;
339338 // Temporary CWallet accessors and aliases.
340339 friend class CWallet ;
341340 friend class ReserveDestination ;
0 commit comments