iris-chat 0.1.58

Iris Chat command line client and shared encrypted chat core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use super::*;
use fips_core::config::{PeerConfig, RoutingMode, TransportInstances, UdpConfig};
use fips_core::FipsEndpoint;
use hashtree_core::{BlobRoute, MemoryStore, StoreBlobRoute};
use hashtree_fips_transport::{TcpBlobTransport, TcpBlobTransportConfig, TCP_BLOB_CAPABILITY};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
use std::time::Instant;

fn reserve_udp_addr() -> SocketAddr {
    let socket = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).expect("reserve UDP address");
    socket.local_addr().expect("reserved UDP address")
}

async fn endpoint(
    rendezvous: SocketAddrV4,
    udp_addr: SocketAddr,
    scope: &str,
    peers: Vec<PeerConfig>,
) -> Arc<FipsEndpoint> {
    let mut config = fips_core::Config::new();
    config.node.discovery.nostr.enabled = false;
    config.node.discovery.lan.enabled = false;
    config.node.discovery.local.rendezvous_addr = rendezvous;
    config.node.routing.mode = RoutingMode::ReplyLearned;
    config.peers = peers;
    config.transports.udp = TransportInstances::Single(UdpConfig {
        bind_addr: Some(udp_addr.to_string()),
        advertise_on_nostr: Some(false),
        public: Some(false),
        ..UdpConfig::default()
    });
    Arc::new(
        FipsEndpoint::builder()
            .config(config)
            .discovery_scope(scope)
            .local_rendezvous()
            .without_system_tun()
            .bind()
            .await
            .expect("bind test endpoint"),
    )
}

fn wait_until(mut condition: impl FnMut() -> bool) {
    let deadline = Instant::now() + Duration::from_secs(8);
    while !condition() {
        assert!(Instant::now() < deadline, "condition timed out");
        std::thread::sleep(Duration::from_millis(25));
    }
}

async fn seed_file(store: Arc<MemoryStore>, bytes: &[u8]) -> String {
    let tree = HashTree::new(HashTreeConfig::new(store));
    let (cid, _) = tree.put(bytes).await.expect("seed Hashtree file");
    nhash_encode_full(&NHashData {
        hash: cid.hash,
        decrypt_key: cid.key,
    })
    .expect("encode seeded file")
}

async fn download_bytes(nhash: &str, store: Arc<RoutedStore<AttachmentChunkStore>>) -> Vec<u8> {
    // Other AppCore fixtures run concurrently and replace the process-wide active
    // store. Exercise this profile's real routes without borrowing another test's.
    let data = nhash_decode(nhash).expect("decode attachment hash");
    read_hashtree_attachment(
        &Cid {
            hash: data.hash,
            key: data.decrypt_key,
        },
        store,
        MAX_ATTACHMENT_BYTES,
    )
    .await
    .expect("download attachment")
}

