Skip to content

Commit

Permalink
feat: prepare reth Signature migration to alloy (#732)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
leruaa authored Sep 9, 2024
1 parent 1ff30e2 commit 00076ee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions crates/primitives/src/signature/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub enum Parity {
Parity(bool),
}

impl Default for Parity {
fn default() -> Self {
Self::Parity(false)
}
}

#[cfg(feature = "k256")]
impl From<k256::ecdsa::RecoveryId> for Parity {
fn from(value: k256::ecdsa::RecoveryId) -> Self {
Expand Down
42 changes: 41 additions & 1 deletion crates/primitives/src/signature/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 00076ee

Please sign in to comment.