Skip to content

Commit 2214954

Browse files
committed
Merge #11854: Split up key and script metadata for better type safety
9c8eca7 Split up key and script metadata for better type safety (Russell Yanofsky) Pull request description: Suggested by @TheBlueMatt bitcoin/bitcoin#11403 (comment) Combining the maps was probably never a good arrangement but is more problematic now in presence of WitnessV0ScriptHash and WitnessV0KeyHash types. Tree-SHA512: 9263e9c01090fb49221e91d88a88241a9691dda3e92d86041c8e284306a64d3af5e2438249f9dcc3e6e4a5c11c1a89f975a86d55690adf95bf2636f15f99f92a
2 parents 5d132e8 + 9c8eca7 commit 2214954

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

src/rpc/misc.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,24 @@ UniValue validateaddress(const JSONRPCRequest& request)
187187
ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
188188
}
189189
if (pwallet) {
190-
const auto& meta = pwallet->mapKeyMetadata;
191-
const CKeyID *keyID = boost::get<CKeyID>(&dest);
192-
auto it = keyID ? meta.find(*keyID) : meta.end();
193-
if (it == meta.end()) {
194-
it = meta.find(CScriptID(scriptPubKey));
190+
const CKeyMetadata* meta = nullptr;
191+
if (const CKeyID* key_id = boost::get<CKeyID>(&dest)) {
192+
auto it = pwallet->mapKeyMetadata.find(*key_id);
193+
if (it != pwallet->mapKeyMetadata.end()) {
194+
meta = &it->second;
195+
}
196+
}
197+
if (!meta) {
198+
auto it = pwallet->m_script_metadata.find(CScriptID(scriptPubKey));
199+
if (it != pwallet->m_script_metadata.end()) {
200+
meta = &it->second;
201+
}
195202
}
196-
if (it != meta.end()) {
197-
ret.push_back(Pair("timestamp", it->second.nCreateTime));
198-
if (!it->second.hdKeypath.empty()) {
199-
ret.push_back(Pair("hdkeypath", it->second.hdKeypath));
200-
ret.push_back(Pair("hdmasterkeyid", it->second.hdMasterKeyID.GetHex()));
203+
if (meta) {
204+
ret.push_back(Pair("timestamp", meta->nCreateTime));
205+
if (!meta->hdKeypath.empty()) {
206+
ret.push_back(Pair("hdkeypath", meta->hdKeypath));
207+
ret.push_back(Pair("hdmasterkeyid", meta->hdMasterKeyID.GetHex()));
201208
}
202209
}
203210
}

src/wallet/wallet.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,22 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
282282
}
283283
}
284284

285-
bool CWallet::LoadKeyMetadata(const CTxDestination& keyID, const CKeyMetadata &meta)
285+
bool CWallet::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata &meta)
286286
{
287287
AssertLockHeld(cs_wallet); // mapKeyMetadata
288288
UpdateTimeFirstKey(meta.nCreateTime);
289289
mapKeyMetadata[keyID] = meta;
290290
return true;
291291
}
292292

293+
bool CWallet::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata &meta)
294+
{
295+
AssertLockHeld(cs_wallet); // m_script_metadata
296+
UpdateTimeFirstKey(meta.nCreateTime);
297+
m_script_metadata[script_id] = meta;
298+
return true;
299+
}
300+
293301
bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
294302
{
295303
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
@@ -338,15 +346,15 @@ bool CWallet::AddWatchOnly(const CScript& dest)
338346
{
339347
if (!CCryptoKeyStore::AddWatchOnly(dest))
340348
return false;
341-
const CKeyMetadata& meta = mapKeyMetadata[CScriptID(dest)];
349+
const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
342350
UpdateTimeFirstKey(meta.nCreateTime);
343351
NotifyWatchonlyChanged(true);
344352
return CWalletDB(*dbw).WriteWatchOnly(dest, meta);
345353
}
346354

347355
bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
348356
{
349-
mapKeyMetadata[CScriptID(dest)].nCreateTime = nCreateTime;
357+
m_script_metadata[CScriptID(dest)].nCreateTime = nCreateTime;
350358
return AddWatchOnly(dest);
351359
}
352360

src/wallet/wallet.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,11 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
761761

762762
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool);
763763

764-
// Map from Key ID (for regular keys) or Script ID (for watch-only keys) to
765-
// key metadata.
766-
std::map<CTxDestination, CKeyMetadata> mapKeyMetadata;
764+
// Map from Key ID to key metadata.
765+
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
766+
767+
// Map from Script ID to key metadata (for watch-only keys).
768+
std::map<CScriptID, CKeyMetadata> m_script_metadata;
767769

768770
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
769771
MasterKeyMap mapMasterKeys;
@@ -874,7 +876,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
874876
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
875877
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
876878
//! Load metadata (used by LoadWallet)
877-
bool LoadKeyMetadata(const CTxDestination& pubKey, const CKeyMetadata &metadata);
879+
bool LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata &metadata);
880+
bool LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata &metadata);
878881

879882
bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
880883
void UpdateTimeFirstKey(int64_t nCreateTime);

src/wallet/walletdb.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -423,27 +423,23 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
423423
}
424424
wss.fIsEncrypted = true;
425425
}
426-
else if (strType == "keymeta" || strType == "watchmeta")
426+
else if (strType == "keymeta")
427427
{
428-
CTxDestination keyID;
429-
if (strType == "keymeta")
430-
{
431-
CPubKey vchPubKey;
432-
ssKey >> vchPubKey;
433-
keyID = vchPubKey.GetID();
434-
}
435-
else if (strType == "watchmeta")
436-
{
437-
CScript script;
438-
ssKey >> script;
439-
keyID = CScriptID(script);
440-
}
441-
428+
CPubKey vchPubKey;
429+
ssKey >> vchPubKey;
442430
CKeyMetadata keyMeta;
443431
ssValue >> keyMeta;
444432
wss.nKeyMeta++;
445-
446-
pwallet->LoadKeyMetadata(keyID, keyMeta);
433+
pwallet->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
434+
}
435+
else if (strType == "watchmeta")
436+
{
437+
CScript script;
438+
ssKey >> script;
439+
CKeyMetadata keyMeta;
440+
ssValue >> keyMeta;
441+
wss.nKeyMeta++;
442+
pwallet->LoadScriptMetadata(CScriptID(script), keyMeta);
447443
}
448444
else if (strType == "defaultkey")
449445
{

0 commit comments

Comments
 (0)