Struct hedera::PrivateKey

source ·
pub struct PrivateKey(/* private fields */);
Expand description

A private key on the Hedera network.

Implementations§

source§

impl PrivateKey

source

pub fn generate_ed25519() -> Self

Generates a new Ed25519 PrivateKey.

source

pub fn generate_ecdsa() -> Self

Generates a new ECDSA(secp256k1) PrivateKey.

source

pub fn public_key(&self) -> PublicKey

Gets the PublicKey which corresponds to this PrivateKey.

source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Parse a PrivateKey from a sequence of bytes.

§Errors
source

pub fn from_bytes_ed25519(bytes: &[u8]) -> Result<Self>

Parse a Ed25519 PrivateKey from a sequence of bytes.

§Errors
source

pub fn from_bytes_ecdsa(bytes: &[u8]) -> Result<Self>

Parse a ECDSA(secp256k1) PrivateKey from a sequence of bytes.

§Errors
  • Error::KeyParse if bytes cannot be parsed into a ECDSA(secp256k1) PrivateKey.
source

pub fn from_bytes_der(bytes: &[u8]) -> Result<Self>

Parse a PrivateKey from a sequence of der encoded bytes.

§Errors
source

pub fn from_str_der(s: &str) -> Result<Self>

Parse a PrivateKey from a der encoded string.

Optionally strips a 0x prefix. See from_bytes_der.

§Errors
source

pub fn from_str_ed25519(s: &str) -> Result<Self>

Parse a Ed25519 PrivateKey from a string containing the raw key material.

Optionally strips a 0x prefix. See: from_bytes_ed25519.

§Errors
source

pub fn from_str_ecdsa(s: &str) -> Result<Self>

Parse a ECDSA(secp256k1) PrivateKey from a string containing the raw key material.

Optionally strips a 0x prefix. See: from_str_ecdsa.

§Errors
  • Error::KeyParse if s cannot be parsed into a ECDSA(secp256k1) PrivateKey.
source

pub fn from_pem(pem: impl AsRef<[u8]>) -> Result<Self>

Parse a PrivateKey from PEM encoded bytes.

§Errors
source

pub fn from_pem_with_password( pem: impl AsRef<[u8]>, password: impl AsRef<[u8]> ) -> Result<Self>

Parse a PrivateKey from encrypted PEM encoded bytes.

§Errors
§Examples
use hedera::PrivateKey;
use hex_literal::hex;

// ⚠️ WARNING ⚠️
// don't use this private key in your applications, it is compromised by virtue of being here.
let pem = "-----BEGIN ENCRYPTED PRIVATE KEY-----
MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAw
DAYIKoZIhvcNAgkFADAdBglghkgBZQMEAQIEENfMacg1/Txd/LhKkxZtJe0EQEVL
mez3xb+sfUIF3TKEIDJtw7H0xBNlbAfLxTV11pofiar0z1/WRBHFFUuGIYSiKjlU
V9RQhAnemO84zcZfTYs=
-----END ENCRYPTED PRIVATE KEY-----";

let password = "test";

let sk = PrivateKey::from_pem_with_password(pem, password)?;

let expected_signature = hex!(
    "a0e5f7d1cf06a4334be4f856aeb427f7"
    "fd53ea7e5c66f10eaad083d736a5adfd"
    "0ac7e4fd3fa90f6b6aad8f1df4149ecd"
    "330a91d5ebff832b11bf14d43eaf5600"
);
assert_eq!(sk.sign(b"message").as_slice(), expected_signature.as_slice());
source

pub fn to_bytes_der(&self) -> Vec<u8>

Return this PrivateKey, serialized as der encoded bytes.

source

pub fn to_bytes(&self) -> Vec<u8>

Return this PrivateKey, serialized as bytes.

If this is an ed25519 private key, this is equivalent to to_bytes_raw If this is an ecdsa private key, this is equivalent to to_bytes_der

source

pub fn to_bytes_raw(&self) -> Vec<u8>

Return this PrivateKey, serialized as bytes.

source

pub fn to_string_der(&self) -> String

DER encodes self, then hex encodes the result.

source

pub fn to_string_raw(&self) -> String

Returns the raw bytes of self after hex encoding.

source

pub fn to_account_id(&self, shard: u64, realm: u64) -> AccountId

Creates an AccountId with the given shard, realm, and self.public_key() as an alias.

§Examples
use hedera::PrivateKey;

let key: PrivateKey = "3030020100300706052b8104000a042204208776c6b831a1b61ac10dac0304a2843de4716f54b1919bb91a2685d0fe3f3048".parse().unwrap();

let account_id = key.to_account_id(0, 0);
assert_eq!(account_id.to_string(), "0.0.302d300706052b8104000a03220002703a9370b0443be6ae7c507b0aec81a55e94e4a863b9655360bd65358caa6588");
source

pub fn is_ed25519(&self) -> bool

Returns true if self is an Ed25519 PrivateKey.

§Examples
use hedera::PrivateKey;
let sk = PrivateKey::generate_ed25519();

assert!(sk.is_ed25519());
use hedera::PrivateKey;
let sk = PrivateKey::generate_ecdsa();

assert!(!sk.is_ed25519());
source

pub fn is_ecdsa(&self) -> bool

Returns true if this is an ECDSA(secp256k1) PrivateKey.

§Examples
use hedera::PrivateKey;
let sk = PrivateKey::generate_ecdsa();

assert!(sk.is_ecdsa());
use hedera::PrivateKey;
let sk = PrivateKey::generate_ed25519();

assert!(!sk.is_ecdsa());
source

pub fn sign(&self, message: &[u8]) -> Vec<u8>

Signs the given message.

source

pub fn sign_transaction<D: TransactionExecute>( &self, transaction: &mut Transaction<D> ) -> Result<Vec<u8>>

Signs the given transaction.

§Errors

This function will freeze the transaction if it is not frozen. As such, any error that can be occur during Transaction::freeze can also occur here.

source

pub fn is_derivable(&self) -> bool

Returns true if calling derive on self would succeed.

source

pub fn derive(&self, index: i32) -> Result<Self>

Derives a child key based on index.

§Errors
source

pub fn legacy_derive(&self, index: i64) -> Result<Self>

Derive a PrivateKey based on index.

§Errors
source

pub fn from_mnemonic(mnemonic: &Mnemonic, passphrase: &str) -> Self

Recover a PrivateKey from a mnemonic phrase and a passphrase.

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

§

type Err = Error

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

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

Parses a string s to return a value of this type. Read more

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<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> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more