Struct hedera::PrivateKey
source · pub struct PrivateKey(/* private fields */);
Expand description
A private key on the Hedera network.
Implementations§
source§impl PrivateKey
impl PrivateKey
sourcepub fn generate_ed25519() -> Self
pub fn generate_ed25519() -> Self
Generates a new Ed25519 PrivateKey
.
sourcepub fn generate_ecdsa() -> Self
pub fn generate_ecdsa() -> Self
Generates a new ECDSA(secp256k1) PrivateKey
.
sourcepub fn public_key(&self) -> PublicKey
pub fn public_key(&self) -> PublicKey
Gets the PublicKey
which corresponds to this PrivateKey
.
sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Parse a PrivateKey
from a sequence of bytes.
§Errors
Error::KeyParse
ifbytes
cannot be parsed into aPrivateKey
.
sourcepub fn from_bytes_ed25519(bytes: &[u8]) -> Result<Self>
pub fn from_bytes_ed25519(bytes: &[u8]) -> Result<Self>
Parse a Ed25519 PrivateKey
from a sequence of bytes.
§Errors
Error::KeyParse
ifbytes
cannot be parsed into a ed25519PrivateKey
.
sourcepub fn from_bytes_ecdsa(bytes: &[u8]) -> Result<Self>
pub fn from_bytes_ecdsa(bytes: &[u8]) -> Result<Self>
Parse a ECDSA(secp256k1) PrivateKey
from a sequence of bytes.
§Errors
Error::KeyParse
ifbytes
cannot be parsed into a ECDSA(secp256k1)PrivateKey
.
sourcepub fn from_bytes_der(bytes: &[u8]) -> Result<Self>
pub fn from_bytes_der(bytes: &[u8]) -> Result<Self>
Parse a PrivateKey
from a sequence of der encoded bytes.
§Errors
Error::KeyParse
ifbytes
cannot be parsed into aPrivateKey
.
sourcepub fn from_str_der(s: &str) -> Result<Self>
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
Error::KeyParse
ifs
cannot be parsed into aPrivateKey
.
sourcepub fn from_str_ed25519(s: &str) -> Result<Self>
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
Error::KeyParse
ifs
cannot be parsed into a ed25519PrivateKey
.
sourcepub fn from_str_ecdsa(s: &str) -> Result<Self>
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
ifs
cannot be parsed into a ECDSA(secp256k1)PrivateKey
.
sourcepub fn from_pem(pem: impl AsRef<[u8]>) -> Result<Self>
pub fn from_pem(pem: impl AsRef<[u8]>) -> Result<Self>
Parse a PrivateKey
from PEM encoded bytes.
§Errors
Error::KeyParse
ifpem
is not valid PEM.Error::KeyParse
if the type label (BEGIN XYZ) is notPRIVATE KEY
.Error::KeyParse
if the data contained inside the PEM is not a validPrivateKey
.
sourcepub fn from_pem_with_password(
pem: impl AsRef<[u8]>,
password: impl AsRef<[u8]>
) -> Result<Self>
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
Error::KeyParse
ifpem
is not valid PEM.Error::KeyParse
if the type label (BEGIN XYZ
) is notENCRYPTED PRIVATE KEY
.Error::KeyParse
if decrypting the private key fails.
§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());
sourcepub fn to_bytes_der(&self) -> Vec<u8> ⓘ
pub fn to_bytes_der(&self) -> Vec<u8> ⓘ
Return this PrivateKey
, serialized as der encoded bytes.
sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
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
sourcepub fn to_bytes_raw(&self) -> Vec<u8> ⓘ
pub fn to_bytes_raw(&self) -> Vec<u8> ⓘ
Return this PrivateKey
, serialized as bytes.
sourcepub fn to_string_der(&self) -> String
pub fn to_string_der(&self) -> String
DER encodes self, then hex encodes the result.
sourcepub fn to_string_raw(&self) -> String
pub fn to_string_raw(&self) -> String
Returns the raw bytes of self
after hex encoding.
sourcepub fn to_account_id(&self, shard: u64, realm: u64) -> AccountId
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");
sourcepub fn is_ed25519(&self) -> bool
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());
sourcepub fn is_ecdsa(&self) -> bool
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());
sourcepub fn sign_transaction<D: TransactionExecute>(
&self,
transaction: &mut Transaction<D>
) -> Result<Vec<u8>>
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.
sourcepub fn is_derivable(&self) -> bool
pub fn is_derivable(&self) -> bool
Returns true if calling derive
on self
would succeed.
sourcepub fn derive(&self, index: i32) -> Result<Self>
pub fn derive(&self, index: i32) -> Result<Self>
Derives a child key based on index
.
§Errors
Error::KeyDerive
if this is an Ecdsa key (unsupported operation)Error::KeyDerive
if this key has nochain_code
(key is not derivable)
sourcepub fn legacy_derive(&self, index: i64) -> Result<Self>
pub fn legacy_derive(&self, index: i64) -> Result<Self>
Derive a PrivateKey
based on index
.
§Errors
Error::KeyDerive
if this is an Ecdsa key (unsupported operation)
sourcepub fn from_mnemonic(mnemonic: &Mnemonic, passphrase: &str) -> Self
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
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 Display for PrivateKey
impl Display for PrivateKey
Auto Trait Implementations§
impl Freeze for PrivateKey
impl RefUnwindSafe for PrivateKey
impl Send for PrivateKey
impl Sync for PrivateKey
impl Unpin for PrivateKey
impl UnwindSafe for PrivateKey
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request