Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prepare reth Signature migration to alloy #732

Merged
merged 9 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
leruaa marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}