Skip to content

Commit

Permalink
connect: support both NIP04 and NIP44 for message decryption
Browse files Browse the repository at this point in the history
This updates the NIP46 message handling to use either NIP04 or NIP44 decryption by checking for `?iv=` in the event content.

Closes #697

Co-authored-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
erskingardner and yukibtc committed Dec 29, 2024
1 parent c665521 commit 6ae5c67
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* sdk: refactor POW difficulty management ([Yuki Kishimoto])
* connect: require `fmt::Debug`, `Send` and `Sync` for `AuthUrlHandler` ([Yuki Kishimoto])
* connect: improve secret matching for `NostrConnectRemoteSigner` ([Yuki Kishimoto])
* connect: support both NIP04 and NIP44 for message decryption ([erskingardner])
* zapper: bump `webln` to 0.4 ([Yuki Kishimoto])
* zapper: require `fmt::Debug`, `Send` and `Sync` for `NostrZapper` ([Yuki Kishimoto])
* bindings: refactor `SendEventOutput` and `SubscribeOutput` ([Yuki Kishimoto])
Expand Down
8 changes: 4 additions & 4 deletions crates/nostr-connect/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::sync::broadcast::Receiver;
use tokio::sync::OnceCell;

use crate::error::Error;
use crate::util;

/// Nostr Connect Client
///
Expand Down Expand Up @@ -258,8 +259,8 @@ impl NostrConnect {
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::NostrConnect {
let msg = nip04::decrypt(secret_key, &event.pubkey, &event.content)?;
let msg = Message::from_json(msg)?;
let msg: String = util::decrypt(secret_key, &event)?;
let msg: Message = Message::from_json(msg)?;

tracing::debug!("Received NIP46 message: '{msg}'");

Expand Down Expand Up @@ -413,8 +414,7 @@ async fn get_remote_signer_public_key(
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::NostrConnect {
// Decrypt content
let msg: String =
nip04::decrypt(app_keys.secret_key(), &event.pubkey, event.content)?;
let msg: String = util::decrypt(app_keys.secret_key(), &event)?;

tracing::debug!("Received Nostr Connect message: '{msg}'");

Expand Down
5 changes: 4 additions & 1 deletion crates/nostr-connect/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::convert::Infallible;

use nostr::event::builder;
use nostr::nips::{nip04, nip46};
use nostr::nips::{nip04, nip44, nip46};
use nostr::PublicKey;
use thiserror::Error;
use tokio::sync::SetError;
Expand All @@ -21,6 +21,9 @@ pub enum Error {
/// NIP04 error
#[error(transparent)]
NIP04(#[from] nip04::Error),
/// NIP44 error
#[error(transparent)]
NIP44(#[from] nip44::Error),
/// NIP46 error
#[error(transparent)]
NIP46(#[from] nip46::Error),
Expand Down
1 change: 1 addition & 0 deletions crates/nostr-connect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pub mod client;
pub mod error;
pub mod prelude;
pub mod signer;
mod util;
7 changes: 2 additions & 5 deletions crates/nostr-connect/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use nostr::nips::nip46::{Message, Request, ResponseResult};
use nostr_relay_pool::prelude::*;

use crate::error::Error;
use crate::util;

/// Nostr Connect Keys
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -176,11 +177,7 @@ impl NostrConnectRemoteSigner {
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind == Kind::NostrConnect {
if let Ok(msg) = nip04::decrypt(
self.keys.signer.secret_key(),
&event.pubkey,
event.content,
) {
if let Ok(msg) = util::decrypt(self.keys.signer.secret_key(), &event) {
tracing::debug!("New Nostr Connect message received: {msg}");

let msg: Message = Message::from_json(msg)?;
Expand Down
25 changes: 25 additions & 0 deletions crates/nostr-connect/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use nostr::nips::{nip04, nip44};
use nostr::{Event, SecretKey};

use crate::error::Error;

/// Decrypt a NIP46 message. Support both NIP04 and NIP44.
pub fn decrypt(secret_key: &SecretKey, event: &Event) -> Result<String, Error> {
if event.content.contains("?iv=") {
Ok(nip04::decrypt(
secret_key,
&event.pubkey,
event.content.as_str(),
)?)
} else {
Ok(nip44::decrypt(
secret_key,
&event.pubkey,
event.content.as_str(),
)?)
}
}

0 comments on commit 6ae5c67

Please sign in to comment.