Struct nostr_types::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 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.

source§

impl PrivateKey

source

pub fn shared_secret( &self, other: &PublicKey, algo: ContentEncryptionAlgorithm, ) -> [u8; 32]

Get the shared secret

source

pub fn encrypt( &self, other: &PublicKey, plaintext: &str, algo: ContentEncryptionAlgorithm, ) -> Result<String, Error>

Encrypt

source

pub fn detect_encryption_algorithm( ciphertext: &str, ) -> ContentEncryptionAlgorithm

Detect encryption algorithm of ciphertext

source

pub fn decrypt( &self, other: &PublicKey, ciphertext: &str, ) -> Result<String, Error>

Decrypt (detects encryption version)

source

pub fn decrypt_nip04( &self, other: &PublicKey, ciphertext: &str, ) -> Result<Vec<u8>, Error>

Decrypt NIP-04 only

source

pub fn decrypt_nip44( &self, other: &PublicKey, ciphertext: &str, ) -> Result<String, Error>

Decrypt NIP-44 only, version is detected

source§

impl PrivateKey

source

pub fn new() -> PrivateKey

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

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 as_bech32_string(&mut self) -> String

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 as_secret_key(&self) -> SecretKey

As a secp256k1::SecretKey

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)

Trait Implementations§

source§

impl Debug for PrivateKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for PrivateKey

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Drop for PrivateKey

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Signer for PrivateKey

source§

fn decrypt(&self, other: &PublicKey, ciphertext: &str) -> Result<String, Error>

Decrypt NIP-44

source§

fn nip44_conversation_key(&self, other: &PublicKey) -> Result<[u8; 32], Error>

Get NIP-44 conversation key

source§

fn is_locked(&self) -> bool

Is the signer locked?
source§

fn unlock(&mut self, _password: &str) -> Result<(), Error>

Try to unlock access to the private key
source§

fn lock(&mut self)

Lock access to the private key
source§

fn change_passphrase( &mut self, _old: &str, _new: &str, _log_n: u8, ) -> Result<(), Error>

Change the passphrase used for locking access to the private key
source§

fn upgrade(&mut self, _pass: &str, _log_n: u8) -> Result<(), Error>

Upgrade the encrypted private key to the latest format
source§

fn public_key(&self) -> PublicKey

What is the signer’s public key?
source§

fn encrypted_private_key(&self) -> Option<&EncryptedPrivateKey>

What is the signer’s encrypted private key?
source§

fn export_private_key_in_hex( &mut self, _pass: &str, _log_n: u8, ) -> Result<(String, bool), Error>

Export the private key in hex. Read more
source§

fn export_private_key_in_bech32( &mut self, _pass: &str, _log_n: u8, ) -> Result<(String, bool), Error>

Export the private key in bech32. Read more
source§

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

Sign a 32-bit hash
source§

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

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

fn encrypt( &self, other: &PublicKey, plaintext: &str, algo: ContentEncryptionAlgorithm, ) -> Result<String, Error>

Encrypt
source§

fn key_security(&self) -> Result<KeySecurity, Error>

Get the security level of the private key
source§

fn generate_delegation_signature( &self, delegated_pubkey: PublicKey, delegation_conditions: &DelegationConditions, ) -> Result<Signature, Error>

Generate delegation signature
source§

fn verify_delegation_signature( &self, delegated_pubkey: PublicKey, delegation_conditions: &DelegationConditions, signature: &Signature, ) -> Result<(), Error>

Verify delegation signature
source§

fn sign_event(&self, input: PreEvent) -> Result<Event, Error>

Sign an event
source§

fn sign_event2(&self, input: PreEventV2) -> Result<EventV2, Error>

Sign an event
source§

fn sign_event_with_pow( &self, input: PreEvent, zero_bits: u8, work_sender: Option<Sender<u8>>, ) -> Result<Event, Error>

Sign an event with Proof-of-Work
source§

fn giftwrap(&self, input: PreEvent, pubkey: PublicKey) -> Result<Event, Error>

Giftwrap an event
source§

fn giftwrap2( &self, input: PreEventV2, pubkey: PublicKey, ) -> Result<EventV2, Error>

Giftwrap an event
source§

fn create_metadata_event( &self, input: PreEvent, metadata: Metadata, ) -> Result<Event, Error>

Create an event that sets Metadata
source§

fn create_zap_request_event( &self, recipient_pubkey: PublicKey, zapped_event: Option<Id>, millisatoshis: u64, relays: Vec<String>, content: String, ) -> Result<Event, Error>

Create a ZapRequest event These events are not published to nostr, they are sent to a lnurl.
source§

fn decrypt_event_contents(&self, event: &Event) -> Result<String, Error>

Decrypt the contents of an event
source§

fn unwrap_giftwrap(&self, event: &Event) -> Result<Rumor, Error>

If a gift wrap event, unwrap and return the inner Rumor
source§

fn unwrap_giftwrap2(&self, event: &EventV2) -> Result<RumorV2, Error>

If a gift wrap event, unwrap and return the inner Rumor @deprecated for migrations only
source§

fn unwrap_giftwrap1(&self, event: &EventV1) -> Result<RumorV1, Error>

If a gift wrap event, unwrap and return the inner Rumor @deprecated for migrations only

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

§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V