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(primitives): add Address::from_private_key #483

Merged
merged 2 commits into from
Jan 13, 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
11 changes: 10 additions & 1 deletion crates/primitives/src/bits/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,24 @@ impl Address {
Self::from_slice(&digest[12..])
}

/// Converts an ECDSA public key to its corresponding Ethereum address.
/// Converts an ECDSA verifying key to its corresponding Ethereum address.
#[inline]
#[cfg(feature = "k256")]
#[doc(alias = "from_verifying_key")]
pub fn from_public_key(pubkey: &k256::ecdsa::VerifyingKey) -> Self {
use k256::elliptic_curve::sec1::ToEncodedPoint;
let affine: &k256::AffinePoint = pubkey.as_ref();
let encoded = affine.to_encoded_point(false);
Self::from_raw_public_key(&encoded.as_bytes()[1..])
}

/// Converts an ECDSA signing key to its corresponding Ethereum address.
#[inline]
#[cfg(feature = "k256")]
#[doc(alias = "from_signing_key")]
pub fn from_private_key(private_key: &k256::ecdsa::SigningKey) -> Self {
Self::from_public_key(private_key.verifying_key())
}
}

/// Stack-allocated buffer for efficiently computing address checksums.
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<const N: usize> TryFrom<&[u8]> for FixedBytes<N> {

#[inline]
fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
<&Self>::try_from(slice).map(|this| *this)
<&Self>::try_from(slice).copied()
}
}

Expand Down
27 changes: 27 additions & 0 deletions crates/sol-types/tests/ui/keywords.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -1793,3 +1793,30 @@ error[E0747]: constant provided when a type was expected
|
6 | struct $kw {
| ^^^

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> tests/ui/keywords.rs:5:9
|
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
10 | | function $kw(uint $kw);
11 | | }
| | ^
| | |
| | impl doesn't use only types from inside the current crate
| |_________this is not defined in the current crate because this is a foreign trait
| this is not defined in the current crate because tuples are always foreign
...
42 | / kws! {
43 | | crate
44 | | self
45 | | Self
46 | | super
47 | | }
| |_- in this macro invocation
|
= note: define and implement a trait or new type instead
= note: this error originates in the macro `sol` which comes from the expansion of the macro `kws` (in Nightly builds, run with -Z macro-backtrace for more info)