Advertisement
Guest User

Nostr interfacr

a guest
Dec 29th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.05 KB | None | 0 0
  1. // [package]
  2. // name = "nostr_interfacr"
  3. // version = "0.1.0"
  4. // edition = "2021"
  5. //
  6. // # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  7. //
  8. // [dependencies]
  9. // nostr-sdk = "0.9"
  10. // serde = "1.0.151"
  11. // serde_json = "1.0.91"
  12. // tokio = { version = "1", features = ["full"] }
  13.  
  14. use nostr_sdk::nostr::util::time::timestamp;
  15. use nostr_sdk::nostr::{SubscriptionFilter, Kind, KindBase};
  16. use nostr_sdk::{Client, Result, RelayPoolNotifications};
  17.  
  18. use tokio::task;
  19.  
  20. #[tokio::main]
  21. async fn main() -> Result<()> {
  22.     let my_keys = Client::generate_keys();
  23.     let my_keys_cpy = my_keys.clone();
  24.  
  25.     let for_loc = task::spawn(async move {
  26.  
  27.         let foreign_client = Client::new(&my_keys);
  28.         let foreign_relays: Vec<&str> = vec!["wss://relay.damus.io", "wss://relay.nostr.ch"];
  29.  
  30.         // Add relays
  31.         for foreign_relay in foreign_relays {
  32.             foreign_client.add_relay(foreign_relay, None).await.unwrap();
  33.         }
  34.  
  35.         foreign_client.connect().await.unwrap();
  36.  
  37.         let subscription = SubscriptionFilter::new().since(timestamp());
  38.         foreign_client.subscribe(vec![subscription]).await.unwrap();
  39.  
  40.  
  41.         let local_client = Client::new(&my_keys);
  42.         local_client.add_relay("ws://localhost:8080", None).await.unwrap();
  43.         local_client.connect().await.unwrap();
  44.         // Display public key
  45.         println!("Foreign->Local: Ready !");
  46.         println!("\tPublic key: {}", my_keys.public_key_as_str());
  47.         loop {
  48.             let mut notifications = foreign_client.notifications();
  49.             while let Ok(notification) = notifications.recv().await {
  50.                 if let RelayPoolNotifications::ReceivedEvent(event) = notification {
  51.                     if event.kind == Kind::Base(KindBase::TextNote) {
  52.                         match event.verify() {
  53.                             Ok(_) => {
  54.                                 local_client.send_event(event).await.unwrap();
  55.                             }
  56.                             Err(e) => {
  57.                                 println!("Error verifying event: {}", e);
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     });
  65.  
  66.     let _loc_for = task::spawn(async move {
  67.  
  68.         let foreign_client = Client::new(&my_keys_cpy);
  69.         let foreign_relays: Vec<&str> = vec!["wss://relay.damus.io", "wss://relay.nostr.ch"];
  70.  
  71.         // Add relays
  72.         for foreign_relay in foreign_relays {
  73.             foreign_client.add_relay(foreign_relay, None).await.unwrap();
  74.         }
  75.  
  76.         foreign_client.connect().await.unwrap();
  77.  
  78.  
  79.         let local_client = Client::new(&my_keys_cpy);
  80.         local_client.add_relay("ws://localhost:8080", None).await.unwrap();
  81.         local_client.connect().await.unwrap();
  82.  
  83.         let subscription = SubscriptionFilter::new().since(timestamp());
  84.         local_client.subscribe(vec![subscription]).await.unwrap();
  85.         // Display public key
  86.         println!("Local->Foreign: Ready !");
  87.         println!("\tPublic key: {}", my_keys_cpy.public_key_as_str());
  88.         loop {
  89.             let mut notifications = local_client.notifications();
  90.             while let Ok(notification) = notifications.recv().await {
  91.                 if let RelayPoolNotifications::ReceivedEvent(event) = notification {
  92.                     if event.kind == Kind::Base(KindBase::TextNote) {
  93.                         println!("Received event: {:?}", event);
  94.                         match event.verify() {
  95.                             Ok(_) => {
  96.                                 println!("Received verified event: {:?}", event);
  97.                                 foreign_client.send_event(event).await.unwrap();
  98.                             }
  99.                             Err(e) => {
  100.                                 println!("Error verifying event: {}", e);
  101.                             }
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     });
  108.     for_loc.await.unwrap();
  109.     Ok(())
  110. }
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement