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 7 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
47 changes: 45 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 @@ -588,6 +593,7 @@ mod tests {

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

#[test]
#[cfg(feature = "k256")]
Expand Down Expand Up @@ -782,4 +788,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::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 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);
}
}