pub struct PrivateKey { /* private fields */ }
Expand description

Representation of an Ethereum private key.

Private key can be created using a textual representation, a raw binary form using array of bytes.

With PrivateKey you are able to sign messages, derive public keys. Cryptography-related methods use SECP256K1 elliptic curves.

Implementations§

source§

impl PrivateKey

source

pub fn from_bytes(bytes: [u8; 32]) -> Result<PrivateKey, Error>

Convert a given slice of bytes into a valid private key.

Input bytes are validated and an Error is returned if they are invalid

  • bytes - A static array of length 32
source

pub fn to_bytes(self) -> [u8; 32]

Get bytes back from a PrivateKey

source

pub fn to_address(self) -> Address

Get the address key for a given private key.

This is well explained in the Ethereum Yellow Paper Appendix F.

§Examples
use clarity::PrivateKey;
let private_key : PrivateKey = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f1e".parse().unwrap();
let public_key = private_key.to_address();
source

pub fn sign_hash(&self, data: &[u8]) -> Signature

Signs a message that is represented by a hash contained in a binary form.

Requires the data buffer to be exactly 32 bytes in length. You can prepare an input using a hashing function such as Keccak256 which will return a buffer of exact size.

You are advised, though, to use sign_msg which is more user friendly version that uses Keccak256 internally.

§Example
let private_key : PrivateKey = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f1e".parse().unwrap();
let hash = Keccak256::digest("Hello, world!".as_bytes());
let signature = private_key.sign_hash(&hash);
source

pub fn sign_insecure_msg(&self, data: &[u8]) -> Signature

Signs any message represented by a slice of data.

Internally it makes Keccak256 hash out of your data, and then creates a signature.

This is more user friendly version of sign_hash which means it will use Keccak256 function to hash your input data.

This method is provided on the assumption you know what you are doing, it does not prevent signed messages from being possibly valid transactions. No Ethereum signed message salt is appended. Use with Caution!

§Example
let private_key : PrivateKey = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f1e".parse().unwrap();
let signature = private_key.sign_insecure_msg("Hello, world!".as_bytes());
source

pub fn sign_ethereum_msg(&self, data: &[u8]) -> Signature

Signs any message represented by a slice of data.

Internally it makes Keccak256 hash out of your data, and then creates a signature.

This is more user friendly version of sign_hash which means it will use Keccak256 function to hash your input data.

Remember this function appends \x19Ethereum Signed Message:\n32 to your hash! so you may need to take that into account when you go to verify

§Example
let private_key : PrivateKey = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f1e".parse().unwrap();
let signature = private_key.sign_ethereum_msg("Hello, world!".as_bytes());

Trait Implementations§

source§

impl Clone for PrivateKey

source§

fn clone(&self) -> PrivateKey

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 PrivateKey

source§

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

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

impl<'de> Deserialize<'de> for PrivateKey

source§

fn deserialize<D>(deserializer: D) -> Result<PrivateKey, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for PrivateKey

source§

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

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

impl FromStr for PrivateKey

source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parse a textual representation of a private key back into PrivateKey type.

It has to be a string that represents 64 characters that are hexadecimal representation of 32 bytes. Optionally this string can be prefixed with 0x at the start.

§

type Err = Error

The associated error which can be returned from parsing.
source§

impl Hash for PrivateKey

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl LowerHex for PrivateKey

source§

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

Formats the value using the given formatter.
source§

impl Ord for PrivateKey

source§

fn cmp(&self, other: &PrivateKey) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for PrivateKey

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for PrivateKey

source§

fn partial_cmp(&self, other: &PrivateKey) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for PrivateKey

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<[u8; 32]> for PrivateKey

§

type Error = Error

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

fn try_from(val: [u8; 32]) -> Result<PrivateKey, Error>

Performs the conversion.
source§

impl UpperHex for PrivateKey

source§

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

Formats the value using the given formatter.
source§

impl Copy for PrivateKey

source§

impl Eq for PrivateKey

source§

impl StructuralPartialEq for PrivateKey

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<U> As for U

source§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. 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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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

§

type Output = T

Should always be Self
source§

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

§

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§

default 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>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,