@fangorn-network/sdk 2026.7.27 → 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.
- package/README.md +163 -8
- package/lib/cli/cli.js +16 -11
- package/lib/cli/index.d.ts +13 -13
- package/lib/config.d.ts +86 -79
- package/lib/config.js +11 -2
- package/lib/contracts/data-registry/abi.d.ts +80 -2
- package/lib/contracts/data-registry/abi.js +107 -12
- package/lib/contracts/data-registry/index.d.ts +91 -19
- package/lib/contracts/data-registry/index.js +153 -24
- package/lib/contracts/index.d.ts +2 -2
- package/lib/contracts/index.js +2 -2
- package/lib/engine/index.d.ts +36 -12
- package/lib/engine/index.js +51 -19
- package/lib/engine/test-helpers.d.ts +7 -4
- package/lib/engine/test-helpers.js +9 -6
- package/lib/fangorn.d.ts +76 -12
- package/lib/fangorn.js +102 -32
- package/lib/index.d.ts +7 -2
- package/lib/index.js +7 -2
- package/lib/test/testbed.d.ts +21 -3
- package/lib/test/testbed.js +38 -7
- package/package.json +1 -1
package/lib/engine/index.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
147
|
-
|
|
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
|
|
169
|
-
* git `push` step. The commit and its CAR are already durable
|
|
170
|
-
* (createCommit uploads them), so this is just the compare-and-swap
|
|
171
|
-
* the current head. Unless `force`, the on-chain head must be an
|
|
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
|
-
/**
|
|
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 };
|
package/lib/engine/index.js
CHANGED
|
@@ -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
|
-
/**
|
|
118
|
-
|
|
119
|
-
|
|
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
|
|
260
|
-
* git `push` step. The commit and its CAR are already durable
|
|
261
|
-
* (createCommit uploads them), so this is just the compare-and-swap
|
|
262
|
-
* the current head. Unless `force`, the on-chain head must be an
|
|
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 =
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
|
|
38
|
-
getNamespaceHead(_publisher: string): Promise<
|
|
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
|
-
/**
|
|
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
|
-
|
|
56
|
-
getNamespaceHead(_publisher) {
|
|
57
|
-
return Promise.resolve(this.
|
|
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.
|
|
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,5 +1,5 @@
|
|
|
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";
|
|
@@ -21,7 +21,7 @@ interface NamespaceChange extends NamespaceDiff {
|
|
|
21
21
|
}
|
|
22
22
|
interface SubscribeOptions {
|
|
23
23
|
namespace: string;
|
|
24
|
-
/** Publisher whose
|
|
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
|
-
/**
|
|
126
|
-
|
|
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
|
|
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,13 +193,32 @@ declare class Fangorn {
|
|
|
148
193
|
*/
|
|
149
194
|
inspectNamespace(repoName: string): Promise<NamespaceContents>;
|
|
150
195
|
/**
|
|
151
|
-
* Stream
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
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
|
-
/**
|
|
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;
|
package/lib/fangorn.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { FangornConfig } from "./config.js";
|
|
2
2
|
import { SignedUrlBackend } from "./providers/storage/signed-url.js";
|
|
3
3
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
4
|
-
import { FangornEngine, MetagraphRegistry, assertValidNamespace } from "./engine/index.js";
|
|
5
|
-
import { DataRegistryClient } from "./contracts/data-registry/index.js";
|
|
4
|
+
import { FangornEngine, MetagraphRegistry, assertValidNamespace, rootHexFromCid } from "./engine/index.js";
|
|
5
|
+
import { DataRegistryClient, subspaceId } from "./contracts/data-registry/index.js";
|
|
6
|
+
import "./contracts/index.js";
|
|
6
7
|
import { createPublicClient, createWalletClient, http } from "viem";
|
|
7
8
|
import { privateKeyToAccount } from "viem/accounts";
|
|
8
9
|
import { CID } from "multiformats/cid";
|
|
@@ -57,14 +58,14 @@ var Fangorn = class Fangorn {
|
|
|
57
58
|
walletClient,
|
|
58
59
|
metadataStorage,
|
|
59
60
|
domain,
|
|
60
|
-
dataRegistry: new DataRegistryClient(resolvedConfig.dataRegistryContractAddress, publicClient, walletClient),
|
|
61
|
+
dataRegistry: new DataRegistryClient(resolvedConfig.dataRegistryContractAddress, resolvedConfig.appId, publicClient, walletClient),
|
|
61
62
|
config: resolvedConfig
|
|
62
63
|
});
|
|
63
64
|
}
|
|
64
|
-
async reset() {
|
|
65
|
+
async reset(namespace) {
|
|
65
66
|
const registry = this.getDataRegistry();
|
|
66
|
-
const currentRoot = await registry.getNamespaceHead(this.getAddress());
|
|
67
|
-
await registry.commitStateRoot(currentRoot, ZERO_BYTES32);
|
|
67
|
+
const currentRoot = await registry.getNamespaceHead(this.getAddress(), namespace);
|
|
68
|
+
await registry.commitStateRoot(namespace, currentRoot, ZERO_BYTES32);
|
|
68
69
|
}
|
|
69
70
|
/**
|
|
70
71
|
* Register permissive (no required fields) schemas for every tag/relation a
|
|
@@ -128,7 +129,7 @@ var Fangorn = class Fangorn {
|
|
|
128
129
|
txHash: void 0,
|
|
129
130
|
alreadyInitialized: true
|
|
130
131
|
};
|
|
131
|
-
const head = await this.engine.resolveHeadCommit(publisher);
|
|
132
|
+
const head = await this.engine.resolveHeadCommit(publisher, repoName);
|
|
132
133
|
const { commitCid } = await this.createCommit({
|
|
133
134
|
namespace: repoName,
|
|
134
135
|
base: head,
|
|
@@ -136,7 +137,7 @@ var Fangorn = class Fangorn {
|
|
|
136
137
|
edges: [],
|
|
137
138
|
message: `Initialize repository workspace: ${repoName}`
|
|
138
139
|
});
|
|
139
|
-
const { txHash } = await this.engine.pushCommit(publisher, commitCid);
|
|
140
|
+
const { txHash } = await this.engine.pushCommit(publisher, repoName, commitCid);
|
|
140
141
|
return {
|
|
141
142
|
cid: commitCid.toString(),
|
|
142
143
|
commitCid: commitCid.toString(),
|
|
@@ -149,7 +150,7 @@ var Fangorn = class Fangorn {
|
|
|
149
150
|
*/
|
|
150
151
|
async upload(repoName, payload, schemaId) {
|
|
151
152
|
const publisher = this.getAddress();
|
|
152
|
-
const head = await this.engine.resolveHeadCommit(publisher);
|
|
153
|
+
const head = await this.engine.resolveHeadCommit(publisher, repoName);
|
|
153
154
|
if (!head) throw new Error(`Repository '${repoName}' does not exist. Call initRepo() prior to uploading.`);
|
|
154
155
|
const { commitCid, vertexCids } = await this.createCommit({
|
|
155
156
|
namespace: repoName,
|
|
@@ -162,7 +163,7 @@ var Fangorn = class Fangorn {
|
|
|
162
163
|
edges: [],
|
|
163
164
|
message: `Upload to namespace ${repoName} under schema ${schemaId}`
|
|
164
165
|
});
|
|
165
|
-
const { txHash } = await this.engine.pushCommit(publisher, commitCid);
|
|
166
|
+
const { txHash } = await this.engine.pushCommit(publisher, repoName, commitCid);
|
|
166
167
|
return {
|
|
167
168
|
payloadCid: vertexCids.v0,
|
|
168
169
|
commitCid: commitCid.toString(),
|
|
@@ -179,7 +180,7 @@ var Fangorn = class Fangorn {
|
|
|
179
180
|
assertValidNamespace(repoName);
|
|
180
181
|
if (vertices.length === 0 && edges.length === 0) throw new Error("uploadBatch requires at least one vertex or edge");
|
|
181
182
|
const publisher = this.getAddress();
|
|
182
|
-
const head = await this.engine.resolveHeadCommit(publisher);
|
|
183
|
+
const head = await this.engine.resolveHeadCommit(publisher, repoName);
|
|
183
184
|
if (!head) throw new Error(`Repository '${repoName}' does not exist. Call initRepo() prior to uploading.`);
|
|
184
185
|
const { commitCid, vertexCids } = await this.createCommit({
|
|
185
186
|
namespace: repoName,
|
|
@@ -188,7 +189,7 @@ var Fangorn = class Fangorn {
|
|
|
188
189
|
edges,
|
|
189
190
|
message: `Batch upload to namespace ${repoName}`
|
|
190
191
|
});
|
|
191
|
-
const { txHash } = await this.engine.pushCommit(publisher, commitCid);
|
|
192
|
+
const { txHash } = await this.engine.pushCommit(publisher, repoName, commitCid);
|
|
192
193
|
return {
|
|
193
194
|
commitCid: commitCid.toString(),
|
|
194
195
|
txHash,
|
|
@@ -219,17 +220,59 @@ var Fangorn = class Fangorn {
|
|
|
219
220
|
vertexCids
|
|
220
221
|
};
|
|
221
222
|
}
|
|
222
|
-
/**
|
|
223
|
-
|
|
224
|
-
|
|
223
|
+
/**
|
|
224
|
+
* Build a commit on top of `owner`'s current on-chain head and return it
|
|
225
|
+
* together with the UNSIGNED settlement tx — the self-custodial publish path.
|
|
226
|
+
*
|
|
227
|
+
* `commit` + `push` assume the caller holds the publishing key. A relay server
|
|
228
|
+
* doesn't: it builds and pins the commit with a keyless service wallet, then
|
|
229
|
+
* hands the tx to the user's browser wallet to sign. The head only moves when
|
|
230
|
+
* the publisher themselves signs, so the server is never a trusted party.
|
|
231
|
+
*
|
|
232
|
+
* Nothing is on-chain when this returns. The commit and its CAR are durable in
|
|
233
|
+
* storage, so if the user never signs, the result is an orphaned commit — junk
|
|
234
|
+
* in IPFS, not a corrupted timeline.
|
|
235
|
+
*/
|
|
236
|
+
async prepareCommit(opts) {
|
|
237
|
+
assertValidNamespace(opts.namespace);
|
|
238
|
+
const registry = this.getDataRegistry();
|
|
239
|
+
const oldRoot = await registry.getNamespaceHead(opts.owner, opts.namespace);
|
|
240
|
+
const { commitCid, vertexCids } = await this.createCommit({
|
|
241
|
+
namespace: opts.namespace,
|
|
242
|
+
base: oldRoot === ZERO_BYTES32 ? null : CID.parse(this.engine.commitCidFromRootHex(oldRoot)),
|
|
243
|
+
vertices: opts.vertices ?? [],
|
|
244
|
+
edges: opts.edges ?? [],
|
|
245
|
+
message: opts.message,
|
|
246
|
+
replace: opts.replace
|
|
247
|
+
});
|
|
248
|
+
const newRoot = rootHexFromCid(commitCid);
|
|
249
|
+
return {
|
|
250
|
+
commitCid: commitCid.toString(),
|
|
251
|
+
oldRoot,
|
|
252
|
+
newRoot,
|
|
253
|
+
staged: {
|
|
254
|
+
vertices: opts.vertices?.length ?? 0,
|
|
255
|
+
edges: opts.edges?.length ?? 0
|
|
256
|
+
},
|
|
257
|
+
tx: await registry.prepareCommitStateRoot(opts.owner, opts.namespace, oldRoot, newRoot),
|
|
258
|
+
vertexCids
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Settle a local commit as the on-chain head of `namespace` (fast-forward
|
|
263
|
+
* unless `force`). Each namespace has its own timeline, so pushes to
|
|
264
|
+
* different namespaces never contend for the same compare-and-swap.
|
|
265
|
+
*/
|
|
266
|
+
async push(namespace, commitCid, opts) {
|
|
267
|
+
const { txHash, onChainTip } = await this.engine.pushCommit(this.getAddress(), namespace, CID.parse(commitCid), opts);
|
|
225
268
|
return {
|
|
226
269
|
txHash,
|
|
227
270
|
onChainTip: onChainTip.toString()
|
|
228
271
|
};
|
|
229
272
|
}
|
|
230
|
-
/** The owner's current on-chain tip
|
|
231
|
-
async onChainTip(owner) {
|
|
232
|
-
const cid = await this.engine.resolveHeadCommit(owner);
|
|
273
|
+
/** The owner's current on-chain tip for a namespace, or null if they have none. */
|
|
274
|
+
async onChainTip(owner, namespace) {
|
|
275
|
+
const cid = await this.engine.resolveHeadCommit(owner, namespace);
|
|
233
276
|
return cid ? cid.toString() : null;
|
|
234
277
|
}
|
|
235
278
|
/** Walk commit history from `head` (newest first). */
|
|
@@ -255,14 +298,31 @@ var Fangorn = class Fangorn {
|
|
|
255
298
|
return this.engine.listNamespace(repoName, this.getAddress());
|
|
256
299
|
}
|
|
257
300
|
/**
|
|
258
|
-
* Stream
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
301
|
+
* Stream changes to one publisher's namespace. Optionally replays from
|
|
302
|
+
* `fromBlock` first (resume cursor), then watches live until the `signal`
|
|
303
|
+
* aborts. Each yielded change carries `blockNumber` — persist it to resume
|
|
304
|
+
* exactly where you left off.
|
|
262
305
|
*/
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
306
|
+
subscribe(opts) {
|
|
307
|
+
return this.streamCommits({
|
|
308
|
+
publisher: opts.owner ?? this.getAddress(),
|
|
309
|
+
namespace: opts.namespace
|
|
310
|
+
}, opts);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Stream changes across the *whole app* — every publisher, every namespace.
|
|
314
|
+
* The app-level feed: one topic filter on `app_id`, no per-publisher fan-out
|
|
315
|
+
* and no global root to keep in sync. Narrow it with `namespace` (every
|
|
316
|
+
* publisher's `docs`) or `owner` (one publisher's whole app footprint).
|
|
317
|
+
*/
|
|
318
|
+
subscribeApp(opts = {}) {
|
|
319
|
+
return this.streamCommits({
|
|
320
|
+
publisher: opts.owner,
|
|
321
|
+
namespace: opts.namespace
|
|
322
|
+
}, opts);
|
|
323
|
+
}
|
|
324
|
+
/** Shared catch-up-then-live loop behind `subscribe` and `subscribeApp`. */
|
|
325
|
+
async *streamCommits(filter, opts) {
|
|
266
326
|
const registry = this.getDataRegistry();
|
|
267
327
|
const seen = /* @__PURE__ */ new Set();
|
|
268
328
|
const buffer = [];
|
|
@@ -276,7 +336,7 @@ var Fangorn = class Fangorn {
|
|
|
276
336
|
wake = null;
|
|
277
337
|
w?.();
|
|
278
338
|
};
|
|
279
|
-
const unwatch = registry.watchStateCommitted(
|
|
339
|
+
const unwatch = registry.watchStateCommitted(filter, (log) => {
|
|
280
340
|
buffer.push(log);
|
|
281
341
|
signal();
|
|
282
342
|
}, (err) => {
|
|
@@ -289,10 +349,10 @@ var Fangorn = class Fangorn {
|
|
|
289
349
|
};
|
|
290
350
|
opts.signal?.addEventListener("abort", onAbort);
|
|
291
351
|
try {
|
|
292
|
-
if (opts.fromBlock !== void 0) for (const log of await registry.getStateCommittedLogs(
|
|
352
|
+
if (opts.fromBlock !== void 0) for (const log of await registry.getStateCommittedLogs(filter, opts.fromBlock)) {
|
|
293
353
|
if (seen.has(log.logId)) continue;
|
|
294
354
|
seen.add(log.logId);
|
|
295
|
-
const change = await this.toNamespaceChange(log
|
|
355
|
+
const change = await this.toNamespaceChange(log);
|
|
296
356
|
if (change) yield change;
|
|
297
357
|
}
|
|
298
358
|
while (!subState.stopped) {
|
|
@@ -307,7 +367,7 @@ var Fangorn = class Fangorn {
|
|
|
307
367
|
if (!log) continue;
|
|
308
368
|
if (seen.has(log.logId)) continue;
|
|
309
369
|
seen.add(log.logId);
|
|
310
|
-
const change = await this.toNamespaceChange(log
|
|
370
|
+
const change = await this.toNamespaceChange(log);
|
|
311
371
|
if (change) yield change;
|
|
312
372
|
}
|
|
313
373
|
} finally {
|
|
@@ -315,13 +375,23 @@ var Fangorn = class Fangorn {
|
|
|
315
375
|
opts.signal?.removeEventListener("abort", onAbort);
|
|
316
376
|
}
|
|
317
377
|
}
|
|
318
|
-
/**
|
|
319
|
-
|
|
378
|
+
/**
|
|
379
|
+
* Resolve a raw `StateCommitted` log into a namespace change, or null if the
|
|
380
|
+
* namespace slice was untouched.
|
|
381
|
+
*
|
|
382
|
+
* The event carries `keccak256(namespace)`, never the name, so the name comes
|
|
383
|
+
* from the commit's own root map — and must hash back to the subspace the
|
|
384
|
+
* commit was published into, or the publisher settled a commit for one
|
|
385
|
+
* namespace against another's timeline.
|
|
386
|
+
*/
|
|
387
|
+
async toNamespaceChange(log) {
|
|
388
|
+
const namespace = await this.engine.namespaceOf(log.newRoot);
|
|
389
|
+
if (subspaceId(namespace) !== log.subspaceId) throw new Error(`commit ${log.newRoot} declares namespace "${namespace}", which does not match the subspace it was committed to (${log.subspaceId})`);
|
|
320
390
|
const diff = await this.engine.namespaceDiff(log.oldRoot === ZERO_BYTES32 ? null : log.oldRoot, log.newRoot, namespace);
|
|
321
391
|
if (!(diff.addedVertices.length || diff.removedVertexCids.length || diff.addedEdges.length || diff.removedEdges.length)) return null;
|
|
322
392
|
return {
|
|
323
393
|
namespace,
|
|
324
|
-
owner,
|
|
394
|
+
owner: log.publisher,
|
|
325
395
|
commitCid: this.engine.commitCidFromRootHex(log.newRoot),
|
|
326
396
|
oldRoot: log.oldRoot,
|
|
327
397
|
newRoot: log.newRoot,
|