rust-nostr / nostr

Nostr protocol implementation, SDK and FFI
https://rust-nostr.org/
MIT License
406 stars 89 forks source link

Automatically re-send event after NIP-42 authentication #509

Open reyamir opened 1 month ago

reyamir commented 1 month ago

Describe the bug
Construct a client which also set automatic_authentication to true (default). It will not automatically auth.

To Reproduce
Repo: https://github.com/reyamir/rust-nostr-debug.git

let keys: Keys = Keys::generate();
let signer = NostrSigner::Keys(keys);

// Config
let opts = Options::new().automatic_authentication(true);

// Setup nostr client
let client = ClientBuilder::default().signer(signer).opts(opts).build();

client.add_relay("wss://relay.damus.io").await?;
client.connect().await;

// Add nip-17 relay
client.add_relay("wss://auth.nostr1.com").await?;
client.connect_relay("wss://auth.nostr1.com").await?;

// Send private message
let receiver =
    PublicKey::from_bech32("npub1zfss807aer0j26mwp2la0ume0jqde3823rmu97ra6sgyyg956e0s6xw445")
        .unwrap();
let message = "Hello world";

match client
    .send_private_msg_to(["wss://auth.nostr1.com"], receiver, message, None)
    .await
{
    Ok(output) => println!("Ok: {:?}", output),
    Err(e) => println!("Err: {}", e),
}

Ok(())

The result is Err: event not published: auth-required: you must auth

Expected behavior
Automatically auth when add new relay.

Build environment

yukibtc commented 1 month ago

As temporary workaround: subscribe with autoclosing option using a filter that get nothing, like Filter::new().kind(Kind::TextNote).limit(0) (or use get_events_of with that filter).

The issue shouldn't be the authentication but the fact that AUTH message is sent after you send the first event (or REQ). So, after the authentication, the event should be re-sent (same issue of few days ago with REQ).

I'll try to search a way to automatically re-send the event if the error is related to authentication.

reyamir commented 1 month ago

Yo thank you, your workaround help me resolve the weird bug in my chat app 😅