From 00076ee9078fc02bd78660a83d9b2ce2209d2be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien?= <3535019+leruaa@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:48:30 +0200 Subject: [PATCH] feat: prepare reth Signature migration to alloy (#732) * feat: impl Default for Parity * feat: impl Hash and Default for Signature * feat: add as_hex_bytes() to Signature * feat: add size() to Signature * chore: upstream reth Signature unit tests * fix: remove size() * nit: move Parity Default impl * fix: remove as_hex_bytes() * fix: remove Default on Signature --- crates/primitives/src/signature/parity.rs | 6 ++++ crates/primitives/src/signature/sig.rs | 42 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/crates/primitives/src/signature/parity.rs b/crates/primitives/src/signature/parity.rs index edecbd63e..c8415f1f0 100644 --- a/crates/primitives/src/signature/parity.rs +++ b/crates/primitives/src/signature/parity.rs @@ -16,6 +16,12 @@ pub enum Parity { Parity(bool), } +impl Default for Parity { + fn default() -> Self { + Self::Parity(false) + } +} + #[cfg(feature = "k256")] impl From for Parity { fn from(value: k256::ecdsa::RecoveryId) -> Self { diff --git a/crates/primitives/src/signature/sig.rs b/crates/primitives/src/signature/sig.rs index 7cc92d608..3835287e3 100644 --- a/crates/primitives/src/signature/sig.rs +++ b/crates/primitives/src/signature/sig.rs @@ -13,7 +13,7 @@ const SECP256K1N_ORDER: U256 = uint!(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141_U256); /// An Ethereum ECDSA signature. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub struct Signature { v: Parity, r: U256, @@ -583,11 +583,14 @@ impl proptest::arbitrary::Arbitrary for Signature { #[cfg(test)] #[allow(unused_imports)] mod tests { + use crate::Bytes; + use super::*; use std::str::FromStr; #[cfg(feature = "rlp")] use alloy_rlp::{Decodable, Encodable}; + use hex::FromHex; #[test] #[cfg(feature = "k256")] @@ -782,4 +785,41 @@ mod tests { // Assert that the length of the Signature matches the expected length assert_eq!(sig.length(), 69); } + + #[cfg(feature = "rlp")] + #[test] + fn test_rlp_vrs_len() { + let signature = Signature::test_signature(); + assert_eq!(67, signature.rlp_vrs_len()); + } + + #[cfg(feature = "rlp")] + #[test] + fn test_encode_and_decode() { + let signature = Signature::test_signature(); + + let mut encoded = Vec::new(); + signature.encode(&mut encoded); + assert_eq!(encoded.len(), signature.length()); + let decoded = Signature::decode(&mut &*encoded).unwrap(); + assert_eq!(signature, decoded); + } + + #[test] + fn test_as_bytes() { + let signature = Signature::new( + U256::from_str( + "18515461264373351373200002665853028612451056578545711640558177340181847433846", + ) + .unwrap(), + U256::from_str( + "46948507304638947509940763649030358759909902576025900602547168820602576006531", + ) + .unwrap(), + Parity::Parity(false), + ); + + let expected = Bytes::from_hex("0x28ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa63627667cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d831b").unwrap(); + assert_eq!(signature.as_bytes(), **expected); + } }