@fangorn-network/sdk 2026.9.22-dev → 2026.10.1-9.0-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 (91) hide show
  1. package/README.md +190 -194
  2. package/lib/cli/cli.d.ts +0 -0
  3. package/lib/cli/cli.js +177 -201
  4. package/lib/cli/index.d.ts +3 -91
  5. package/lib/cli/index.js +5 -2
  6. package/lib/config.d.ts +358 -117
  7. package/lib/config.js +43 -23
  8. package/lib/contracts/app-registry/abi.d.ts +1033 -277
  9. package/lib/contracts/app-registry/abi.js +1214 -335
  10. package/lib/contracts/app-registry/index.d.ts +105 -65
  11. package/lib/contracts/app-registry/index.js +182 -175
  12. package/lib/contracts/data-registry/abi.d.ts +166 -187
  13. package/lib/contracts/data-registry/abi.js +207 -237
  14. package/lib/contracts/data-registry/index.d.ts +4 -151
  15. package/lib/contracts/data-registry/index.js +5 -279
  16. package/lib/contracts/index.d.ts +6 -7
  17. package/lib/contracts/index.js +6 -8
  18. package/lib/contracts/logs.d.ts +4 -1
  19. package/lib/contracts/logs.js +9 -2
  20. package/lib/contracts/membership-registry/abi.d.ts +1145 -0
  21. package/lib/contracts/membership-registry/abi.js +1333 -0
  22. package/lib/contracts/membership-registry/index.d.ts +79 -0
  23. package/lib/contracts/membership-registry/index.js +104 -0
  24. package/lib/contracts/types.d.ts +3 -1
  25. package/lib/contracts/types.js +3 -1
  26. package/lib/contracts/write.d.ts +34 -2
  27. package/lib/contracts/write.js +48 -1
  28. package/lib/crypto/encryption.js +4 -4
  29. package/lib/engine/car.d.ts +2 -2
  30. package/lib/engine/car.js +10 -5
  31. package/lib/engine/index.d.ts +99 -143
  32. package/lib/engine/index.js +156 -295
  33. package/lib/engine/records.d.ts +110 -0
  34. package/lib/engine/records.js +165 -0
  35. package/lib/engine/test-helpers.d.ts +7 -14
  36. package/lib/engine/test-helpers.js +7 -18
  37. package/lib/fangorn.d.ts +129 -156
  38. package/lib/fangorn.js +250 -250
  39. package/lib/feed.d.ts +7 -4
  40. package/lib/feed.js +4 -1
  41. package/lib/index.d.ts +9 -9
  42. package/lib/index.js +8 -9
  43. package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
  44. package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
  45. package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
  46. package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
  47. package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
  48. package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
  49. package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
  50. package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
  51. package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
  52. package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
  53. package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
  54. package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
  55. package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
  56. package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
  57. package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
  58. package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
  59. package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
  60. package/lib/node_modules/chai/index.js +2874 -0
  61. package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
  62. package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
  63. package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
  64. package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
  65. package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
  66. package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
  67. package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
  68. package/lib/providers/storage/signed-url.d.ts +16 -6
  69. package/lib/providers/storage/signed-url.js +20 -8
  70. package/lib/registry.d.ts +116 -0
  71. package/lib/registry.js +182 -0
  72. package/lib/test/mem-registry.d.ts +51 -0
  73. package/lib/test/mem-registry.js +107 -0
  74. package/lib/test/sdk-suite.d.ts +13 -0
  75. package/lib/test/sdk-suite.js +226 -0
  76. package/lib/test/testbed.d.ts +16411 -61
  77. package/lib/test/testbed.js +190 -84
  78. package/lib/types/index.d.ts +13 -11
  79. package/package.json +89 -73
  80. package/lib/contracts/data-registry/test.d.ts +0 -1
  81. package/lib/contracts/data-registry/test.js +0 -1
  82. package/lib/contracts/settlement-registry/abi.d.ts +0 -466
  83. package/lib/contracts/settlement-registry/abi.js +0 -535
  84. package/lib/contracts/settlement-registry/index.d.ts +0 -163
  85. package/lib/contracts/settlement-registry/index.js +0 -264
  86. package/lib/contracts/subscription-registry/abi.d.ts +0 -179
  87. package/lib/contracts/subscription-registry/abi.js +0 -195
  88. package/lib/contracts/subscription-registry/index.d.ts +0 -59
  89. package/lib/contracts/subscription-registry/index.js +0 -116
  90. package/lib/engine/graph.d.ts +0 -70
  91. package/lib/engine/graph.js +0 -73
