@@ -211,8 +211,8 @@ struct PubkeyProvider
211211 */
212212 virtual bool ToNormalizedString (const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr ) const = 0;
213213
214- /* * Derive a private key, if private data is available in arg. */
215- virtual bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const = 0;
214+ /* * Derive a private key, if private data is available in arg and put it into out . */
215+ virtual void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const = 0;
216216
217217 /* * Return the non-extended public key for this PubkeyProvider, if it has one. */
218218 virtual std::optional<CPubKey> GetRootPubKey () const = 0;
@@ -274,9 +274,9 @@ class OriginPubkeyProvider final : public PubkeyProvider
274274 }
275275 return true ;
276276 }
277- bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const override
277+ void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const override
278278 {
279- return m_provider->GetPrivKey (pos, arg, key );
279+ m_provider->GetPrivKey (pos, arg, out );
280280 }
281281 std::optional<CPubKey> GetRootPubKey () const override
282282 {
@@ -298,6 +298,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
298298 CPubKey m_pubkey;
299299 bool m_xonly;
300300
301+ std::optional<CKey> GetPrivKey (const SigningProvider& arg) const
302+ {
303+ CKey key;
304+ if (!(m_xonly ? arg.GetKeyByXOnly (XOnlyPubKey (m_pubkey), key) :
305+ arg.GetKey (m_pubkey.GetID (), key))) return std::nullopt ;
306+ return key;
307+ }
308+
301309public:
302310 ConstPubkeyProvider (uint32_t exp_index, const CPubKey& pubkey, bool xonly) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
303311 std::optional<CPubKey> GetPubKey (int pos, const SigningProvider&, FlatSigningProvider& out, const DescriptorCache* read_cache = nullptr , DescriptorCache* write_cache = nullptr ) const override
@@ -314,20 +322,21 @@ class ConstPubkeyProvider final : public PubkeyProvider
314322 std::string ToString (StringType type) const override { return m_xonly ? HexStr (m_pubkey).substr (2 ) : HexStr (m_pubkey); }
315323 bool ToPrivateString (const SigningProvider& arg, std::string& ret) const override
316324 {
317- CKey key;
318- if (!GetPrivKey ( /* pos= */ 0 , arg, key) ) return false ;
319- ret = EncodeSecret (key);
325+ std::optional< CKey> key = GetPrivKey (arg) ;
326+ if (!key) return false ;
327+ ret = EncodeSecret (* key);
320328 return true ;
321329 }
322330 bool ToNormalizedString (const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
323331 {
324332 ret = ToString (StringType::PUBLIC);
325333 return true ;
326334 }
327- bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const override
335+ void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const override
328336 {
329- return m_xonly ? arg.GetKeyByXOnly (XOnlyPubKey (m_pubkey), key) :
330- arg.GetKey (m_pubkey.GetID (), key);
337+ std::optional<CKey> key = GetPrivKey (arg);
338+ if (!key) return ;
339+ out.keys .emplace (key->GetPubKey ().GetID (), *key);
331340 }
332341 std::optional<CPubKey> GetRootPubKey () const override
333342 {
@@ -542,15 +551,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider
542551 }
543552 return true ;
544553 }
545- bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const override
554+ void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const override
546555 {
547556 CExtKey extkey;
548557 CExtKey dummy;
549- if (!GetDerivedExtKey (arg, extkey, dummy)) return false ;
550- if (m_derive == DeriveType::UNHARDENED && !extkey.Derive (extkey, pos)) return false ;
551- if (m_derive == DeriveType::HARDENED && !extkey.Derive (extkey, pos | 0x80000000UL )) return false ;
552- key = extkey.key ;
553- return true ;
558+ if (!GetDerivedExtKey (arg, extkey, dummy)) return ;
559+ if (m_derive == DeriveType::UNHARDENED && !extkey.Derive (extkey, pos)) return ;
560+ if (m_derive == DeriveType::HARDENED && !extkey.Derive (extkey, pos | 0x80000000UL )) return ;
561+ out.keys .emplace (extkey.key .GetPubKey ().GetID (), extkey.key );
554562 }
555563 std::optional<CPubKey> GetRootPubKey () const override
556564 {
@@ -736,9 +744,7 @@ class DescriptorImpl : public Descriptor
736744 void ExpandPrivate (int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
737745 {
738746 for (const auto & p : m_pubkey_args) {
739- CKey key;
740- if (!p->GetPrivKey (pos, provider, key)) continue ;
741- out.keys .emplace (key.GetPubKey ().GetID (), key);
747+ p->GetPrivKey (pos, provider, out);
742748 }
743749 for (const auto & arg : m_subdescriptor_args) {
744750 arg->ExpandPrivate (pos, provider, out);
0 commit comments