hopr_crypto_types::types

Struct PublicKey

Source
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

Source

pub const SIZE_COMPRESSED: usize = 33usize

Size of the compressed public key in bytes

Source

pub const SIZE_UNCOMPRESSED_PLAIN: usize = 64usize

Source

pub const SIZE_UNCOMPRESSED: usize = 65usize

Size of the uncompressed public key in bytes

Source

pub fn from_privkey(private_key: &[u8]) -> Result<PublicKey>

Source

pub fn from_signature(msg: &[u8], signature: &Signature) -> Result<PublicKey>

Source

pub fn from_signature_hash( hash: &[u8], signature: &Signature, ) -> Result<PublicKey>

Source

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.

Source

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.

Source

pub fn random() -> Self

Generates a new random public key. Because the corresponding private key is discarded, this might be useful only for testing purposes.

Source

pub fn to_address(&self) -> Address

Converts the public key to an Ethereum address

Source

pub fn to_bytes(&self, compressed: bool) -> Box<[u8]>

Serializes the public key to a binary form.

Source

pub fn to_hex(&self, compressed: bool) -> String

Serializes the public key to a binary form and converts it to hexadecimal string representation.

Trait Implementations§

Source§

impl Clone for PublicKey

Source§

fn clone(&self) -> PublicKey

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PublicKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PublicKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&PublicKey> for CurvePoint

Source§

fn from(pubkey: &PublicKey) -> Self

Converts to this type from the input type.
Source§

impl From<&PublicKey> for ProjectivePoint

Source§

fn from(value: &PublicKey) -> Self

Converts to this type from the input type.
Source§

impl From<PublicKey<Secp256k1>> for PublicKey

Source§

fn from(key: PublicKey<Secp256k1>) -> Self

Converts to this type from the input type.
Source§

impl From<PublicKey> for CompressedPublicKey

Source§

fn from(value: PublicKey) -> Self

Converts to this type from the input type.
Source§

impl From<PublicKey> for CurvePoint

Source§

fn from(pubkey: PublicKey) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for PublicKey

Source§

fn eq(&self, other: &PublicKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<&[u8]> for PublicKey

Source§

type Error = GeneralError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<AffinePoint> for PublicKey

Source§

type Error = CryptoError

The type returned in the event of a conversion error.
Source§

fn try_from(value: AffinePoint) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<CurvePoint> for PublicKey

Source§

type Error = CryptoError

The type returned in the event of a conversion error.
Source§

fn try_from(value: CurvePoint) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for PublicKey

Source§

impl StructuralPartialEq for PublicKey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> JsonSchemaMaybe for T