@fangorn-network/sdk 2026.9.22-dev → 2026.10.19-dev

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.
Files changed (89) hide show
  1. package/README.md +177 -192
  2. package/lib/cli/cli.d.ts +0 -0
  3. package/lib/cli/cli.js +165 -194
  4. package/lib/config.d.ts +32 -27
  5. package/lib/config.js +17 -7
  6. package/lib/contracts/app-registry/abi.d.ts +1033 -277
  7. package/lib/contracts/app-registry/abi.js +1214 -335
  8. package/lib/contracts/app-registry/index.d.ts +105 -65
  9. package/lib/contracts/app-registry/index.js +182 -175
  10. package/lib/contracts/data-registry/abi.d.ts +166 -187
  11. package/lib/contracts/data-registry/abi.js +207 -237
  12. package/lib/contracts/data-registry/index.d.ts +4 -151
  13. package/lib/contracts/data-registry/index.js +5 -279
  14. package/lib/contracts/index.d.ts +6 -7
  15. package/lib/contracts/index.js +6 -8
  16. package/lib/contracts/logs.d.ts +4 -1
  17. package/lib/contracts/logs.js +9 -2
  18. package/lib/contracts/membership-registry/abi.d.ts +1145 -0
  19. package/lib/contracts/membership-registry/abi.js +1333 -0
  20. package/lib/contracts/membership-registry/index.d.ts +79 -0
  21. package/lib/contracts/membership-registry/index.js +104 -0
  22. package/lib/contracts/types.d.ts +3 -1
  23. package/lib/contracts/types.js +3 -1
  24. package/lib/contracts/write.d.ts +34 -2
  25. package/lib/contracts/write.js +48 -1
  26. package/lib/crypto/encryption.js +4 -4
  27. package/lib/engine/car.d.ts +2 -2
  28. package/lib/engine/car.js +10 -5
  29. package/lib/engine/index.d.ts +99 -143
  30. package/lib/engine/index.js +156 -295
  31. package/lib/engine/records.d.ts +110 -0
  32. package/lib/engine/records.js +165 -0
  33. package/lib/engine/test-helpers.d.ts +7 -14
  34. package/lib/engine/test-helpers.js +7 -18
  35. package/lib/fangorn.d.ts +129 -156
  36. package/lib/fangorn.js +250 -250
  37. package/lib/feed.d.ts +7 -4
  38. package/lib/feed.js +4 -1
  39. package/lib/index.d.ts +9 -9
  40. package/lib/index.js +8 -9
  41. package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
  42. package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
  43. package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
  44. package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
  45. package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
  46. package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
  47. package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
  48. package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
  49. package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
  50. package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
  51. package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
  52. package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
  53. package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
  54. package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
  55. package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
  56. package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
  57. package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
  58. package/lib/node_modules/chai/index.js +2874 -0
  59. package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
  60. package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
  61. package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
  62. package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
  63. package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
  64. package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
  65. package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
  66. package/lib/providers/storage/signed-url.d.ts +16 -6
  67. package/lib/providers/storage/signed-url.js +20 -8
  68. package/lib/registry.d.ts +116 -0
  69. package/lib/registry.js +182 -0
  70. package/lib/test/mem-registry.d.ts +51 -0
  71. package/lib/test/mem-registry.js +107 -0
  72. package/lib/test/sdk-suite.d.ts +13 -0
  73. package/lib/test/sdk-suite.js +226 -0
  74. package/lib/test/testbed.d.ts +16411 -61
  75. package/lib/test/testbed.js +190 -84
  76. package/lib/types/index.d.ts +13 -11
  77. package/package.json +89 -73
  78. package/lib/contracts/data-registry/test.d.ts +0 -1
  79. package/lib/contracts/data-registry/test.js +0 -1
  80. package/lib/contracts/settlement-registry/abi.d.ts +0 -466
  81. package/lib/contracts/settlement-registry/abi.js +0 -535
  82. package/lib/contracts/settlement-registry/index.d.ts +0 -163
  83. package/lib/contracts/settlement-registry/index.js +0 -264
  84. package/lib/contracts/subscription-registry/abi.d.ts +0 -179
  85. package/lib/contracts/subscription-registry/abi.js +0 -195
  86. package/lib/contracts/subscription-registry/index.d.ts +0 -59
  87. package/lib/contracts/subscription-registry/index.js +0 -116
  88. package/lib/engine/graph.d.ts +0 -70
  89. package/lib/engine/graph.js +0 -73
