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:
+21
-23
@@ -1,8 +1,10 @@
|
||||
import { getListing, getStatus, search, type Status } from "./api.ts";
|
||||
import { getListing, getStatus, search } from "./api.ts";
|
||||
import { append, clear, h } from "./dom.ts";
|
||||
import { date, price, rooms, shortKey } from "./format.ts";
|
||||
import { decodeGeohash } from "./geohash.ts";
|
||||
import { lang, t, type Key } from "./i18n.ts";
|
||||
import { contactPanel, showConversation, showInbox } from "./chat.ts";
|
||||
import { state, verifyOpts } from "./state.ts";
|
||||
import { verifyListing, type Listing, type Verdict } from "./verify.ts";
|
||||
|
||||
const PAGE = 20;
|
||||
@@ -10,17 +12,9 @@ const GEOHASH_PREFIX = /^[0-9bcdefghjkmnpqrstuvwxyz]{1,6}$/;
|
||||
|
||||
const app = document.getElementById("app")!;
|
||||
const statusEl = document.getElementById("status")!;
|
||||
// Not called `status`: that would collide with the built-in window.status.
|
||||
let nodeInfo: Status | null = null;
|
||||
/** Bumped on every navigation so late responses for an old view are dropped. */
|
||||
let generation = 0;
|
||||
|
||||
// --- helpers --------------------------------------------------------------
|
||||
|
||||
function verifyOpts() {
|
||||
return { now: Math.floor(Date.now() / 1000), minPowBits: nodeInfo?.difficulty ?? 0 };
|
||||
}
|
||||
|
||||
/** Translates a value from a listing if it is a known one; unknown values are shown as plain text. */
|
||||
function label(prefix: "kind" | "cat", value: string | undefined): string {
|
||||
if (!value) return "";
|
||||
@@ -78,7 +72,7 @@ function select(name: string, current: string, options: [string, string][]): HTM
|
||||
|
||||
function searchForm(params: URLSearchParams): HTMLFormElement {
|
||||
const get = (k: string) => params.get(k) ?? "";
|
||||
const regions = h("datalist", { id: "regions" }, ...(nodeInfo?.regions ?? []).map((r) => h("option", { value: r })));
|
||||
const regions = h("datalist", { id: "regions" }, ...(state.node?.regions ?? []).map((r) => h("option", { value: r })));
|
||||
const form = h(
|
||||
"form",
|
||||
{ class: "filters" },
|
||||
@@ -95,7 +89,7 @@ function searchForm(params: URLSearchParams): HTMLFormElement {
|
||||
),
|
||||
field(
|
||||
t("filter.region"),
|
||||
h("input", { name: "geohash", value: get("geohash"), list: "regions", autocomplete: "off", spellcheck: "false", maxlength: 6, placeholder: nodeInfo?.regions[0] ?? "u1x" }),
|
||||
h("input", { name: "geohash", value: get("geohash"), list: "regions", autocomplete: "off", spellcheck: "false", maxlength: 6, placeholder: state.node?.regions[0] ?? "u1x" }),
|
||||
t("filter.regionHint"),
|
||||
),
|
||||
field(t("filter.maxPrice"), h("input", { name: "max_price", type: "number", min: 0, step: 50, inputmode: "decimal", value: get("max_price") })),
|
||||
@@ -160,12 +154,12 @@ async function loadResults(gen: number, q: URLSearchParams, root: HTMLElement):
|
||||
paged.set("offset", String(offset));
|
||||
items = await search(paged);
|
||||
} catch {
|
||||
if (gen !== generation) return;
|
||||
if (gen !== state.generation) return;
|
||||
clear(foot);
|
||||
foot.append(h("p", { class: "error" }, t("results.error")));
|
||||
return;
|
||||
}
|
||||
if (gen !== generation) return;
|
||||
if (gen !== state.generation) return;
|
||||
|
||||
const opts = verifyOpts();
|
||||
for (const raw of items) {
|
||||
@@ -190,7 +184,7 @@ async function loadResults(gen: number, q: URLSearchParams, root: HTMLElement):
|
||||
}
|
||||
|
||||
function showSearch(params: URLSearchParams): void {
|
||||
const gen = generation;
|
||||
const gen = state.generation;
|
||||
clear(app);
|
||||
const results = h("div", { class: "results", "aria-live": "polite" });
|
||||
app.append(h("p", { class: "tagline" }, t("app.tagline")), searchForm(params), results);
|
||||
@@ -288,12 +282,12 @@ function listingBody(l: Listing): HTMLElement[] {
|
||||
l.features?.length
|
||||
? h("section", { class: "panel" }, h("h2", null, t("detail.features")), h("ul", { class: "chips" }, ...l.features.map((f) => h("li", null, f))))
|
||||
: h("span"),
|
||||
h("section", { class: "panel contact" }, h("h2", null, t("detail.contact")), h("p", null, t("detail.contactSoon"))),
|
||||
contactPanel(l),
|
||||
];
|
||||
}
|
||||
|
||||
async function showDetail(author: string, id: string): Promise<void> {
|
||||
const gen = generation;
|
||||
const gen = state.generation;
|
||||
clear(app);
|
||||
const body = h("div", null, h("p", { class: "muted" }, t("results.loading")));
|
||||
app.append(h("a", { class: "back", href: "#/" }, t("detail.back")), body);
|
||||
@@ -302,12 +296,12 @@ async function showDetail(author: string, id: string): Promise<void> {
|
||||
try {
|
||||
raw = await getListing(author, id);
|
||||
} catch {
|
||||
if (gen !== generation) return;
|
||||
if (gen !== state.generation) return;
|
||||
clear(body);
|
||||
body.append(h("p", { class: "error" }, t("results.error")));
|
||||
return;
|
||||
}
|
||||
if (gen !== generation) return;
|
||||
if (gen !== state.generation) return;
|
||||
clear(body);
|
||||
|
||||
if (raw === null) {
|
||||
@@ -331,13 +325,16 @@ async function showDetail(author: string, id: string): Promise<void> {
|
||||
// --- boot -----------------------------------------------------------------
|
||||
|
||||
function route(): void {
|
||||
generation++;
|
||||
state.generation++;
|
||||
const hash = location.hash.replace(/^#/, "") || "/";
|
||||
const q = hash.indexOf("?");
|
||||
const path = q < 0 ? hash : hash.slice(0, q);
|
||||
const params = new URLSearchParams(q < 0 ? "" : hash.slice(q + 1));
|
||||
const m = /^\/l\/([0-9a-f]{64})\/([0-9a-f]{32})$/.exec(path);
|
||||
const c = /^\/c\/([0-9a-f]{64})\/([0-9a-f]{32})\/([0-9a-f]{64})$/.exec(path);
|
||||
if (m) void showDetail(m[1]!, m[2]!);
|
||||
else if (c) void showConversation(c[1]!, c[2]!, c[3]!);
|
||||
else if (path === "/messages") showInbox();
|
||||
else showSearch(params);
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
@@ -345,12 +342,13 @@ function route(): void {
|
||||
async function boot(): Promise<void> {
|
||||
document.documentElement.lang = lang;
|
||||
document.getElementById("foot")!.textContent = t("footer.text");
|
||||
document.getElementById("nav")!.append(h("a", { href: "#/messages" }, t("nav.messages")));
|
||||
try {
|
||||
nodeInfo = await getStatus();
|
||||
state.node = await getStatus();
|
||||
statusEl.textContent = t("status.line", {
|
||||
listings: nodeInfo.listings,
|
||||
peers: nodeInfo.peers,
|
||||
regions: nodeInfo.regions.join(", "),
|
||||
listings: state.node.listings,
|
||||
peers: state.node.peers,
|
||||
regions: state.node.regions.join(", "),
|
||||
});
|
||||
} catch {
|
||||
statusEl.textContent = t("status.unreachable");
|
||||
|
||||
Reference in New Issue
Block a user