use crate::ChainId;
#[inline]
pub const fn to_eip155_v(v: u8, chain_id: ChainId) -> ChainId {
(v as u64) + 35 + chain_id * 2
}
#[cfg(feature = "k256")]
#[inline]
pub(crate) const fn normalize_v(v: u64) -> k256::ecdsa::RecoveryId {
let byte = normalize_v_to_byte(v);
debug_assert!(byte <= k256::ecdsa::RecoveryId::MAX);
match k256::ecdsa::RecoveryId::from_byte(byte) {
Some(recid) => recid,
None => unsafe { core::hint::unreachable_unchecked() },
}
}
pub(crate) const fn normalize_v_to_byte(v: u64) -> u8 {
match v {
0..=26 => (v % 4) as u8,
27..=34 => ((v - 27) % 4) as u8,
35.. => ((v - 1) % 2) as u8,
}
}
#[cfg(test)]
mod test {
#[test]
#[cfg(feature = "k256")]
fn normalizes_v() {
use super::*;
assert_eq!(normalize_v(27), k256::ecdsa::RecoveryId::from_byte(0).unwrap());
assert_eq!(normalize_v(28), k256::ecdsa::RecoveryId::from_byte(1).unwrap());
}
}