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

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.

Implementations

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

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

👎 Deprecated since 0.3.2:

Please use sign_ethereum_msg or sign_insecure_msg instead

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 deprecated as insecure, since it does not prevent signed messages from being possibly valid transactions by appending the standard \x19Ethereum Signed Message:\n32

Example

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

pub fn sign_insecure_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.

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_msg("Hello, world!".as_bytes());

pub fn sign_ethereum_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.

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

impl Clone for PrivateKey[src]

impl Copy for PrivateKey[src]

impl Debug for PrivateKey[src]

impl Default for PrivateKey[src]

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

impl Display for PrivateKey[src]

impl Eq for PrivateKey[src]

impl From<[u8; 32]> 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 LowerHex for PrivateKey[src]

impl Ord for PrivateKey[src]

impl PartialEq<PrivateKey> for PrivateKey[src]

impl PartialOrd<PrivateKey> for PrivateKey[src]

impl Serialize for PrivateKey[src]

impl StructuralEq for PrivateKey[src]

impl StructuralPartialEq for PrivateKey[src]

impl UpperHex for PrivateKey[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

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

impl<T> From<T> for T[src]

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

impl<T> Same<T> for T

type Output = T

Should always be Self

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

type Owned = T

The resulting type after obtaining ownership.

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.