Split up key and script metadata for better type safety
Suggested by Matt Corallo <git@bluematt.me> https://github.com/bitcoin/bitcoin/pull/11403#discussion_r155599383 Combining the maps was probably never a good arrangement but is more problematic now in presence of WitnessV0ScriptHash and WitnessV0KeyHash types.
This commit is contained in:
parent
4ef4dfebbc
commit
9c8eca7704
4 changed files with 48 additions and 34 deletions
|
@ -187,17 +187,24 @@ UniValue validateaddress(const JSONRPCRequest& request)
|
||||||
ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
|
ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name));
|
||||||
}
|
}
|
||||||
if (pwallet) {
|
if (pwallet) {
|
||||||
const auto& meta = pwallet->mapKeyMetadata;
|
const CKeyMetadata* meta = nullptr;
|
||||||
const CKeyID *keyID = boost::get<CKeyID>(&dest);
|
if (const CKeyID* key_id = boost::get<CKeyID>(&dest)) {
|
||||||
auto it = keyID ? meta.find(*keyID) : meta.end();
|
auto it = pwallet->mapKeyMetadata.find(*key_id);
|
||||||
if (it == meta.end()) {
|
if (it != pwallet->mapKeyMetadata.end()) {
|
||||||
it = meta.find(CScriptID(scriptPubKey));
|
meta = &it->second;
|
||||||
}
|
}
|
||||||
if (it != meta.end()) {
|
}
|
||||||
ret.push_back(Pair("timestamp", it->second.nCreateTime));
|
if (!meta) {
|
||||||
if (!it->second.hdKeypath.empty()) {
|
auto it = pwallet->m_script_metadata.find(CScriptID(scriptPubKey));
|
||||||
ret.push_back(Pair("hdkeypath", it->second.hdKeypath));
|
if (it != pwallet->m_script_metadata.end()) {
|
||||||
ret.push_back(Pair("hdmasterkeyid", it->second.hdMasterKeyID.GetHex()));
|
meta = &it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (meta) {
|
||||||
|
ret.push_back(Pair("timestamp", meta->nCreateTime));
|
||||||
|
if (!meta->hdKeypath.empty()) {
|
||||||
|
ret.push_back(Pair("hdkeypath", meta->hdKeypath));
|
||||||
|
ret.push_back(Pair("hdmasterkeyid", meta->hdMasterKeyID.GetHex()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -284,7 +284,7 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CWallet::LoadKeyMetadata(const CTxDestination& keyID, const CKeyMetadata &meta)
|
bool CWallet::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata &meta)
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_wallet); // mapKeyMetadata
|
AssertLockHeld(cs_wallet); // mapKeyMetadata
|
||||||
UpdateTimeFirstKey(meta.nCreateTime);
|
UpdateTimeFirstKey(meta.nCreateTime);
|
||||||
|
@ -292,6 +292,14 @@ bool CWallet::LoadKeyMetadata(const CTxDestination& keyID, const CKeyMetadata &m
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CWallet::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata &meta)
|
||||||
|
{
|
||||||
|
AssertLockHeld(cs_wallet); // m_script_metadata
|
||||||
|
UpdateTimeFirstKey(meta.nCreateTime);
|
||||||
|
m_script_metadata[script_id] = meta;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
|
bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
|
||||||
{
|
{
|
||||||
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
|
return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
|
||||||
|
@ -340,7 +348,7 @@ bool CWallet::AddWatchOnly(const CScript& dest)
|
||||||
{
|
{
|
||||||
if (!CCryptoKeyStore::AddWatchOnly(dest))
|
if (!CCryptoKeyStore::AddWatchOnly(dest))
|
||||||
return false;
|
return false;
|
||||||
const CKeyMetadata& meta = mapKeyMetadata[CScriptID(dest)];
|
const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
|
||||||
UpdateTimeFirstKey(meta.nCreateTime);
|
UpdateTimeFirstKey(meta.nCreateTime);
|
||||||
NotifyWatchonlyChanged(true);
|
NotifyWatchonlyChanged(true);
|
||||||
return CWalletDB(*dbw).WriteWatchOnly(dest, meta);
|
return CWalletDB(*dbw).WriteWatchOnly(dest, meta);
|
||||||
|
@ -348,7 +356,7 @@ bool CWallet::AddWatchOnly(const CScript& dest)
|
||||||
|
|
||||||
bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
|
bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
|
||||||
{
|
{
|
||||||
mapKeyMetadata[CScriptID(dest)].nCreateTime = nCreateTime;
|
m_script_metadata[CScriptID(dest)].nCreateTime = nCreateTime;
|
||||||
return AddWatchOnly(dest);
|
return AddWatchOnly(dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -761,9 +761,11 @@ public:
|
||||||
|
|
||||||
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool);
|
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool);
|
||||||
|
|
||||||
// Map from Key ID (for regular keys) or Script ID (for watch-only keys) to
|
// Map from Key ID to key metadata.
|
||||||
// key metadata.
|
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
|
||||||
std::map<CTxDestination, CKeyMetadata> mapKeyMetadata;
|
|
||||||
|
// Map from Script ID to key metadata (for watch-only keys).
|
||||||
|
std::map<CScriptID, CKeyMetadata> m_script_metadata;
|
||||||
|
|
||||||
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
|
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
|
||||||
MasterKeyMap mapMasterKeys;
|
MasterKeyMap mapMasterKeys;
|
||||||
|
@ -874,7 +876,8 @@ public:
|
||||||
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
|
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
|
||||||
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
|
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
|
||||||
//! Load metadata (used by LoadWallet)
|
//! 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); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
|
bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
|
||||||
void UpdateTimeFirstKey(int64_t nCreateTime);
|
void UpdateTimeFirstKey(int64_t nCreateTime);
|
||||||
|
|
|
@ -423,27 +423,23 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
|
||||||
}
|
}
|
||||||
wss.fIsEncrypted = true;
|
wss.fIsEncrypted = true;
|
||||||
}
|
}
|
||||||
else if (strType == "keymeta" || strType == "watchmeta")
|
else if (strType == "keymeta")
|
||||||
{
|
|
||||||
CTxDestination keyID;
|
|
||||||
if (strType == "keymeta")
|
|
||||||
{
|
{
|
||||||
CPubKey vchPubKey;
|
CPubKey vchPubKey;
|
||||||
ssKey >> vchPubKey;
|
ssKey >> vchPubKey;
|
||||||
keyID = vchPubKey.GetID();
|
CKeyMetadata keyMeta;
|
||||||
|
ssValue >> keyMeta;
|
||||||
|
wss.nKeyMeta++;
|
||||||
|
pwallet->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
|
||||||
}
|
}
|
||||||
else if (strType == "watchmeta")
|
else if (strType == "watchmeta")
|
||||||
{
|
{
|
||||||
CScript script;
|
CScript script;
|
||||||
ssKey >> script;
|
ssKey >> script;
|
||||||
keyID = CScriptID(script);
|
|
||||||
}
|
|
||||||
|
|
||||||
CKeyMetadata keyMeta;
|
CKeyMetadata keyMeta;
|
||||||
ssValue >> keyMeta;
|
ssValue >> keyMeta;
|
||||||
wss.nKeyMeta++;
|
wss.nKeyMeta++;
|
||||||
|
pwallet->LoadScriptMetadata(CScriptID(script), keyMeta);
|
||||||
pwallet->LoadKeyMetadata(keyID, keyMeta);
|
|
||||||
}
|
}
|
||||||
else if (strType == "defaultkey")
|
else if (strType == "defaultkey")
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue