[][src]Struct clarity::private_key::PrivateKey

pub struct PrivateKey(_);

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.

Methods

impl PrivateKey
[src]

pub fn from_slice(slice: &[u8]) -> Result<PrivateKey, Error>
[src]

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

Input bytes are validated for a length only.

  • slice - A slice of raw bytes with a length of 32.

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

Get bytes back from a PrivateKey

pub fn to_public_key(&self) -> Result<Address, Error>
[src]

Create a public 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_public_key().unwrap();

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

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 inpput 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);

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

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.

Example

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

Trait Implementations

impl From<[u8; 32]> for PrivateKey
[src]

impl Eq for PrivateKey
[src]

impl PartialOrd<PrivateKey> for PrivateKey
[src]

impl Copy for PrivateKey
[src]

impl Default for PrivateKey
[src]

impl PartialEq<PrivateKey> for PrivateKey
[src]

impl ToString for PrivateKey
[src]

fn to_string(&self) -> String
[src]

Converts PrivateKey into a textual representation.

impl Clone for PrivateKey
[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl Ord for PrivateKey
[src]

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl Debug for PrivateKey
[src]

impl UpperHex for PrivateKey
[src]

impl LowerHex for PrivateKey
[src]

impl FromStr for PrivateKey
[src]

type Err = Error

The associated error which can be returned from parsing.

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

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 beggining.

impl Serialize for PrivateKey
[src]

impl<'de> Deserialize<'de> for PrivateKey
[src]

Auto Trait Implementations

impl Send for PrivateKey

impl Sync for PrivateKey

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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

impl<T> Same for T

type Output = T

Should always be Self