Skip to content

Commit

Permalink
Avoid dependence on CTxDestination index order
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed May 24, 2021
1 parent 31df02a commit 41839bd
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,19 @@ class DescriptorImpl : public Descriptor
std::optional<OutputType> GetOutputType() const override { return std::nullopt; }
};

static std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
if (std::holds_alternative<PKHash>(dest) ||
std::holds_alternative<ScriptHash>(dest)) {
return OutputType::LEGACY;
}
if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
std::holds_alternative<WitnessV0ScriptHash>(dest) ||
std::holds_alternative<WitnessUnknown>(dest)) {
return OutputType::BECH32;
}
return std::nullopt;
}

/** A parsed addr(A) descriptor. */
class AddressDescriptor final : public DescriptorImpl
{
Expand All @@ -651,15 +664,7 @@ class AddressDescriptor final : public DescriptorImpl

std::optional<OutputType> 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; }
};
Expand All @@ -679,15 +684,7 @@ class RawDescriptor final : public DescriptorImpl
{
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; }
};
Expand Down

0 comments on commit 41839bd

Please sign in to comment.