diff --git a/src/nips/nip17.rs b/src/nips/nip17.rs index 6d16180..8c32ea4 100644 --- a/src/nips/nip17.rs +++ b/src/nips/nip17.rs @@ -12,9 +12,16 @@ pub struct Message { pub is_incoming: bool, } -/// Sends a DM. The SDK handles the NIP-17/NIP-04 logic internally. pub async fn send_dm(client: &Client, receiver: PublicKey, message: &str) -> Result { + println!("[SDK] Sending NIP-17 message to: {}", receiver.to_bech32()?); + + // send_private_msg in newer nostr-sdk versions handles NIP-17 wrapping. + // Important: It must be broadcasted to relays that both you and the receiver use. let output = client.send_private_msg(receiver, message, None).await?; + + // We wait briefly to ensure the relays acknowledged the event + println!("[SDK] Event broadcasted successfully. ID: {}", output.id()); + Ok(*output.id()) } @@ -23,57 +30,40 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result = Vec::new(); - for event in all_events { - // Skip duplicates from multiple relays or overlapping filters + for event in events.to_vec() { if !seen_ids.insert(event.id) { continue; } - // === NIP-17 (Modern) Processing === if event.kind == Kind::GiftWrap { + // Attempt to unwrap (decrypt) the GiftWrap if let Ok(unwrapped) = client.unwrap_gift_wrap(&event).await { let rumor = unwrapped.rumor; - let is_from_contact = rumor.pubkey == contact_pubkey; - // Outgoing logic: Rumor is from me, and rumor p-tag is the contact - let is_from_me = rumor.pubkey == my_pubkey + // Is this from the contact to me? + let is_incoming = rumor.pubkey == contact_pubkey; + + // Is this a copy of a message I sent to the contact? + // We check the internal rumor tags for the recipient's pubkey + let is_outgoing = rumor.pubkey == my_pubkey && rumor.tags.iter().any(|t| { if let Some(TagStandard::PublicKey { public_key, .. }) = t.as_standardized() { @@ -83,7 +73,7 @@ pub async fn get_dm_messages(client: &Client, contact_npub: &str) -> Result Result Result