Struct ethcontract::secret::PrivateKey
source · pub struct PrivateKey(/* private fields */);
Expand description
A secret key used for signing and hashing.
This type has a safe Debug
implementation that does not leak information.
Additionally, it implements Drop
to zeroize the memory to make leaking
passwords less likely.
Implementations§
source§impl PrivateKey
impl PrivateKey
sourcepub fn from_raw(raw: [u8; 32]) -> Result<Self, InvalidPrivateKey>
pub fn from_raw(raw: [u8; 32]) -> Result<Self, InvalidPrivateKey>
Creates a new private key from raw bytes.
sourcepub fn from_slice<B: AsRef<[u8]>>(raw: B) -> Result<Self, InvalidPrivateKey>
pub fn from_slice<B: AsRef<[u8]>>(raw: B) -> Result<Self, InvalidPrivateKey>
Creates a new private key from a slice of bytes.
sourcepub fn from_hex_str<S: AsRef<str>>(s: S) -> Result<Self, InvalidPrivateKey>
pub fn from_hex_str<S: AsRef<str>>(s: S) -> Result<Self, InvalidPrivateKey>
Creates a new private key from a hex string representation. Accepts hex
string with or without leading "0x"
.
sourcepub fn public_address(&self) -> Address
pub fn public_address(&self) -> Address
Gets the public address for a given private key.
Methods from Deref<Target = SecretKey>§
sourcepub fn display_secret(&self) -> DisplaySecret
pub fn display_secret(&self) -> DisplaySecret
Formats the explicit byte value of the secret key kept inside the type as a little-endian hexadecimal string using the provided formatter.
This is the only method that outputs the actual secret key value, and, thus, should be used with extreme caution.
§Examples
use secp256k1::SecretKey;
let key = SecretKey::from_str("0000000000000000000000000000000000000000000000000000000000000001").unwrap();
// Normal debug hides value (`Display` is not implemented for `SecretKey`).
// E.g., `format!("{:?}", key)` prints "SecretKey(#2518682f7819fb2d)".
// Here we explicitly display the secret value:
assert_eq!(
"0000000000000000000000000000000000000000000000000000000000000001",
format!("{}", key.display_secret())
);
// Also, we can explicitly display with `Debug`:
assert_eq!(
format!("{:?}", key.display_secret()),
format!("DisplaySecret(\"{}\")", key.display_secret())
);
sourcepub fn secret_bytes(&self) -> [u8; 32]
pub fn secret_bytes(&self) -> [u8; 32]
Returns the secret key as a byte value.
sourcepub fn keypair<C>(&self, secp: &Secp256k1<C>) -> KeyPairwhere
C: Signing,
pub fn keypair<C>(&self, secp: &Secp256k1<C>) -> KeyPairwhere
C: Signing,
Returns the KeyPair
for this SecretKey
.
This is equivalent to using KeyPair::from_secret_key
.
sourcepub fn public_key<C>(&self, secp: &Secp256k1<C>) -> PublicKeywhere
C: Signing,
pub fn public_key<C>(&self, secp: &Secp256k1<C>) -> PublicKeywhere
C: Signing,
Returns the PublicKey
for this SecretKey
.
This is equivalent to using PublicKey::from_secret_key
.
sourcepub fn x_only_public_key<C>(
&self,
secp: &Secp256k1<C>
) -> (XOnlyPublicKey, Parity)where
C: Signing,
pub fn x_only_public_key<C>(
&self,
secp: &Secp256k1<C>
) -> (XOnlyPublicKey, Parity)where
C: Signing,
Returns the XOnlyPublicKey
(and it’s Parity
) for this SecretKey
.
This is equivalent to XOnlyPublicKey::from_keypair(self.keypair(secp))
.
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 more