#[test]
fn single_device_without_relays_reuses_provider_and_preserves_fallback_and_outbound_link() {
    let provider_runtime = tokio::runtime::Runtime::new().expect("provider runtime");
    let rendezvous = match reserve_udp_addr() {
        SocketAddr::V4(addr) => addr,
        SocketAddr::V6(_) => unreachable!("IPv4 reservation"),
    };
    let sibling_rendezvous = match reserve_udp_addr() {
        SocketAddr::V4(addr) => addr,
        SocketAddr::V6(_) => unreachable!("IPv4 reservation"),
    };
    let sibling_addr = reserve_udp_addr();
    let sibling = provider_runtime.block_on(endpoint(
        sibling_rendezvous,
        sibling_addr,
        "iris-chat-sibling-test",
        Vec::new(),
    ));
    let provider_endpoint = provider_runtime.block_on(endpoint(
        rendezvous,
        reserve_udp_addr(),
        "iris-drive-provider-test",
        Vec::new(),
    ));
    let provider_local = Arc::new(MemoryStore::new());
    let provider_data = b"attachment supplied by the same-host provider".to_vec();
    let provider_nhash =
        provider_runtime.block_on(seed_file(provider_local.clone(), &provider_data));
    let provider = Arc::new(
        provider_runtime
            .block_on(TcpBlobTransport::bind_advertised_with_config(
                provider_endpoint.clone(),
                provider_local,
                TcpBlobTransportConfig::default(),
                100,
            ))
            .expect("bind provider"),
    );
    let fallback = Arc::new(MemoryStore::new());
    let before = b"standalone fallback after provider miss".to_vec();
    let after = b"standalone fallback after provider death".to_vec();
    let before_nhash = provider_runtime.block_on(seed_file(fallback.clone(), &before));
    let after_nhash = provider_runtime.block_on(seed_file(fallback.clone(), &after));
    let fallback: Arc<dyn BlobRoute> = Arc::new(StoreBlobRoute::new(fallback));

    let owner = nostr::Keys::generate();
    let device = nostr::Keys::generate();
    let temp_dir = tempfile::tempdir().expect("Chat data directory");
    let (update_tx, _update_rx) = flume::unbounded();
    let mut core = AppCore::new(
        update_tx,
        flume::unbounded().0,
        temp_dir.path().to_string_lossy().to_string(),
        Arc::new(RwLock::new(AppState::empty())),
    );
    core.logged_in = Some(LoggedInState {
        owner_pubkey: owner.public_key(),
        owner_keys: Some(owner),
        device_keys: device.clone(),
        client: nostr_sdk::Client::new(device),
        relay_urls: Vec::new(),
        authorization_state: LocalAuthorizationState::Authorized,
    });
    assert!(
        core.app_keys.is_empty(),
        "test profile has no roster sibling"
    );
    core.reconcile_same_host_hashtree_for_test(
        rendezvous,
        fallback,
        vec![PeerConfig::new(
            sibling.npub(),
            "udp",
            sibling_addr.to_string(),
        )],
    );
    let (consumer, has_device_sync, sibling_count, store) = core
        .same_host_runtime_for_test()
        .expect("shared FIPS runtime and attachment store");
    assert!(!has_device_sync, "device-sync service stays disabled");
    assert_eq!(sibling_count, 0, "no device-sync peer is synthesized");

    wait_until(|| {
        consumer
            .local_instance_advertisements()
            .is_ok_and(|adverts| {
                adverts.iter().any(|advert| {
                    advert.npub == provider_endpoint.npub()
                        && advert.capability(TCP_BLOB_CAPABILITY).is_some()
                })
            })
    });
    wait_until(|| {
        core.runtime.block_on(consumer.peers()).is_ok_and(|peers| {
            peers.iter().any(|peer| {
                peer.npub == sibling.npub()
                    && peer.connected
                    && peer.transport_type.as_deref() == Some("udp")
            })
        })
    });

    assert_eq!(
        core.runtime
            .block_on(download_bytes(&provider_nhash, store.store.clone())),
        provider_data
    );
    assert_eq!(
        core.runtime
            .block_on(download_bytes(&before_nhash, store.store.clone())),
        before
    );
    wait_until(|| {
        provider_endpoint
            .local_instance_advertisements()
            .is_ok_and(|adverts| {
                adverts.iter().any(|advert| {
                    advert.npub == consumer.npub()
                        && advert.capability(TCP_BLOB_CAPABILITY).is_some()
                })
            })
    });
    let consumer_identity = fips_core::PeerIdentity::from_npub(consumer.npub()).unwrap();
    let downloaded = nhash_decode(&provider_nhash).unwrap();
    let cached_ciphertext = provider_runtime
        .block_on(provider.fetch_from_peer(&downloaded.hash, consumer_identity))
        .unwrap()
        .expect("Chat serves downloaded ciphertext");
    assert_eq!(
        hashtree_core::decrypt_chk(&cached_ciphertext, &downloaded.decrypt_key.unwrap()).unwrap(),
        provider_data
    );

    // Exercise the real encrypted upload path, then fetch its ciphertext from Chat.
    let upload_path = temp_dir.path().join("peer-attachment.txt");
    let plaintext = b"private file shared from the Chat attachment cache";
    std::fs::write(&upload_path, plaintext).unwrap();
    let (upload_url, upload_server) = super::upload_tests::serve_one_blossom_upload("200 OK");
    let uploading = Arc::new(
        UploadingBlossomStore::new(nostr::Keys::generate(), Vec::new(), vec![upload_url], None)
            .unwrap()
            .with_local(Some(
                core.attachment_service.as_ref().unwrap().store.clone(),
            )),
    );
    let uploaded = core
        .runtime
        .block_on(upload_file_to_store(&upload_path, uploading))
        .unwrap();
    upload_server.join().unwrap();
    let uploaded = nhash_decode(&uploaded).unwrap();
    let ciphertext = provider_runtime
        .block_on(provider.fetch_from_peer(&uploaded.hash, consumer_identity))
        .unwrap()
        .expect("Chat serves its uploaded chunk");
    assert_ne!(ciphertext, plaintext);
    assert_eq!(
        hashtree_core::decrypt_chk(&ciphertext, &uploaded.decrypt_key.unwrap()).unwrap(),
        plaintext
    );
    drop(provider);
    provider_runtime
        .block_on(provider_endpoint.shutdown())
        .unwrap();
    wait_until(|| {
        consumer
            .local_instance_advertisements()
            .is_ok_and(|adverts| {
                adverts.iter().all(|advert| {
                    advert.npub != provider_endpoint.npub()
                        || advert.capability(TCP_BLOB_CAPABILITY).is_none()
                })
            })
    });

    assert_eq!(
        core.runtime
            .block_on(download_bytes(&after_nhash, store.store.clone())),
        after
    );
    assert!(core
        .runtime
        .block_on(consumer.peers())
        .unwrap()
        .iter()
        .any(|peer| {
            peer.npub == sibling.npub()
                && peer.connected
                && peer.transport_type.as_deref() == Some("udp")
        }));

    drop(store);
    core.stop_device_sync();
    core.runtime.block_on(async {
        tokio::time::sleep(Duration::from_millis(50)).await;
    });
    provider_runtime.block_on(sibling.shutdown()).unwrap();
}

