Skip to main content

vector_push/
lib.rs

1//! Sealed push: background notifications nobody in the middle can read or attribute.
2//!
3//! A device that wants notifications hands each contact a [`Ticket`]. When that contact
4//! sends it a message, the contact's client writes the notification itself, encrypts it to
5//! the device's Web Push keys ([`webpush`]), and asks a pusher to deliver it ([`request`]).
6//! The pusher opens only the part of the ticket sealed to it, the device's push address,
7//! and never sees who wrote, who receives, or what was said.
8//!
9//! - [`webpush`]: RFC 8291 message encryption, done by the sender.
10//! - `vapid`: RFC 8292 keys; the device owns its own, the pusher signs with it.
11//! - [`ticket`]: what a contact holds, and the capability only the pusher can open.
12//! - [`request`]: the Nostr event a sender publishes for the pusher.
13//! - [`payload`]: the notification itself, authenticated per contact.
14
15pub mod payload;
16pub mod request;
17pub mod ticket;
18#[cfg(feature = "vapid")]
19pub mod vapid;
20pub mod webpush;
21
22pub use payload::Notice;
23pub use request::{Control, Hold, Incoming, Request, HOLD_FOREVER};
24pub use ticket::{Capability, Ticket};
25
26/// The pusher Vector runs. A ticket names its own, so a device may pick another.
27pub const DEFAULT_PUSHER: &str = "cdfdb82459cb5f6a328fa8df5150e0f8482efe12f6a571f801b59ecd53ecbb9c";
28/// Where the default pusher listens. Two operators: whichever a sender already holds a
29/// connection to carries the request, and either one alone is enough.
30pub const DEFAULT_PUSHER_RELAYS: &[&str] = &["wss://jskitty.com/nostr", "wss://asia.vectorapp.io/nostr"];
31
32/// `d` tag of the rumor that carries a device's tickets to a contact.
33pub const TICKET_RUMOR_D: &str = "vector-push";
34
35#[derive(Debug)]
36pub enum Error {
37    Key(&'static str),
38    Crypto(&'static str),
39    Format(String),
40}
41
42impl std::fmt::Display for Error {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            Error::Key(m) | Error::Crypto(m) => f.write_str(m),
46            Error::Format(m) => f.write_str(m),
47        }
48    }
49}
50
51impl std::error::Error for Error {}
52
53pub type Result<T> = std::result::Result<T, Error>;
54
55pub(crate) fn b64url(bytes: &[u8]) -> String {
56    base64::Engine::encode(&base64::engine::general_purpose::URL_SAFE_NO_PAD, bytes)
57}
58
59/// Lenient on padding: browsers hand out keys with and without it.
60pub(crate) fn unb64url(s: &str) -> Result<Vec<u8>> {
61    base64::Engine::decode(&base64::engine::general_purpose::URL_SAFE_NO_PAD, s.trim_end_matches('='))
62        .map_err(|_| Error::Format("bad base64url".into()))
63}
64
65pub(crate) fn hex(bytes: &[u8]) -> String {
66    bytes.iter().map(|b| format!("{b:02x}")).collect()
67}
68
69pub(crate) fn unhex(s: &str) -> Result<Vec<u8>> {
70    if s.len() % 2 != 0 {
71        return Err(Error::Format("odd hex".into()));
72    }
73    (0..s.len())
74        .step_by(2)
75        .map(|i| u8::from_str_radix(&s[i..i + 2], 16).map_err(|_| Error::Format("bad hex".into())))
76        .collect()
77}
78
79pub(crate) fn random<const N: usize>() -> [u8; N] {
80    use rand::RngCore;
81    let mut out = [0u8; N];
82    rand::rngs::OsRng.fill_bytes(&mut out);
83    out
84}