Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
grandima committed Oct 27, 2024
1 parent fe3fcd6 commit 6d23d9f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
62 changes: 30 additions & 32 deletions crates/primitives/src/signature/ecdsa_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,54 @@ pub struct EcdsaSignature {
}

impl EcdsaSignature {
/// Returns the recovery ID.
#[cfg(feature = "k256")]
#[inline]
const fn recid(&self) -> k256::ecdsa::RecoveryId {
self.v.recid()
}

#[cfg(feature = "k256")]
#[doc(hidden)]
#[deprecated(note = "use `Signature::recid` instead")]
pub const fn recovery_id(&self) -> k256::ecdsa::RecoveryId {
self.recid()
}

#[doc(hidden)]
fn test_signature() -> Self {
Self::from_scalars_and_parity(
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap()
}
}

impl Signature<'_> for EcdsaSignature {
/// Instantiate a new signature from `r`, `s`, and `v` values.
#[allow(clippy::missing_const_for_fn)]
pub fn new(r: U256, s: U256, v: Parity) -> Self {
fn new(r: U256, s: U256, v: Parity) -> Self {
Self { r, s, v }
}

/// Returns the `r` component of this signature.
#[inline]
pub const fn r(&self) -> U256 {
fn r(&self) -> U256 {
self.r
}

/// Returns the `s` component of this signature.
#[inline]
pub const fn s(&self) -> U256 {
fn s(&self) -> U256 {
self.s
}

/// Returns the recovery ID as a `u8`.
#[inline]
pub const fn v(&self) -> Parity {
fn v(&self) -> Parity {
self.v
}

/// Returns the recovery ID.
#[cfg(feature = "k256")]
#[inline]
const fn recid(&self) -> k256::ecdsa::RecoveryId {
self.v.recid()
}

#[cfg(feature = "k256")]
#[doc(hidden)]
#[deprecated(note = "use `Signature::recid` instead")]
pub const fn recovery_id(&self) -> k256::ecdsa::RecoveryId {
self.recid()
}
}

impl Signature<'_> for EcdsaSignature {
#[cfg(feature = "rlp")]
fn decode_rlp_vrs(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
use alloy_rlp::Decodable;
Expand All @@ -73,16 +81,6 @@ impl Signature<'_> for EcdsaSignature {
.map_err(|_| alloy_rlp::Error::Custom("attempted to decode invalid field element"))
}

#[doc(hidden)]
fn test_signature() -> Self {
Self::from_scalars_and_parity(
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)
.unwrap()
}

/// Returns the byte-array representation of this signature.
///
/// The first 32 bytes are the `r` value, the second 32 bytes the `s` value
Expand Down
20 changes: 15 additions & 5 deletions crates/primitives/src/signature/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ use core::str::FromStr;
pub trait Signature<'a>:
TryFrom<&'a [u8], Error = SignatureError> + FromStr<Err = SignatureError>
{
/// Instantiate a new signature from `r`, `s`, and `v` values.
fn new(r: U256, s: U256, v: Parity) -> Self;

/// Returns the `r` component of this signature.
#[inline]
fn r(&self) -> U256;

/// Returns the `s` component of this signature.
#[inline]
fn s(&self) -> U256;

/// Returns the recovery ID as a `u8`.
#[inline]
fn v(&self) -> Parity;

/// Decode an RLP-encoded VRS signature.
#[cfg(feature = "rlp")]
fn decode_rlp_vrs(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error>
where
Self: Sized;

#[doc(hidden)]
fn test_signature() -> Self
where
Self: Sized;

/// Returns the byte-array representation of this signature.
fn as_bytes(&self) -> [u8; 65];

Expand Down

0 comments on commit 6d23d9f

Please sign in to comment.