@@ -0,0 +1,110 @@
1
+ import { CID } from "multiformats/cid";
2
+
3
+ //#region src/engine/records.d.ts
4
+ /** A content-addressed IPLD block. */
5
+ interface Block {
6
+ cid: CID;
7
+ bytes: Uint8Array;
8
+ }
9
+ /** Encode any value as canonical dag-cbor and derive its CIDv1 (sha256). */
10
+ declare function encodeBlock(obj: unknown): Promise<Block>;
11
+ /**
12
+ * Decode a dag-cbor block. The decoder is strict (minimal encodings, sorted
13
+ * map keys), so a block has one accepted byte form (FOUNDATIONS P1).
14
+ */
15
+ declare function decodeBlock<T>(bytes: Uint8Array): T;
16
+ /** Throw unless `bytes` hash to `cid`. Hosts are untrusted; every fetched block goes through this. */
17
+ declare function assertBlockMatches(cid: CID, bytes: Uint8Array): void;
18
+ /** Record block: one version of an entity. `id` is repeated so a record can't be filed under another key. */
19
+ interface RecordBlock {
20
+ id: string;
21
+ type: string;
22
+ payload: Record<string, unknown>;
23
+ }
24
+ /**
25
+ * A reference from one record to another (FOUNDATIONS §2.2), stored as a plain
26
+ * payload value. `local`: a record id in the same repo state, follows
27
+ * updates. `pinned`: one exact record version, by CID.
28
+ * ponytail: no `live` (cross-repo) refs and no resolver yet — add with
29
+ * the §10 graph extension.
30
+ */
31
+ type Ref = {
32
+ local: string;
33
+ } | {
34
+ pinned: string;
35
+ };
36
+ /**
37
+ * Commit block: γ = (e, parent, root) of WHITEPAPER_1 §3.1, whose name is the
38
+ * on-chain head. `root` is the repo's MST root.
39
+ *
40
+ * Three more fields, until the registry computes heads from (e, parent, root)
41
+ * itself (docs/REGISTRY_SPEC.md):
42
+ * • `repo`: the repo's name, which events carry only as keccak256(name);
43
+ * • `message`: a commit note;
44
+ * • `car`: the opaque storage URI of the CAR file holding every block this
45
+ * commit introduced. The commit block itself is stored OUTSIDE that CAR
46
+ * (individually addressable), so a reader goes digest → commit → CAR with
47
+ * no index lookups.
48
+ */
49
+ interface CommitBlock {
50
+ epoch: number;
51
+ parent: CID | null;
52
+ root: CID;
53
+ repo: string;
54
+ message: string;
55
+ car: string;
56
+ }
57
+ /** Upper bound on a record id's length. */
58
+ declare const MAX_RECORD_ID_LENGTH = 256;
59
+ /**
60
+ * Record ids are printable ASCII, so JS string order is byte order and every
61
+ * implementation sorts MST keys the same way. Ids of sealed data should be
62
+ * random (FOUNDATIONS P7): a guessable id lets an observer test what exists.
63
+ */
64
+ declare function assertValidRecordId(id: string): void;
65
+ /** b: expected fanout 2^b = 16. */
66
+ declare const MST_BITS = 4;
67
+ interface MstEntry {
68
+ key: string;
69
+ value: CID;
70
+ /** Subtree of keys between this key and the next one. */
71
+ right: CID | null;
72
+ }
73
+ interface MstNode {
74
+ level: number;
75
+ /** Subtree of keys before the first entry. */
76
+ left: CID | null;
77
+ entries: MstEntry[];
78
+ }
79
+ /** level(k) = ⌊leading zero bits of sha256(k) / b⌋ */
80
+ declare function keyLevel(key: string): number;
81
+ /**
82
+ * Build the MST for a state. Returns the root and every node block; callers
83
+ * diff node CIDs against the parent's to find what to upload.
84
+ * ponytail: rebuilds the whole tree per commit (O(n) hashing, same as the
85
+ * page model it replaced); update only the changed paths if large repos
86
+ * make commits CPU-bound.
87
+ */
88
+ declare function buildMst(state: Map<string, CID>): Promise<{
89
+ root: CID;
90
+ nodes: Block[];
91
+ }>;
92
+ /**
93
+ * Read a whole MST back into its state, rejecting any tree that isn't exactly
94
+ * what `buildMst` makes of that state (MSTCheck, FOUNDATIONS P2): rebuilding
95
+ * and comparing roots catches duplicate, misordered, misplaced and wrong-level
96
+ * keys in one check.
97
+ */
98
+ declare function readMst(root: CID, getBlock: (cid: CID) => Promise<Uint8Array>): Promise<{
99
+ state: Map<string, CID>;
100
+ nodeCids: Set<string>;
101
+ }>;
102
+ /**
103
+ * The record CID stored under `key`, or null if absent — one node per level.
104
+ * ponytail: trusts node ordering on the path, so a malformed tree can hide a
105
+ * record (never forge one: the caller checks the record's id). Run `readMst`
106
+ * first where absence must be proven.
107
+ */
108
+ declare function lookupMst(root: CID, key: string, getBlock: (cid: CID) => Promise<Uint8Array>): Promise<CID | null>;
109
+ //#endregion
110
+ export { Block, CommitBlock, MAX_RECORD_ID_LENGTH, MST_BITS, MstEntry, MstNode, RecordBlock, Ref, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, keyLevel, lookupMst, readMst };
@@ -0,0 +1,165 @@
1
+ import { CID } from "multiformats/cid";
2
+ import * as dagCbor from "@ipld/dag-cbor";
3
+ import { createHash } from "crypto";
4
+ import { sha256 } from "multiformats/hashes/sha2";
5
+
6
+ //#region src/engine/records.ts
7
+ /** Encode any value as canonical dag-cbor and derive its CIDv1 (sha256). */
8
+ async function encodeBlock(obj) {
9
+ const bytes = dagCbor.encode(obj);
10
+ const hash = await sha256.digest(bytes);
11
+ return {
12
+ cid: CID.createV1(dagCbor.code, hash),
13
+ bytes
14
+ };
15
+ }
16
+ /**
17
+ * Decode a dag-cbor block. The decoder is strict (minimal encodings, sorted
18
+ * map keys), so a block has one accepted byte form (FOUNDATIONS P1).
19
+ */
20
+ function decodeBlock(bytes) {
21
+ return dagCbor.decode(bytes);
22
+ }
23
+ /** Throw unless `bytes` hash to `cid`. Hosts are untrusted; every fetched block goes through this. */
24
+ function assertBlockMatches(cid, bytes) {
25
+ const digest = createHash("sha256").update(bytes).digest();
26
+ if (!Buffer.from(cid.multihash.digest).equals(digest)) throw new Error(`block ${cid.toString()} does not match its content hash`);
27
+ }
28
+ /** Upper bound on a record id's length. */
29
+ const MAX_RECORD_ID_LENGTH = 256;
30
+ /**
31
+ * Record ids are printable ASCII, so JS string order is byte order and every
32
+ * implementation sorts MST keys the same way. Ids of sealed data should be
33
+ * random (FOUNDATIONS P7): a guessable id lets an observer test what exists.
34
+ */
35
+ function assertValidRecordId(id) {
36
+ if (typeof id !== "string" || id.length === 0 || id.length > MAX_RECORD_ID_LENGTH || !/^[\x21-\x7e]+$/.test(id)) throw new Error(`invalid record id ${JSON.stringify(id)}: must be 1-${MAX_RECORD_ID_LENGTH.toString()} printable ASCII characters, no spaces`);
37
+ }
38
+ /** b: expected fanout 2^b = 16. */
39
+ const MST_BITS = 4;
40
+ /** Levels come from a 256-bit hash, so no honest tree is deeper (FOUNDATIONS P22). */
41
+ const MAX_DEPTH = 256 / MST_BITS + 1;
42
+ /** level(k) = ⌊leading zero bits of sha256(k) / b⌋ */
43
+ function keyLevel(key) {
44
+ const digest = createHash("sha256").update(key).digest();
45
+ let zeros = 0;
46
+ for (const byte of digest) {
47
+ if (byte !== 0) {
48
+ zeros += Math.clz32(byte) - 24;
49
+ break;
50
+ }
51
+ zeros += 8;
52
+ }
53
+ return Math.floor(zeros / MST_BITS);
54
+ }
55
+ /**
56
+ * Build the MST for a state. Returns the root and every node block; callers
57
+ * diff node CIDs against the parent's to find what to upload.
58
+ * ponytail: rebuilds the whole tree per commit (O(n) hashing, same as the
59
+ * page model it replaced); update only the changed paths if large repos
60
+ * make commits CPU-bound.
61
+ */
62
+ async function buildMst(state) {
63
+ const items = [...state].map(([key, value]) => ({
64
+ key,
65
+ value,
66
+ level: keyLevel(key)
67
+ })).sort((a, b) => a.key < b.key ? -1 : 1);
68
+ const nodes = [];
69
+ const build = async (slice, level) => {
70
+ if (slice.length === 0) return null;
71
+ const node = {
72
+ level,
73
+ left: null,
74
+ entries: []
75
+ };
76
+ let gap = [];
77
+ const closeGap = async () => {
78
+ const child = level === 0 ? null : await build(gap, level - 1);
79
+ const last = node.entries.at(-1);
80
+ if (last) last.right = child;
81
+ else node.left = child;
82
+ gap = [];
83
+ };
84
+ for (const item of slice) {
85
+ if (item.level !== level) {
86
+ gap.push(item);
87
+ continue;
88
+ }
89
+ await closeGap();
90
+ node.entries.push({
91
+ key: item.key,
92
+ value: item.value,
93
+ right: null
94
+ });
95
+ }
96
+ await closeGap();
97
+ const block = await encodeBlock(node);
98
+ nodes.push(block);
99
+ return block.cid;
100
+ };
101
+ let root = await build(items, items.reduce((max, i) => Math.max(max, i.level), 0));
102
+ if (!root) {
103
+ const empty = await encodeBlock({
104
+ level: 0,
105
+ left: null,
106
+ entries: []
107
+ });
108
+ nodes.push(empty);
109
+ root = empty.cid;
110
+ }
111
+ return {
112
+ root,
113
+ nodes
114
+ };
115
+ }
116
+ /**
117
+ * Read a whole MST back into its state, rejecting any tree that isn't exactly
118
+ * what `buildMst` makes of that state (MSTCheck, FOUNDATIONS P2): rebuilding
119
+ * and comparing roots catches duplicate, misordered, misplaced and wrong-level
120
+ * keys in one check.
121
+ */
122
+ async function readMst(root, getBlock) {
123
+ const state = /* @__PURE__ */ new Map();
124
+ const nodeCids = /* @__PURE__ */ new Set();
125
+ const walk = async (cid, depth) => {
126
+ if (depth > MAX_DEPTH) throw new Error(`MST at ${root.toString()} is deeper than ${MAX_DEPTH.toString()}`);
127
+ nodeCids.add(cid.toString());
128
+ const node = decodeBlock(await getBlock(cid));
129
+ if (!Array.isArray(node.entries)) throw new Error(`${cid.toString()} is not a record tree node — graph-era data (vertices/edges) is no longer readable. Reset the on-chain head (\`fangorn reset\`) and re-publish.`);
130
+ if (node.left) await walk(node.left, depth + 1);
131
+ for (const e of node.entries) {
132
+ state.set(e.key, e.value);
133
+ if (e.right) await walk(e.right, depth + 1);
134
+ }
135
+ };
136
+ await walk(root, 0);
137
+ if ((await buildMst(state)).root.toString() !== root.toString()) throw new Error(`MST at ${root.toString()} is not canonical`);
138
+ return {
139
+ state,
140
+ nodeCids
141
+ };
142
+ }
143
+ /**
144
+ * The record CID stored under `key`, or null if absent — one node per level.
145
+ * ponytail: trusts node ordering on the path, so a malformed tree can hide a
146
+ * record (never forge one: the caller checks the record's id). Run `readMst`
147
+ * first where absence must be proven.
148
+ */
149
+ async function lookupMst(root, key, getBlock) {
150
+ let cursor = root;
151
+ for (let depth = 0; cursor && depth <= MAX_DEPTH; depth++) {
152
+ const node = decodeBlock(await getBlock(cursor));
153
+ let next = node.left;
154
+ for (const e of node.entries) {
155
+ if (e.key === key) return e.value;
156
+ if (e.key > key) break;
157
+ next = e.right;
158
+ }
159
+ cursor = next;
160
+ }
161
+ return null;
162
+ }
163
+
164
+ //#endregion
165
+ export { MAX_RECORD_ID_LENGTH, MST_BITS, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, keyLevel, lookupMst, readMst };
@@ -1,13 +1,12 @@
1
- import { DataRegistryClient } from "../contracts/data-registry/index.js";
2
- import "../contracts/index.js";
3
1
  import { MetadataStorage, RawBlock, StorageMeta } from "../providers/storage/types.js";
2
+ import { MemRegistry } from "../test/mem-registry.js";
4
3
  import { Hex } from "viem";
5
4
  import { CID } from "multiformats/cid";
6
5
 
7
6
  //#region src/engine/test-helpers.d.ts
8
7
  declare const ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
9
8
  /** The raw 32-byte root hex a commit CID settles to on-chain (bare sha256 digest). */
10
- declare const rootHex: (commitCid: CID) => string;
9
+ declare const rootHex: (commitCid: CID) => `0x${string}`;
11
10
  /**
12
11
  * In-memory MetadataStorage: individually-pinned blocks + opaque files, with
13
12
  * write/read counters — remote writes are the publish cost driver, so tests
@@ -32,15 +31,9 @@ declare class MemStorage implements MetadataStorage {
32
31
  get<T>(_uri: string): Promise<T>;
33
32
  delete(_uri: string): Promise<void>;
34
33
  }
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
- */
39
- declare class StubRegistry {
40
- heads: Map<string, `0x${string}`>;
41
- getNamespaceHead(_publisher: string, namespace: string): Promise<Hex>;
42
- commitStateRoot(namespace: string, _oldRoot: Hex, newRoot: Hex): Promise<Hex>;
43
- asClient(): DataRegistryClient;
44
- }
34
+ /** The paper's registry in memory, signing as PUBLISHER. */
35
+ declare const PUBLISHER: Hex;
36
+ declare const APP: Hex;
37
+ declare const newRegistry: () => MemRegistry;
45
38
  //#endregion
