key: add DeriveExtKey() helper

Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com>
This commit is contained in:
Sjors Provoost
2025-06-20 10:13:31 +02:00
parent 71c06c5cbc
commit dab525eb77
3 changed files with 51 additions and 0 deletions

View File

@@ -16,6 +16,8 @@
#include <secp256k1_recovery.h>
#include <secp256k1_schnorrsig.h>
#include <algorithm>
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<std::pair<CExtKey, KeyOriginInfo>> DeriveExtKey(const CExtKey& ext_key, const std::vector<uint32_t>& 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<const std::byte> seed)
{
Assert(16 <= seed.size() && seed.size() <= 64);