Skip to content

Commit

Permalink
sdk: add Client::reset and switch-account example
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Nov 1, 2024
1 parent c2dcb6b commit 64fc455
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
* sdk: add `Client::sync` and `Client::sync_with` methods ([Yuki Kishimoto])
* sdk: add gossip support to `Client::sync` ([Yuki Kishimoto])
* sdk: add `Client::force_remove_all_relays` ([Yuki Kishimoto])
* sdk: add `Client::reset` and `switch-account` example ([Yuki Kishimoto])
* signer: add `NostrSigner::gift_wrap` ([Yuki Kishimoto])
* zapper: add `WebLNZapper` struct (moved from `nostr-webln` crate) ([Yuki Kishimoto])
* ffi(nostr): add `tag_kind_to_string` func ([Yuki Kishimoto])
Expand Down
3 changes: 3 additions & 0 deletions crates/nostr-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ required-features = ["all-nips"]
name = "subscriptions"
required-features = ["all-nips"]

[[example]]
name = "switch-account"

[[example]]
name = "tor"
required-features = ["tor"]
Expand Down
59 changes: 59 additions & 0 deletions crates/nostr-sdk/examples/switch-account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::time::Duration;

use nostr_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

// Account 1
let keys1 = Keys::parse("nsec12kcgs78l06p30jz7z7h3n2x2cy99nw2z6zspjdp7qc206887mwvs95lnkx")?;
let client = Client::new(keys1.clone());

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

// Subscribe
let filter = Filter::new()
.author(keys1.public_key)
.kind(Kind::TextNote)
.limit(10);
client.subscribe(vec![filter], None).await?;

// Wait a little
tokio::time::sleep(Duration::from_secs(20)).await;

println!("Switching account...");

// Reset client to change account
client.reset().await?;

// Account 2
let keys2 = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;
client.set_signer(keys2.clone()).await;

client.add_relay("wss://nostr.oxtr.dev").await?;
client.connect().await;

println!("Account switched");

// Subscribe
let filter = Filter::new()
.author(keys2.public_key)
.kind(Kind::TextNote)
.limit(5);
client.subscribe(vec![filter], None).await?;

client
.handle_notifications(|notification| async move {
println!("{notification:?}");
Ok(false)
})
.await?;

Ok(())
}
25 changes: 25 additions & 0 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,31 @@ impl Client {
self.pool.filtering()
}

/// Reset client
///
/// This method reset the client to simplify the switch to another account.
///
/// This method will:
/// * unsubscribe from all subscriptions
/// * disconnect and force remove all relays
/// * unset the signer
/// * unset the zapper
/// * clear the [`RelayFiltering`]
///
/// This method will NOT:
/// * reset [`Options`]
/// * remove the database
/// * clear the gossip graph
pub async fn reset(&self) -> Result<(), Error> {
self.unsubscribe_all().await;
self.force_remove_all_relays().await?;
self.unset_signer().await;
#[cfg(feature = "nip57")]
self.unset_zapper().await;
self.filtering().clear().await;
Ok(())
}

/// Completely shutdown client
#[inline]
pub async fn shutdown(&self) -> Result<(), Error> {
Expand Down

0 comments on commit 64fc455

Please sign in to comment.