@@ -211,8 +211,8 @@ struct PubkeyProvider
211
211
*/
212
212
virtual bool ToNormalizedString (const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr ) const = 0;
213
213
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;
216
216
217
217
/* * Return the non-extended public key for this PubkeyProvider, if it has one. */
218
218
virtual std::optional<CPubKey> GetRootPubKey () const = 0;
@@ -274,9 +274,9 @@ class OriginPubkeyProvider final : public PubkeyProvider
274
274
}
275
275
return true ;
276
276
}
277
- bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const override
277
+ void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const override
278
278
{
279
- return m_provider->GetPrivKey (pos, arg, key );
279
+ m_provider->GetPrivKey (pos, arg, out );
280
280
}
281
281
std::optional<CPubKey> GetRootPubKey () const override
282
282
{
@@ -298,6 +298,14 @@ class ConstPubkeyProvider final : public PubkeyProvider
298
298
CPubKey m_pubkey;
299
299
bool m_xonly;
300
300
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
+
301
309
public:
302
310
ConstPubkeyProvider (uint32_t exp_index, const CPubKey& pubkey, bool xonly) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
303
311
std::optional<CPubKey> GetPubKey (int pos, const SigningProvider& arg, FlatSigningProvider& out, const DescriptorCache* read_cache = nullptr , DescriptorCache* write_cache = nullptr ) const override
@@ -314,20 +322,21 @@ class ConstPubkeyProvider final : public PubkeyProvider
314
322
std::string ToString (StringType type) const override { return m_xonly ? HexStr (m_pubkey).substr (2 ) : HexStr (m_pubkey); }
315
323
bool ToPrivateString (const SigningProvider& arg, std::string& ret) const override
316
324
{
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);
320
328
return true ;
321
329
}
322
330
bool ToNormalizedString (const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
323
331
{
324
332
ret = ToString (StringType::PUBLIC);
325
333
return true ;
326
334
}
327
- bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const override
335
+ void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const override
328
336
{
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);
331
340
}
332
341
std::optional<CPubKey> GetRootPubKey () const override
333
342
{
@@ -545,15 +554,14 @@ class BIP32PubkeyProvider final : public PubkeyProvider
545
554
}
546
555
return true ;
547
556
}
548
- bool GetPrivKey (int pos, const SigningProvider& arg, CKey& key ) const override
557
+ void GetPrivKey (int pos, const SigningProvider& arg, FlatSigningProvider& out ) const override
549
558
{
550
559
CExtKey extkey;
551
560
CExtKey dummy;
552
- if (!GetDerivedExtKey (arg, extkey, dummy)) return false ;
553
- if (m_derive == DeriveType::UNHARDENED && !extkey.Derive (extkey, pos)) return false ;
554
- if (m_derive == DeriveType::HARDENED && !extkey.Derive (extkey, pos | 0x80000000UL )) return false ;
555
- key = extkey.key ;
556
- return true ;
561
+ if (!GetDerivedExtKey (arg, extkey, dummy)) return ;
562
+ if (m_derive == DeriveType::UNHARDENED && !extkey.Derive (extkey, pos)) return ;
563
+ if (m_derive == DeriveType::HARDENED && !extkey.Derive (extkey, pos | 0x80000000UL )) return ;
564
+ out.keys .emplace (extkey.key .GetPubKey ().GetID (), extkey.key );
557
565
}
558
566
std::optional<CPubKey> GetRootPubKey () const override
559
567
{
@@ -739,9 +747,7 @@ class DescriptorImpl : public Descriptor
739
747
void ExpandPrivate (int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
740
748
{
741
749
for (const auto & p : m_pubkey_args) {
742
- CKey key;
743
- if (!p->GetPrivKey (pos, provider, key)) continue ;
744
- out.keys .emplace (key.GetPubKey ().GetID (), key);
750
+ p->GetPrivKey (pos, provider, out);
745
751
}
746
752
for (const auto & arg : m_subdescriptor_args) {
747
753
arg->ExpandPrivate (pos, provider, out);
0 commit comments