Implement end-to-end encrypted buyer/seller messaging
Wire format (internal/message): sealed boxes to the listing's device key (X25519 + HKDF + ChaCha20-Poly1305), padded, proof-of-work stamped, with signed owner replies and address grants. Relay validation, bounded queues, gossip on per-region message topics and sync (store, p2p). Owner side (internal/inbox, API): inbox with hidden link messages and a per-day new-thread limit, replies, grants, and a private per-listing address. Public POST/GET /api/v1/messages for buyers. Browser: noble-based sealing and reply verification checked against a Go fixture, contact form, conversation view, message list, de/en strings. Docs: PROTOCOL.md messaging section matches the implementation. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
// The buyer's conversations, kept in this browser only. There are no accounts:
|
||||
// a conversation is its thread key, which is also in the private link the
|
||||
// buyer can bookmark. Losing both loses the conversation, by design.
|
||||
//
|
||||
// Browser storage can be missing (private windows, blocked site data), so every
|
||||
// access is guarded and the page keeps working from memory.
|
||||
|
||||
export interface StoredMessage {
|
||||
dir: "in" | "out";
|
||||
n: number;
|
||||
sent_at: number;
|
||||
t: "text" | "grant";
|
||||
text?: string;
|
||||
address?: string;
|
||||
phone?: string;
|
||||
email?: string;
|
||||
note?: string;
|
||||
valid_until?: number;
|
||||
/** Envelope hash, for messages received from the owner. */
|
||||
hash?: string;
|
||||
}
|
||||
|
||||
export interface Conversation {
|
||||
author: string;
|
||||
id: string;
|
||||
/** The thread secret, hex. */
|
||||
secret: string;
|
||||
title: string;
|
||||
created: number;
|
||||
updated: number;
|
||||
messages: StoredMessage[];
|
||||
/** Mailbox poll position (unix seconds). */
|
||||
since: number;
|
||||
/** Envelopes already looked at (hash -> their expiry), so they are not verified again. */
|
||||
seen: Record<string, number>;
|
||||
}
|
||||
|
||||
const KEY = "allmende.conversations.v1";
|
||||
|
||||
const cache = new Map<string, Conversation>();
|
||||
let loaded = false;
|
||||
let persistent = true;
|
||||
|
||||
const idOf = (author: string, id: string) => `${author}/${id}`;
|
||||
const HEX = (n: number) => new RegExp(`^[0-9a-f]{${n}}$`);
|
||||
|
||||
function wellFormed(c: unknown): c is Conversation {
|
||||
if (typeof c !== "object" || c === null) return false;
|
||||
const x = c as Record<string, unknown>;
|
||||
return (
|
||||
typeof x.author === "string" && HEX(64).test(x.author) &&
|
||||
typeof x.id === "string" && HEX(32).test(x.id) &&
|
||||
typeof x.secret === "string" && HEX(64).test(x.secret) &&
|
||||
typeof x.title === "string" &&
|
||||
typeof x.created === "number" && typeof x.updated === "number" &&
|
||||
Array.isArray(x.messages) && typeof x.since === "number" &&
|
||||
typeof x.seen === "object" && x.seen !== null
|
||||
);
|
||||
}
|
||||
|
||||
function load(): void {
|
||||
if (loaded) return;
|
||||
loaded = true;
|
||||
try {
|
||||
const raw = localStorage.getItem(KEY);
|
||||
if (!raw) return;
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
if (typeof parsed !== "object" || parsed === null) return;
|
||||
for (const c of Object.values(parsed)) {
|
||||
if (wellFormed(c)) cache.set(idOf(c.author, c.id), c);
|
||||
}
|
||||
} catch {
|
||||
persistent = false;
|
||||
}
|
||||
}
|
||||
|
||||
function flush(): void {
|
||||
try {
|
||||
localStorage.setItem(KEY, JSON.stringify(Object.fromEntries(cache)));
|
||||
} catch {
|
||||
persistent = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** False when this browser refuses to store anything, so conversations vanish with the page. */
|
||||
export function isPersistent(): boolean {
|
||||
load();
|
||||
if (persistent) {
|
||||
try {
|
||||
localStorage.setItem(KEY + ".probe", "1");
|
||||
localStorage.removeItem(KEY + ".probe");
|
||||
} catch {
|
||||
persistent = false;
|
||||
}
|
||||
}
|
||||
return persistent;
|
||||
}
|
||||
|
||||
export function getConversation(author: string, id: string): Conversation | undefined {
|
||||
load();
|
||||
return cache.get(idOf(author, id));
|
||||
}
|
||||
|
||||
export function saveConversation(c: Conversation): void {
|
||||
load();
|
||||
cache.set(idOf(c.author, c.id), c);
|
||||
flush();
|
||||
}
|
||||
|
||||
export function deleteConversation(author: string, id: string): void {
|
||||
load();
|
||||
cache.delete(idOf(author, id));
|
||||
flush();
|
||||
}
|
||||
|
||||
/** Most recently active first. */
|
||||
export function listConversations(): Conversation[] {
|
||||
load();
|
||||
return [...cache.values()].sort((a, b) => b.updated - a.updated);
|
||||
}
|
||||
|
||||
/** The private link to a conversation. The secret stays in the URL fragment, which is never sent to a server. */
|
||||
export function conversationHash(author: string, id: string, secret: string): string {
|
||||
return `#/c/${author}/${id}/${secret}`;
|
||||
}
|
||||
Reference in New Issue
Block a user