-
Notifications
You must be signed in to change notification settings - Fork 161
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: add Signature trait #782
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the renaming of Signature
to EcdsaSignature.
try and leave as many default trait method implementations as possible, then I will review again.
pub trait Signature<'a>: | ||
TryFrom<&'a [u8], Error = SignatureError> FromStr<Err = SignatureError> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub trait Signature<'a>: | |
TryFrom<&'a [u8], Error = SignatureError> FromStr<Err = SignatureError> | |
pub trait Signature: | |
for<'a> TryFrom<&'a [u8], Error = SignatureError> FromStr<Err = SignatureError> |
fn test_signature() -> Self | ||
where | ||
Self: Sized; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this shouldn't be in the trait imo. if it is, then with attribute #[cfg(test)]
or #[cfg(feature = test-util]
, but that would change the logic here, but this is a refactor pr, so leave it out, just move it to the EcdsaSignature
impl body.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
half the review sent, my bad
/// 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 { | ||
Self { r, s, v } | ||
} | ||
|
||
/// Returns the `r` component of this signature. | ||
#[inline] | ||
pub const fn r(&self) -> U256 { | ||
self.r | ||
} | ||
|
||
/// Returns the `s` component of this signature. | ||
#[inline] | ||
pub const fn s(&self) -> U256 { | ||
self.s | ||
} | ||
|
||
/// Returns the recovery ID as a `u8`. | ||
#[inline] | ||
pub const fn v(&self) -> Parity { | ||
self.v | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add these as trait methods too, they make sense in reth integration
#[cfg(feature = "rlp")] | ||
fn decode_rlp_vrs(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> | ||
where | ||
Self: Sized; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this for example can have a default impl, since the trait method Signature::from_rs_and_parity
is accessible
6d23d9f
to
e9d3a60
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
pub trait ArbitrarySuperSig: | ||
arbitrary::Arbitrary | ||
proptest::arbitrary::Arbitrary<Parameters=(),Strategy=proptest::strategy::FilterMap< | ||
<(U256, U256, Parity) as proptest::arbitrary::Arbitrary>::Strategy, | ||
fn((U256, U256, Parity)) -> Option<Self>, | ||
>> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be enough to do
pub trait ArbitrarySuperSig: | |
arbitrary::Arbitrary | |
proptest::arbitrary::Arbitrary<Parameters=(),Strategy=proptest::strategy::FilterMap< | |
<(U256, U256, Parity) as proptest::arbitrary::Arbitrary>::Strategy, | |
fn((U256, U256, Parity)) -> Option<Self>, | |
>> {} | |
pub trait ArbitrarySuperSig: | |
for <'a> arbitrary::Arbitrary<'a> {} |
#[cfg(feature = "rlp")] | ||
fn decode_rlp_vrs(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> { | ||
use alloy_rlp::Decodable; | ||
|
||
let parity: Parity = Decodable::decode(buf)?; | ||
let r = Decodable::decode(buf)?; | ||
let s = Decodable::decode(buf)?; | ||
|
||
Self::from_rs_and_parity(r, s, parity) | ||
.map_err(|_| alloy_rlp::Error::Custom("attempted to decode invalid field element")) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on second thought, would be cool to move all the feature gated methods into their respective XSuperSig trait actually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that too! However, I took iterative approach and decided to implement it in the future commits to this PR. I just did not expect you to review it since I did not ping you. Anyway, thanks for valuable suggestions!
Hey @emhane! |
a353cf6
to
2ac8cad
Compare
@emhane please review |
add ArbitrarySuperSig, K256SuperSig, RlpSuperSig, SerdeSuperSig feature traits to Signature
b03f172
to
0e5c533
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
I don't know exactly who from code owners to tag so I'll just pick @mattsse Please review. |
Motivation
Create
trait Signature
and move all possible methods and traits fromstruct Signature
totrait Signature
. Resolve #766Solution
trait Signature
.trait Signature
.trait Signature
to all possible supertraits.struct Signature
intostruct EcdsaSignature
.PR Checklist