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>
138 lines
5.0 KiB
TypeScript
138 lines
5.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import { test } from "node:test";
|
|
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
|
|
import { canonicalize } from "../src/jcs.ts";
|
|
import { verifyListing, type Listing } from "../src/verify.ts";
|
|
|
|
// Written by the Go implementation (internal/listing/fixture_test.go).
|
|
const fixture = JSON.parse(
|
|
readFileSync(new URL("../../internal/listing/testdata/fixture.json", import.meta.url), "utf8"),
|
|
) as { now: number; difficulty: number; canonical: string; listing: Listing };
|
|
|
|
const opts = { now: fixture.now, minPowBits: fixture.difficulty };
|
|
const fresh = (): Listing => structuredClone(fixture.listing);
|
|
const failing = (l: unknown, o = opts) =>
|
|
verifyListing(l, o).checks.filter((c) => !c.ok).map((c) => c.id);
|
|
|
|
test("canonicalization is byte-identical to Go", () => {
|
|
assert.equal(canonicalize(fixture.listing), fixture.canonical);
|
|
});
|
|
|
|
test("a listing signed by Go verifies in the browser", () => {
|
|
const v = verifyListing(fixture.listing, opts);
|
|
assert.deepEqual(failing(fixture.listing), []);
|
|
assert.equal(v.ok, true);
|
|
assert.ok(v.powBits >= fixture.difficulty);
|
|
assert.equal(v.checks.length, 6);
|
|
});
|
|
|
|
test("tampering with any signed field is detected", () => {
|
|
const edits: Record<string, (l: Listing) => void> = {
|
|
price: (l) => (l.price!.amount_cents = 1),
|
|
title: (l) => (l.title = "Betrug"),
|
|
description: (l) => (l.description += "!"),
|
|
geohash: (l) => (l.geohash = "u30000"),
|
|
seq: (l) => (l.seq += 1),
|
|
expires_at: (l) => (l.expires_at += 1),
|
|
feature: (l) => l.features!.push("pool"),
|
|
};
|
|
for (const [name, edit] of Object.entries(edits)) {
|
|
const l = fresh();
|
|
edit(l);
|
|
assert.ok(failing(l).includes("signature"), `${name} change not detected`);
|
|
}
|
|
});
|
|
|
|
test("tampering with the certificate is detected", () => {
|
|
const l = fresh();
|
|
l.cert.expires_at += 86_400;
|
|
assert.deepEqual(failing(l), ["certificate"]);
|
|
|
|
const m = fresh();
|
|
m.cert.device = "00".repeat(32);
|
|
assert.ok(failing(m).includes("certificate"));
|
|
});
|
|
|
|
test("a certificate for another identity or device is rejected", () => {
|
|
const l = fresh();
|
|
l.author = "ab".repeat(32);
|
|
assert.ok(failing(l).includes("binding"));
|
|
assert.ok(failing(l).includes("signature"));
|
|
});
|
|
|
|
test("a self-signed listing from a new identity is internally consistent", () => {
|
|
// Verification proves integrity and the identity -> device chain. It does
|
|
// not make an unknown identity trustworthy; that is a separate question.
|
|
const idKey = ed25519.utils.randomSecretKey();
|
|
const devKey = ed25519.utils.randomSecretKey();
|
|
const hex = (b: Uint8Array) => Buffer.from(b).toString("hex");
|
|
const enc = new TextEncoder();
|
|
|
|
const cert: Record<string, unknown> = {
|
|
v: 1,
|
|
author: hex(ed25519.getPublicKey(idKey)),
|
|
device: hex(ed25519.getPublicKey(devKey)),
|
|
issued_at: fixture.now - 3600,
|
|
expires_at: fixture.now + 30 * 86_400,
|
|
};
|
|
cert.sig = hex(ed25519.sign(enc.encode("allmende-cert-v1\n" + canonicalize(cert)), idKey));
|
|
|
|
const l: Record<string, unknown> = {
|
|
v: 1,
|
|
id: "11".repeat(16),
|
|
author: cert.author,
|
|
device: cert.device,
|
|
seq: 1,
|
|
created_at: fixture.now,
|
|
expires_at: fixture.now + 86_400,
|
|
geohash: "u1xjhx",
|
|
pow: 0,
|
|
cert,
|
|
};
|
|
// Mine a little proof-of-work so it also passes the PoW check.
|
|
for (let pow = 0; ; pow++) {
|
|
l.pow = pow;
|
|
const v = verifyListing({ ...l, sig: "00".repeat(64) }, { ...opts, minPowBits: 8 });
|
|
if (v.powBits >= 8) break;
|
|
}
|
|
l.sig = hex(ed25519.sign(enc.encode("allmende-listing-v1\n" + canonicalize(l)), devKey));
|
|
|
|
assert.deepEqual(failing(l, { ...opts, minPowBits: 8 }), []);
|
|
});
|
|
|
|
test("a signature made for another purpose does not verify (domain separation)", () => {
|
|
// Sign the listing bytes with the certificate context instead of the listing one.
|
|
const l = fresh();
|
|
assert.ok(failing({ ...l, sig: l.cert.sig }).includes("signature"));
|
|
});
|
|
|
|
test("time windows", () => {
|
|
const day = 86_400;
|
|
assert.ok(failing(fixture.listing, { ...opts, now: fixture.now + 20 * day }).includes("validity"), "expired listing");
|
|
assert.ok(failing(fixture.listing, { ...opts, now: fixture.now - 3600 }).includes("validity"), "created in the future");
|
|
assert.ok(failing(fixture.listing, { ...opts, now: fixture.listing.cert.expires_at + 1 }).includes("validity"), "expired certificate");
|
|
|
|
const l = fresh();
|
|
l.created_at = l.cert.issued_at - 10; // before the certificate existed
|
|
assert.ok(failing(l).includes("validity"));
|
|
});
|
|
|
|
test("insufficient proof-of-work is reported", () => {
|
|
const v = verifyListing(fixture.listing, { ...opts, minPowBits: 40 });
|
|
assert.deepEqual(
|
|
v.checks.filter((c) => !c.ok).map((c) => c.id),
|
|
["pow"],
|
|
);
|
|
});
|
|
|
|
test("garbage never throws and never verifies", () => {
|
|
for (const junk of [null, undefined, 5, "x", [], {}, { cert: 5 }, { cert: {} }, { v: 1, cert: [] }]) {
|
|
assert.equal(verifyListing(junk, opts).ok, false);
|
|
}
|
|
const floaty = fresh();
|
|
floaty.price!.amount_cents = 1.5;
|
|
assert.equal(verifyListing(floaty, opts).ok, false);
|
|
});
|