vector-core 0.13.0

Core library for Vector: the single source of truth for all Vector clients, SDKs and interfaces.
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
429
430
431
432
433
434
//! Deleting what the local user sent to a Community, from its relays.
//!
//! Every wrap on a plane is signed by the plane's shared key, so a deletion by that key could
//! come from any member: CORD-01 §Deletions has relays refuse it and honour the wrap's
//! `p`-tagged owner instead. Vector derives that `p` key from the author's key and the message
//! ([`derive::wrap_owner_key`]), so every device of the author can delete any of their
//! messages. A remote-signer account can't derive it, so its sending device keeps a random one.
//! Owners are kept in `community_wrap_owners`, plane keys in `community_message_keys`.
//!
//! A delete sends the owner's deletion and, best-effort, the plane key's: a relay that only
//! honours authors drops the wrap on that one, and a relay following the spec ignores it. A
//! message sent before owners were kept has only its plane key, so those relays keep its wrap
//! and the in-plane delete alone hides it.

use nostr_sdk::prelude::*;
use crate::event_ext::FinalizeOnClock;

use super::transport::Transport;
use super::v2::{chat::ChatEvent, derive, stream};

/// How long the in-plane delete of a message its owner deleted from relays stays on them.
const OWNED_DELETE_EXPIRY_SECS: u64 = crate::deletion::COOPERATIVE_HIDE_EXPIRY_SECS;

/// The `p` key for my wrap of `rumor_id` at `address`: derived when this device holds the
/// identity's key, otherwise a fresh one only this device will hold.
pub(crate) fn own_wrap_owner(author: &PublicKey, address: &PublicKey, rumor_id: &EventId) -> Keys {
    derived_owner(author, address, rumor_id).unwrap_or_else(Keys::generate)
}

fn derived_owner(author: &PublicKey, address: &PublicKey, rumor_id: &EventId) -> Option<Keys> {
    crate::state::MY_SECRET_KEY
        .with_keys(|keys| {
            let secret: &[u8; 32] = keys.secret_key().as_secret_bytes().try_into().ok()?;
            (keys.public_key() == *author).then(|| derive::wrap_owner_key(secret, address, rumor_id.as_bytes()))
        })
        .flatten()
}

/// Keep what deletes my own `wrap`, met on a relay and sent from this device or another: its
/// owner when it is the key this identity derives, and `plane`, the key that signed it, when
/// the caller holds it. Fills only a confirmed gap. Returns whether its owner is newly kept.
pub(crate) fn heal_own_wrap(event: &ChatEvent, wrap: &Event, plane: Option<&Keys>, relays: &[String]) -> bool {
    if !matches!(event, ChatEvent::Message { .. } | ChatEvent::Reaction { .. } | ChatEvent::Edit { .. }) {
        return false;
    }
    let opened = event.opened();
    if crate::state::my_public_key() != Some(opened.author) {
        return false;
    }
    let (rumor_hex, wrap_hex) = (opened.rumor_id.to_hex(), wrap.id.to_hex());
    if let Some(plane) = plane {
        if matches!(crate::db::community::has_message_key(&rumor_hex), Ok(false)) {
            let _ = crate::db::community::store_message_key(&rumor_hex, &wrap_hex, plane, relays);
        }
    }
    if !matches!(crate::db::community::has_wrap_owner(&rumor_hex), Ok(false)) {
        return false;
    }
    let Some(owner) = derived_owner(&opened.author, &wrap.pubkey, &opened.rumor_id) else {
        return false;
    };
    stream::wrap_owner(wrap) == Some(owner.public_key())
        && crate::db::community::store_wrap_owner(&rumor_hex, &wrap_hex, &owner, relays).is_ok()
}

/// What deleting one of my own events from relays reached.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Scrubbed {
    /// Its owner's deletion went out, which every relay following CORD-01 honours.
    pub by_owner: bool,
    /// Its plane key's deletion was sent: a relay judging by author honours it, and one following
    /// CORD-01 refuses it, which is why its answer counts for nothing.
    pub by_plane: bool,
}

impl Scrubbed {
    /// Whether every relay that honours a deletion at all was asked in the way it honours.
    pub fn everywhere(&self) -> bool {
        self.by_owner && self.by_plane
    }
}

