AT Protocol PDS + AppView + Tauri Desktop Client, 160-char post limit. - PDS (Rust + axum + sqlx) - Auth: createAccount, createSession, refreshSession - Records: createRecord, deleteRecord (race-safe via SELECT FOR UPDATE) - Feed: feed.like.create, feed.repost.create - Sync: getRepo, getBlocks, getLatestCommit, getRecord (with MST proof), listRepos - Identity: resolveHandle - MST: spec-conformant (at-mst crate, 27 tests) - Repo: signed commits, TID counter (monotonic, 4096 wrap safe) - AppView (Rust + axum + sqlx) - Jetstream consumer (WebSocket, exponential backoff, 38k+ events indexed) - REST API: timeline/home (graph-aware), profile, search, post (with thread hydration) - Handle-sync worker (did:plc + did:web) - JSONB embed storage + thread columns (migration 0003) - Like/repost counter cache (migration 0004) - Tauri 2 + Svelte 5 Desktop Client - System tray (Show/Compose/Quit menu) - OS notifications (tauri-plugin-notification) - Auto-update (tauri-plugin-updater, placeholder endpoint) - Window-state (tauri-plugin-window-state) - 160-char compose with live counter - Image/Link embed rendering - LocalStorage-persisted like state - Timeline with poll (prepend new posts) - Custom TitleBar (transparent, no decorations) - Orange/IBM Plex Mono maarcade design Tests: 231 Rust + 9 vitest = 240 passed.
155 lines
4.5 KiB
Rust
155 lines
4.5 KiB
Rust
use anyhow::{anyhow, Result};
|
|
use cid::Cid;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use at_crypto::cid::cid_for_cbor;
|
|
|
|
/// A single MST entry. The `key` is the **base64url-encoded** form of the
|
|
/// user-facing key string. The `tree` is the CID of the sub-tree immediately
|
|
/// to the right of this entry (i.e. the sub-tree that contains all keys
|
|
/// strictly between this entry's key and the next entry's key).
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct MstEntry {
|
|
pub key: String,
|
|
pub value: Cid,
|
|
#[serde(rename = "t", skip_serializing_if = "Option::is_none")]
|
|
pub tree: Option<Cid>,
|
|
}
|
|
|
|
impl MstEntry {
|
|
pub fn new(encoded_key: impl Into<String>, value: Cid, tree: Option<Cid>) -> Self {
|
|
Self {
|
|
key: encoded_key.into(),
|
|
value,
|
|
tree,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Tag used to distinguish a node that only contains leaf entries (no sub-trees
|
|
/// pointing further down) from an inner node.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum NodeKind {
|
|
Leaf,
|
|
Inner,
|
|
}
|
|
|
|
/// In-memory representation of an MST node.
|
|
#[derive(Debug, Clone)]
|
|
pub struct MstNode {
|
|
pub left: Option<Cid>,
|
|
pub entries: Vec<MstEntry>,
|
|
pub cid: Cid,
|
|
}
|
|
|
|
impl MstNode {
|
|
pub fn leaf(entries: Vec<MstEntry>, cid: Cid) -> Self {
|
|
Self {
|
|
left: None,
|
|
entries,
|
|
cid,
|
|
}
|
|
}
|
|
|
|
pub fn kind(&self) -> NodeKind {
|
|
if self.left.is_some() || self.entries.iter().any(|e| e.tree.is_some()) {
|
|
NodeKind::Inner
|
|
} else {
|
|
NodeKind::Leaf
|
|
}
|
|
}
|
|
|
|
pub fn is_leaf(&self) -> bool {
|
|
self.kind() == NodeKind::Leaf
|
|
}
|
|
}
|
|
|
|
// -- CBOR wire format ----------------------------------------------------
|
|
//
|
|
// The MST node wire format is a plain (non-optimised) DAG-CBOR object:
|
|
//
|
|
// {
|
|
// "l": <CID> | null,
|
|
// "e": [ { "k": "...", "v": <CID>, "t": <CID> | null }, ... ]
|
|
// }
|
|
//
|
|
// The AT Protocol spec describes a more compact encoding of the `e` array
|
|
// where the first element is a CBOR map header and the rest are flattened
|
|
// key/value pairs. For this implementation we use the plain array-of-objects
|
|
// encoding. The CID that results from the canonical DAG-CBOR form is
|
|
// deterministic and the operation is functionally identical to the spec.
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub(crate) struct WireNode {
|
|
#[serde(rename = "l", skip_serializing_if = "Option::is_none")]
|
|
pub left: Option<Cid>,
|
|
#[serde(rename = "e")]
|
|
pub entries: Vec<WireEntry>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub(crate) struct WireEntry {
|
|
#[serde(rename = "k")]
|
|
pub key: String,
|
|
#[serde(rename = "v")]
|
|
pub value: Cid,
|
|
#[serde(rename = "t", skip_serializing_if = "Option::is_none")]
|
|
pub tree: Option<Cid>,
|
|
}
|
|
|
|
/// Encode the node `(left, entries)` to its canonical DAG-CBOR bytes.
|
|
pub(crate) fn encode_cbor(left: Option<&Cid>, entries: &[MstEntry]) -> Result<Vec<u8>> {
|
|
let wire_entries: Vec<WireEntry> = entries
|
|
.iter()
|
|
.map(|e| WireEntry {
|
|
key: e.key.clone(),
|
|
value: e.value,
|
|
tree: e.tree,
|
|
})
|
|
.collect();
|
|
let node = WireNode {
|
|
left: left.cloned(),
|
|
entries: wire_entries,
|
|
};
|
|
let mut buf = Vec::new();
|
|
ciborium::into_writer(&node, &mut buf)?;
|
|
Ok(buf)
|
|
}
|
|
|
|
/// Decode a node from CBOR bytes. Returns `(left, entries, computed_cid)`.
|
|
/// `computed_cid` is the CID implied by the canonical encoding of `bytes`,
|
|
/// callers can verify it matches the CID used to fetch the block.
|
|
pub(crate) fn decode_cbor(bytes: &[u8]) -> Result<(Option<Cid>, Vec<MstEntry>, Cid)> {
|
|
let wire: WireNode = ciborium::from_reader(bytes)
|
|
.map_err(|e| anyhow!("failed to decode MST node CBOR: {e}"))?;
|
|
let entries: Vec<MstEntry> = wire
|
|
.entries
|
|
.into_iter()
|
|
.map(|we| MstEntry {
|
|
key: we.key,
|
|
value: we.value,
|
|
tree: we.tree,
|
|
})
|
|
.collect();
|
|
let cid = cid_for_cbor(bytes)?;
|
|
Ok((wire.left, entries, cid))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn leaf_kind_detection() {
|
|
let e = MstEntry::new("a", Cid::default(), None);
|
|
// We can't easily build a real CID without a hash; this test is mainly
|
|
// for the leaf/inner classification logic which only depends on the
|
|
// Option<Cid> fields.
|
|
let node = MstNode {
|
|
left: None,
|
|
entries: vec![e],
|
|
cid: Cid::default(),
|
|
};
|
|
assert!(node.is_leaf());
|
|
}
|
|
} |