Skip to content

Commit 77a7771

Browse files
committed
Rename EncryptKeys to Encrypt and pass in the encrypted batch to use
1 parent 35f962f commit 77a7771

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,15 @@ bool LegacyScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key
238238
return true;
239239
}
240240

241-
bool LegacyScriptPubKeyMan::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
241+
bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch)
242242
{
243+
AssertLockHeld(cs_wallet);
243244
LOCK(cs_KeyStore);
244-
if (!mapCryptedKeys.empty() || IsCrypted())
245+
encrypted_batch = batch;
246+
if (!mapCryptedKeys.empty() || IsCrypted()) {
247+
encrypted_batch = nullptr;
245248
return false;
249+
}
246250

247251
fUseCrypto = true;
248252
KeyMap keys_to_encrypt;
@@ -253,11 +257,16 @@ bool LegacyScriptPubKeyMan::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
253257
CPubKey vchPubKey = key.GetPubKey();
254258
CKeyingMaterial vchSecret(key.begin(), key.end());
255259
std::vector<unsigned char> vchCryptedSecret;
256-
if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
260+
if (!EncryptSecret(master_key, vchSecret, vchPubKey.GetHash(), vchCryptedSecret)) {
261+
encrypted_batch = nullptr;
257262
return false;
258-
if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
263+
}
264+
if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) {
265+
encrypted_batch = nullptr;
259266
return false;
267+
}
260268
}
269+
encrypted_batch = nullptr;
261270
return true;
262271
}
263272

src/wallet/scriptpubkeyman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class ScriptPubKeyMan
154154

155155
//! Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the keys handled by it.
156156
virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; }
157+
virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }
157158

158159
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool) { return false; }
159160
virtual void KeepDestination(int64_t index, const OutputType& type) {}
@@ -280,9 +281,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
280281
bool GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) override;
281282
isminetype IsMine(const CScript& script) const override;
282283

283-
//! will encrypt previously unencrypted keys
284-
bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
285284
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
285+
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
286286

287287
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool) override;
288288
void KeepDestination(int64_t index, const OutputType& type) override;

src/wallet/wallet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
532532
{
533533
LOCK(cs_wallet);
534534
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
535-
assert(!encrypted_batch);
536-
encrypted_batch = new WalletBatch(*database);
535+
WalletBatch* encrypted_batch = new WalletBatch(*database);
537536
if (!encrypted_batch->TxnBegin()) {
538537
delete encrypted_batch;
539538
encrypted_batch = nullptr;
@@ -542,7 +541,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
542541
encrypted_batch->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
543542

544543
if (auto spk_man = m_spk_man.get()) {
545-
if (!spk_man->EncryptKeys(_vMasterKey)) {
544+
if (!spk_man->Encrypt(_vMasterKey, encrypted_batch)) {
546545
encrypted_batch->TxnAbort();
547546
delete encrypted_batch;
548547
encrypted_batch = nullptr;

src/wallet/wallet.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,6 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
743743
{
744744
// Should not have slots connected at this point.
745745
assert(NotifyUnload.empty());
746-
delete encrypted_batch;
747-
encrypted_batch = nullptr;
748746
}
749747

750748
bool IsCrypted() const { return fUseCrypto; }
@@ -1145,8 +1143,6 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
11451143
LegacyScriptPubKeyMan::CryptedKeyMap& mapCryptedKeys GUARDED_BY(cs_KeyStore) = m_spk_man->mapCryptedKeys;
11461144
LegacyScriptPubKeyMan::WatchOnlySet& setWatchOnly GUARDED_BY(cs_KeyStore) = m_spk_man->setWatchOnly;
11471145
LegacyScriptPubKeyMan::WatchKeyMap& mapWatchKeys GUARDED_BY(cs_KeyStore) = m_spk_man->mapWatchKeys;
1148-
WalletBatch*& encrypted_batch GUARDED_BY(cs_wallet) = m_spk_man->encrypted_batch;
1149-
using CryptedKeyMap = LegacyScriptPubKeyMan::CryptedKeyMap;
11501146

11511147
/** Get last block processed height */
11521148
int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)

0 commit comments

Comments
 (0)