/// Delete my own `message_id` from relays, with its stored edits (they carry its text), by
/// every key kept for their wraps: one deletion per wrap, so none ties wraps together for a
/// relay. `relays` is where its Community lives now: a relay move copies wraps over. An owner
/// key is dropped once its deletion went out, a plane key once its deletion was sent.
pub async fn scrub_own<T: Transport + ?Sized>(transport: &T, relays: &[String], message_id: &str) -> Result<Scrubbed, String> {
    crate::db::scoped(async move {
        if !crate::db::current_session().is_live() {
            return Err("account changed; aborting delete".to_string());
        }
        let mut targets = vec![message_id.to_string()];
        targets.extend(crate::db::events::edits_of(message_id).unwrap_or_default());

        let mut scrubbed = Scrubbed::default();
        for target in &targets {
            let owners = crate::db::community::wrap_owners(target)?;
            let by_owner = owners.iter().map(|k| deletion_by(k, relays)).collect::<Result<Vec<_>, _>>()?;
            let by_plane = crate::db::community::get_message_key(target)?.map(|k| deletion_by(&k, relays)).transpose()?;
            // The owner's deletion must land. The plane key's is a single best-effort send: a relay
            // following CORD-01 refuses it, and a durable publish would wait out that refusal.
            let (owner_sent, plane_sent) = futures_util::future::join(
                futures_util::future::join_all(by_owner.iter().map(|(d, to)| transport.publish_durable(d, to))),
                async {
                    match &by_plane {
                        Some((d, to)) => Some(transport.publish(d, to).await),
                        None => None,
                    }
                },
            )
            .await;
            if let Some(e) = owner_sent.iter().find_map(|r| r.as_ref().err()) {
                crate::log_warn!("[Community] an owner's deletion of {target} failed: {e}");
            }
            if !owners.is_empty() && owner_sent.iter().all(Result::is_ok) {
                crate::db::community::forget_wrap_owners(target)?;
                scrubbed.by_owner |= target == message_id;
            }
            if let Some(sent) = plane_sent {
                if let Err(e) = sent {
                    crate::log_debug!("[Community] the plane key's deletion of {target} was not taken: {e}");
                }
                crate::db::community::delete_message_key(target)?;
                scrubbed.by_plane |= target == message_id;
            }
        }
        Ok(scrubbed)
    })
    .await
}

/// One wrap a second: a deleted Channel's history leaves the relays without flooding them, or
/// this device, however much of it there was.
const SCRUB_PACE: std::time::Duration = std::time::Duration::from_secs(1);

struct ScrubbingWraps;

fn scrubbing() -> std::sync::Arc<std::sync::atomic::AtomicBool> {
    crate::db::current_session().scoped::<ScrubbingWraps, _>()
}

/// Queue my own `message_ids` in `community_id` to leave the relays, one at a time
/// ([`SCRUB_PACE`]), resuming across restarts.
pub(crate) fn queue_scrubs(community_id: &str, message_ids: &[String]) {
    if message_ids.is_empty() {
        return;
    }
    if let Err(e) = crate::db::community::queue_wrap_scrubs(community_id, message_ids) {
        crate::log_warn!("[Community] queueing {} wrap deletion(s) failed: {e}", message_ids.len());
        return;
    }
    resume_scrubs();
}

/// Work through the queued scrubs until none is left. One that can't go out stays queued, at
/// the back, and the next resume tries again.
pub(crate) fn resume_scrubs() {
    if crate::state::nostr_client().is_none() || !matches!(crate::db::community::next_wrap_scrub(), Ok(Some(_))) {
        return;
    }
    let running = scrubbing();
    if running.swap(true, std::sync::atomic::Ordering::AcqRel) {
        return;
    }
    crate::db::spawn_bound(async move {
        struct Release(std::sync::Arc<std::sync::atomic::AtomicBool>);
        impl Drop for Release {
            fn drop(&mut self) {
                self.0.store(false, std::sync::atomic::Ordering::Release);
            }
        }
        let _release = Release(running);
        let transport = super::transport::LiveTransport::with_timeout(std::time::Duration::from_secs(20));
        drain_scrubs(&transport, SCRUB_PACE).await;
    });
}

