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:
+42
-1
@@ -1,5 +1,8 @@
|
||||
// Talks to the node that served this page. Everything it returns is untrusted
|
||||
// until verify.ts has checked it.
|
||||
// until verify.ts (listings) or message.ts (replies) has checked it.
|
||||
|
||||
import { canonicalize } from "./jcs.ts";
|
||||
import type { Envelope } from "./message.ts";
|
||||
|
||||
export interface Status {
|
||||
peer_id: string;
|
||||
@@ -9,6 +12,8 @@ export interface Status {
|
||||
peers: number;
|
||||
listings: number;
|
||||
difficulty: number;
|
||||
/** Proof-of-work bits a message needs; absent on nodes without messaging. */
|
||||
message_difficulty?: number;
|
||||
}
|
||||
|
||||
async function getJSON(url: string): Promise<unknown> {
|
||||
@@ -32,3 +37,39 @@ export async function search(params: URLSearchParams): Promise<unknown[]> {
|
||||
export async function getListing(author: string, id: string): Promise<unknown | null> {
|
||||
return getJSON(`/api/v1/listings/${encodeURIComponent(author)}/${encodeURIComponent(id)}`);
|
||||
}
|
||||
|
||||
/** A node refused a request. `detail` is the node's own explanation (English). */
|
||||
export class HttpError extends Error {
|
||||
readonly status: number;
|
||||
readonly detail: string;
|
||||
constructor(status: number, detail: string) {
|
||||
super(`${status} ${detail}`);
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
}
|
||||
}
|
||||
|
||||
/** Hands an encrypted envelope to the node, which gossips it to the listing's region. */
|
||||
export async function postMessage(envelope: Envelope): Promise<void> {
|
||||
const res = await fetch("/api/v1/messages", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: canonicalize(envelope), // the node only accepts the canonical encoding
|
||||
});
|
||||
if (res.ok) return;
|
||||
let detail = res.statusText;
|
||||
try {
|
||||
const body = (await res.json()) as { error?: unknown };
|
||||
if (typeof body.error === "string") detail = body.error;
|
||||
} catch {
|
||||
// keep the status text
|
||||
}
|
||||
throw new HttpError(res.status, detail);
|
||||
}
|
||||
|
||||
/** Envelopes in a mailbox created at or after `since` (unix seconds). Unverified. */
|
||||
export async function pollMailbox(tag: string, since: number): Promise<unknown[]> {
|
||||
const data = await getJSON(`/api/v1/messages?mailbox=${encodeURIComponent(tag)}&since=${since}`);
|
||||
if (!Array.isArray(data)) throw new Error("unexpected response");
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user