Every node now serves a search and listing-detail UI at GET /. It is vanilla TypeScript, bundled unminified into web/dist (committed) and embedded with go:embed, so `go build` is enough to run a node. The browser verifies each listing itself (certificate, binding, validity, signature, proof-of-work) with pure-JS ed25519, so a gateway can withhold listings but not forge them. Unverifiable results are hidden and their content is never shown. Listing text is only inserted via textContent and the UI is served with a strict CSP. Canonical JSON is reimplemented in TypeScript and tested against a golden fixture signed by the Go code. /api/v1/status now reports difficulty. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
// Canonical JSON (RFC 8785), restricted to what Allmende uses: numbers must be
|
|
// safe integers. This must produce byte-for-byte what internal/jcs produces in
|
|
// Go; test/verify.test.ts checks it against a fixture written by Go.
|
|
|
|
export function canonicalize(value: unknown): string {
|
|
if (value === null) return "null";
|
|
switch (typeof value) {
|
|
case "boolean":
|
|
return value ? "true" : "false";
|
|
case "number":
|
|
if (!Number.isSafeInteger(value)) {
|
|
throw new Error("jcs: only safe integers are supported");
|
|
}
|
|
return String(value);
|
|
case "string":
|
|
// JSON.stringify escapes exactly what RFC 8785 requires: quote,
|
|
// backslash, \b \f \n \r \t, and other control characters as \u00xx.
|
|
return JSON.stringify(value);
|
|
case "object": {
|
|
if (Array.isArray(value)) {
|
|
return "[" + value.map(canonicalize).join(",") + "]";
|
|
}
|
|
const obj = value as Record<string, unknown>;
|
|
// Default sort compares UTF-16 code units, which is what RFC 8785 wants.
|
|
const keys = Object.keys(obj).sort();
|
|
return (
|
|
"{" +
|
|
keys.map((k) => JSON.stringify(k) + ":" + canonicalize(obj[k])).join(",") +
|
|
"}"
|
|
);
|
|
}
|
|
default:
|
|
throw new Error("jcs: unsupported value");
|
|
}
|
|
}
|