Skip to content

Commit 41839bd

Browse files
committed
Avoid dependence on CTxDestination index order
1 parent 31df02a commit 41839bd

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/script/descriptor.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,19 @@ class DescriptorImpl : public Descriptor
638638
std::optional<OutputType> GetOutputType() const override { return std::nullopt; }
639639
};
640640

641+
static std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
642+
if (std::holds_alternative<PKHash>(dest) ||
643+
std::holds_alternative<ScriptHash>(dest)) {
644+
return OutputType::LEGACY;
645+
}
646+
if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
647+
std::holds_alternative<WitnessV0ScriptHash>(dest) ||
648+
std::holds_alternative<WitnessUnknown>(dest)) {
649+
return OutputType::BECH32;
650+
}
651+
return std::nullopt;
652+
}
653+
641654
/** A parsed addr(A) descriptor. */
642655
class AddressDescriptor final : public DescriptorImpl
643656
{
@@ -651,15 +664,7 @@ class AddressDescriptor final : public DescriptorImpl
651664

652665
std::optional<OutputType> GetOutputType() const override
653666
{
654-
switch (m_destination.index()) {
655-
case 1 /* PKHash */:
656-
case 2 /* ScriptHash */: return OutputType::LEGACY;
657-
case 3 /* WitnessV0ScriptHash */:
658-
case 4 /* WitnessV0KeyHash */:
659-
case 5 /* WitnessUnknown */: return OutputType::BECH32;
660-
case 0 /* CNoDestination */:
661-
default: return std::nullopt;
662-
}
667+
return OutputTypeFromDestination(m_destination);
663668
}
664669
bool IsSingleType() const final { return true; }
665670
};
@@ -679,15 +684,7 @@ class RawDescriptor final : public DescriptorImpl
679684
{
680685
CTxDestination dest;
681686
ExtractDestination(m_script, dest);
682-
switch (dest.index()) {
683-
case 1 /* PKHash */:
684-
case 2 /* ScriptHash */: return OutputType::LEGACY;
685-
case 3 /* WitnessV0ScriptHash */:
686-
case 4 /* WitnessV0KeyHash */:
687-
case 5 /* WitnessUnknown */: return OutputType::BECH32;
688-
case 0 /* CNoDestination */:
689-
default: return std::nullopt;
690-
}
687+
return OutputTypeFromDestination(dest);
691688
}
692689
bool IsSingleType() const final { return true; }
693690
};

0 commit comments

Comments
 (0)