#[test]
fn peer_retrieves_pending_ciphertext_after_store_reopen_without_http_servers() {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let dir = tempfile::tempdir().unwrap();
    let storage = dir.path().join("ciphertext");
    let original = AttachmentChunkStore::open(&storage, local_store::DEFAULT_BUDGET).unwrap();
    let path = dir.path().join("pending.bin");
    let plaintext = b"durable peer attachment while every HTTP server is offline";
    std::fs::write(&path, plaintext).unwrap();
    let session = original.begin(None).unwrap();
    let nhash = runtime
        .block_on(upload_file_to_store(&path, session.clone()))
        .unwrap();
    original
        .finish_batch(&[(session, nhash.clone())], &[])
        .unwrap();
    drop(original);
    let reopened = AttachmentChunkStore::open(&storage, local_store::DEFAULT_BUDGET).unwrap();
    let rendezvous = match reserve_udp_addr() {
        SocketAddr::V4(addr) => addr,
        _ => unreachable!(),
    };
    let provider = runtime.block_on(endpoint(
        rendezvous,
        reserve_udp_addr(),
        "durable-provider",
        Vec::new(),
    ));
    let consumer = runtime.block_on(endpoint(
        rendezvous,
        reserve_udp_addr(),
        "durable-consumer",
        Vec::new(),
    ));
    let serving = runtime
        .block_on(TcpBlobTransport::bind_advertised_with_config(
            provider.clone(),
            reopened,
            TcpBlobTransportConfig::default(),
            100,
        ))
        .unwrap();
    let fetching = runtime
        .block_on(TcpBlobTransport::bind(
            consumer.clone(),
            Arc::new(MemoryStore::new()),
        ))
        .unwrap();
    wait_until(|| {
        consumer
            .local_instance_advertisements()
            .is_ok_and(|adverts| {
                adverts.iter().any(|advert| {
                    advert.npub == provider.npub()
                        && advert.capability(TCP_BLOB_CAPABILITY).is_some()
                })
            })
    });
    let decoded = nhash_decode(&nhash).unwrap();
    let bytes = runtime
        .block_on(fetching.fetch_from_peer(
            &decoded.hash,
            fips_core::PeerIdentity::from_npub(provider.npub()).unwrap(),
        ))
        .unwrap()
        .unwrap();
    assert_ne!(bytes, plaintext);
    assert_eq!(
        hashtree_core::decrypt_chk(&bytes, &decoded.decrypt_key.unwrap()).unwrap(),
        plaintext
    );
    drop(serving);
    drop(fetching);
    runtime.block_on(consumer.shutdown()).unwrap();
    runtime.block_on(provider.shutdown()).unwrap();
}

