mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
descriptors: Have GetPrivKey fill keys directly
Instead of GetPrivKey returning a key and having the caller fill the FlatSigningProvider, have GetPrivKey take the FlatSigningProvider and fill it by itself. This will be necessary for descriptors such as musig() where there are private keys that need to be added to the FlatSigningProvider but do not directly appear in any resulting scripts. GetPrivKey is now changed to void as the caller no longer cares whether it succeeds or fails.
This commit is contained in:
parent
4b0303197e
commit
acee5c59e6
1 changed files with 25 additions and 19 deletions
|
@ -211,8 +211,8 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const = 0;
|
virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const = 0;
|
||||||
|
|
||||||
/** Derive a private key, if private data is available in arg. */
|
/** Derive a private key, if private data is available in arg and put it into out. */
|
||||||
virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
|
virtual void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const = 0;
|
||||||
|
|
||||||
/** Return the non-extended public key for this PubkeyProvider, if it has one. */
|
/** Return the non-extended public key for this PubkeyProvider, if it has one. */
|
||||||
virtual std::optional<CPubKey> GetRootPubKey() const = 0;
|
virtual std::optional<CPubKey> GetRootPubKey() const = 0;
|
||||||
|
@ -274,9 +274,9 @@ public:
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
|
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
|
||||||
{
|
{
|
||||||
return m_provider->GetPrivKey(pos, arg, key);
|
m_provider->GetPrivKey(pos, arg, out);
|
||||||
}
|
}
|
||||||
std::optional<CPubKey> GetRootPubKey() const override
|
std::optional<CPubKey> GetRootPubKey() const override
|
||||||
{
|
{
|
||||||
|
@ -298,6 +298,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
|
||||||
CPubKey m_pubkey;
|
CPubKey m_pubkey;
|
||||||
bool m_xonly;
|
bool m_xonly;
|
||||||
|
|
||||||
|
std::optional<CKey> GetPrivKey(const SigningProvider& arg) const
|
||||||
|
{
|
||||||
|
CKey key;
|
||||||
|
if (!(m_xonly ? arg.GetKeyByXOnly(XOnlyPubKey(m_pubkey), key) :
|
||||||
|
arg.GetKey(m_pubkey.GetID(), key))) return std::nullopt;
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey, bool xonly) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
|
ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey, bool xonly) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
|
||||||
std::optional<CPubKey> GetPubKey(int pos, const SigningProvider&, FlatSigningProvider& out, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) const override
|
std::optional<CPubKey> GetPubKey(int pos, const SigningProvider&, FlatSigningProvider& out, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) const override
|
||||||
|
@ -314,9 +322,9 @@ public:
|
||||||
std::string ToString(StringType type) const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); }
|
std::string ToString(StringType type) const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); }
|
||||||
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
||||||
{
|
{
|
||||||
CKey key;
|
std::optional<CKey> key = GetPrivKey(arg);
|
||||||
if (!GetPrivKey(/*pos=*/0, arg, key)) return false;
|
if (!key) return false;
|
||||||
ret = EncodeSecret(key);
|
ret = EncodeSecret(*key);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
|
bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
|
||||||
|
@ -324,10 +332,11 @@ public:
|
||||||
ret = ToString(StringType::PUBLIC);
|
ret = ToString(StringType::PUBLIC);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
|
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
|
||||||
{
|
{
|
||||||
return m_xonly ? arg.GetKeyByXOnly(XOnlyPubKey(m_pubkey), key) :
|
std::optional<CKey> key = GetPrivKey(arg);
|
||||||
arg.GetKey(m_pubkey.GetID(), key);
|
if (!key) return;
|
||||||
|
out.keys.emplace(key->GetPubKey().GetID(), *key);
|
||||||
}
|
}
|
||||||
std::optional<CPubKey> GetRootPubKey() const override
|
std::optional<CPubKey> GetRootPubKey() const override
|
||||||
{
|
{
|
||||||
|
@ -542,15 +551,14 @@ public:
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
|
void GetPrivKey(int pos, const SigningProvider& arg, FlatSigningProvider& out) const override
|
||||||
{
|
{
|
||||||
CExtKey extkey;
|
CExtKey extkey;
|
||||||
CExtKey dummy;
|
CExtKey dummy;
|
||||||
if (!GetDerivedExtKey(arg, extkey, dummy)) return false;
|
if (!GetDerivedExtKey(arg, extkey, dummy)) return;
|
||||||
if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return false;
|
if (m_derive == DeriveType::UNHARDENED && !extkey.Derive(extkey, pos)) return;
|
||||||
if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return false;
|
if (m_derive == DeriveType::HARDENED && !extkey.Derive(extkey, pos | 0x80000000UL)) return;
|
||||||
key = extkey.key;
|
out.keys.emplace(extkey.key.GetPubKey().GetID(), extkey.key);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
std::optional<CPubKey> GetRootPubKey() const override
|
std::optional<CPubKey> GetRootPubKey() const override
|
||||||
{
|
{
|
||||||
|
@ -736,9 +744,7 @@ public:
|
||||||
void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
|
void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
|
||||||
{
|
{
|
||||||
for (const auto& p : m_pubkey_args) {
|
for (const auto& p : m_pubkey_args) {
|
||||||
CKey key;
|
p->GetPrivKey(pos, provider, out);
|
||||||
if (!p->GetPrivKey(pos, provider, key)) continue;
|
|
||||||
out.keys.emplace(key.GetPubKey().GetID(), key);
|
|
||||||
}
|
}
|
||||||
for (const auto& arg : m_subdescriptor_args) {
|
for (const auto& arg : m_subdescriptor_args) {
|
||||||
arg->ExpandPrivate(pos, provider, out);
|
arg->ExpandPrivate(pos, provider, out);
|
||||||
|
|
Loading…
Add table
Reference in a new issue