Advertisement
Brord

psk error rust version

Aug 10th, 2021
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.55 KB | None | 0 0
  1. use std::env;
  2.  
  3. use rand::Rng;
  4.  
  5. use iota_streams::{
  6.     core::{println, psk::Psk, Result},
  7.     app::{
  8.         transport::{
  9.             tangle::client::{
  10.                 Client,
  11.             },
  12.         },
  13.     },
  14.     app_channels::api::{
  15.         pskid_from_psk,
  16.         ChannelType,
  17.         tangle::{
  18.             Author, Bytes, Subscriber, MessageContent, UnwrappedMessage
  19.         },
  20.     },
  21. };
  22.  
  23. #[tokio::main]
  24. async fn main() -> Result<()> {
  25.     let node_url = env::var("URL").unwrap_or_else(|_| "https://api.lb-0.testnet.chrysalis2.com:443".to_string());
  26.  
  27.     let mut client = Client::new_from_url(&node_url);
  28.  
  29.     let alph9 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9";
  30.     // Generate a unique seed for the author
  31.     let seed: &str = &(0..10)
  32.         .map(|_| alph9.chars().nth(rand::thread_rng().gen_range(0, 27)).unwrap())
  33.         .collect::<String>();
  34.  
  35.    
  36.     // generate presharedkey
  37.     let presharedkey = rand::thread_rng().gen::<[u8; 32]>();
  38.  
  39.     let mut author = Author::new(seed, ChannelType::MultiBranch, client.clone());
  40.  
  41.     println!("\nAnnounce Channel");
  42.     // Create the channel with an announcement message. Make sure to save the resulting link somewhere,
  43.     let announcement_link = author.send_announce()?;
  44.     println!("Announcement Link: {}", &announcement_link);
  45.  
  46.     let psk = Psk::clone_from_slice(&presharedkey);
  47.     let pskid = pskid_from_psk(&psk);
  48.     author.store_psk(pskid.clone(), psk.clone());
  49.  
  50.     // author sends keyload using preshared key
  51.     let (_keyload_link, keyload_seq_link) = author.send_keyload(&announcement_link,
  52.         &[pskid.clone()],
  53.         &vec![])?;
  54.     println!("Sent Keyload for Sub A: {}, seq: {:?}", _keyload_link, &keyload_seq_link);
  55.  
  56.     // author sends package
  57.     let (signed_msg_link, signed_seq_link) = author.send_signed_packet(&announcement_link,
  58.         &Bytes("".as_bytes().to_vec()),
  59.         &Bytes("message from the grand author".as_bytes().to_vec()))?;
  60.    
  61.     println!("Sent msg from Sub author: {}, seq: {:?}", signed_msg_link, signed_seq_link);
  62.  
  63.     // In their own separate instances generate the subscriber(s) that will be attaching to the channel
  64.     let mut subscriber_a = Subscriber::new("SubscriberA", client.clone());
  65.     // Receive the announcement message to start listening to the channel
  66.     subscriber_a.receive_announcement(&announcement_link)?;
  67.     // subscriber stores psk
  68.     subscriber_a.store_psk(pskid.clone(), psk.clone());
  69.  
  70.     // A new subscriber SubscriberB is added and sends a signed_packet to his branch
  71.     let mut subscriber_b = Subscriber::new("SubscriberB", client.clone());
  72.     subscriber_b.receive_announcement(&announcement_link)?;
  73.  
  74.     let subscribe_msg_b = subscriber_b.send_subscribe(&announcement_link)?;
  75.     let sub_b_pk = subscriber_b.get_pk();
  76.  
  77.     author.receive_subscribe(&subscribe_msg_b)?;
  78.  
  79.     let (_keyload_2_link, keyload_2_seq_link) = author.send_keyload(&announcement_link,
  80.         &[pskid.clone()],
  81.         &vec![sub_b_pk.clone()])?;
  82.     println!("Sent Keyload for Sub B: {}, seq: {:?}", _keyload_2_link.clone(), keyload_2_seq_link);
  83.     subscriber_b.sync_state();
  84.  
  85.     let prev_msg_link = _keyload_2_link;
  86.     let message = "very basic message from b";
  87.  
  88.     let (res_signed_package_b_link, res_signed_package_b_seq_link) =
  89.         subscriber_b.send_signed_packet(&prev_msg_link,
  90.             &Bytes("ssw".as_bytes().to_vec()),
  91.             &Bytes(message.as_bytes().to_vec()))?;
  92.  
  93.     println!("Sent msg from Sub B: {}, seq: {:?}", res_signed_package_b_link, res_signed_package_b_seq_link);
  94.  
  95.     print("Sub A", subscriber_a.fetch_all_next_msgs());
  96.  
  97.     print("Sub B", subscriber_b.fetch_all_next_msgs());
  98.  
  99.     print("Author", author.fetch_all_next_msgs());
  100.    
  101.     Ok(())
  102. }
  103.  
  104. fn print(name: &str, msg: Vec<UnwrappedMessage>) {
  105.     for r in msg {
  106.         let content = &r.body;
  107.         match content {
  108.             MessageContent::SignedPacket {
  109.                 pk: _,
  110.                 public_payload,
  111.                 masked_payload,
  112.             } => {
  113.                 println!("Received message for: {}", name);
  114.                 println!("public: {}", String::from_utf8(public_payload.0.to_vec()).unwrap());
  115.                 println!("masked: {}", String::from_utf8(masked_payload.0.to_vec()).unwrap())
  116.             },
  117.             _ => println!("Received message for: {}, {:?}", name, content),
  118.         };
  119.  
  120.         //let details = client.get_link_details(&r.link)?;
  121.         println!("prev link: {}", &r.prev_link);
  122.         println!("link: {}", &r.link);
  123.         //println!("id: {}\n", details.metadata.message_id);
  124.     };
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement