toip.tools

SDK library · Browser and Node

eventlog

Maintain a signed key-event log and content-addressed trails with rotations, witness receipts, and duplicity evidence. The main entry is browser-safe; file persistence and Node HTTP helpers are exported from @toip-tools/eventlog/node.

@toip-tools/eventlogv0.1.0Source

Setup

Install the package from npm. It needs Node 22.13 or later, or a bundler for browser entry points.

ShellInstall
npm install @toip-tools/eventlog

Runtime: Browsers and Node. A file store and an HTTP witness are in the Node entry.

TypeScriptImport
import { Controller, Trail, MemoryStore } from "@toip-tools/eventlog";

Usage

Create a controller and append a witnessed trail entry

Choose hot signers, commit future keys using coldKeysFromSeed or explicit next-key digests, and call Controller.incept with a store and clock. Open a trail with openTrail(purpose). Use appendWitnessed when an external action must wait for receipts; ordinary append only signs and stores locally. Publish to witness clients and inspect missing receipts and evidence.

Rotate keys and recover a log

Call Controller.rotate with the committed future signers or retained cold keys. Rotation seals trail heads automatically. For recovery, cutAfter cuts later trail entries, and supersede can replace interaction events with a rotation. Persist newly active signers from signers() securely. Passing next: [] abandons the identifier and prevents further controller events.

Verify history and disclose selected content

Run verifyKel and verifyTrail on received history, then inspect problems and witness status. Use checkTrailRef with a witness client to verify a referenced entry, or verifySignature to distinguish a valid signature from its current/retired and anchored status. Export selected bodies or entries with exportBundle and verify received bundles with verifyBundle. A Watcher can ingest first-seen copies and poll witnesses for conflicting histories.

Example

An agent keeps a witnessed audit trail under a KERI identifier; a reviewer verifies the bundle.

TypeScriptsite/examples/snippets/eventlog.ts
import { Controller, MemoryStore, Witness, coldKeysFromSeed, ed25519Signer, exportBundle, localWitnessClient, signerFromIdentity, verifyBundle } from "@toip-tools/eventlog";
import { SystemClock, identityFromSeed, seedFrom } from "@toip-tools/trust-core";

const clock = new SystemClock();
const agent = identityFromSeed(seedFrom("example/agent"));
const witnesses = [1, 2].map((n) => new Witness({ signer: ed25519Signer(seedFrom(`example/witness/${n}`)), store: new MemoryStore(), clock }));

const controller = await Controller.incept({
  hot: [signerFromIdentity(agent)], // signs today
  cold: coldKeysFromSeed(seedFrom("example/agent/rotation"), "agent"), // pre-rotation: tomorrow's keys, committed now
  witnesses: witnesses.map((w) => w.aid),
  bt: 1,
  bind: agent, // links the AID to the agent's did:key
  store: new MemoryStore(),
  clock,
});

const clients = witnesses.map(localWitnessClient);
const trail = await controller.openTrail("agent/actions");
await trail.append({ kind: "agent/tool-call", body: { tool: "search", query: "bike shops near me" } });
await trail.appendWitnessed({ kind: "agent/payment", body: { to: "did:key:z6Mk…", cents: 4500 } }, clients); // before paying: wait for a receipt
await controller.rotate(); // new keys; every open trail is sealed at its head
await controller.publish(clients); // witnesses receipt everything they haven't seen

const bundle = await exportBundle(controller.store, controller.aid);
const check = await verifyBundle(bundle, { expectDid: agent.did });
console.log(check.ok ? "bundle verifies" : check.problems);
ShellRun from the repository root
npx tsx --tsconfig site/tsconfig.json site/examples/snippets/eventlog.ts
OutputRecorded output
bundle verifies

Lifecycle and persistence

  • Persist logs, bodies, receipts, and evidence through a LogStore. MemoryStore.dump() can be restored through its constructor, but it is not durable by itself. Node's JsonFileStore batches writes; call flush() or close() at shutdown to write pending changes.

  • Keep cold-key material separate from current hot keys. Controller.load re-verifies stored logs; supplying hot keys supports appending and interacting, while rotation additionally needs committed future signers or matching cold keys.

  • Trail.close() records a signed end-of-trail event. It is a protocol operation, not a resource cleanup call. Watcher.poll() is caller-driven; own and cancel any application polling timer and close HTTP servers/file stores that you create.

Error handling

WitnessError

appendWitnessed throws if fewer than max(1, bt) witness receipts are held. The newly appended entry remains in the local log, pending.

Do not perform the dependent external action. Inspect details.missing, retry publishing the existing entry, and reconcile its receipt state instead of appending a duplicate action.

Problem: unsupported / signatureInvalid / notWitnessed

Verification reports structured problems with a code, message, and optional log/sequence location. Unsupported protocol features are identified separately from invalid signatures or missing witness thresholds.

Check result problems before accepting history. Fetch missing receipts or use a compatible implementation for unsupported features; do not bypass signature checks.

SubmitResult: duplicity / unknownController / clockSkew / invalid

Witness submission can refuse history and may return receipts for messages accepted before the refusal. Duplicity may include the first-seen conflicting stream.

Retain valid partial receipts and conflicting evidence. Supply the controller KEL before its trails, correct clock skew, and investigate conflicts before acting. Controller.publish collects such failures in its report.

API reference

Choose an entry point for the complete export list. Each export has a reference page with its signature, parameter types, return type, fields, and public methods where applicable.