/// Scrub the queue in order, `pace` apart, until it is empty or one can't go out. Returns how
/// many left the queue.
pub(crate) async fn drain_scrubs<T: Transport + ?Sized>(transport: &T, pace: std::time::Duration) -> usize {
    let mut done = 0;
    while let Ok(Some((message_id, community_id))) = crate::db::community::next_wrap_scrub() {
        let relays = crate::community::id32_from_hex(&community_id)
            .and_then(|id| crate::db::community::load_community_v2(&crate::community::CommunityId(id)).ok().flatten())
            .map(|c| c.relays)
            .unwrap_or_default();
        let gone = scrub_own(transport, &relays, &message_id).await.is_ok() && matches!(crate::db::community::has_wrap_owner(&message_id), Ok(false));
        if crate::db::community::settle_wrap_scrub(&message_id, !gone).is_err() || !gone {
            break;
        }
        done += 1;
        if !pace.is_zero() {
            crate::rt::time::sleep(pace).await;
        }
    }
    done
}

/// When the in-plane delete of `message_id` may leave relays, given what its scrub reached:
/// once the wrap was deleted [`Scrubbed::everywhere`], 30 days on, or with the message when
/// that comes sooner. Otherwise never, since it alone hides a wrap a relay still holds.
pub async fn in_plane_delete_expiry(message_id: &str, scrubbed: Scrubbed) -> Option<u64> {
    if !scrubbed.everywhere() {
        return None;
    }
    let until = crate::clock::timestamp().as_secs().saturating_add(OWNED_DELETE_EXPIRY_SECS);
    let message = crate::self_destruct::edit_expiry(message_id).await.ok().flatten();
    Some(message.map_or(until, |m| m.min(until)))
}

/// The deletion of one wrap by the key kept for it, and the relays it goes to.
fn deletion_by((keys, outer, sent_to): &(Keys, String, Vec<String>), relays: &[String]) -> Result<(Event, Vec<String>), String> {
    let wrap = EventId::from_hex(outer).map_err(|e| e.to_string())?;
    let deletion = crate::deletion::wrap_deletion(&[wrap]).finalize_on_clock(keys).map_err(|e| e.to_string())?;
    Ok((deletion, union(relays, sent_to)))
}