46
- export { MemStorage, StubRegistry, ZERO_BYTES32, rootHex };
39
+ export { APP, MemStorage, PUBLISHER, ZERO_BYTES32, newRegistry, rootHex };
@@ -1,3 +1,5 @@
1
+ import { MemRegistry } from "../test/mem-registry.js";
2
+
1
3
  //#region src/engine/test-helpers.ts
2
4
  const ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
3
5
  /** The raw 32-byte root hex a commit CID settles to on-chain (bare sha256 digest). */
@@ -50,23 +52,10 @@ var MemStorage = class {
50
52
  return Promise.resolve();
51
53
  }
52
54
  };
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
- */
57
- var StubRegistry = class {
58
- heads = /* @__PURE__ */ new Map();
59
- getNamespaceHead(_publisher, namespace) {
60
- return Promise.resolve(this.heads.get(namespace) ?? ZERO_BYTES32);
61
- }
62
- commitStateRoot(namespace, _oldRoot, newRoot) {
63
- this.heads.set(namespace, newRoot);
64
- return Promise.resolve("0xdeadbeef");
65
- }
66
- asClient() {
67
- return this;
68
- }
69
- };
55
+ /** The paper's registry in memory, signing as PUBLISHER. */
56
+ const PUBLISHER = "0x0000000000000000000000000000000000000001";
57
+ const APP = `0x${"a".repeat(64)}`;
58
+ const newRegistry = () => new MemRegistry(PUBLISHER);
70
59
 
71
60
  //#endregion
72
- export { MemStorage, StubRegistry, ZERO_BYTES32, rootHex };
61
+ export { APP, MemStorage, PUBLISHER, ZERO_BYTES32, newRegistry, rootHex };