Skip to content

Commit 777b3db

Browse files
committed
fixup! Use importmulti timestamp when importing watch only keys
Index key metadata map by CTxDestination instead of uint160 Suggested by Alex Morcos <[email protected]>. This is more type safe and avoids potential collisions.
1 parent 6a52c4d commit 777b3db

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

src/wallet/rpcdump.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,17 @@ UniValue dumpwallet(const JSONRPCRequest& request)
575575
if (!file.is_open())
576576
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
577577

578-
std::map<CKeyID, int64_t> mapKeyBirth;
578+
std::map<CTxDestination, int64_t> mapKeyBirth;
579579
std::set<CKeyID> setKeyPool;
580580
pwalletMain->GetKeyBirthTimes(mapKeyBirth);
581581
pwalletMain->GetAllReserveKeys(setKeyPool);
582582

583583
// sort time/key pairs
584584
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
585-
for (std::map<CKeyID, int64_t>::const_iterator it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
586-
vKeyBirth.push_back(std::make_pair(it->second, it->first));
585+
for (auto it = mapKeyBirth.begin(); it != mapKeyBirth.end(); it++) {
586+
if (const CKeyID* keyID = boost::get<CKeyID>(&it->first)) {
587+
vKeyBirth.push_back(std::make_pair(it->second, *keyID));
588+
}
587589
}
588590
mapKeyBirth.clear();
589591
std::sort(vKeyBirth.begin(), vKeyBirth.end());

src/wallet/wallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
206206
return false;
207207
}
208208

209-
bool CWallet::LoadKeyMetadata(uint160 id, const CKeyMetadata &meta)
209+
bool CWallet::LoadKeyMetadata(const CTxDestination& keyID, const CKeyMetadata &meta)
210210
{
211211
AssertLockHeld(cs_wallet); // mapKeyMetadata
212212
UpdateTimeFirstKey(meta.nCreateTime);
213-
mapKeyMetadata[id] = meta;
213+
mapKeyMetadata[keyID] = meta;
214214
return true;
215215
}
216216

@@ -3257,12 +3257,12 @@ class CAffectedKeysVisitor : public boost::static_visitor<void> {
32573257
void operator()(const CNoDestination &none) {}
32583258
};
32593259

3260-
void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
3260+
void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) const {
32613261
AssertLockHeld(cs_wallet); // mapKeyMetadata
32623262
mapKeyBirth.clear();
32633263

32643264
// get birth times for keys with metadata
3265-
for (std::map<uint160, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
3265+
for (auto it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
32663266
if (it->second.nCreateTime)
32673267
mapKeyBirth[it->first] = it->second.nCreateTime;
32683268

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
616616

617617
// Map from Key ID (for regular keys) or Script ID (for watch-only keys) to
618618
// key metadata.
619-
std::map<uint160, CKeyMetadata> mapKeyMetadata;
619+
std::map<CTxDestination, CKeyMetadata> mapKeyMetadata;
620620

621621
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
622622
MasterKeyMap mapMasterKeys;
@@ -708,7 +708,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
708708
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
709709
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
710710
//! Load metadata (used by LoadWallet)
711-
bool LoadKeyMetadata(uint160 id, const CKeyMetadata &metadata);
711+
bool LoadKeyMetadata(const CTxDestination& pubKey, const CKeyMetadata &metadata);
712712

713713
bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
714714
void UpdateTimeFirstKey(int nCreateTime);
@@ -740,7 +740,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
740740
bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
741741
bool EncryptWallet(const SecureString& strWalletPassphrase);
742742

743-
void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
743+
void GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) const;
744744

745745
/**
746746
* Increment the next transaction order id

src/wallet/walletdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,25 +459,25 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
459459
}
460460
else if (strType == "keymeta" || strType == "watchmeta")
461461
{
462-
uint160 keyId;
462+
CTxDestination keyID;
463463
if (strType == "keymeta")
464464
{
465465
CPubKey vchPubKey;
466466
ssKey >> vchPubKey;
467-
keyId = vchPubKey.GetID();
467+
keyID = vchPubKey.GetID();
468468
}
469469
else if (strType == "watchmeta")
470470
{
471471
CScript script;
472472
ssKey >> *(CScriptBase*)(&script);
473-
keyId = CScriptID(script);
473+
keyID = CScriptID(script);
474474
}
475475

476476
CKeyMetadata keyMeta;
477477
ssValue >> keyMeta;
478478
wss.nKeyMeta++;
479479

480-
pwallet->LoadKeyMetadata(keyId, keyMeta);
480+
pwallet->LoadKeyMetadata(keyID, keyMeta);
481481
}
482482
else if (strType == "defaultkey")
483483
{

0 commit comments

Comments
 (0)