Guide ยท @toip-tools/eventlog, @toip-tools/trust-core
Keep a tamper-evident audit log with KERI key event logs
Write an append-only, witnessed audit log under a KERI identifier, rotate keys with pre-rotation, and detect an edited entry offline.
An ordinary log can be edited by whoever runs it. A tamper-evident log can't be changed quietly: every entry is signed and chained, independent witnesses receipt the first version they see, and anyone can verify a copy offline.
This program creates a KERI identifier for a service, appends three audit entries (waiting for a witness receipt on the one that matters), rotates its keys, exports a bundle and verifies it. Then it rewrites one entry and verifies again.
Before you start
You need Node 22.13 or later. In a new folder, install the packages and tsx, a TypeScript runner:
mkdir tamper-evident-audit-log && cd tamper-evident-audit-log
npm init -y && npm pkg set type=module
npm install @toip-tools/eventlog @toip-tools/trust-core tsx
The code
Save this as tamper-evident-audit-log.ts. It is the complete program; the site's checks run it on every build.
// Keep a tamper-evident audit log under a KERI identifier: signed entries, witness receipts, key rotation
// with pre-rotation, and an offline bundle anyone can verify. Editing an entry afterwards is detected.
import { Controller, MemoryStore, Witness, coldKeysFromSeed, ed25519Signer, exportBundle, localWitnessClient, signerFromIdentity, verifyBundle } from "@toip-tools/eventlog";
import { SystemClock, identityFromSeed } from "@toip-tools/trust-core";
const clock = new SystemClock();
const random = () => crypto.getRandomValues(new Uint8Array(32));
const service = identityFromSeed(random());
// Two independent witnesses. Each receipts the first version of every event it sees.
const witnesses = [1, 2].map(() => new Witness({ signer: ed25519Signer(random()), store: new MemoryStore(), clock }));
const clients = witnesses.map(localWitnessClient);
// Incept a KERI identifier (AID). The cold keys are tomorrow's keys: only their digest is published now.
const controller = await Controller.incept({
hot: [signerFromIdentity(service)],
cold: coldKeysFromSeed(random(), "service"),
witnesses: witnesses.map((w) => w.aid),
bt: 1, // events need a receipt from at least one witness
bind: service, // links the AID to the service's did:key
store: new MemoryStore(),
clock,
});
console.log("AID:", controller.aid);
// A trail is an append-only log owned by the AID. Each entry is a signed header committing to a private body.
const trail = await controller.openTrail("admin/actions");
await trail.append({ kind: "user/role-changed", body: { user: "dana", from: "viewer", to: "admin", by: "ops-bot" } });
await trail.append({ kind: "export/started", body: { table: "customers", rows: 1204 } });
// For entries that matter, wait for a witness receipt before acting.
await trail.appendWitnessed({ kind: "key/deleted", body: { key: "backup-2025" } }, clients);
await controller.rotate(); // switch to the pre-committed keys; the old key can no longer extend the trail
await controller.publish(clients); // witnesses receipt everything they haven't seen
// Export everything needed to check the log offline, including the entry bodies, and verify it.
const bundle = await exportBundle(controller.store, controller.aid, { bodies: "all" });
const check = await verifyBundle(bundle, { expectDid: service.did });
console.log("Bundle verifies:", check.ok);
for (const e of check.trails[0].entries) console.log(` #${e.s} ${e.t} ${e.kind || "(trail opened)"}: ${e.status}`);
// Quietly rewrite history: change who granted admin. The body no longer matches the digest its entry signed.
const edited = structuredClone(bundle);
const body = edited.trails[0].bodies!.find((b) => b.user === "dana")!;
body.by = "nobody";
const after = await verifyBundle(edited, { expectDid: service.did });
console.log("After editing an entry:", after.ok, after.problems.map((p) => `${p.code}: ${p.message}`));
Run it:
npx tsx tamper-evident-audit-log.ts
AID: EDKhARiiy2xaTIASYxHuLvkZTkQBOv6Cs0HPDFBeEmPg
Bundle verifies: true
#0 tin (trail opened): valid
#1 ten user/role-changed: valid
#2 ten export/started: valid
#3 ten key/deleted: valid
After editing an entry: false [ 'bodyMismatch: body of entry 1 does not match its SAID' ]
Keys are random, so identifiers and sizes differ on each run. From a clone of the repository, run npx tsx --tsconfig site/tsconfig.json site/examples/guides/tamper-evident-audit-log.ts, or read it in site/examples/guides/tamper-evident-audit-log.ts.
How it works
Controller.inceptcreates a KERI identifier (an AID) and its key event log. The KEL's events are KERI 1.0; the package's tests hand them to KERIpy 1.3.6, which computes the same key state.- Pre-rotation: inception publishes only a digest of the next keys (
cold).rotate()switches to them, so someone who steals today's key still can't take over the identifier. - A trail is an audit log owned by the AID. Each entry is a small signed header that commits to a private body by its digest. Rotation seals every open trail at its head, so a retired key can't extend it.
appendWitnessedwaits until enough witnesses (bt) have receipted the entry. Witnesses receipt the first version they see at each position, so two different histories can't both collect receipts.exportBundlepacks the KEL, the trails, the receipts and the chosen bodies;verifyBundlechecks all of it offline and returns problems instead of throwing. Here it reportsbodyMismatchfor the edited entry.
Limits and production notes
- Trails are this project's own format, built from KERI's rules and CESR primitives; no other KERI implementation reads them. The KEL itself is plain KERI.
- Use persistent stores in production:
JsonFileStorefrom@toip-tools/eventlog/node, and witnesses run by parties other than the writer. - Delegated identifiers, weighted thresholds and KERI 2.0 are not supported.