diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -259,18 +259,25 @@ ret.pushKV("account", pwallet->mapAddressBook[dest].name); } if (pwallet) { - const auto &meta = pwallet->mapKeyMetadata; - const CKeyID *keyID = boost::get(&dest); - auto it = keyID ? meta.find(*keyID) : meta.end(); - if (it == meta.end()) { - it = meta.find(CScriptID(scriptPubKey)); + const CKeyMetadata *meta = nullptr; + if (const CKeyID *key_id = boost::get(&dest)) { + auto it = pwallet->mapKeyMetadata.find(*key_id); + if (it != pwallet->mapKeyMetadata.end()) { + meta = &it->second; + } + } + if (!meta) { + auto it = + pwallet->m_script_metadata.find(CScriptID(scriptPubKey)); + if (it != pwallet->m_script_metadata.end()) { + meta = &it->second; + } } - if (it != meta.end()) { - ret.pushKV("timestamp", it->second.nCreateTime); - if (!it->second.hdKeypath.empty()) { - ret.pushKV("hdkeypath", it->second.hdKeypath); - ret.pushKV("hdmasterkeyid", - it->second.hdMasterKeyID.GetHex()); + if (meta) { + ret.pushKV("timestamp", meta->nCreateTime); + if (!meta->hdKeypath.empty()) { + ret.pushKV("hdkeypath", meta->hdKeypath); + ret.pushKV("hdmasterkeyid", meta->hdMasterKeyID.GetHex()); } } } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -754,9 +754,11 @@ void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool); - // Map from Key ID (for regular keys) or Script ID (for watch-only keys) to - // key metadata. - std::map mapKeyMetadata; + // Map from Key ID to key metadata. + std::map mapKeyMetadata; + + // Map from Script ID to key metadata (for watch-only keys). + std::map m_script_metadata; typedef std::map MasterKeyMap; MasterKeyMap mapMasterKeys; @@ -886,8 +888,9 @@ } //! Load metadata (used by LoadWallet) - bool LoadKeyMetadata(const CTxDestination &pubKey, - const CKeyMetadata &metadata); + bool LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata); + bool LoadScriptMetadata(const CScriptID &script_id, + const CKeyMetadata &metadata); bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -288,8 +288,7 @@ mapKeyMetadata[vchPubKey.GetID()]); } -bool CWallet::LoadKeyMetadata(const CTxDestination &keyID, - const CKeyMetadata &meta) { +bool CWallet::LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &meta) { // mapKeyMetadata AssertLockHeld(cs_wallet); UpdateTimeFirstKey(meta.nCreateTime); @@ -297,6 +296,15 @@ return true; } +bool CWallet::LoadScriptMetadata(const CScriptID &script_id, + const CKeyMetadata &meta) { + // m_script_metadata + AssertLockHeld(cs_wallet); + UpdateTimeFirstKey(meta.nCreateTime); + m_script_metadata[script_id] = meta; + return true; +} + bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); @@ -349,14 +357,14 @@ return false; } - const CKeyMetadata &meta = mapKeyMetadata[CScriptID(dest)]; + const CKeyMetadata &meta = m_script_metadata[CScriptID(dest)]; UpdateTimeFirstKey(meta.nCreateTime); NotifyWatchonlyChanged(true); return CWalletDB(*dbw).WriteWatchOnly(dest, meta); } bool CWallet::AddWatchOnly(const CScript &dest, int64_t nCreateTime) { - mapKeyMetadata[CScriptID(dest)].nCreateTime = nCreateTime; + m_script_metadata[CScriptID(dest)].nCreateTime = nCreateTime; return AddWatchOnly(dest); } diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -447,23 +447,21 @@ return false; } wss.fIsEncrypted = true; - } else if (strType == "keymeta" || strType == "watchmeta") { - CTxDestination keyID; - if (strType == "keymeta") { - CPubKey vchPubKey; - ssKey >> vchPubKey; - keyID = vchPubKey.GetID(); - } else if (strType == "watchmeta") { - CScript script; - ssKey >> script; - keyID = CScriptID(script); - } - + } else if (strType == "keymeta") { + CPubKey vchPubKey; + ssKey >> vchPubKey; CKeyMetadata keyMeta; ssValue >> keyMeta; wss.nKeyMeta++; - pwallet->LoadKeyMetadata(keyID, keyMeta); + pwallet->LoadKeyMetadata(vchPubKey.GetID(), keyMeta); + } else if (strType == "watchmeta") { + CScript script; + ssKey >> script; + CKeyMetadata keyMeta; + ssValue >> keyMeta; + wss.nKeyMeta++; + pwallet->LoadScriptMetadata(CScriptID(script), keyMeta); } else if (strType == "defaultkey") { // We don't want or need the default key, but if there is one set, // we want to make sure that it is valid so that we can detect