diff --git a/src/key.cpp b/src/key.cpp index 000c533b7d9..7bafc885ec4 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -16,6 +16,8 @@ #include #include +#include + static secp256k1_context* secp256k1_context_sign = nullptr; /** These functions are taken from the libsecp256k1 distribution and are very ugly. */ @@ -364,6 +366,18 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const { return key.Derive(out.key, out.chaincode, _nChild, chaincode); } +std::optional> DeriveExtKey(const CExtKey& ext_key, const std::vector& path) +{ + CExtKey descendant = ext_key; + KeyOriginInfo origin; + origin.fingerprint = ext_key.id_key_fingerprint(); + origin.path = path; + for (uint32_t i : path) { + if (!descendant.Derive(descendant, i)) return std::nullopt; + } + return std::make_pair(descendant, origin); +} + void CExtKey::SetSeed(std::span seed) { Assert(16 <= seed.size() && seed.size() <= 64); diff --git a/src/key.h b/src/key.h index 56f416ea8d0..f16009ea88b 100644 --- a/src/key.h +++ b/src/key.h @@ -8,11 +8,14 @@ #define BITCOIN_KEY_H #include +#include