diff --git a/crates/nostr/src/nips/nip44/mod.rs b/crates/nostr/src/nips/nip44/mod.rs index 62345c30d..43545b643 100644 --- a/crates/nostr/src/nips/nip44/mod.rs +++ b/crates/nostr/src/nips/nip44/mod.rs @@ -181,6 +181,19 @@ pub fn decrypt( public_key: &XOnlyPublicKey, payload: T, ) -> Result +where + T: AsRef<[u8]>, +{ + let bytes: Vec = decrypt_to_bytes(secret_key, public_key, payload)?; + String::from_utf8(bytes.to_vec()).map_err(|_| Error::Utf8Encode) +} + +/// Decrypt **without** converting bytes to UTF-8 string +pub fn decrypt_to_bytes( + secret_key: &SecretKey, + public_key: &XOnlyPublicKey, + payload: T, +) -> Result, Error> where T: AsRef<[u8]>, { @@ -212,12 +225,11 @@ where let mut buffer: Vec = ciphertext.to_vec(); cipher.apply_keystream(&mut buffer); - // Convert bytes to string - String::from_utf8(buffer.to_vec()).map_err(|_| Error::Utf8Encode) + Ok(buffer) } Version::V2 => { let conversation_key: ConversationKey = ConversationKey::derive(secret_key, public_key); - v2::decrypt(&conversation_key, &payload) + v2::decrypt_to_bytes(&conversation_key, &payload) } } } diff --git a/crates/nostr/src/nips/nip44/v2.rs b/crates/nostr/src/nips/nip44/v2.rs index 03a23226e..f5cae9af1 100644 --- a/crates/nostr/src/nips/nip44/v2.rs +++ b/crates/nostr/src/nips/nip44/v2.rs @@ -227,17 +227,6 @@ where Ok(payload) } -/// Decrypt with NIP44 (v2) -/// -/// **The payload MUST be already decoded from base64** -pub fn decrypt(conversation_key: &ConversationKey, payload: T) -> Result -where - T: AsRef<[u8]>, -{ - let bytes: Vec = decrypt_to_bytes(conversation_key, payload)?; - String::from_utf8(bytes).map_err(|e| Error::V2(ErrorV2::from(e))) -} - /// Decrypt with NIP44 (v2) /// /// **The payload MUST be already decoded from base64** @@ -631,7 +620,7 @@ mod tests { let note = vector.get("note").unwrap().as_str().unwrap(); let payload: Vec = general_purpose::STANDARD.decode(ciphertext).unwrap(); - let result = decrypt(&conversation_key, &payload); + let result = decrypt_to_bytes(&conversation_key, &payload); assert!(result.is_err(), "Should not have decrypted: {}", note); let err = result.unwrap_err(); diff --git a/crates/nostr/src/prelude.rs b/crates/nostr/src/prelude.rs index 17a081262..4b15df896 100644 --- a/crates/nostr/src/prelude.rs +++ b/crates/nostr/src/prelude.rs @@ -29,7 +29,7 @@ pub use crate::message::*; // NIPs pub use crate::nips::nip01::{self, *}; #[cfg(feature = "nip04")] -pub use crate::nips::nip04::{self, *}; +pub use crate::nips::nip04; #[cfg(all(feature = "std", feature = "nip05"))] pub use crate::nips::nip05::{self, *}; #[cfg(feature = "nip06")]