fn union(a: &[String], b: &[String]) -> Vec<String> {
    let mut out = a.to_vec();
    for r in b {
        if !out.contains(r) {
            out.push(r.clone());
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::community::transport::memory::MemoryRelay;
    use crate::community::transport::{PlaneFetch, Query};

    /// A transport whose publish always fails (fetch returns nothing).
    struct FailingRelay;
    #[async_trait::async_trait]
    impl Transport for FailingRelay {
        async fn fetch_plane(&self, _plane: &Keys, query: &Query, relays: &[String]) -> Result<PlaneFetch, String> {
            Ok(PlaneFetch::whole(self.fetch(query, relays).await?))
        }
        async fn publish(&self, _event: &Event, _relays: &[String]) -> Result<(), String> {
            Err("relay unreachable".to_string())
        }
        async fn publish_durable(&self, _event: &Event, _relays: &[String]) -> Result<(), String> {
            Err("relay unreachable".to_string())
        }
        async fn fetch(&self, _query: &Query, _relays: &[String]) -> Result<Vec<Event>, String> {
            Ok(Vec::new())
        }
    }

    static TEST_COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(7700);

    fn make_test_npub(n: u32) -> String {
        const BECH32: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";
        let mut payload = vec![b'q'; 58];
        let mut x = n as u64;
        let mut i = 58;
        while x > 0 && i > 0 {
            i -= 1;
            payload[i] = BECH32[(x as usize) % 32];
            x /= 32;
        }
        format!("npub1{}", std::str::from_utf8(&payload).unwrap())
    }

    fn init_test_db() -> (tempfile::TempDir, std::sync::MutexGuard<'static, ()>) {
        let guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
        crate::db::close_database();
        crate::db::clear_id_caches();
        let tmp = tempfile::tempdir().unwrap();
        let account = make_test_npub(TEST_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed));
        std::fs::create_dir_all(tmp.path().join(&account)).unwrap();
        crate::db::set_app_data_dir(crate::db::shared_test_data_dir().to_path_buf());
        crate::db::set_current_account(account.clone()).unwrap();
        crate::db::init_database(&account).unwrap();
        (tmp, guard)
    }

    fn relays() -> Vec<String> {
        vec!["r1".into(), "r2".into(), "r3".into()]
    }

    /// A wrap as a plane publishes it: signed by the shared `plane` key, `p`-tagged to `owner`.
    async fn published(relay: &MemoryRelay, plane: &Keys, owner: &Keys) -> Event {
        let wrap = EventBuilder::new(Kind::Custom(1059), "sealed").tag(Tag::public_key(owner.public_key())).finalize(plane).unwrap();
        relay.publish_durable(&wrap, &relays()).await.unwrap();
        wrap
    }

    async fn still_there(relay: &MemoryRelay, id: EventId) -> bool {
        let q = Query { ids: vec![id.to_hex()], ..Default::default() };
        !relay.fetch(&q, &relays()).await.unwrap().is_empty()
    }

    #[tokio::test]
    async fn a_relay_following_cord01_takes_a_wraps_deletion_only_from_its_owner() {
        let relay = MemoryRelay::new();
        relay.follow_cord01(&relays());
        let (plane, owner) = (Keys::generate(), Keys::generate());
        let wrap = published(&relay, &plane, &owner).await;

        let by_plane = crate::deletion::wrap_deletion(&[wrap.id]).finalize(&plane).unwrap();
        relay.publish_durable(&by_plane, &relays()).await.unwrap();
        assert!(still_there(&relay, wrap.id).await, "any member holds the plane key, so it deletes nothing");

        let by_owner = crate::deletion::wrap_deletion(&[wrap.id]).finalize(&owner).unwrap();
        relay.publish_durable(&by_owner, &["r1".to_string()]).await.unwrap();
        assert!(still_there(&relay, wrap.id).await, "a deletion on a subset leaves it on the rest");
        relay.publish_durable(&by_owner, &relays()).await.unwrap();
        assert!(!still_there(&relay, wrap.id).await);
    }

    #[tokio::test]
    async fn a_scrub_deletes_by_owner_and_plane_and_spends_both_keys() {
        let (_tmp, _guard) = init_test_db();
        let (cord01, stock) = (MemoryRelay::new(), MemoryRelay::new());
        cord01.follow_cord01(&relays());
        let (plane, owner) = (Keys::generate(), Keys::generate());
        let message_id = "00".repeat(32);
        for relay in [&cord01, &stock] {
            let wrap = published(relay, &plane, &owner).await;
            crate::db::community::store_message_key(&message_id, &wrap.id.to_hex(), &plane, &relays()).unwrap();
            crate::db::community::store_wrap_owner(&message_id, &wrap.id.to_hex(), &owner, &relays()).unwrap();

            assert_eq!(scrub_own(relay, &relays(), &message_id).await.unwrap(), Scrubbed { by_owner: true, by_plane: true });
            assert!(!still_there(relay, wrap.id).await, "the owner deletes it where CORD-01 holds, the plane key elsewhere");
            assert!(!crate::db::community::has_wrap_owner(&message_id).unwrap());
            assert!(!crate::db::community::has_message_key(&message_id).unwrap());
        }
    }

    /// Records which publish each event went through, accepting everything.
    #[derive(Default)]
    struct Recording {
        durable: std::sync::Mutex<Vec<PublicKey>>,
        once: std::sync::Mutex<Vec<PublicKey>>,
    }
    #[async_trait::async_trait]
    impl Transport for Recording {
        async fn fetch_plane(&self, _plane: &Keys, _query: &Query, _relays: &[String]) -> Result<PlaneFetch, String> {
            Ok(PlaneFetch::whole(Vec::new()))
        }
        async fn publish(&self, event: &Event, _relays: &[String]) -> Result<(), String> {
            self.once.lock().unwrap().push(event.pubkey);
            Ok(())
        }
        async fn publish_durable(&self, event: &Event, _relays: &[String]) -> Result<(), String> {
            self.durable.lock().unwrap().push(event.pubkey);
            Ok(())
        }
        async fn fetch(&self, _query: &Query, _relays: &[String]) -> Result<Vec<Event>, String> {
            Ok(Vec::new())
        }
    }

    #[tokio::test]
    async fn only_the_owners_deletion_waits_to_land() {
        let (_tmp, _guard) = init_test_db();
        let (plane, owner) = (Keys::generate(), Keys::generate());
        let message_id = "66".repeat(32);
        crate::db::community::store_message_key(&message_id, &"77".repeat(32), &plane, &relays()).unwrap();
        crate::db::community::store_wrap_owner(&message_id, &"77".repeat(32), &owner, &relays()).unwrap();
        let relay = Recording::default();
        scrub_own(&relay, &relays(), &message_id).await.unwrap();
        assert_eq!(*relay.durable.lock().unwrap(), vec![owner.public_key()]);
        assert_eq!(*relay.once.lock().unwrap(), vec![plane.public_key()], "a CORD-01 relay refuses it, so nothing waits on it");
    }

    #[tokio::test]
    async fn an_owner_deletion_no_relay_took_keeps_its_key_and_expires_nothing() {
        let (_tmp, _guard) = init_test_db();
        let message_id = "44".repeat(32);
        crate::db::community::store_message_key(&message_id, &"55".repeat(32), &Keys::generate(), &relays()).unwrap();
        crate::db::community::store_wrap_owner(&message_id, &"55".repeat(32), &Keys::generate(), &relays()).unwrap();

        let scrubbed = scrub_own(&FailingRelay, &relays(), &message_id).await.unwrap();
        assert_eq!(scrubbed, Scrubbed { by_owner: false, by_plane: true }, "a refused plane deletion still counts as sent");
        assert!(crate::db::community::has_wrap_owner(&message_id).unwrap(), "the owner key stays for another try");
        assert!(!crate::db::community::has_message_key(&message_id).unwrap());
        assert_eq!(in_plane_delete_expiry(&message_id, scrubbed).await, None);
    }

    #[tokio::test]
    async fn a_message_sent_before_owners_were_kept_stays_on_a_cord01_relay() {
        let (_tmp, _guard) = init_test_db();
        let relay = MemoryRelay::new();
        relay.follow_cord01(&relays());
        let plane = Keys::generate();
        let wrap = published(&relay, &plane, &Keys::generate()).await;
        let message_id = "11".repeat(32);
        crate::db::community::store_message_key(&message_id, &wrap.id.to_hex(), &plane, &relays()).unwrap();

        let scrubbed = scrub_own(&relay, &relays(), &message_id).await.unwrap();
        assert_eq!(scrubbed, Scrubbed { by_owner: false, by_plane: true });
        assert!(still_there(&relay, wrap.id).await);
        assert_eq!(in_plane_delete_expiry(&message_id, scrubbed).await, None, "its in-plane delete never leaves");
        let owner_only = Scrubbed { by_owner: true, by_plane: false };
        assert_eq!(in_plane_delete_expiry(&message_id, owner_only).await, None, "nor while a relay judging by author holds it");
    }

    #[tokio::test]
    async fn a_scrub_reaches_the_communitys_relays_beyond_those_it_was_sent_to() {
        let (_tmp, _guard) = init_test_db();
        let relay = MemoryRelay::new();
        let moved = vec!["r9".to_string()];
        relay.follow_cord01(&relays());
        relay.follow_cord01(&moved);
        let (plane, owner) = (Keys::generate(), Keys::generate());
        let wrap = published(&relay, &plane, &owner).await;
        relay.inject(&wrap, &moved);
        let message_id = "22".repeat(32);
        crate::db::community::store_wrap_owner(&message_id, &wrap.id.to_hex(), &owner, &relays()).unwrap();

        scrub_own(&relay, &moved, &message_id).await.unwrap();
        let q = Query { ids: vec![wrap.id.to_hex()], ..Default::default() };
        assert!(relay.fetch(&q, &moved).await.unwrap().is_empty(), "the copy a relay move made goes too");
        assert!(!still_there(&relay, wrap.id).await);
    }

    #[tokio::test]
    async fn an_owned_delete_leaves_relays_after_30_days() {
        let (_tmp, _guard) = init_test_db();
        let now = Timestamp::now().as_secs();
        let until = in_plane_delete_expiry(&"33".repeat(32), Scrubbed { by_owner: true, by_plane: true }).await.unwrap();
        assert!((now + OWNED_DELETE_EXPIRY_SECS..=now + OWNED_DELETE_EXPIRY_SECS + 5).contains(&until));
    }

    #[tokio::test]
    async fn scrubbing_a_message_we_hold_no_keys_for_reaches_nothing() {
        let (_tmp, _guard) = init_test_db();
        assert_eq!(scrub_own(&MemoryRelay::new(), &relays(), &"00".repeat(32)).await.unwrap(), Scrubbed::default());
    }
}