@@ -1,129 +1,97 @@
1
- import { DataRegistryClient } from "../contracts/data-registry/index.js";
2
- import "../contracts/index.js";
1
+ import { CommitBlock, Ref } from "./records.js";
2
+ import { Registry } from "../registry.js";
3
3
  import { MetadataStorage } from "../providers/storage/types.js";
4
4
  import { Address, Hash, Hex } from "viem";
5
5
  import { CID } from "multiformats/cid";
6
6
 
7
7
  //#region src/engine/index.d.ts
8
- type SchemaID = string;
9
- type RelationLabel = string;
10
- type NamespaceID = string;
11
- interface Vertex {
12
- schemaId: SchemaID;
8
+ type RecordType = string;
9
+ type RepoName = string;
10
+ /** A staged (typed) record */
11
+ interface RecordInput {
12
+ id?: string;
13
+ type: RecordType;
13
14
  payload: Record<string, unknown>;
14
15
  }
15
- interface Edge {
16
- sourceCid: string;
17
- relation: RelationLabel;
18
- targetCid: string;
16
+ /** A published record */
17
+ interface StoredRecord {
18
+ id: string;
19
+ cid: string;
20
+ type: RecordType;
21
+ payload: Record<string, unknown>;
19
22
  }
20
- interface VertexSchema {
21
- id: SchemaID;
23
+ interface RecordSchema {
24
+ type: RecordType;
22
25
  requiredFields: string[];
23
26
  }
24
- interface EdgeSchema {
25
- sourceSchema: SchemaID;
26
- relation: RelationLabel;
27
- targetSchema: SchemaID;
28
- }
29
- /** A decoded commit — the git-like history object the on-chain digest points at. */
30
- interface CommitObject {
31
- /** Link to the publisher's root map (namespace → tree). */
32
- root: CID;
33
- parents: CID[];
34
- timestamp: number;
35
- message: string;
36
- /** Opaque storage URI of the CAR file holding the blocks this commit introduced. */
37
- car: string;
27
+ /** A decoded commit, γ = (e, parent, root) plus its repo name, message and CAR (see CommitBlock). */
28
+ type CommitObject = CommitBlock;
29
+ interface RepoContents {
30
+ records: StoredRecord[];
38
31
  }
39
- interface NamespaceContents {
40
- vertices: {
41
- cid: string;
42
- schemaId: SchemaID;
43
- payload: Record<string, unknown>;
44
- }[];
45
- edges: Edge[];
46
- }
47
- /** Namespace diff between a namespace's state graph */
48
- interface NamespaceDiff {
49
- addedVertices: NamespaceContents["vertices"];
50
- addedEdges: Edge[];
51
- removedVertexCids: string[];
52
- removedEdges: Edge[];
32
+ /** Net change to a repo between two roots. */
33
+ interface RepoDiff {
34
+ /** Records that are new, or whose content changed (same id, new cid). */
35
+ puts: StoredRecord[];
36
+ /** Ids of records that are gone. */
37
+ deletes: string[];
53
38
  }
54
39
  /**
55
- * The Metagraph registry is a class for building and validating knowledge graphs
56
- *
57
- * The vertices each represent a unique piece of data, while the edges
58
- * represent the relationships that connect them into a graph.
59
- *
40
+ * Per-repo record schemas. Types without a registered schema are
41
+ * accepted as-is (permissive by default); register one to require fields.
60
42
  */
61
- declare class MetagraphRegistry {
62
- private vertexSchemas;
63
- private edgeSchemas;
64
- registerVertex(namespace: NamespaceID, schema: VertexSchema): void;
65
- hasVertexSchema(namespace: NamespaceID, schemaId: SchemaID): boolean;
66
- registerEdge(namespace: NamespaceID, rule: EdgeSchema): void;
67
- hasEdgeSchema(namespace: NamespaceID, sourceSchema: SchemaID, relation: RelationLabel, targetSchema: SchemaID): boolean;
68
- validateVertex(namespace: NamespaceID, schemaId: SchemaID, payload: Record<string, unknown>): boolean;
69
- validateEdge(namespace: NamespaceID, sourceSchema: SchemaID, relation: RelationLabel, targetSchema: SchemaID): boolean;
43
+ declare class SchemaRegistry {
44
+ private schemas;
45
+ register(repo: RepoName, schema: RecordSchema): void;
46
+ validate(repo: RepoName, type: RecordType, payload: Record<string, unknown>): boolean;
70
47
  }
71
48
  /**
72
- * Upper bound on a namespace name's length. Namespaces are keys of the root
73
- * map block, so this is just a sanity bound keeping that block small and
49
+ * Upper bound on a repo name's length. The name is stored in every commit
50
+ * block, so this is just a sanity bound keeping that block small and
74
51
  * rejecting garbage input early.
75
52
  */
76
- declare const MAX_NAMESPACE_LENGTH = 256;
53
+ declare const MAX_REPO_NAME_LENGTH = 256;
77
54
  /**
78
55
  * The root of the graph, committed to IPFS, as hex
79
56
  */
80
57
  declare function rootHexFromCid(commitCid: CID): Hex;
81
- /** Namespaces must be non-empty strings whose length does not exceed MAX_NAMESPACE_LENGTH */
82
- declare function assertValidNamespace(namespace: NamespaceID): void;
83
- /** One line of a commit's diff against its first parent. */
58
+ /** Repos must be non-empty strings whose length does not exceed MAX_REPO_NAME_LENGTH */
59
+ declare function assertValidRepoName(repo: RepoName): void;
60
+ /** One line of a commit's diff against its parent. */
84
61
  interface CommitDiff {
85
62
  message: string;
86
- parents: string[];
87
- timestamp: number;
63
+ parent: string | null;
64
+ epoch: number;
65
+ repo: string;
88
66
  root: string;
89
- /** namespaced entries (`<ns>/v/<cid>`, `<ns>/e/<cid>`) new vs. the parent. */
67
+ /** `<ns>/<id>` of records new vs. the parent. */
90
68
  added: string[];
91
- /** namespaced entries present in the parent but gone in this commit. */
69
+ /** `<ns>/<id>` of records whose content changed. */
70
+ updated: string[];
71
+ /** `<ns>/<id>` of records present in the parent but gone in this commit. */
92
72
  removed: string[];
93
73
  }
94
- /** Input to `createCommit`: a graph staged under one namespace. */
74
+ /** Input to `createCommit`: puts and deletes, by record id, under one repo. */
95
75
  interface CreateCommitOptions {
96
- namespace: NamespaceID;
76
+ repo: RepoName;
97
77
  /** Parent commit this builds on (local HEAD or on-chain tip), or null for a root commit. */
98
78
  base: CID | null;
99
- /** Vertices keyed by a caller-local id, so edges can reference them. */
100
- vertices: {
101
- id: string;
102
- schemaId: SchemaID;
103
- payload: Record<string, unknown>;
104
- }[];
105
- /**
106
- * Edges between vertices. Endpoints are local ids of vertices staged in
107
- * this same call, or full CIDs of vertices committed earlier (CIDs are
108
- * content-addressed and stable across commits).
109
- */
110
- edges: {
111
- relation: RelationLabel;
112
- from: string;
113
- to: string;
114
- }[];
79
+ /** Records to put. A put replaces any record with the same id. */
80
+ records: RecordInput[];
81
+ /** Ids of records to remove. Applied before `records`. */
82
+ deletes?: string[];
115
83
  message: string;
116
84
  /**
117
- * When true, the staged graph REPLACES the namespace's previous contents
118
- * (a snapshot commit — anything omitted is removed). Default is additive:
119
- * new state = parent state ∪ staged.
85
+ * When true, the staged records REPLACE the repo's previous contents
86
+ * (a snapshot commit — anything omitted is removed). Default: puts and
87
+ * deletes apply on top of the parent state.
120
88
  */
121
89
  replace?: boolean;
122
90
  }
123
91
  declare class FangornEngine {
124
92
  private storage;
125
- private metagraph;
126
- private registryClient;
93
+ private schemas;
94
+ private registry;
127
95
  /**
128
96
  * Local block cache. Written blocks land here immediately; remote blocks
129
97
  * are pulled in whole-CAR-at-a-time by `loadCar`. Content addressing makes
@@ -132,7 +100,7 @@ declare class FangornEngine {
132
100
  private cache;
133
101
  /** Commits whose CAR has already been loaded into the cache. */
134
102
  private loadedCars;
135
- constructor(storage: MetadataStorage, metagraph: MetagraphRegistry, registryClient: DataRegistryClient);
103
+ constructor(storage: MetadataStorage, schemas: SchemaRegistry, registry: Registry);
136
104
  /**
137
105
  * Reconstructs the CID a commit's raw digest hex refers to. The contract only
138
106
  * stores the bare 32-byte sha256 digest (see commitStateRoot), so rebuilding
@@ -151,95 +119,83 @@ declare class FangornEngine {
151
119
  private getBlock;
152
120
  private putLocal;
153
121
  /**
154
- * Reconstruct the current on-chain head of one publisher's namespace as a
155
- * commit CID, or null if none. Each namespace is its own on-chain timeline,
156
- * so this is per-(publisher, namespace) rather than per-publisher.
122
+ * Reconstruct the current on-chain head of one publisher's repo as a
123
+ * commit CID, or null if none. Each repo is its own on-chain timeline,
124
+ * so this is per-(publisher, repo) rather than per-publisher.
157
125
  */
158
- resolveHeadCommit(publisher: Address, namespace: NamespaceID): Promise<CID | null>;
159
- /** Decode the root map behind a commit. */
160
- private rootMapAt;
126
+ resolveHeadCommit(publisher: Address, repo: RepoName): Promise<CID | null>;
161
127
  /**
162
- * Which namespace a commit belongs to, read from its root map.
163
- *
164
- * Each namespace is its own on-chain timeline, so a commit's root map carries
165
- * exactly one entry — which is how an app-wide subscriber recovers the
166
- * namespace *name* from a stream that only carries `keccak256(name)`. A commit
167
- * with several entries predates per-namespace timelines and can't be
168
- * attributed to one.
128
+ * Which repo a commit belongs to. Events carry only `keccak256(name)`, so
129
+ * this is how an app-wide subscriber recovers the name.
169
130
  */
170
- namespaceOf(rootHex: Hex): Promise<NamespaceID>;
171
- /** The full vertex/edge link sets one commit records for `namespace`. */
172
- private treeLinksAt;
131
+ repoOf(rootHex: Hex): Promise<RepoName>;
132
+ /** The record state a commit holds. */
133
+ private stateAt;
173
134
  /**
174
- * TODO: revisit the storage model
175
- * Q: what if CAR is not supported?
176
- *
177
135
  * Build a new commit on top of `base` — entirely in memory, then persisted
178
136
  * as exactly TWO uploads: one CAR file holding every block this commit
179
137
  * introduced (git's packfile), and the small commit block itself (stored
180
138
  * individually so the on-chain digest resolves without any index).
181
139
  *
182
- * The new namespace state is the parent state plus the staged graph;
183
- * unchanged blocks are shared byte-for-byte with ancestors and are NOT
140
+ * Unchanged records and MST nodes re-derive their old CIDs and are NOT
184
141
  * re-uploaded. Does not touch the chain — `pushCommit` settles it.
185
142
  */
186
143
  createCommit(opts: CreateCommitOptions): Promise<{
187
144
  commitCid: CID;
188
145
  root: CID;
189
- vertexCids: Record<string, string>;
146
+ /** id → record CID for every record staged (including generated ids). */
147
+ recordCids: Record<string, string>;
190
148
  }>;
191
149
  /**
192
- * Settle a local commit as the on-chain head of one namespace — the
193
- * permissioned, git `push` step. The commit and its CAR are already durable
194
- * in storage (createCommit uploads them), so this is just the compare-and-swap
195
- * against the current head. Unless `force`, the on-chain head must be an
196
- * ancestor of `commitCid` (fast-forward only).
150
+ * Settle a local commit as the on-chain head of one repo — the `update` of
151
+ * WHITEPAPER_1 §3.2. The commit and its CAR are already durable in storage
152
+ * (createCommit uploads them), so this is just the compare-and-swap.
153
+ *
154
+ * The commit's parent must BE the current head, not merely an ancestor of
155
+ * it: then the parent chain and the registry's log are one history
156
+ * (Theorem 3). Today's contract only checks the old root, so the parent is
157
+ * checked here.
197
158
  */
198
- pushCommit(publisher: Address, namespace: NamespaceID, commitCid: CID, opts?: {
199
- force?: boolean;
200
- }): Promise<{
159
+ pushCommit(publisher: Address, repo: RepoName, commitCid: CID): Promise<{
201
160
  txHash: Hash;
202
161
  onChainTip: CID;
203
162
  }>;
204
- /** Walk the first-parent chain from `tip` toward the root, newest first. */
163
+ /** Walk the parent chain from `tip` toward the root, newest first. */
205
164
  walkParents(tip: CID, max?: number): AsyncGenerator<{
206
165
  cid: CID;
207
166
  commit: CommitObject;
208
167
  }>;
209
- /** True if `ancestor` appears on the first-parent chain of `tip`. */
210
- private isAncestor;
211
168
  /** The commit CID string a raw on-chain root hex points at. */
212
169
  commitCidFromRootHex(hex: string): string;
213
170
  /** Inverse of `commitCidFromRootHex`: the on-chain root hex for a commit CID. */
214
171
  rootHexFromCommitCid(commitCid: string): Hex;
215
- /** Decode a vertex block into the public read shape. */
216
- private decodeVertex;
217
- /** Decode an edge block into the public read shape. */
218
- private decodeEdge;
172
+ /** Decode a record block, checking it is filed under its own id. */
173
+ private decodeRecord;
219
174
  /**
220
- * Net change to a single namespace between two on-chain roots — the core of
221
- * the subscription light-client. Namespace trees are explicit link sets, so
222
- * the diff is a plain set difference; only blocks that actually changed are
223
- * decoded (each side resolves through its own commit's CAR chain).
175
+ * Net change to a single repo between two on-chain roots — the core of
176
+ * the subscription light-client. A plain comparison of the two id → CID
177
+ * maps; only records that actually changed are decoded.
224
178
  *
225
179
  * Diffing root-to-root (rather than walking commits) is deliberately robust:
226
180
  * it yields the correct net delta even across a force-push / non-fast-forward
227
- * reorg — exactly what an index builder needs (which vertices to (re)embed,
228
- * which to evict).
181
+ * reorg — exactly what an index builder needs (what to (re)embed, what to evict).
182
+ * ponytail: reads both whole trees; skip subtrees with equal CIDs if diffs
183
+ * of large repos get slow.
229
184
  */
230
- namespaceDiff(oldRootHex: string | null, newRootHex: string, namespace: NamespaceID): Promise<NamespaceDiff>;
231
- /** Diff a commit against its first parent, across every namespace. */
185
+ repoDiff(oldRootHex: string | null, newRootHex: string): Promise<RepoDiff>;
186
+ /** Diff a commit against its parent. */
232
187
  diffCommit(commitCid: CID): Promise<CommitDiff>;
233
- /** check if the namespace already exists (head resolves) */
234
- namespaceExists(namespace: NamespaceID, publisher: Address): Promise<boolean>;
235
- /** Lists every vertex and edge currently committed under a namespace, resolved from the on-chain root. */
236
- listNamespace(namespace: NamespaceID, publisher: Address): Promise<NamespaceContents>;
188
+ /** check if the repo already exists (head resolves) */
189
+ repoExists(publisher: Address, repo: RepoName): Promise<boolean>;
190
+ /** Lists every record currently committed under a repo, resolved from the on-chain root. */
191
+ listRepo(publisher: Address, repo: RepoName): Promise<RepoContents>;
192
+ /** Every record in the state a commit holds; none for no commit. */
193
+ listAt(head: CID | null): Promise<RepoContents>;
237
194
  /**
238
- * Fetch a single vertex by CID, resolving through the publisher's commit
239
- * CAR chain (vertex blocks are not individually pinned — they live inside
240
- * commit CARs).
195
+ * Fetch the current version of one record by id — one MST path from the
196
+ * on-chain root, then the record block. Null if the repo has no such id.
241
197
  */
242
- readVertex(cid: string, publisher: Address, namespace: NamespaceID): Promise<NamespaceContents["vertices"][number]>;
198
+ readRecord(publisher: Address, repo: RepoName, id: string): Promise<StoredRecord | null>;
243
199
  }
244
200
  //#endregion
245
- export { CommitDiff, CommitObject, CreateCommitOptions, Edge, EdgeSchema, FangornEngine, MAX_NAMESPACE_LENGTH, MetagraphRegistry, NamespaceContents, NamespaceDiff, NamespaceID, RelationLabel, SchemaID, Vertex, VertexSchema, assertValidNamespace, rootHexFromCid };
201
+ export { CommitDiff, CommitObject, CreateCommitOptions, FangornEngine, MAX_REPO_NAME_LENGTH, RecordInput, RecordSchema, RecordType, type Ref, RepoContents, RepoDiff, RepoName, SchemaRegistry, StoredRecord, assertValidRepoName, rootHexFromCid };