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 5 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 @@ -23,6 +23,12 @@ impl From<k256::ecdsa::RecoveryId> for Parity {
}
}

impl Default for Parity {
leruaa marked this conversation as resolved.
Show resolved Hide resolved
fn default() -> Self {
Self::Parity(false)
}
}

impl TryFrom<U64> for Parity {
type Error = <Self as TryFrom<u64>>::Error;
fn try_from(value: U64) -> Result<Self, Self::Error> {
Expand Down
70 changes: 68 additions & 2 deletions crates/primitives/src/signature/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
hex,
signature::{Parity, SignatureError},
uint, U256,
uint, Bytes, U256,
};
use alloc::vec::Vec;
use core::str::FromStr;
Expand All @@ -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, Default, PartialEq, Eq)]
pub struct Signature {
leruaa marked this conversation as resolved.
Show resolved Hide resolved
v: Parity,
r: U256,
Expand Down Expand Up @@ -314,6 +314,11 @@ impl Signature {
sig
}

/// Turn this signature into its hex-encoded representation.
pub fn as_hex_bytes(&self) -> Bytes {
leruaa marked this conversation as resolved.
Show resolved Hide resolved
self.as_bytes().into()
}

/// Sets the recovery ID by normalizing a `v` value.
#[inline]
pub fn with_parity<T: Into<Parity>>(self, parity: T) -> Self {
Expand Down Expand Up @@ -351,6 +356,12 @@ impl Signature {
self.write_rlp_v(out);
self.write_rlp_rs(out);
}

/// Calculates a heuristic for the in-memory size of the [Signature].
#[inline]
pub const fn size(&self) -> usize {
leruaa marked this conversation as resolved.
Show resolved Hide resolved
core::mem::size_of::<Self>()
}
}

#[cfg(feature = "rlp")]
Expand Down Expand Up @@ -588,6 +599,7 @@ mod tests {

#[cfg(feature = "rlp")]
use alloy_rlp::{Decodable, Encodable};
use hex::FromHex;

#[test]
#[cfg(feature = "k256")]
Expand Down Expand Up @@ -782,4 +794,58 @@ 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::default();
assert_eq!(3, signature.rlp_vrs_len());
}

#[cfg(feature = "rlp")]
#[test]
fn test_encode_and_decode() {
let signature = Signature::default();

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 ensure_size_equals_sum_of_fields() {
let signature = Signature::new(
U256::from_str(
"18515461264373351373200002665853028612451056578545711640558177340181847433846",
)
.unwrap(),
U256::from_str(
"46948507304638947509940763649030358759909902576025900602547168820602576006531",
)
.unwrap(),
Parity::Parity(false),
);

assert!(signature.size() >= 65);
}

#[test]
fn test_as_hex_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_hex_bytes(), expected);
}
}