nostro2_signer/lib.rs
1#![warn(
2 clippy::all,
3 clippy::style,
4 clippy::unseparated_literal_suffix,
5 clippy::pedantic,
6 clippy::nursery
7)]
8//! # `NostrO2` Signer
9//!
10//! Key management and signing for the Nostr protocol.
11//!
12//! `nostro2-signer` provides keypair management, signing, and cryptographic
13//! operations for Nostr. It supports multiple key formats (hex, nsec, mnemonics),
14//! modern encryption standards (NIP-04, NIP-44), and privacy features (NIP-59).
15//!
16//! ## Quick Start
17//!
18//! ### Creating Keypairs
19//!
20//! ```rust
21//! use nostro2_signer::{NostrKeypair, Language};
22//!
23//! // Generate new random keypair
24//! let keypair = NostrKeypair::new();
25//!
26//! // Generate extractable keypair (allows exporting private key)
27//! let keypair = NostrKeypair::new_extractable();
28//!
29//! // From hex private key (64 hex characters)
30//! let keypair = NostrKeypair::from_hex(
31//! "a992011980303ea8c43f66087634283026e7796e7fcea8b61710239e19ee28c8",
32//! true
33//! )?;
34//!
35//! // From nsec
36//! let keypair = NostrKeypair::from_nsec(
37//! "nsec14xfqzxvqxql233plvcy8vdpgxqnww7tw0l823dshzq3eux0w9ryqulcv53",
38//! true
39//! )?;
40//!
41//! // From mnemonic (12 or 24 words)
42//! let keypair = NostrKeypair::from_mnemonic(
43//! "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
44//! Language::English,
45//! true
46//! )?;
47//! # Ok::<(), nostro2_signer::errors::NostrKeypairError>(())
48//! ```
49//!
50//! ### Signing Notes
51//!
52//! ```rust
53//! use nostro2::NostrNote;
54//! use nostro2_signer::NostrKeypair;
55//! use nostro2::NostrSigner;
56//!
57//! let keypair = NostrKeypair::new();
58//! let mut note = NostrNote::text_note("Hello, Nostr!");
59//!
60//! // Sign the note
61//! keypair.sign_note(&mut note)?;
62//! assert!(note.verify());
63//! # Ok::<(), nostro2_signer::errors::NostrKeypairError>(())
64//! ```
65//!
66//! ### Encryption (NIP-44)
67//!
68//! ```rust
69//! use nostro2::NostrNote;
70//! use nostro2_signer::{NostrKeypair, EncryptionScheme};
71//!
72//! let alice = NostrKeypair::new_extractable();
73//! let bob = NostrKeypair::new_extractable();
74//!
75//! let mut dm = NostrNote::with_kind(4)
76//! .with_content("Secret message");
77//!
78//! // Encrypt and sign
79//! let bob_pk = bob.pubkey();
80//! alice.sign_encrypted_note(
81//! &mut dm,
82//! &bob_pk,
83//! &EncryptionScheme::Nip44
84//! )?;
85//!
86//! // Decrypt
87//! let alice_pk = alice.pubkey();
88//! let decrypted = bob.decrypt_note(
89//! &dm,
90//! &alice_pk,
91//! &EncryptionScheme::Nip44
92//! )?;
93//! assert_eq!(decrypted, "Secret message");
94//! # Ok::<(), nostro2_signer::errors::NostrKeypairError>(())
95//! ```
96//!
97//! ### Gift Wrapping (NIP-59)
98//!
99//! ```rust
100//! use nostro2::NostrNote;
101//! use nostro2_signer::{NostrKeypair, GiftwrapScheme};
102//!
103//! let sender = NostrKeypair::new_extractable();
104//! let recipient = NostrKeypair::new_extractable();
105//!
106//! let mut rumor = NostrNote::text_note("Private message");
107//!
108//! // Wrap the note
109//! let wrapped = sender.giftwrap_note(
110//! &mut rumor,
111//! &recipient.pubkey(),
112//! &GiftwrapScheme::Ephemeral
113//! )?;
114//!
115//! // Unwrap
116//! let unwrapped = recipient.extract_rumor(&wrapped)?;
117//! assert_eq!(unwrapped.content, "Private message");
118//! # Ok::<(), nostro2_signer::errors::NostrKeypairError>(())
119//! ```
120//!
121//! ## Features
122//!
123//! - **Multiple Key Formats**: Hex, nsec (bech32), and BIP39 mnemonic support
124//! - **Smart Key Detection**: [`FromStr`](std::str::FromStr) tries all formats automatically
125//! - **NIP-04 & NIP-44**: Modern and legacy encryption standards
126//! - **NIP-59**: Gift wrap for sealed sender privacy
127//! - **Extractable Keys**: Optional key extraction protection
128//! - **Type Safety**: Comprehensive error handling with [`Result`](type.Result.html)
129//!
130//! ## Security
131//!
132//! - Keys are zeroized on drop when using extractable mode
133//! - Constant-time operations for cryptographic primitives
134//! - Uses pure Rust `k256` library for WASM compatibility
135//! - Optional key extraction protection
136pub mod errors;
137pub mod k256_keypair;
138pub extern crate nostro2;
139pub extern crate nostro2_nips;
140
141pub use bip39::Language;
142pub use k256_keypair::{EncryptionScheme, GiftwrapScheme, K256Keypair};
143
144/// Type alias for compatibility - `K256Keypair` is now the default
145pub type NostrKeypair = K256Keypair;
146
147/// Convenience type alias for Results with `NostrKeypairError`
148pub type Result<T> = std::result::Result<T, errors::NostrKeypairError>;