# @saga-sync/client

The consumer side of **saga-sync** — the reference implementation of
[privacy-protocol-state-distribution](../../README.md). Given a manifest URL it
reconstructs a privacy protocol's event history by downloading the published
static files and **verifying every one** against the manifest — no trust in the
publisher beyond the manifest itself (and, optionally, its signature).

Install just this package to consume published state — it pulls in
[`@saga-sync/core`](../core) and `@noble/*`, and **nothing else**: no viem, no
`@google-cloud/storage`, no scraper.

```bash
npm install @saga-sync/client
```

Ships a library (`@saga-sync/client`) and a CLI (`state-client`). The library
entry is **browser-safe** — gzip via the web-standard `DecompressionStream`,
`Uint8Array` + `TextDecoder` instead of `Buffer`, no `node:` imports (a guard test
enforces it). The CLI and the optional disk cache are Node-only.

## Quick start (library)

```ts
import { Client, HttpStore } from "@saga-sync/client";

// `source` is the Store the manifest + chunks live behind (a CDN/bucket URL).
const client = new Client({ source: new HttpStore("https://cdn.example/pp-state/") });

// List what's published.
for (const id of await client.listProtocols()) console.log(id);

// Stream one protocol's full event history in block order.
for await (const event of client.streamEvents("tornado-cash-1-eth-0.1")) {
  // event: CanonicalRecord — for a log stream, a CanonicalEvent:
  // { contractAddress, eventTopic, topics, data, blockNumber, logIndex }
  handle(event);
}
```

`streamEvents` returns an **`AsyncGenerator<CanonicalRecord>`**, not an array — you
consume it once with `for await`. That's a deliberate memory choice: events are
produced lazily and peak memory is bounded to roughly one decompressed chunk
(~10 MiB) × the fetch concurrency, **independent of total history size**, so you
can process a stream larger than RAM. It also means an early `break` never
downloads or verifies the chunks you didn't reach, and you start processing the
first chunk while later ones are still downloading. If you genuinely want the whole
array, collect it yourself:

```ts
const all: CanonicalRecord[] = [];
for await (const e of client.streamEvents(id)) all.push(e);
```

### Filtering

`streamEvents(id, opts)` accepts:

- **`from` / `to`** — restrict to a block range (`[from, to)`); only overlapping
  chunks are fetched.
- **`addresses`** — keep only events from these contract addresses.
- **`eventTopics`** — keep only these event `topic0`s (event types).

`addresses`/`eventTopics` are validated against the manifest's `trackedAddresses` /
`trackedEventTopics` and **throw** if you ask for something the stream doesn't track
— a mismatched filter is a bug, not a silently-empty result.

```ts
for await (const e of client.streamEvents(id, {
  from: "0x8b1d26",
  eventTopics: ["0xa945e51e…"], // Deposit only
})) { … }
```

### Selecting a stream by address

Don't know (or want to hardcode) the stream id? Pass a **selector** — a contract
address, optionally with a chain-id guard — instead of an id. The client resolves
it against the manifest's `trackedAddresses` (and `chainId`) to exactly one stream
and streams it, implicitly filtered to that address:

```ts
for await (const e of client.streamEvents({ address: "0x12d66f87…", chainId: "0x1" })) {
  handle(e);
}

// Or resolve without streaming:
const id = await client.resolveProtocolId({ address: "0x12d66f87…" });
```

`chainId` is optional. A manifest is single-chain in practice (mainnet and Sepolia
are separate buckets), so it mostly guards against pointing at the wrong one.
Resolution **throws** if the address matches no stream, or more than one — a
selector must name exactly one stream (pass an explicit id to disambiguate).
Combining a selector with `opts.addresses` also throws (the address is the
selector).

## Verification model

Everything the client serves is verified — **cache hits included**:

- **`verifyDigest(meta, bytes)`** recomputes the sha256 of a chunk's uncompressed
  JSONL and compares it to the manifest entry; a mismatch throws
  `DigestMismatchError`.
- **`verifyChunkEvents(meta, events)`** then enforces the canonical form the digest
  can't catch on its own — every record within the chunk's `[from, to)` range,
  strictly ascending (logs by `(blockNumber, logIndex)`, entity records by
  `(blockNumber, transactionIndex, opIndex)`), every one of those quantities a
  minimal lower-case `0x`-hex string, with the in-block ones (`logIndex`,
  `transactionIndex`, `opIndex`) within `Number.MAX_SAFE_INTEGER`, and no chunk
  mixing the two kinds; violations throw `CanonicalFormError`. Given the stream's expectations (which
  `streamEvents` always passes), it also checks every log carries exactly the
  base six fields plus the stream's declared `extraLogFields`, in order — so a
  `/v2` stream's `transactionHash` is guaranteed present and well-formed.
- A missing file throws `ChunkNotFoundError`, kept distinct from a digest mismatch
  so callers can tell "absent" from "tampered".

### Manifest signatures (optional)

Supply a publisher public key and the client fetches the manifest's signature
(`index.json.sigs`, falling back to the legacy `index.json.sig`) and verifies it
**over the raw `index.json` bytes before parsing** — mandatory once enabled
(missing or mismatched signature throws). Because the manifest holds every chunk's
digest, one signature transitively authenticates the whole dataset.

```ts
const client = new Client({ source: new HttpStore(baseUrl), publicKey: "0x…" });
```

A manifest may carry several signatures, one per algorithm. Pass one key or
several; each key's algorithm is inferred from its length (32 bytes → Ed25519,
33 or 65 → secp256k1), and verification passes if **any** of them verifies:

