nostr icon indicating copy to clipboard operation
nostr copied to clipboard

Write Rust Nostr book

Open yukibtc opened this issue 1 year ago • 3 comments

.

yukibtc avatar Sep 29 '23 11:09 yukibtc

I'm missing a guide of how to receive nip-17 private direct messages. My code works fine with nip-04 encrypted messages and now I tried to migrate it to nip-17. On the sender side I'm sending with client.send_private_msg(pubkey, &message, None).

The code on the receiver side looks like this:

let my_pubkey = NostrPubkey::from_bech32(&self.nostr_client.get_npub()?)?;
        let filter_note = Filter::new()
            .kind(Kind::GiftWrap)
            .pubkey(my_pubkey)
            .since(Timestamp::now());

        self.nostr_client
            .client
            .subscribe(vec![filter_note], None)
            .await?;
        let mut notifications = self.nostr_client.client.notifications();

        while let Ok(notification) = notifications.recv().await {
            if let RelayPoolNotification::Event { event, .. } = notification {
                if let Ok(unwrapped_gift) = self.nostr_client.client.unwrap_gift_wrap(&event).await
                {
                    dbg!("Received event: {:?}", &unwrapped_gift);
                    if let Ok((contract_hash, contract)) =
                        self.parse_contract(&unwrapped_gift.rumor.content).await
                    {
                    ...
                    }

The problem is I don't get any events. I changed the kind to PrivateDirectMessage, but it doesn't work either, tough I think GiftWrap should be the right kind to receive. If I get the code working I would also contribute with a code example or a section in the book.

rodant avatar Aug 15 '24 16:08 rodant

I'm missing a guide of how to receive nip-17 private direct messages. My code works fine with nip-04 encrypted messages and now I tried to migrate it to nip-17. On the sender side I'm sending with client.send_private_msg(pubkey, &message, None).

The code on the receiver side looks like this:

let my_pubkey = NostrPubkey::from_bech32(&self.nostr_client.get_npub()?)?;
        let filter_note = Filter::new()
            .kind(Kind::GiftWrap)
            .pubkey(my_pubkey)
            .since(Timestamp::now());

        self.nostr_client
            .client
            .subscribe(vec![filter_note], None)
            .await?;
        let mut notifications = self.nostr_client.client.notifications();

        while let Ok(notification) = notifications.recv().await {
            if let RelayPoolNotification::Event { event, .. } = notification {
                if let Ok(unwrapped_gift) = self.nostr_client.client.unwrap_gift_wrap(&event).await
                {
                    dbg!("Received event: {:?}", &unwrapped_gift);
                    if let Ok((contract_hash, contract)) =
                        self.parse_contract(&unwrapped_gift.rumor.content).await
                    {
                    ...
                    }

The problem is I don't get any events. I changed the kind to PrivateDirectMessage, but it doesn't work either, tough I think GiftWrap should be the right kind to receive. If I get the code working I would also contribute with a code example or a section in the book.

Use .limit(0) instead of .since(Timestamp::now()). The gift wraps have a tweaked timestamp (in the past).

It's available as example in the bot.rs file (nostr-sdk crate). But probably I should add a nip17.rs example too.

yukibtc avatar Aug 15 '24 17:08 yukibtc

Ah, this makes sense and it works now. Thanks for your quick help.

rodant avatar Aug 15 '24 19:08 rodant

Hey there, I would like to try NIP17, I didn't know it was supported. It would be great if we could have a documentation on how to receive_private_msg, send_private_msg is pretty clear.

Thank you for your work

ethicnology avatar Oct 23 '24 01:10 ethicnology

Hey there, I would like to try NIP17, I didn't know it was supported. It would be great if we could have a documentation on how to receive_private_msg, send_private_msg is pretty clear.

Thank you for your work

Hey, I would contribute a Rust example. Though, it'll be as early as next week, I'm now having some vacations.

rodant avatar Oct 23 '24 07:10 rodant

Hey there, I would like to try NIP17, I didn't know it was supported. It would be great if we could have a documentation on how to receive_private_msg, send_private_msg is pretty clear.

Check the bot example in nostr-sdk crate: https://github.com/rust-nostr/nostr/blob/master/crates%2Fnostr-sdk%2Fexamples%2Fbot.rs

Is not available a receive_private_msg method. You have to subscribe to gift wrap filter and handle events with handle_notifications loop. When you receive a gift wrap event, you can use the Client::unwrap_gift_wrap method to extract the rumor and the sender (the client must have a signer to do this).

yukibtc avatar Oct 23 '24 10:10 yukibtc

Hey there, I would like to try NIP17, I didn't know it was supported. It would be great if we could have a documentation on how to receive_private_msg, send_private_msg is pretty clear.

Thank you for your work

Hi, I just submitted a PR adding example code for NIP-17 to the book. You can take a look at the code in the PR as long as it is pending.

rodant avatar Oct 31 '24 12:10 rodant