Skip to content

Commit 03a9958

Browse files
committed
Implement key origin lookup in CWallet
1 parent 3b01efa commit 03a9958

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,26 +4429,9 @@ void AddKeypathToMap(const CWallet* pwallet, const CKeyID& keyID, std::map<CPubK
44294429
if (!pwallet->GetPubKey(keyID, vchPubKey)) {
44304430
return;
44314431
}
4432-
CKeyMetadata meta;
4433-
auto it = pwallet->mapKeyMetadata.find(keyID);
4434-
if (it != pwallet->mapKeyMetadata.end()) {
4435-
meta = it->second;
4436-
}
44374432
KeyOriginInfo info;
4438-
if (!meta.hdKeypath.empty()) {
4439-
if (!ParseHDKeypath(meta.hdKeypath, info.path)) {
4440-
throw JSONRPCError(RPC_INTERNAL_ERROR, "Internal keypath is broken");
4441-
}
4442-
// Get the proper master key id
4443-
CKey key;
4444-
pwallet->GetKey(meta.hd_seed_id, key);
4445-
CExtKey masterKey;
4446-
masterKey.SetSeed(key.begin(), key.size());
4447-
// Compute identifier
4448-
CKeyID masterid = masterKey.key.GetPubKey().GetID();
4449-
std::copy(masterid.begin(), masterid.begin() + 4, info.fingerprint);
4450-
} else { // Single pubkeys get the master fingerprint of themselves
4451-
std::copy(keyID.begin(), keyID.begin() + 4, info.fingerprint);
4433+
if (!pwallet->GetKeyOrigin(keyID, info)) {
4434+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Internal keypath is broken");
44524435
}
44534436
hd_keypaths.emplace(vchPubKey, std::move(info));
44544437
}

src/wallet/wallet.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,3 +4466,29 @@ std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outpu
44664466
}
44674467
return groups;
44684468
}
4469+
4470+
bool CWallet::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& info) const
4471+
{
4472+
CKeyMetadata meta;
4473+
{
4474+
LOCK(cs_wallet);
4475+
auto it = mapKeyMetadata.find(keyID);
4476+
if (it != mapKeyMetadata.end()) {
4477+
meta = it->second;
4478+
}
4479+
}
4480+
if (!meta.hdKeypath.empty()) {
4481+
if (!ParseHDKeypath(meta.hdKeypath, info.path)) return false;
4482+
// Get the proper master key id
4483+
CKey key;
4484+
GetKey(meta.hd_seed_id, key);
4485+
CExtKey masterKey;
4486+
masterKey.SetSeed(key.begin(), key.size());
4487+
// Compute identifier
4488+
CKeyID masterid = masterKey.key.GetPubKey().GetID();
4489+
std::copy(masterid.begin(), masterid.begin() + 4, info.fingerprint);
4490+
} else { // Single pubkeys get the master fingerprint of themselves
4491+
std::copy(keyID.begin(), keyID.begin() + 4, info.fingerprint);
4492+
}
4493+
return true;
4494+
}

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
12111211
LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);
12121212
};
12131213

1214+
/** Implement lookup of key origin information through wallet key metadata. */
1215+
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
12141216
};
12151217

12161218
/** A key allocated from the key pool. */

0 commit comments

Comments
 (0)