Struct PrivateKey

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

This is a private key which is to be kept secret and is used to prove identity

Implementations§

Source§

impl PrivateKey

Source

pub fn generate() -> PrivateKey

Generate a new PrivateKey (which can be used to get the PublicKey)

Source

pub fn public_key(&self) -> PublicKey

Get the PublicKey matching this PrivateKey

Source

pub fn key_security(&self) -> KeySecurity

Get the security level of the private key

Source

pub fn as_hex_string(&mut self) -> String

Render into a hexadecimal string

WARNING: This weakens the security of your key. Your key will be marked with KeySecurity::Weak if you execute this.

Source

pub fn try_from_hex_string(v: &str) -> Result<PrivateKey, Error>

Create from a hexadecimal string

This creates a key with KeySecurity::Weak. Use generate() or import_encrypted() for KeySecurity::Medium

Source

pub fn try_as_bech32_string(&mut self) -> Result<String, Error>

Export as a bech32 encoded string

WARNING: This weakens the security of your key. Your key will be marked with KeySecurity::Weak if you execute this.

Source

pub fn try_from_bech32_string(s: &str) -> Result<PrivateKey, Error>

Import from a bech32 encoded string

This creates a key with KeySecurity::Weak. Use generate() or import_encrypted() for KeySecurity::Medium

Source

pub fn sign_id(&self, id: Id) -> Result<Signature, Error>

Sign a 32-bit hash

Source

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

Sign a message (this hashes with SHA-256 first internally)

Source

pub fn nip04_encrypt( &self, other: &PublicKey, plaintext: &[u8], ) -> Result<([u8; 16], Vec<u8>), Error>

Encrypt content via a shared secret according to NIP-04. Returns (IV, Ciphertext) pair.

Source

pub fn nip04_decrypt( &self, other: &PublicKey, ciphertext: &[u8], iv: [u8; 16], ) -> Result<Vec<u8>, Error>

Decrypt content via a shared secret according to NIP-04

Source

pub fn export_encrypted( &self, password: &str, log2_rounds: u8, ) -> Result<EncryptedPrivateKey, Error>

Export in a (non-portable) encrypted form. This does not downgrade the security of the key, but you are responsible to keep it encrypted. You should not attempt to decrypt it, only use import_encrypted() on it, or something similar in another library/client which also respects key security.

This currently exports into EncryptedPrivateKey version 2.

We recommend you zeroize() the password you pass in after you are done with it.

Source

pub fn import_encrypted( encrypted: &EncryptedPrivateKey, password: &str, ) -> Result<PrivateKey, Error>

Import an encrypted private key which was exported with export_encrypted().

We recommend you zeroize() the password you pass in after you are done with it.

This is backwards-compatible with keys that were exported with older code.

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, 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T