Skip to main content

vector_core/transfer/
mod.rs

1//! Device transfer: move an account to another device by scanning a QR or typing a short code.
2//! Both devices meet on relays, prove the code to each other with a PAKE, and the key moves only
3//! after the user approves on the device that holds it.
4
5pub mod avatar;
6pub mod code;
7pub mod crypto;
8pub mod service;
9pub mod session;
10pub mod wire;
11
12#[cfg(test)]
13mod relay_probe {
14    use futures_util::StreamExt;
15    use crate::ClientRelayExt;
16    use nostr_sdk::prelude::*;
17    use std::time::Duration;
18
19    /// Live check that each trusted relay carries the transfer kind between two fresh keys, and
20    /// whether it replays it to a later subscriber. `cargo test -p vector-core relay_probe -- --ignored --nocapture`
21    #[tokio::test(flavor = "multi_thread")]
22    #[ignore]
23    async fn every_trusted_relay_carries_an_ephemeral_transfer_event() {
24        for url in crate::state::TRUSTED_RELAYS {
25            let room = Keys::generate().public_key();
26            let filter = Filter::new().kind(Kind::Custom(24681)).pubkey(room).since(crate::clock::timestamp() - 60);
27
28            let listener = Client::builder().build();
29            listener.add_managed_relay(*url).await.unwrap();
30            listener.connect().and_wait(Duration::from_secs(10)).await;
31            let mut notes = listener.notifications();
32            listener.subscribe(filter.clone()).await.unwrap();
33
34            let sender_keys = Keys::generate();
35            let sender = Client::builder().build();
36            sender.add_managed_relay(*url).await.unwrap();
37            sender.connect().and_wait(Duration::from_secs(10)).await;
38            tokio::time::sleep(Duration::from_millis(800)).await;
39            // As large as the biggest real message (a sender's hello with its avatar), with room to spare.
40            let event = EventBuilder::new(Kind::Custom(24681), "x".repeat(32 * 1024))
41                .tag(Tag::public_key(room))
42                .finalize(&sender_keys)
43                .unwrap();
44            let sent = sender.send_event(&event).await;
45
46            let got = tokio::time::timeout(Duration::from_secs(10), async {
47                while let Some(n) = notes.next().await {
48                    if let ClientNotification::Event { event: e, .. } = n {
49                        if e.id == event.id { return true; }
50                    }
51                }
52                false
53            }).await.unwrap_or(false);
54
55            let late = Client::builder().build();
56            late.add_managed_relay(*url).await.unwrap();
57            late.connect().and_wait(Duration::from_secs(10)).await;
58            let replayed = late.fetch_events(filter).timeout(Duration::from_secs(6)).await
59                .map(|evs| evs.iter().any(|e| e.id == event.id)).unwrap_or(false);
60
61            println!("{url}: publish={:?} live={got} replayed_to_late={replayed}",
62                sent.as_ref().map(|o| (o.success.len(), o.failed.values().cloned().collect::<Vec<_>>())).map_err(|e| e.to_string()));
63            for c in [listener, sender, late] { c.shutdown().await; }
64        }
65    }
66}