Skip to content

Commit 4a0abf6

Browse files
committed
Pass CTxDestination to ScriptPubKeyMan::GetMetadata
Pass CTxDestination instead of more ambiguous uint160 hash value. This is more type safe and more efficient since it avoids doing map lookups that will always fail and were not done previously before a18edd7 from bitcoin/bitcoin#17304 Change suggested by Andrew Chow <[email protected]> in bitcoin/bitcoin#17304 (comment) and bitcoin/bitcoin#17381 (comment)
1 parent b07b07c commit 4a0abf6

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3765,15 +3765,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
37653765

37663766
ScriptPubKeyMan* spk_man = pwallet->GetScriptPubKeyMan();
37673767
if (spk_man) {
3768-
CKeyID key_id = GetKeyForDestination(*provider, dest);
3769-
const CKeyMetadata* meta = nullptr;
3770-
if (!key_id.IsNull()) {
3771-
meta = spk_man->GetMetadata(key_id);
3772-
}
3773-
if (!meta) {
3774-
meta = spk_man->GetMetadata(CScriptID(scriptPubKey));
3775-
}
3776-
if (meta) {
3768+
if (const CKeyMetadata* meta = spk_man->GetMetadata(dest)) {
37773769
ret.pushKV("timestamp", meta->nCreateTime);
37783770
if (meta->has_key_origin) {
37793771
ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path));

src/wallet/scriptpubkeyman.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,24 @@ int64_t LegacyScriptPubKeyMan::GetTimeFirstKey() const
476476
return nTimeFirstKey;
477477
}
478478

479-
const CKeyMetadata* LegacyScriptPubKeyMan::GetMetadata(uint160 id) const
479+
const CKeyMetadata* LegacyScriptPubKeyMan::GetMetadata(const CTxDestination& dest) const
480480
{
481481
AssertLockHeld(cs_wallet);
482-
auto it = mapKeyMetadata.find(CKeyID(id));
483-
if (it != mapKeyMetadata.end()) {
484-
return &it->second;
485-
} else {
486-
auto it2 = m_script_metadata.find(CScriptID(id));
487-
if (it2 != m_script_metadata.end()) {
488-
return &it2->second;
482+
483+
CKeyID key_id = GetKeyForDestination(*this, dest);
484+
if (!key_id.IsNull()) {
485+
auto it = mapKeyMetadata.find(key_id);
486+
if (it != mapKeyMetadata.end()) {
487+
return &it->second;
489488
}
490489
}
490+
491+
CScript scriptPubKey = GetScriptForDestination(dest);
492+
auto it = m_script_metadata.find(CScriptID(scriptPubKey));
493+
if (it != m_script_metadata.end()) {
494+
return &it->second;
495+
}
496+
491497
return nullptr;
492498
}
493499

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ class ScriptPubKeyMan
186186

187187
virtual int64_t GetTimeFirstKey() const { return 0; }
188188

189-
virtual const CKeyMetadata* GetMetadata(uint160 id) const { return nullptr; }
189+
//! Return address metadata
190+
virtual const CKeyMetadata* GetMetadata(const CTxDestination& dest) const { return nullptr; }
190191
};
191192

192193
class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProvider
@@ -302,7 +303,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
302303

303304
int64_t GetTimeFirstKey() const override;
304305

305-
const CKeyMetadata* GetMetadata(uint160 id) const override;
306+
const CKeyMetadata* GetMetadata(const CTxDestination& dest) const override;
306307

307308
bool CanGetAddresses(bool internal = false) override;
308309

0 commit comments

Comments
 (0)