Skip to content

Commit

Permalink
fix: allow non-boolean v values for PrimitiveSignature (#832)
Browse files Browse the repository at this point in the history
* fix: allow non-boolean v values for PrimitiveSignature

* fix
  • Loading branch information
klkvr authored Dec 26, 2024
1 parent f8d55dd commit bd25eb3
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions crates/primitives/src/signature/primitive_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl proptest::arbitrary::Arbitrary for PrimitiveSignature {
mod signature_serde {
use serde::{Deserialize, Deserializer, Serialize};

use crate::{U256, U64};
use crate::{normalize_v, U256, U64};

use super::PrimitiveSignature;

Expand Down Expand Up @@ -375,22 +375,28 @@ mod signature_serde {
where
D: Deserializer<'de>,
{
let (y_parity, r, s) = if deserializer.is_human_readable() {
let (y_parity, v, r, s) = if deserializer.is_human_readable() {
let HumanReadableRepr { y_parity, v, r, s } = <_>::deserialize(deserializer)?;
let y_parity = y_parity
.or(v)
.ok_or_else(|| serde::de::Error::custom("missing `yParity` or `v`"))?;
(y_parity, r, s)
(y_parity, v, r, s)
} else {
let NonHumanReadableRepr((r, s, y_parity)) = <_>::deserialize(deserializer)?;
(y_parity, r, s)
(Some(y_parity), None, r, s)
};

if y_parity > U64::from(1) {
Err(serde::de::Error::custom("invalid y_parity"))
// Attempt to extract `y_parity` bit from either `yParity` key or `v` value.
let y_parity = if let Some(y_parity) = y_parity {
if y_parity > U64::from(1) {
return Err(serde::de::Error::custom("invalid yParity"));
}

y_parity == U64::from(1)
} else if let Some(v) = v {
normalize_v(v.to()).ok_or(serde::de::Error::custom("invalid v"))?
} else {
Ok(Self::new(r, s, y_parity == U64::from(1)))
}
return Err(serde::de::Error::custom("missing `yParity` or `v`"));
};

Ok(Self::new(r, s, y_parity))
}
}
}
Expand Down

0 comments on commit bd25eb3

Please sign in to comment.