Fix LeafVersion serde

The default implementation maps to visit_u64. The current implementation
does not roundtrip with many deserializers, including serde_json. See
failing test in the subsequent commit
This commit is contained in:
sanket1729 2023-01-01 11:42:29 -08:00
parent 98203bc8b3
commit b3246bf73f
1 changed files with 7 additions and 1 deletions

View File

@ -944,10 +944,16 @@ impl<'de> serde::Deserialize<'de> for LeafVersion {
formatter.write_str("a valid consensus-encoded taproot leaf version") formatter.write_str("a valid consensus-encoded taproot leaf version")
} }
fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E> fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where where
E: serde::de::Error, E: serde::de::Error,
{ {
let value = u8::try_from(value).map_err(|_| {
E::invalid_value(
serde::de::Unexpected::Unsigned(value),
&"consensus-encoded leaf version as u8",
)
})?;
LeafVersion::from_consensus(value).map_err(|_| { LeafVersion::from_consensus(value).map_err(|_| {
E::invalid_value( E::invalid_value(
::serde::de::Unexpected::Unsigned(value as u64), ::serde::de::Unexpected::Unsigned(value as u64),