@fangorn-network/sdk 2026.7.24 → 2026.7.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { DataRegistryClient } from "../contracts/data-registry/index.js";
2
2
  import "../contracts/index.js";
3
3
  import { MetadataStorage } from "../providers/storage/types.js";
4
- import { Address, Hash } from "viem";
4
+ import { Address, Hash, Hex } from "viem";
5
5
  import { CID } from "multiformats/cid";
6
6
 
7
7
  //#region src/engine/index.d.ts
@@ -70,6 +70,14 @@ declare class MetagraphRegistry {
70
70
  * map block, so this is just a sanity bound keeping that block small and
71
71
  * rejecting garbage input early.
72
72
  */
73
+ /**
74
+ * The 32-byte root a commit settles to on-chain.
75
+ *
76
+ * The contract stores the commit CID's bare multihash digest — NOT a hash of the
77
+ * CID. Re-hashing here would produce a root that resolves to nothing, so this is
78
+ * the one encoding every caller must share rather than re-derive.
79
+ */
80
+ declare function rootHexFromCid(commitCid: CID): Hex;
73
81
  declare const MAX_NAMESPACE_LENGTH = 256;
74
82
  /** Reject invalid namespace names up front with a clear error. */
75
83
  declare function assertValidNamespace(namespace: NamespaceID): void;
@@ -143,10 +151,24 @@ declare class FangornEngine {
143
151
  */
144
152
  private getBlock;
145
153
  private putLocal;
146
- /** Reconstruct the publisher's current on-chain head as a commit CID, or null if none. */
147
- resolveHeadCommit(publisher: Address): Promise<CID | null>;
154
+ /**
155
+ * Reconstruct the current on-chain head of one publisher's namespace as a
156
+ * commit CID, or null if none. Each namespace is its own on-chain timeline,
157
+ * so this is per-(publisher, namespace) rather than per-publisher.
158
+ */
159
+ resolveHeadCommit(publisher: Address, namespace: NamespaceID): Promise<CID | null>;
148
160
  /** Decode the root map behind a commit. */
149
161
  private rootMapAt;
162
+ /**
163
+ * Which namespace a commit belongs to, read from its root map.
164
+ *
165
+ * Each namespace is its own on-chain timeline, so a commit's root map carries
166
+ * exactly one entry — which is how an app-wide subscriber recovers the
167
+ * namespace *name* from a stream that only carries `keccak256(name)`. A commit
168
+ * with several entries predates per-namespace timelines and can't be
169
+ * attributed to one.
170
+ */
171
+ namespaceOf(rootHex: Hex): Promise<NamespaceID>;
150
172
  /** The full vertex/edge link sets one commit records for `namespace`. */
151
173
  private treeLinksAt;
152
174
  /**
@@ -165,13 +187,13 @@ declare class FangornEngine {
165
187
  vertexCids: Record<string, string>;
166
188
  }>;
167
189
  /**
168
- * Settle a local commit as the publisher's on-chain head — the permissioned,
169
- * git `push` step. The commit and its CAR are already durable in storage
170
- * (createCommit uploads them), so this is just the compare-and-swap against
171
- * the current head. Unless `force`, the on-chain head must be an ancestor of
172
- * `commitCid` (fast-forward only).
190
+ * Settle a local commit as the on-chain head of one namespace — the
191
+ * permissioned, git `push` step. The commit and its CAR are already durable
192
+ * in storage (createCommit uploads them), so this is just the compare-and-swap
193
+ * against the current head. Unless `force`, the on-chain head must be an
194
+ * ancestor of `commitCid` (fast-forward only).
173
195
  */
174
- pushCommit(publisher: Address, commitCid: CID, opts?: {
196
+ pushCommit(publisher: Address, namespace: NamespaceID, commitCid: CID, opts?: {
175
197
  force?: boolean;
176
198
  }): Promise<{
177
199
  txHash: Hash;
@@ -186,6 +208,8 @@ declare class FangornEngine {
186
208
  private isAncestor;
187
209
  /** The commit CID string a raw on-chain root hex points at. */
188
210
  commitCidFromRootHex(hex: string): string;
211
+ /** Inverse of `commitCidFromRootHex`: the on-chain root hex for a commit CID. */
212
+ rootHexFromCommitCid(commitCid: string): Hex;
189
213
  /** Decode a vertex block into the public read shape. */
190
214
  private decodeVertex;
191
215
  /** Decode an edge block into the public read shape. */
@@ -204,7 +228,7 @@ declare class FangornEngine {
204
228
  namespaceDiff(oldRootHex: string | null, newRootHex: string, namespace: NamespaceID): Promise<NamespaceDiff>;
205
229
  /** Diff a commit against its first parent, across every namespace. */
206
230
  diffCommit(commitCid: CID): Promise<CommitDiff>;
207
- /** Does this namespace exist under the publisher's on-chain root yet? */
231
+ /** Has this publisher committed to this namespace on-chain yet? */
208
232
  namespaceExists(namespace: NamespaceID, publisher: Address): Promise<boolean>;
209
233
  /** Lists every vertex and edge currently committed under a namespace, resolved from the on-chain root. */
210
234
  listNamespace(namespace: NamespaceID, publisher: Address): Promise<NamespaceContents>;
@@ -213,7 +237,7 @@ declare class FangornEngine {
213
237
  * CAR chain (vertex blocks are not individually pinned — they live inside
214
238
  * commit CARs).
215
239
  */
216
- readVertex(cid: string, publisher: Address): Promise<NamespaceContents["vertices"][number]>;
240
+ readVertex(cid: string, publisher: Address, namespace: NamespaceID): Promise<NamespaceContents["vertices"][number]>;
217
241
  }
218
242
  //#endregion
219
- export { CommitDiff, CommitObject, CreateCommitOptions, Edge, EdgeSchema, FangornEngine, MAX_NAMESPACE_LENGTH, MetagraphRegistry, NamespaceContents, NamespaceDiff, NamespaceID, RelationLabel, SchemaID, Vertex, VertexSchema, assertValidNamespace };
243
+ export { CommitDiff, CommitObject, CreateCommitOptions, Edge, EdgeSchema, FangornEngine, MAX_NAMESPACE_LENGTH, MetagraphRegistry, NamespaceContents, NamespaceDiff, NamespaceID, RelationLabel, SchemaID, Vertex, VertexSchema, assertValidNamespace, rootHexFromCid };
@@ -35,6 +35,16 @@ const ZERO_BYTES32 = "0x00000000000000000000000000000000000000000000000000000000
35
35
  * map block, so this is just a sanity bound keeping that block small and
36
36
  * rejecting garbage input early.
37
37
  */
38
+ /**
39
+ * The 32-byte root a commit settles to on-chain.
40
+ *
41
+ * The contract stores the commit CID's bare multihash digest — NOT a hash of the
42
+ * CID. Re-hashing here would produce a root that resolves to nothing, so this is
43
+ * the one encoding every caller must share rather than re-derive.
44
+ */
45
+ function rootHexFromCid(commitCid) {
46
+ return `0x${Buffer.from(commitCid.multihash.digest).toString("hex")}`;
47
+ }
38
48
  const MAX_NAMESPACE_LENGTH = 256;
39
49
  /** Reject invalid namespace names up front with a clear error. */
40
50
  function assertValidNamespace(namespace) {
@@ -114,9 +124,13 @@ var FangornEngine = class {
114
124
  putLocal(block) {
115
125
  this.cache.set(block.cid.toString(), block.bytes);
116
126
  }
117
- /** Reconstruct the publisher's current on-chain head as a commit CID, or null if none. */
118
- async resolveHeadCommit(publisher) {
119
- const currentHead = await this.registryClient.getNamespaceHead(publisher);
127
+ /**
128
+ * Reconstruct the current on-chain head of one publisher's namespace as a
129
+ * commit CID, or null if none. Each namespace is its own on-chain timeline,
130
+ * so this is per-(publisher, namespace) rather than per-publisher.
131
+ */
132
+ async resolveHeadCommit(publisher, namespace) {
133
+ const currentHead = await this.registryClient.getNamespaceHead(publisher, namespace);
120
134
  if (currentHead === ZERO_BYTES32) return null;
121
135
  return this.hexToRootCid(currentHead);
122
136
  }
@@ -128,6 +142,20 @@ var FangornEngine = class {
128
142
  for (const [ns, link] of Object.entries(raw)) rootMap[ns] = CID.parse(String(link));
129
143
  return rootMap;
130
144
  }
145
+ /**
146
+ * Which namespace a commit belongs to, read from its root map.
147
+ *
148
+ * Each namespace is its own on-chain timeline, so a commit's root map carries
149
+ * exactly one entry — which is how an app-wide subscriber recovers the
150
+ * namespace *name* from a stream that only carries `keccak256(name)`. A commit
151
+ * with several entries predates per-namespace timelines and can't be
152
+ * attributed to one.
153
+ */
154
+ async namespaceOf(rootHex) {
155
+ const names = Object.keys(await this.rootMapAt(this.hexToRootCid(rootHex)));
156
+ if (names.length !== 1) throw new Error(`commit ${rootHex} spans ${names.length.toString()} namespaces; expected exactly one`);
157
+ return names[0];
158
+ }
131
159
  /** The full vertex/edge link sets one commit records for `namespace`. */
132
160
  async treeLinksAt(commitCid, namespace) {
133
161
  const rootMap = await this.rootMapAt(commitCid);
@@ -256,21 +284,21 @@ var FangornEngine = class {
256
284
  };
257
285
  }
258
286
  /**
259
- * Settle a local commit as the publisher's on-chain head — the permissioned,
260
- * git `push` step. The commit and its CAR are already durable in storage
261
- * (createCommit uploads them), so this is just the compare-and-swap against
262
- * the current head. Unless `force`, the on-chain head must be an ancestor of
263
- * `commitCid` (fast-forward only).
287
+ * Settle a local commit as the on-chain head of one namespace — the
288
+ * permissioned, git `push` step. The commit and its CAR are already durable
289
+ * in storage (createCommit uploads them), so this is just the compare-and-swap
290
+ * against the current head. Unless `force`, the on-chain head must be an
291
+ * ancestor of `commitCid` (fast-forward only).
264
292
  */
265
- async pushCommit(publisher, commitCid, opts) {
266
- const onChainHex = await this.registryClient.getNamespaceHead(publisher);
293
+ async pushCommit(publisher, namespace, commitCid, opts) {
294
+ const onChainHex = await this.registryClient.getNamespaceHead(publisher, namespace);
267
295
  const onChainCommit = onChainHex === ZERO_BYTES32 ? null : this.hexToRootCid(onChainHex);
268
296
  if (!opts?.force && onChainCommit && onChainCommit.toString() !== commitCid.toString()) {
269
297
  if (!await this.isAncestor(onChainCommit, commitCid)) throw new Error("non-fast-forward: the on-chain tip is not an ancestor of this commit — clone/rebuild onto the current tip, or push with force.");
270
298
  }
271
- const newRootHex = `0x${Buffer.from(commitCid.multihash.digest).toString("hex")}`;
299
+ const newRootHex = rootHexFromCid(commitCid);
272
300
  return {
273
- txHash: await this.registryClient.commitStateRoot(onChainHex, newRootHex),
301
+ txHash: await this.registryClient.commitStateRoot(namespace, onChainHex, newRootHex),
274
302
  onChainTip: commitCid
275
303
  };
276
304
  }
@@ -296,6 +324,10 @@ var FangornEngine = class {
296
324
  commitCidFromRootHex(hex) {
297
325
  return this.hexToRootCid(hex).toString();
298
326
  }
327
+ /** Inverse of `commitCidFromRootHex`: the on-chain root hex for a commit CID. */
328
+ rootHexFromCommitCid(commitCid) {
329
+ return rootHexFromCid(CID.parse(commitCid));
330
+ }
299
331
  /** Decode a vertex block into the public read shape. */
300
332
  async decodeVertex(link, hint) {
301
333
  const decoded = decodeBlock(await this.getBlock(link, hint));
@@ -398,15 +430,15 @@ var FangornEngine = class {
398
430
  removed: removed.sort()
399
431
  };
400
432
  }
401
- /** Does this namespace exist under the publisher's on-chain root yet? */
433
+ /** Has this publisher committed to this namespace on-chain yet? */
402
434
  async namespaceExists(namespace, publisher) {
403
- const head = await this.resolveHeadCommit(publisher);
435
+ const head = await this.resolveHeadCommit(publisher, namespace);
404
436
  if (!head) return false;
405
437
  return namespace in await this.rootMapAt(head);
406
438
  }
407
439
  /** Lists every vertex and edge currently committed under a namespace, resolved from the on-chain root. */
408
440
  async listNamespace(namespace, publisher) {
409
- const head = await this.resolveHeadCommit(publisher);
441
+ const head = await this.resolveHeadCommit(publisher, namespace);
410
442
  if (!head) return {
411
443
  vertices: [],
412
444
  edges: []
@@ -426,12 +458,12 @@ var FangornEngine = class {
426
458
  * CAR chain (vertex blocks are not individually pinned — they live inside
427
459
  * commit CARs).
428
460
  */
429
- async readVertex(cid, publisher) {
430
- const head = await this.resolveHeadCommit(publisher);
431
- if (!head) throw new Error(`publisher ${publisher} has no on-chain commits`);
461
+ async readVertex(cid, publisher, namespace) {
462
+ const head = await this.resolveHeadCommit(publisher, namespace);
463
+ if (!head) throw new Error(`publisher ${publisher} has no on-chain commits in namespace ${namespace}`);
432
464
  return this.decodeVertex(CID.parse(cid), head);
433
465
  }
434
466
  };
435
467
 
436
468
  //#endregion
437
- export { FangornEngine, MAX_NAMESPACE_LENGTH, MetagraphRegistry, assertValidNamespace };
469
+ export { FangornEngine, MAX_NAMESPACE_LENGTH, MetagraphRegistry, assertValidNamespace, rootHexFromCid };
@@ -32,11 +32,14 @@ declare class MemStorage implements MetadataStorage {
32
32
  get<T>(_uri: string): Promise<T>;
33
33
  delete(_uri: string): Promise<void>;
34
34
  }
35
- /** Registry stub holding one publisher head in memory (enough for the engine). */
35
+ /**
36
+ * Registry stub holding one head per namespace in memory (enough for the
37
+ * engine) — mirroring the contract, where each namespace is its own timeline.
38
+ */
36
39
  declare class StubRegistry {
37
- head: string;
38
- getNamespaceHead(_publisher: string): Promise<string>;
39
- commitStateRoot(_oldRoot: Hex, newRoot: Hex): Promise<Hex>;
40
+ heads: Map<string, `0x${string}`>;
41
+ getNamespaceHead(_publisher: string, namespace: string): Promise<Hex>;
42
+ commitStateRoot(namespace: string, _oldRoot: Hex, newRoot: Hex): Promise<Hex>;
40
43
  asClient(): DataRegistryClient;
41
44
  }
42
45
  //#endregion
@@ -50,14 +50,17 @@ var MemStorage = class {
50
50
  return Promise.resolve();
51
51
  }
52
52
  };
53
- /** Registry stub holding one publisher head in memory (enough for the engine). */
53
+ /**
54
+ * Registry stub holding one head per namespace in memory (enough for the
55
+ * engine) — mirroring the contract, where each namespace is its own timeline.
56
+ */
54
57
  var StubRegistry = class {
55
- head = ZERO_BYTES32;
56
- getNamespaceHead(_publisher) {
57
- return Promise.resolve(this.head);
58
+ heads = /* @__PURE__ */ new Map();
59
+ getNamespaceHead(_publisher, namespace) {
60
+ return Promise.resolve(this.heads.get(namespace) ?? ZERO_BYTES32);
58
61
  }
59
- commitStateRoot(_oldRoot, newRoot) {
60
- this.head = newRoot;
62
+ commitStateRoot(namespace, _oldRoot, newRoot) {
63
+ this.heads.set(namespace, newRoot);
61
64
  return Promise.resolve("0xdeadbeef");
62
65
  }
63
66
  asClient() {
package/lib/fangorn.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { AppConfig } from "./config.js";
2
- import { DataRegistryClient } from "./contracts/data-registry/index.js";
2
+ import { DataRegistryClient, PreparedTx } from "./contracts/data-registry/index.js";
3
3
  import "./contracts/index.js";
4
4
  import { MetadataStorage } from "./providers/storage/types.js";
5
5
  import { CommitDiff, FangornEngine, MetagraphRegistry, NamespaceContents, NamespaceDiff } from "./engine/index.js";
6
- import { FangornCreateOptions } from "./types/index.js";
6
+ import { FangornCreateOptions, StorageConfig } from "./types/index.js";
7
7
  import { Hex, WalletClient } from "viem";
8
8
 
9
9
  //#region src/fangorn.d.ts
@@ -21,7 +21,7 @@ interface NamespaceChange extends NamespaceDiff {
21
21
  }
22
22
  interface SubscribeOptions {
23
23
  namespace: string;
24
- /** Publisher whose root to watch (defaults to this wallet's address). */
24
+ /** Publisher whose namespace to watch (defaults to this wallet's address). */
25
25
  owner?: Hex;
26
26
  /**
27
27
  * Replay `StateCommitted` events from this block before going live (the resume
@@ -42,7 +42,7 @@ declare class Fangorn {
42
42
  get engine(): FangornEngine;
43
43
  get metagraph(): MetagraphRegistry;
44
44
  static create(options: FangornCreateOptions): Fangorn;
45
- reset(): Promise<void>;
45
+ reset(namespace: string): Promise<void>;
46
46
  /**
47
47
  * Register permissive (no required fields) schemas for every tag/relation a
48
48
  * staged graph uses, unless the caller has already registered stricter rules
@@ -122,15 +122,60 @@ declare class Fangorn {
122
122
  root: string;
123
123
  vertexCids: Record<string, string>;
124
124
  }>;
125
- /** Settle a local commit as the on-chain head (fast-forward unless `force`). */
126
- push(commitCid: string, opts?: {
125
+ /**
126
+ * Build a commit on top of `owner`'s current on-chain head and return it
127
+ * together with the UNSIGNED settlement tx — the self-custodial publish path.
128
+ *
129
+ * `commit` + `push` assume the caller holds the publishing key. A relay server
130
+ * doesn't: it builds and pins the commit with a keyless service wallet, then
131
+ * hands the tx to the user's browser wallet to sign. The head only moves when
132
+ * the publisher themselves signs, so the server is never a trusted party.
133
+ *
134
+ * Nothing is on-chain when this returns. The commit and its CAR are durable in
135
+ * storage, so if the user never signs, the result is an orphaned commit — junk
136
+ * in IPFS, not a corrupted timeline.
137
+ */
138
+ prepareCommit(opts: {
139
+ /** The publisher whose head advances — NOT this client's service wallet. */
140
+ owner: Hex;
141
+ namespace: string;
142
+ vertices?: {
143
+ id: string;
144
+ tag: string;
145
+ payload: unknown;
146
+ }[];
147
+ edges?: {
148
+ rel: string;
149
+ from: string;
150
+ to: string;
151
+ }[];
152
+ message: string;
153
+ /** Snapshot semantics: the staged graph replaces the namespace's contents. */
154
+ replace?: boolean;
155
+ }): Promise<{
156
+ commitCid: string;
157
+ oldRoot: Hex;
158
+ newRoot: Hex;
159
+ staged: {
160
+ vertices: number;
161
+ edges: number;
162
+ };
163
+ tx: PreparedTx;
164
+ vertexCids: Record<string, string>;
165
+ }>;
166
+ /**
167
+ * Settle a local commit as the on-chain head of `namespace` (fast-forward
168
+ * unless `force`). Each namespace has its own timeline, so pushes to
169
+ * different namespaces never contend for the same compare-and-swap.
170
+ */
171
+ push(namespace: string, commitCid: string, opts?: {
127
172
  force?: boolean;
128
173
  }): Promise<{
129
174
  txHash: string;
130
175
  onChainTip: string;
131
176
  }>;
132
- /** The owner's current on-chain tip commit CID, or null if they have no commits. */
133
- onChainTip(owner: Hex): Promise<string | null>;
177
+ /** The owner's current on-chain tip for a namespace, or null if they have none. */
178
+ onChainTip(owner: Hex, namespace: string): Promise<string | null>;
134
179
  /** Walk commit history from `head` (newest first). */
135
180
  log(head: string, max?: number): AsyncGenerator<{
136
181
  cid: string;
@@ -148,17 +193,42 @@ declare class Fangorn {
148
193
  */
149
194
  inspectNamespace(repoName: string): Promise<NamespaceContents>;
150
195
  /**
151
- * Stream namespace-scoped changes as the on-chain root advances. Optionally
152
- * replays from `fromBlock` first (resume cursor), then watches live until the
153
- * `signal` aborts. Each yielded change carries `blockNumber` — persist it to
154
- * resume exactly where you left off.
196
+ * Stream changes to one publisher's namespace. Optionally replays from
197
+ * `fromBlock` first (resume cursor), then watches live until the `signal`
198
+ * aborts. Each yielded change carries `blockNumber` — persist it to resume
199
+ * exactly where you left off.
155
200
  */
156
201
  subscribe(opts: SubscribeOptions): AsyncGenerator<NamespaceChange>;
157
- /** Resolve a raw `StateCommitted` log into a namespace change, or null if the namespace slice was untouched. */
202
+ /**
203
+ * Stream changes across the *whole app* — every publisher, every namespace.
204
+ * The app-level feed: one topic filter on `app_id`, no per-publisher fan-out
205
+ * and no global root to keep in sync. Narrow it with `namespace` (every
206
+ * publisher's `docs`) or `owner` (one publisher's whole app footprint).
207
+ */
208
+ subscribeApp(opts?: Omit<SubscribeOptions, "namespace"> & {
209
+ namespace?: string;
210
+ }): AsyncGenerator<NamespaceChange>;
211
+ /** Shared catch-up-then-live loop behind `subscribe` and `subscribeApp`. */
212
+ private streamCommits;
213
+ /**
214
+ * Resolve a raw `StateCommitted` log into a namespace change, or null if the
215
+ * namespace slice was untouched.
216
+ *
217
+ * The event carries `keccak256(namespace)`, never the name, so the name comes
218
+ * from the commit's own root map — and must hash back to the subspace the
219
+ * commit was published into, or the publisher settled a commit for one
220
+ * namespace against another's timeline.
221
+ */
158
222
  private toNamespaceChange;
159
223
  getConfig(): AppConfig;
160
224
  getDataRegistry(): DataRegistryClient;
161
225
  getStorage(): MetadataStorage;
226
+ /**
227
+ * Swap the storage backend at runtime — e.g. move from access-worker signed
228
+ * URLs to your own Pinata JWT (or back). Rebuilds the engine on next use so it
229
+ * picks up the new backend.
230
+ */
231
+ setStorage(storage: StorageConfig): void;
162
232
  getWalletClient(): WalletClient;
163
233
  getAddress(): Hex;
164
234
  }