#[test]
fn software_update_blocks_use_attachment_peers_and_are_reshared() {
    let executor = tokio::runtime::Runtime::new().unwrap();
    executor.block_on(async {
        let SocketAddr::V4(rendezvous) = reserve_udp_addr() else {
            unreachable!()
        };
        let source_endpoint =
            endpoint(rendezvous, reserve_udp_addr(), "update-source", Vec::new()).await;
        let app_endpoint =
            endpoint(rendezvous, reserve_udp_addr(), "update-chat", Vec::new()).await;
        let origin = Arc::new(MemoryStore::new());
        let bytes = b"verified public update block".to_vec();
        let hash = hashtree_core::sha256(&bytes);
        origin.put(hash, bytes.clone()).await.unwrap();
        let source = TcpBlobTransport::bind_advertised_with_config(
            source_endpoint.clone(),
            origin,
            TcpBlobTransportConfig::default(),
            10,
        )
        .await
        .unwrap();
        let directory = tempfile::tempdir().unwrap();
        let primary = AttachmentChunkStore::open(directory.path(), 8 * 1024 * 1024).unwrap();
        let runtime = bind_same_host_attachment_store(
            app_endpoint.clone(),
            Arc::new(StoreBlobRoute::new(Arc::new(MemoryStore::new()))),
            Vec::new(),
            primary.clone(),
        )
        .await
        .unwrap();
        tokio::time::timeout(Duration::from_secs(8), async {
            while !runtime
                .provider
                .discovered_provider_ids()
                .unwrap()
                .iter()
                .any(|id| id == source_endpoint.npub())
            {
                tokio::time::sleep(Duration::from_millis(25)).await;
            }
        })
        .await
        .unwrap();
        let updates = hashtree_updater::build_update_store(
            runtime.update_store(),
            None,
            hashtree_updater::SecurePubsubBlossomConfig::default(),
        )
        .unwrap();
        assert_eq!(updates.get(&hash).await.unwrap(), Some(bytes.clone()));
        assert_eq!(primary.get(&hash).await.unwrap(), Some(bytes.clone()));
        source.shutdown().await.unwrap();
        source_endpoint.shutdown().await.unwrap();
        let receiver_endpoint = endpoint(
            rendezvous,
            reserve_udp_addr(),
            "update-third-peer",
            Vec::new(),
        )
        .await;
        let receiver = TcpBlobTransport::in_memory(receiver_endpoint.clone())
            .await
            .unwrap();
        let peer = fips_core::PeerIdentity::from_npub(app_endpoint.npub()).unwrap();
        assert_eq!(receiver.get(&hash, peer).await.unwrap(), Some(bytes));
        receiver.shutdown().await.unwrap();
        receiver_endpoint.shutdown().await.unwrap();
        drop(updates);
        drop(runtime);
        app_endpoint.shutdown().await.unwrap();
    });
}