derivehdkey("m") currently leaves the origin fingerprint as [00000000] because the fingerprint is only populated inside the child-derivation loop, which does not run for an empty path.
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 9cae5b8ecd..1754d26cfa 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -1386,14 +1386,11 @@ std::optional<std::pair<CExtKey, KeyOriginInfo>> DeriveExtKey(const CExtKey& ext
CExtKey descendant = ext_key;
KeyOriginInfo origin;
origin.clear(); // Prevent spurious uninitialized variable warning
+ const CKeyID id = ext_key.key.GetPubKey().GetID();
+ std::copy(id.begin(), id.begin() + sizeof(origin.fingerprint), origin.fingerprint);
origin.path = path;
- bool first = true;
for (uint32_t i : path) {
if (!descendant.Derive(descendant, i)) return std::nullopt;
- if (first) {
- memcpy(origin.fingerprint, descendant.vchFingerprint, 4);
- first = false;
- }
}
return std::make_pair(descendant, origin);
}
diff --git a/src/rpc/util.h b/src/rpc/util.h
index d00920a9eb..70bde19dfe 100644
--- a/src/rpc/util.h
+++ b/src/rpc/util.h
@@ -159,7 +159,7 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
//! Get extended key and origin info for a given path
//! [@param](/bitcoin-bitcoin/contributor/param/)[in] ext_key The extended private key to derive from
//! [@param](/bitcoin-bitcoin/contributor/param/)[in] path The BIP 32 path
-//! [@return](/bitcoin-bitcoin/contributor/return/) the resulting extended private key and origin info (blank if path is empty)
+//! [@return](/bitcoin-bitcoin/contributor/return/) the resulting extended private key and origin info
std::optional<std::pair<CExtKey, KeyOriginInfo>> DeriveExtKey(const CExtKey& ext_key, const std::vector<uint32_t>& path);
//! Parse BIP32 path
diff --git a/test/functional/wallet_derivehdkey.py b/test/functional/wallet_derivehdkey.py
index 118726b94b..0bedc985a8 100755
--- a/test/functional/wallet_derivehdkey.py
+++ b/test/functional/wallet_derivehdkey.py
@@ -35,6 +35,8 @@ class WalletDeriveHDKeyTest(BitcoinTestFramework):
xpub_info = wallet.derivehdkey("m")
assert "xprv" not in xpub_info
xpub = xpub_info["xpub"]
+ root_fingerprint = wallet.derivehdkey("m/0")["origin"][1:9]
+ assert_equal(xpub_info["origin"], f"[{root_fingerprint}]")
xpub_info = wallet.derivehdkey("m", private=True)
xprv = xpub_info["xprv"]