@@ -339,14 +339,15 @@ class DescriptorImpl : public Descriptor
339339{
340340 // ! Public key arguments for this descriptor (size 1 for PK, PKH, WPKH; any size for Multisig).
341341 const std::vector<std::unique_ptr<PubkeyProvider>> m_pubkey_args;
342+ // ! The string name of the descriptor function.
343+ const std::string m_name;
344+
345+ protected:
342346 // ! The sub-descriptor argument (nullptr for everything but SH and WSH).
343347 // ! In doc/descriptors.m this is referred to as SCRIPT expressions sh(SCRIPT)
344348 // ! and wsh(SCRIPT), and distinct from KEY expressions and ADDR expressions.
345349 const std::unique_ptr<DescriptorImpl> m_subdescriptor_arg;
346- // ! The string name of the descriptor function.
347- const std::string m_name;
348350
349- protected:
350351 // ! Return a serialization of anything except pubkey and script arguments, to be prepended to those.
351352 virtual std::string ToStringExtra () const { return " " ; }
352353
@@ -364,7 +365,7 @@ class DescriptorImpl : public Descriptor
364365 virtual std::vector<CScript> MakeScripts (const std::vector<CPubKey>& pubkeys, const CScript* script, FlatSigningProvider& out) const = 0;
365366
366367public:
367- DescriptorImpl (std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, std::unique_ptr<DescriptorImpl> script, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_subdescriptor_arg(std::move(script)), m_name(name ) {}
368+ DescriptorImpl (std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, std::unique_ptr<DescriptorImpl> script, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_arg(std::move(script)) {}
368369
369370 bool IsSolvable () const override
370371 {
@@ -500,6 +501,8 @@ class DescriptorImpl : public Descriptor
500501 out = Merge (out, subprovider);
501502 }
502503 }
504+
505+ Optional<OutputType> GetOutputType () const override { return nullopt ; }
503506};
504507
505508/* * A parsed addr(A) descriptor. */
@@ -512,6 +515,19 @@ class AddressDescriptor final : public DescriptorImpl
512515public:
513516 AddressDescriptor (CTxDestination destination) : DescriptorImpl({}, {}, " addr" ), m_destination(std::move(destination)) {}
514517 bool IsSolvable () const final { return false ; }
518+
519+ Optional<OutputType> GetOutputType () const override
520+ {
521+ switch (m_destination.which ()) {
522+ case 1 /* PKHash */ :
523+ case 2 /* ScriptHash */ : return OutputType::LEGACY;
524+ case 3 /* WitnessV0ScriptHash */ :
525+ case 4 /* WitnessV0KeyHash */ :
526+ case 5 /* WitnessUnknown */ : return OutputType::BECH32;
527+ case 0 /* CNoDestination */ :
528+ default : return nullopt ;
529+ }
530+ }
515531};
516532
517533/* * A parsed raw(H) descriptor. */
@@ -524,6 +540,21 @@ class RawDescriptor final : public DescriptorImpl
524540public:
525541 RawDescriptor (CScript script) : DescriptorImpl({}, {}, " raw" ), m_script(std::move(script)) {}
526542 bool IsSolvable () const final { return false ; }
543+
544+ Optional<OutputType> GetOutputType () const override
545+ {
546+ CTxDestination dest;
547+ ExtractDestination (m_script, dest);
548+ switch (dest.which ()) {
549+ case 1 /* PKHash */ :
550+ case 2 /* ScriptHash */ : return OutputType::LEGACY;
551+ case 3 /* WitnessV0ScriptHash */ :
552+ case 4 /* WitnessV0KeyHash */ :
553+ case 5 /* WitnessUnknown */ : return OutputType::BECH32;
554+ case 0 /* CNoDestination */ :
555+ default : return nullopt ;
556+ }
557+ }
527558};
528559
529560/* * A parsed pk(P) descriptor. */
@@ -547,6 +578,7 @@ class PKHDescriptor final : public DescriptorImpl
547578 }
548579public:
549580 PKHDescriptor (std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), {}, " pkh" ) {}
581+ Optional<OutputType> GetOutputType () const override { return OutputType::LEGACY; }
550582};
551583
552584/* * A parsed wpkh(P) descriptor. */
@@ -561,6 +593,7 @@ class WPKHDescriptor final : public DescriptorImpl
561593 }
562594public:
563595 WPKHDescriptor (std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), {}, " wpkh" ) {}
596+ Optional<OutputType> GetOutputType () const override { return OutputType::BECH32; }
564597};
565598
566599/* * A parsed combo(P) descriptor. */
@@ -612,6 +645,13 @@ class SHDescriptor final : public DescriptorImpl
612645 std::vector<CScript> MakeScripts (const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Vector (GetScriptForDestination (ScriptHash (*script))); }
613646public:
614647 SHDescriptor (std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), " sh" ) {}
648+
649+ Optional<OutputType> GetOutputType () const override
650+ {
651+ assert (m_subdescriptor_arg);
652+ if (m_subdescriptor_arg->GetOutputType () == OutputType::BECH32) return OutputType::P2SH_SEGWIT;
653+ return OutputType::LEGACY;
654+ }
615655};
616656
617657/* * A parsed wsh(...) descriptor. */
@@ -621,6 +661,7 @@ class WSHDescriptor final : public DescriptorImpl
621661 std::vector<CScript> MakeScripts (const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Vector (GetScriptForDestination (WitnessV0ScriptHash (*script))); }
622662public:
623663 WSHDescriptor (std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), " wsh" ) {}
664+ Optional<OutputType> GetOutputType () const override { return OutputType::BECH32; }
624665};
625666
626667// //////////////////////////////////////////////////////////////////////////
0 commit comments