```ts
const client = new Client({ source, publicKey: ["0x…ed25519", "0x…secp256k1"] });
```

Two things worth knowing before pinning more than one key:

- Any single valid signature accepts the manifest, so the trust root is only as
  strong as the **weakest** key you pin. More algorithms mean more reach, not
  more security.
- secp256k1 is **not** bundled by default — it would grow this library by about a
  third. Enable it with a side-effect import:

  ```ts
  import "@saga-sync/core/secp256k1";
  ```

  The `state-client` CLI does this already.

### Logs and entity records

`streamEvents` yields **`CanonicalRecord`**, which is either a log (the six-field
shape every stream has always had) or an **entity record** — something an indexer
derived from data no log carries, such as Railgun's per-transaction operations.
Narrow with `isEntityRecord`:

```ts
import { isEntityRecord } from "@saga-sync/core";

for await (const record of client.streamEvents(id)) {
  if (isEntityRecord(record)) handleEntity(record);   // entity, blockNumber, transactionIndex, opIndex, …
  else handleLog(record);                              // contractAddress, eventTopic, topics, data, …
}
```

A stream carries **one kind**, never a mix — the client rejects a chunk that
interleaves them. So in practice you know which you are getting from the stream id,
and the check above is a type-level narrowing rather than a per-record branch. The
manifest's `protocolMetadata.source` tells you the provenance: absent or `"rpc"`
means chain-derived and independently checkable; anything else means the records
were mirrored from that source and are only as good as it is.

## Library API

- **`Client`** — `streamEvents(target, opts?)` (merged sealed chunks then the hot
  head, sealed fetched with a bounded-concurrency sliding window and optionally
  cached to a local `Store`). `target` is a protocol id **or** a
  `{ address, chainId? }` selector. `resolveProtocolId(selector)` resolves a
  selector to an id without streaming; `listProtocols(prefix?)` enumerates ids;
  plus manifest inspection helpers.
- **`loadManifest(store, key, { publicKey? })`** — one fetch; verifies the signature
  when a key is supplied. Re-uses core's `Manifest` so the shape is defined once.
- **`decodeAndVerify` / `fetchChunkFrom(store, meta)`** — the fetch→gunzip→verify→
  parse→verify-canonical pipeline for a single chunk.
- **`verifyDigest` / `verifyChunkEvents`** and the error types above.

Reads go through core's `Store` seam (`HttpStore` for the network, an optional
local cache `Store`). Sealed chunks are safe to cache — immutable, content-
addressed, and re-verified on every read; the hot head is re-fetched every call and
never cached.

## CLI (`state-client`)

Subcommands take a `<manifest-url>` (the manifest is read from `<url>/index.json`);
the query commands fetch **only** the manifest — no chunk downloads.

```
state-client <command> <manifest-url> [<protocol-id>] [options]

  protocols <url>            list every protocol + summary       (alias: ls)
  info      <url> <id>       range, size, metadata, hot head, gap/contiguity check
  head      <url> <id>       latest covered block                (alias: latest)
  chunks    <url> <id>       list a protocol's chunks
  stream    <url> [<id>]     download + verify + emit NDJSON

  --json                 machine-readable output instead of human tables
  --from-block <hex>     info/chunks/stream: lower bound of the block range
  --to-block <hex>       info/chunks/stream: upper bound (exclusive)
  --address <hex>        stream: with an <id>, a repeatable post-decode filter;
                         without an <id>, a single --address selects the stream
  --chain <hex>          stream: chain-id guard when selecting a stream by --address
  --event-topic <hex>    stream: keep only these event topic0s (repeatable)
  --since-block <hex>    head: exit 3 if no block beyond this is covered
  --hot                  chunks: include the mutable hot head
  --cache-dir <path>     stream: local cache of verified sealed chunks
  --concurrency <n>      stream: parallel chunk fetches (default 4)
  --public-key <hex>     require + verify the manifest's signature (repeatable;
                         Ed25519 or secp256k1, inferred from the key length)
```

Stream by address instead of naming the id:

```bash
state-client stream https://cdn.example/pp-state/ --address 0x12d66f87… --chain 0x1
```

`stream` emits NDJSON on stdout + a summary on stderr; the query commands print a
human table or, with `--json`, structured JSON. `--public-key` applies to all
commands and verifies the manifest's signature before trusting it; pass it more
than once to accept any of several keys.

Exit codes: `0` ok · `1` usage / fetch / not-found · `3` `head --since-block` found
nothing newer.

```bash
# serve some published chunks, then:
node packages/client/dist/cli.js info   http://localhost:8080/ tornado-cash-1-eth-0.1
node packages/client/dist/cli.js stream http://localhost:8080/ tornado-cash-1-eth-0.1 \
  --cache-dir ./client-cache > events.ndjson
```

## Modules

- **`verify.ts`** — `verifyDigest` + `verifyChunkEvents` (both mandatory on every chunk).
- **`fetch.ts`** — `decodeAndVerify` + `fetchChunkFrom`; the per-chunk pipeline.
- **`manifest.ts`** — `loadManifest` + pure `selectSealedChunks` / `selectHotHead`
  range-overlap helpers + signature check.
- **`client.ts`** — the `Client` class and the merged streaming logic.
- **`format.ts`** — `humanBytes` + `table`, the CLI's rendering helpers.
- **`cli.ts`** — the `state-client` entry point.
- **`index.ts`** — the browser-safe library barrel.
