@@ -106,17 +106,19 @@ CPubKey CWallet::GenerateNewKey(CWalletDB &walletdb, bool internal)
106
106
}
107
107
108
108
// Compressed public keys were introduced in version 0.6.0
109
- if (fCompressed )
109
+ if (fCompressed ) {
110
110
SetMinVersion (FEATURE_COMPRPUBKEY);
111
+ }
111
112
112
113
CPubKey pubkey = secret.GetPubKey ();
113
114
assert (secret.VerifyPubKey (pubkey));
114
115
115
116
mapKeyMetadata[pubkey.GetID ()] = metadata;
116
117
UpdateTimeFirstKey (nCreationTime);
117
118
118
- if (!AddKeyPubKey ( secret, pubkey))
119
+ if (!AddKeyPubKeyWithDB (walletdb, secret, pubkey)) {
119
120
throw std::runtime_error (std::string (__func__) + " : AddKey failed" );
121
+ }
120
122
return pubkey;
121
123
}
122
124
@@ -166,29 +168,48 @@ void CWallet::DeriveNewChildKey(CWalletDB &walletdb, CKeyMetadata& metadata, CKe
166
168
throw std::runtime_error (std::string (__func__) + " : Writing HD chain model failed" );
167
169
}
168
170
169
- bool CWallet::AddKeyPubKey ( const CKey& secret, const CPubKey &pubkey)
171
+ bool CWallet::AddKeyPubKeyWithDB (CWalletDB &walletdb, const CKey& secret, const CPubKey &pubkey)
170
172
{
171
173
AssertLockHeld (cs_wallet); // mapKeyMetadata
172
- if (!CCryptoKeyStore::AddKeyPubKey (secret, pubkey))
174
+
175
+ // CCryptoKeyStore has no concept of wallet databases, but calls AddCryptedKey
176
+ // which is overridden below. To avoid flushes, the database handle is
177
+ // tunneled through to it.
178
+ bool needsDB = !pwalletdbEncryption;
179
+ if (needsDB) {
180
+ pwalletdbEncryption = &walletdb;
181
+ }
182
+ if (!CCryptoKeyStore::AddKeyPubKey (secret, pubkey)) {
183
+ if (needsDB) pwalletdbEncryption = NULL ;
173
184
return false ;
185
+ }
186
+ if (needsDB) pwalletdbEncryption = NULL ;
174
187
175
188
// check if we need to remove from watch-only
176
189
CScript script;
177
190
script = GetScriptForDestination (pubkey.GetID ());
178
- if (HaveWatchOnly (script))
191
+ if (HaveWatchOnly (script)) {
179
192
RemoveWatchOnly (script);
193
+ }
180
194
script = GetScriptForRawPubKey (pubkey);
181
- if (HaveWatchOnly (script))
195
+ if (HaveWatchOnly (script)) {
182
196
RemoveWatchOnly (script);
197
+ }
183
198
184
199
if (!IsCrypted ()) {
185
- return CWalletDB (*dbw) .WriteKey (pubkey,
200
+ return walletdb .WriteKey (pubkey,
186
201
secret.GetPrivKey (),
187
202
mapKeyMetadata[pubkey.GetID ()]);
188
203
}
189
204
return true ;
190
205
}
191
206
207
+ bool CWallet::AddKeyPubKey (const CKey& secret, const CPubKey &pubkey)
208
+ {
209
+ CWalletDB walletdb (*dbw);
210
+ return CWallet::AddKeyPubKeyWithDB (walletdb, secret, pubkey);
211
+ }
212
+
192
213
bool CWallet::AddCryptedKey (const CPubKey &vchPubKey,
193
214
const std::vector<unsigned char > &vchCryptedSecret)
194
215
{
0 commit comments