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/eventlog` · v0.1.0 · [Source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/eventlog/src/index.ts)

## Setup

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

Install:

```bash
npm install @toip-tools/eventlog
```

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

Import:

```ts
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.

[`Controller`](https://toip.tools/toolkit/eventlog/api/Controller/) · [`coldKeysFromSeed`](https://toip.tools/toolkit/eventlog/api/coldKeysFromSeed/) · [`Trail`](https://toip.tools/toolkit/eventlog/api/Trail/) · [`MemoryStore`](https://toip.tools/toolkit/eventlog/api/MemoryStore/) · [`httpWitnessClient`](https://toip.tools/toolkit/eventlog/api/httpWitnessClient/)

### 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.

[`Controller`](https://toip.tools/toolkit/eventlog/api/Controller/) · [`RotateOptions`](https://toip.tools/toolkit/eventlog/api/RotateOptions/) · [`nextKeyDigest`](https://toip.tools/toolkit/eventlog/api/nextKeyDigest/) · [`keyQb64`](https://toip.tools/toolkit/eventlog/api/keyQb64/)

### 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.

[`verifyKel`](https://toip.tools/toolkit/eventlog/api/verifyKel/) · [`verifyTrail`](https://toip.tools/toolkit/eventlog/api/verifyTrail/) · [`checkTrailRef`](https://toip.tools/toolkit/eventlog/api/checkTrailRef/) · [`verifySignature`](https://toip.tools/toolkit/eventlog/api/verifySignature/) · [`exportBundle`](https://toip.tools/toolkit/eventlog/api/exportBundle/) · [`verifyBundle`](https://toip.tools/toolkit/eventlog/api/verifyBundle/) · [`Watcher`](https://toip.tools/toolkit/eventlog/api/Watcher/)

## Example

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

site/examples/snippets/eventlog.ts:

```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);
```

Run from the repository root:

```bash
npx tsx --tsconfig site/tsconfig.json site/examples/snippets/eventlog.ts
```

Output:

```text
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.

- [`@toip-tools/eventlog`](https://toip.tools/toolkit/eventlog/api/)

  Browser and Node · 133 exports

- [`@toip-tools/eventlog/node`](https://toip.tools/toolkit/eventlog/api/node/)

  Node only · 3 exports

## Related

### Guides

- [Keep a tamper-evident audit log with KERI key event logs](https://toip.tools/guides/tamper-evident-audit-log/)

### Built on

- [`@toip-tools/trust-core`](https://toip.tools/toolkit/trust-core/): Identities, signed and encrypted Trust Tasks over TSP, credentials, status lists and local policy.

### Used by these examples

- [Agents](https://toip.tools/examples/#agents): AI agents with identities, spending limits, and accountable actions.
- [Trail](https://toip.tools/examples/#trail): Verifiable audit trails for agents, devices, and services.
- [Capture](https://toip.tools/examples/#capture): Verifiable origins for photos and sensor readings.

### Concepts

[Concepts](https://toip.tools/concepts/) explains the protocols behind eventlog; the [FAQ](https://toip.tools/faq/) covers install, runtimes and support.

---

This is the Markdown version of https://toip.tools/toolkit/eventlog/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
