Struct clarity::private_key::PrivateKey
source · 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
impl PrivateKey
sourcepub fn from_bytes(bytes: [u8; 32]) -> Result<PrivateKey, Error>
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
sourcepub fn to_address(self) -> Address
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();
sourcepub fn sign_hash(&self, data: &[u8]) -> Signature
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);
sourcepub fn sign_insecure_msg(&self, data: &[u8]) -> Signature
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());
sourcepub fn sign_ethereum_msg(&self, data: &[u8]) -> Signature
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
impl Clone for PrivateKey
source§fn clone(&self) -> PrivateKey
fn clone(&self) -> PrivateKey
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for PrivateKey
impl Debug for PrivateKey
source§impl<'de> Deserialize<'de> for PrivateKey
impl<'de> Deserialize<'de> for PrivateKey
source§fn deserialize<D>(deserializer: D) -> Result<PrivateKey, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<PrivateKey, D::Error>where
D: Deserializer<'de>,
source§impl Display for PrivateKey
impl Display for PrivateKey
source§impl FromStr for PrivateKey
impl FromStr for PrivateKey
source§impl Hash for PrivateKey
impl Hash for PrivateKey
source§impl LowerHex for PrivateKey
impl LowerHex for PrivateKey
source§impl Ord for PrivateKey
impl Ord for PrivateKey
source§fn cmp(&self, other: &PrivateKey) -> Ordering
fn cmp(&self, other: &PrivateKey) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq for PrivateKey
impl PartialEq for PrivateKey
source§fn eq(&self, other: &PrivateKey) -> bool
fn eq(&self, other: &PrivateKey) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for PrivateKey
impl PartialOrd for PrivateKey
source§fn partial_cmp(&self, other: &PrivateKey) -> Option<Ordering>
fn partial_cmp(&self, other: &PrivateKey) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more