pub struct PublicKey(/* private fields */);
Expand description
Represents a secp256k1 public key.
const PRIVATE_KEY: [u8; 32] = hex!("e17fe86ce6e99f4806715b0c9412f8dad89334bf07f72d5834207a9d8f19d7f8");
// compressed public keys start with `0x02` or `0x03``, depending on sign of y-component
const COMPRESSED: [u8; 33] = hex!("021464586aeaea0eb5736884ca1bf42d165fc8e2243b1d917130fb9e321d7a93b8");
// full public key without prefix
const UNCOMPRESSED_PLAIN: [u8; 64] = hex!("1464586aeaea0eb5736884ca1bf42d165fc8e2243b1d917130fb9e321d7a93b8fb0699d4f177f9c84712f6d7c5f6b7f4f6916116047fa25c79ef806fc6c9523e");
// uncompressed public keys use `0x04` prefix
const UNCOMPRESSED: [u8; 65] = hex!("041464586aeaea0eb5736884ca1bf42d165fc8e2243b1d917130fb9e321d7a93b8fb0699d4f177f9c84712f6d7c5f6b7f4f6916116047fa25c79ef806fc6c9523e");
let from_privkey = PublicKey::from_privkey(&PRIVATE_KEY).unwrap();
let from_compressed = PublicKey::try_from(COMPRESSED.as_ref()).unwrap();
let from_uncompressed_plain = PublicKey::try_from(UNCOMPRESSED_PLAIN.as_ref()).unwrap();
let from_uncompressed = PublicKey::try_from(UNCOMPRESSED.as_ref()).unwrap();
assert_eq!(from_privkey, from_uncompressed);
assert_eq!(from_compressed, from_uncompressed_plain);
assert_eq!(from_uncompressed_plain, from_uncompressed);
// also works from a signed Ethereum transaction
const TX_HASH: [u8; 32] = hex!("eff80b9f035b1d369c6a60f362ac7c8b8c3b61b76d151d1be535145ccaa3e83e");
const R: [u8; 32] = hex!("c8048d137fbb10ddffa1e4ba5141c300fcd19e4fb7d0a4354ca62a7694e46f9b");
const S: [u8; 32] = hex!("5bb43e23d8b430f17ba3649e38b5a94d02815f0bcfaaf171800c52d4794c3136");
const V: u8 = 1u8;
let mut r_and_s = Vec::<u8>::with_capacity(64);
r_and_s.extend_from_slice(&R);
r_and_s.extend_from_slice(&S);
let sig = Signature::new(&r_and_s, V);
let from_transaction_signature = PublicKey::from_signature_hash(&TX_HASH, &sig).unwrap();
assert_eq!(from_uncompressed, from_transaction_signature);
Implementations§
Source§impl PublicKey
impl PublicKey
Sourcepub const SIZE_COMPRESSED: usize = 33usize
pub const SIZE_COMPRESSED: usize = 33usize
Size of the compressed public key in bytes
Sourcepub const SIZE_UNCOMPRESSED: usize = 65usize
pub const SIZE_UNCOMPRESSED: usize = 65usize
Size of the uncompressed public key in bytes
pub const SIZE_UNCOMPRESSED_PLAIN: usize = 64usize
pub fn from_privkey(private_key: &[u8]) -> Result<PublicKey>
pub fn from_signature(msg: &[u8], signature: &Signature) -> Result<PublicKey>
pub fn from_signature_hash( hash: &[u8], signature: &Signature, ) -> Result<PublicKey>
Sourcepub fn combine(summands: &[&PublicKey]) -> PublicKey
pub fn combine(summands: &[&PublicKey]) -> PublicKey
Sums all given public keys together, creating a new public key. Panics if reaches infinity (EC identity point), which is an invalid public key.
Sourcepub fn tweak_add(key: &PublicKey, tweak: &[u8]) -> PublicKey
pub fn tweak_add(key: &PublicKey, tweak: &[u8]) -> PublicKey
Adds the given public key with tweak
times secp256k1 generator, producing a new public key.
Panics if reaches infinity (EC identity point), which is an invalid public key.
Sourcepub fn to_address(&self) -> Address
pub fn to_address(&self) -> Address
Converts the public key to an Ethereum address
Trait Implementations§
Source§impl From<&PublicKey> for CurvePoint
impl From<&PublicKey> for CurvePoint
Source§impl From<PublicKey> for CompressedPublicKey
impl From<PublicKey> for CompressedPublicKey
Source§impl From<PublicKey> for CurvePoint
impl From<PublicKey> for CurvePoint
Source§impl Randomizable for PublicKey
impl Randomizable for PublicKey
Source§impl TryFrom<CurvePoint> for PublicKey
impl TryFrom<CurvePoint> for PublicKey
Source§type Error = CryptoError
type Error = CryptoError
The type returned in the event of a conversion error.
impl Eq for PublicKey
impl StructuralPartialEq for PublicKey
Auto Trait Implementations§
impl !Freeze for PublicKey
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.