From 41839bdb89b3777ece2318877b9c7921ecca2472 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 23 May 2021 18:23:58 -0700 Subject: Avoid dependence on CTxDestination index order --- src/script/descriptor.cpp | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index b54ba204f0..19fad1009e 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -638,6 +638,19 @@ public: std::optional GetOutputType() const override { return std::nullopt; } }; +static std::optional OutputTypeFromDestination(const CTxDestination& dest) { + if (std::holds_alternative(dest) || + std::holds_alternative(dest)) { + return OutputType::LEGACY; + } + if (std::holds_alternative(dest) || + std::holds_alternative(dest) || + std::holds_alternative(dest)) { + return OutputType::BECH32; + } + return std::nullopt; +} + /** A parsed addr(A) descriptor. */ class AddressDescriptor final : public DescriptorImpl { @@ -651,15 +664,7 @@ public: std::optional GetOutputType() const override { - switch (m_destination.index()) { - case 1 /* PKHash */: - case 2 /* ScriptHash */: return OutputType::LEGACY; - case 3 /* WitnessV0ScriptHash */: - case 4 /* WitnessV0KeyHash */: - case 5 /* WitnessUnknown */: return OutputType::BECH32; - case 0 /* CNoDestination */: - default: return std::nullopt; - } + return OutputTypeFromDestination(m_destination); } bool IsSingleType() const final { return true; } }; @@ -679,15 +684,7 @@ public: { CTxDestination dest; ExtractDestination(m_script, dest); - switch (dest.index()) { - case 1 /* PKHash */: - case 2 /* ScriptHash */: return OutputType::LEGACY; - case 3 /* WitnessV0ScriptHash */: - case 4 /* WitnessV0KeyHash */: - case 5 /* WitnessUnknown */: return OutputType::BECH32; - case 0 /* CNoDestination */: - default: return std::nullopt; - } + return OutputTypeFromDestination(dest); } bool IsSingleType() const final { return true; } }; -- cgit v1.2.3