@fangorn-network/sdk 0.1.0-dev → 0.1.0

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 (96) hide show
  1. package/lib/cli/cli.js +328 -128
  2. package/lib/cli/index.d.ts +4 -1
  3. package/lib/cli/index.js +20 -2
  4. package/lib/config.d.ts +111 -48
  5. package/lib/config.js +39 -11
  6. package/lib/contracts/app-registry/abi.d.ts +1265 -0
  7. package/lib/contracts/app-registry/abi.js +1454 -0
  8. package/lib/contracts/app-registry/index.d.ts +161 -0
  9. package/lib/contracts/app-registry/index.js +274 -0
  10. package/lib/contracts/data-registry/abi.d.ts +19 -195
  11. package/lib/contracts/data-registry/abi.js +206 -409
  12. package/lib/contracts/data-registry/index.d.ts +2 -126
  13. package/lib/contracts/data-registry/index.js +1 -174
  14. package/lib/contracts/index.d.ts +7 -4
  15. package/lib/contracts/index.js +7 -4
  16. package/lib/contracts/logs.d.ts +1 -1
  17. package/lib/contracts/logs.js +1 -1
  18. package/lib/contracts/{settlement-registry → membership-registry}/abi.d.ts +636 -301
  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 +10 -0
  23. package/lib/contracts/types.js +12 -0
  24. package/lib/crypto/aes.d.ts +2 -2
  25. package/lib/crypto/aes.js +4 -4
  26. package/lib/crypto/encryption.d.ts +109 -8
  27. package/lib/crypto/encryption.js +114 -16
  28. package/lib/engine/car.d.ts +1 -5
  29. package/lib/engine/car.js +2 -2
  30. package/lib/engine/index.d.ts +14 -33
  31. package/lib/engine/index.js +41 -98
  32. package/lib/engine/records.d.ts +8 -19
  33. package/lib/engine/records.js +7 -81
  34. package/lib/engine/test-helpers.d.ts +3 -2
  35. package/lib/engine/test-helpers.js +5 -7
  36. package/lib/erc8004.js +10 -7
  37. package/lib/fangorn.d.ts +86 -65
  38. package/lib/fangorn.js +153 -115
  39. package/lib/feed.d.ts +8 -7
  40. package/lib/feed.js +8 -7
  41. package/lib/harness/examples/hackmd/build-markdown-graph.d.ts +1 -0
  42. package/lib/harness/examples/hackmd/build-markdown-graph.js +17 -0
  43. package/lib/harness/index.d.ts +58 -0
  44. package/lib/harness/index.js +73 -0
  45. package/lib/index.d.ts +13 -10
  46. package/lib/index.js +13 -10
  47. package/lib/node_modules/{@jridgewell → .pnpm/@jridgewell_sourcemap-codec@1.5.5/node_modules/@jridgewell}/sourcemap-codec/dist/sourcemap-codec.js +1 -1
  48. package/lib/node_modules/{@vitest → .pnpm/@vitest_expect@4.0.18/node_modules/@vitest}/expect/dist/index.js +8 -8
  49. package/lib/node_modules/{@vitest → .pnpm/@vitest_pretty-format@4.0.18/node_modules/@vitest}/pretty-format/dist/index.js +2 -2
  50. package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/chunk-tasks.js +2 -2
  51. package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/index.js +4 -4
  52. package/lib/node_modules/{@vitest → .pnpm/@vitest_snapshot@4.0.18/node_modules/@vitest}/snapshot/dist/index.js +4 -4
  53. package/lib/node_modules/{@vitest → .pnpm/@vitest_spy@4.0.18/node_modules/@vitest}/spy/dist/index.js +1 -1
  54. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/chunk-_commonjsHelpers.js +1 -1
  55. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/chunk-pathe.M-eThtNZ.js +1 -1
  56. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/diff.js +3 -3
  57. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/display.js +2 -2
  58. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/error.js +2 -2
  59. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/helpers.js +1 -1
  60. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/serialize.js +1 -1
  61. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/source-map.js +1 -1
  62. package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/timers.js +1 -1
  63. package/lib/node_modules/{chai → .pnpm/chai@6.2.2/node_modules/chai}/index.js +1 -1
  64. package/lib/node_modules/{magic-string → .pnpm/magic-string@0.30.21/node_modules/magic-string}/dist/magic-string.es.js +2 -2
  65. package/lib/node_modules/{pathe → .pnpm/pathe@2.0.3/node_modules/pathe}/dist/shared/pathe.M-eThtNZ.js +1 -1
  66. package/lib/node_modules/{tinyrainbow → .pnpm/tinyrainbow@3.0.3/node_modules/tinyrainbow}/dist/index.js +1 -1
  67. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/_commonjsHelpers.D26ty3Ew.js +1 -1
  68. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/date.Bq6ZW5rf.js +1 -1
  69. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/utils.DvEY5TfP.js +2 -2
  70. package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/vi.2VT5v0um.js +11 -11
  71. package/lib/providers/storage/signed-url.d.ts +10 -2
  72. package/lib/providers/storage/signed-url.js +15 -5
  73. package/lib/providers/storage/types.d.ts +1 -1
  74. package/lib/providers/storage/utils.js +3 -5
  75. package/lib/registry.d.ts +118 -0
  76. package/lib/registry.js +184 -0
  77. package/lib/test/mem-registry.d.ts +19 -14
  78. package/lib/test/mem-registry.js +37 -44
  79. package/lib/test/sdk-suite.d.ts +4 -1
  80. package/lib/test/sdk-suite.js +89 -18
  81. package/lib/test/testbed.d.ts +5227 -5218
  82. package/lib/test/testbed.js +56 -29
  83. package/lib/types/index.d.ts +11 -6
  84. package/package.json +1 -1
  85. package/lib/cli/sell.d.ts +0 -28
  86. package/lib/cli/sell.js +0 -95
  87. package/lib/cli/stage.d.ts +0 -19
  88. package/lib/cli/stage.js +0 -94
  89. package/lib/contracts/settlement-registry/abi.js +0 -962
  90. package/lib/contracts/settlement-registry/index.d.ts +0 -143
  91. package/lib/contracts/settlement-registry/index.js +0 -213
  92. package/lib/crypto/paid.d.ts +0 -104
  93. package/lib/crypto/paid.js +0 -147
  94. package/lib/test/key-server.d.ts +0 -12
  95. package/lib/test/key-server.js +0 -30
  96. /package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/utils.js +0 -0
@@ -1,7 +1,6 @@
1
- import { CommitUpdate, Registry } from "../contracts/data-registry/index.js";
2
- import "../contracts/index.js";
3
- import { MetadataStorage } from "../providers/storage/types.js";
4
1
  import { CommitBlock, Ref } from "./records.js";
2
+ import { Registry } from "../registry.js";
3
+ import { MetadataStorage } from "../providers/storage/types.js";
5
4
  import { Address, Hash, Hex } from "viem";
6
5
  import { CID } from "multiformats/cid";
7
6
 
@@ -21,7 +20,7 @@ interface StoredRecord {
21
20
  type: RecordType;
22
21
  payload: Record<string, unknown>;
23
22
  }
24
- /** A decoded commit, γ = (e, parent, root) plus its repo name and CAR (see CommitBlock). */
23
+ /** A decoded commit, γ = (e, parent, root) plus its repo name, message and CAR (see CommitBlock). */
25
24
  type CommitObject = CommitBlock;
26
25
  interface RepoContents {
27
26
  records: StoredRecord[];
@@ -32,8 +31,6 @@ interface RepoDiff {
32
31
  puts: StoredRecord[];
33
32
  /** Ids of records that are gone. */
34
33
  deletes: string[];
35
- /** CIDs of the old versions: records deleted, or replaced by a put. An index keyed by record CID drops these. */
36
- superseded: string[];
37
34
  }
38
35
  /**
39
36
  * Upper bound on a repo name's length. The name is stored in every commit
@@ -45,22 +42,11 @@ declare const MAX_REPO_NAME_LENGTH = 256;
45
42
  * The root of the graph, committed to IPFS, as hex
46
43
  */
47
44
  declare function rootHexFromCid(commitCid: CID): Hex;
48
- /** The update that settles `commit`. */
49
- declare function commitUpdate(commit: CommitBlock): CommitUpdate;
50
- /** The head the registry computes for `u`. */
51
- declare function commitHead(u: CommitUpdate): Promise<Hex>;
52
- /** A commit's update, signed by its publisher for anyone to submit. */
53
- interface SignedUpdate {
54
- publisher: Address;
55
- update: CommitUpdate;
56
- signature: Hex;
57
- }
58
- /** The CID a storage backend returned for a file, with or without an `ipfs://` prefix. */
59
- declare function fileCid(uri: string): CID;
60
45
  /** Repos must be non-empty strings whose length does not exceed MAX_REPO_NAME_LENGTH */
61
46
  declare function assertValidRepoName(repo: RepoName): void;
62
47
  /** One line of a commit's diff against its parent. */
63
48
  interface CommitDiff {
49
+ message: string;
64
50
  parent: string | null;
65
51
  epoch: number;
66
52
  repo: string;
@@ -81,6 +67,7 @@ interface CreateCommitOptions {
81
67
  records: RecordInput[];
82
68
  /** Ids of records to remove. Applied before `records`. */
83
69
  deletes?: string[];
70
+ message: string;
84
71
  /**
85
72
  * When true, the staged records REPLACE the repo's previous contents
86
73
  * (a snapshot commit — anything omitted is removed). Default: puts and
@@ -99,12 +86,6 @@ declare class FangornEngine {
99
86
  private cache;
100
87
  /** Commits whose CAR has already been loaded into the cache. */
101
88
  private loadedCars;
102
- /**
103
- * Blocks from a loaded CAR, not yet hash-checked. A CAR holds every block its commit introduced
104
- * (the first commit of a 50k-record repo: all of them), and a diff reads a handful: each block is
105
- * checked when `getBlock` first hands it out, then moves to `cache`.
106
- */
107
- private unverified;
108
89
  constructor(storage: MetadataStorage, registry: Registry);
109
90
  /**
110
91
  * Reconstructs the CID a commit's raw digest hex refers to. The contract only
@@ -131,7 +112,7 @@ declare class FangornEngine {
131
112
  resolveHeadCommit(publisher: Address, repo: RepoName): Promise<CID | null>;
132
113
  /**
133
114
  * Which repo a commit belongs to. Events carry only `keccak256(name)`, so
134
- * this is how a subscriber to every repo recovers the name.
115
+ * this is how an app-wide subscriber recovers the name.
135
116
  */
136
117
  repoOf(rootHex: Hex): Promise<RepoName>;
137
118
  /** The record state a commit holds. */
@@ -155,16 +136,16 @@ declare class FangornEngine {
155
136
  * Settle a local commit as the on-chain head of one repo — the `update` of
156
137
  * WHITEPAPER_1 §3.2. The commit and its CAR are already durable in storage
157
138
  * (createCommit uploads them), so this is just the compare-and-swap.
139
+ *
140
+ * The commit's parent must BE the current head, not merely an ancestor of
141
+ * it: then the parent chain and the registry's log are one history
142
+ * (Theorem 3). Today's contract only checks the old root, so the parent is
143
+ * checked here.
158
144
  */
159
145
  pushCommit(publisher: Address, repo: RepoName, commitCid: CID): Promise<{
160
146
  txHash: Hash;
161
147
  onChainTip: CID;
162
148
  }>;
163
- /** `pushCommit`, signed by this registry's wallet for anyone to submit with `relayCommit`. */
164
- signCommit(publisher: Address, repo: RepoName, commitCid: CID): Promise<SignedUpdate>;
165
- relayCommit(s: SignedUpdate): Promise<Hash>;
166
- /** The update for `commitCid`, once it is one the registry will take as the next head. */
167
- settleable(publisher: Address, repo: RepoName, commitCid: CID): Promise<CommitUpdate>;
168
149
  /** Walk the parent chain from `tip` toward the root, newest first. */
169
150
  walkParents(tip: CID, max?: number): AsyncGenerator<{
170
151
  cid: CID;
@@ -184,8 +165,8 @@ declare class FangornEngine {
184
165
  * Diffing root-to-root (rather than walking commits) is deliberately robust:
185
166
  * it yields the correct net delta even across a force-push / non-fast-forward
186
167
  * reorg — exactly what an index builder needs (what to (re)embed, what to evict).
187
- * Reads only the nodes on changed paths (`diffMst`): 10 new records in a
188
- * 50k-record repo cost ~10 × depth nodes, not two 50k-key walks.
168
+ * ponytail: reads both whole trees; skip subtrees with equal CIDs if diffs
169
+ * of large repos get slow.
189
170
  */
190
171
  repoDiff(oldRootHex: string | null, newRootHex: string): Promise<RepoDiff>;
191
172
  /** Diff a commit against its parent. */
@@ -203,4 +184,4 @@ declare class FangornEngine {
203
184
  readRecord(publisher: Address, repo: RepoName, id: string): Promise<StoredRecord | null>;
204
185
  }
205
186
  //#endregion
206
- export { CommitDiff, CommitObject, CreateCommitOptions, FangornEngine, MAX_REPO_NAME_LENGTH, RecordInput, RecordType, type Ref, RepoContents, RepoDiff, RepoName, SignedUpdate, StoredRecord, assertValidRepoName, commitHead, commitUpdate, fileCid, rootHexFromCid };
187
+ export { CommitDiff, CommitObject, CreateCommitOptions, FangornEngine, MAX_REPO_NAME_LENGTH, RecordInput, RecordType, type Ref, RepoContents, RepoDiff, RepoName, StoredRecord, assertValidRepoName, rootHexFromCid };
@@ -1,10 +1,8 @@
1
- import { assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, diffMst, encodeBlock, lookupMst, readMst } from "./records.js";
1
+ import { assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, lookupMst, readMst } from "./records.js";
2
2
  import { packCar, readCar } from "./car.js";
3
- import { ZERO_HEAD } from "../contracts/data-registry/index.js";
4
- import "../contracts/index.js";
5
- import { bytesToHex, hexToBytes } from "viem";
3
+ import { ZERO_HEAD } from "../registry.js";
6
4
  import { CID } from "multiformats/cid";
7
- import * as dagCbor from "@ipld/dag-cbor";
5
+ import { randomUUID } from "crypto";
8
6
  import { sha256 } from "multiformats/hashes/sha2";
9
7
  import * as Digest from "multiformats/hashes/digest";
10
8
 
@@ -20,36 +18,7 @@ const MAX_REPO_NAME_LENGTH = 256;
20
18
  * The root of the graph, committed to IPFS, as hex
21
19
  */
22
20
  function rootHexFromCid(commitCid) {
23
- return bytesToHex(commitCid.multihash.digest);
24
- }
25
- /** The update that settles `commit`. */
26
- function commitUpdate(commit) {
27
- return {
28
- repo: commit.repo,
29
- epoch: commit.epoch,
30
- parent: commit.parent ? rootHexFromCid(commit.parent) : ZERO_HEAD,
31
- root: rootHexFromCid(commit.root),
32
- car: bytesToHex(commit.car.bytes)
33
- };
34
- }
35
- /** The head the registry computes for `u`. */
36
- async function commitHead(u) {
37
- const link = (digest) => CID.createV1(dagCbor.code, Digest.create(sha256.code, hexToBytes(digest)));
38
- return rootHexFromCid((await encodeBlock({
39
- epoch: u.epoch,
40
- parent: u.parent === ZERO_HEAD ? null : link(u.parent),
41
- root: link(u.root),
42
- repo: u.repo,
43
- car: CID.decode(hexToBytes(u.car))
44
- })).cid);
45
- }
46
- /** The CID a storage backend returned for a file, with or without an `ipfs://` prefix. */
47
- function fileCid(uri) {
48
- try {
49
- return CID.parse(uri.replace(/^ipfs:\/\//, ""));
50
- } catch {
51
- throw new Error(`storage returned ${JSON.stringify(uri)} for a CAR file; expected its CID`);
52
- }
21
+ return `0x${Buffer.from(commitCid.multihash.digest).toString("hex")}`;
53
22
  }
54
23
  /** Repos must be non-empty strings whose length does not exceed MAX_REPO_NAME_LENGTH */
55
24
  function assertValidRepoName(repo) {
@@ -69,12 +38,6 @@ var FangornEngine = class {
69
38
  cache = /* @__PURE__ */ new Map();
70
39
  /** Commits whose CAR has already been loaded into the cache. */
71
40
  loadedCars = /* @__PURE__ */ new Set();
72
- /**
73
- * Blocks from a loaded CAR, not yet hash-checked. A CAR holds every block its commit introduced
74
- * (the first commit of a 50k-record repo: all of them), and a diff reads a handful: each block is
75
- * checked when `getBlock` first hands it out, then moves to `cache`.
76
- */
77
- unverified = /* @__PURE__ */ new Map();
78
41
  constructor(storage, registry) {
79
42
  this.storage = storage;
80
43
  this.registry = registry;
@@ -86,7 +49,8 @@ var FangornEngine = class {
86
49
  * with sha256.digest() would produce an unrelated CID pointing at nothing.
87
50
  */
88
51
  hexToRootCid(hex) {
89
- const rawDigestBytes = hexToBytes(`0x${hex.startsWith("0x") ? hex.slice(2) : hex}`);
52
+ const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
53
+ const rawDigestBytes = Uint8Array.from(Buffer.from(cleanHex, "hex"));
90
54
  const multihash = Digest.create(sha256.code, rawDigestBytes);
91
55
  return CID.createV1(113, multihash);
92
56
  }
@@ -106,16 +70,14 @@ var FangornEngine = class {
106
70
  parent: decoded.parent ? CID.parse(String(decoded.parent)) : null,
107
71
  root: CID.parse(String(decoded.root)),
108
72
  repo: decoded.repo,
109
- car: CID.parse(String(decoded.car))
73
+ message: decoded.message ?? "",
74
+ car: decoded.car ?? ""
110
75
  };
111
76
  }
112
77
  /** Pull every block of `commit`'s CAR into the local cache. */
113
78
  async loadCar(commitCid, commit) {
114
- const bytes = await this.storage.getFile(commit.car.toString());
115
- for (const block of await readCar(bytes, { verify: false })) {
116
- const key = block.cid.toString();
117
- if (!this.cache.has(key)) this.unverified.set(key, block.bytes);
118
- }
79
+ const bytes = await this.storage.getFile(commit.car);
80
+ for (const block of await readCar(bytes)) this.cache.set(block.cid.toString(), block.bytes);
119
81
  this.loadedCars.add(commitCid.toString());
120
82
  }
121
83
  /**
@@ -124,24 +86,14 @@ var FangornEngine = class {
124
86
  */
125
87
  async getBlock(cid, hint) {
126
88
  const key = cid.toString();
127
- const local = () => {
128
- const hit$1 = this.cache.get(key);
129
- if (hit$1) return hit$1;
130
- const raw = this.unverified.get(key);
131
- if (!raw) return null;
132
- assertBlockMatches(cid, raw);
133
- this.unverified.delete(key);
134
- this.cache.set(key, raw);
135
- return raw;
136
- };
137
- const hit = local();
89
+ const hit = this.cache.get(key);
138
90
  if (hit) return hit;
139
91
  let cursor = hint;
140
92
  while (cursor) {
141
93
  const commit = await this.getCommit(cursor);
142
94
  if (!this.loadedCars.has(cursor.toString())) {
143
95
  await this.loadCar(cursor, commit);
144
- const found = local();
96
+ const found = this.cache.get(key);
145
97
  if (found) return found;
146
98
  }
147
99
  cursor = commit.parent;
@@ -166,7 +118,7 @@ var FangornEngine = class {
166
118
  }
167
119
  /**
168
120
  * Which repo a commit belongs to. Events carry only `keccak256(name)`, so
169
- * this is how a subscriber to every repo recovers the name.
121
+ * this is how an app-wide subscriber recovers the name.
170
122
  */
171
123
  async repoOf(rootHex) {
172
124
  return (await this.getCommit(this.hexToRootCid(rootHex))).repo;
@@ -195,7 +147,7 @@ var FangornEngine = class {
195
147
  const carBlocks = [];
196
148
  const recordCids = {};
197
149
  for (const r of opts.records) {
198
- const id = r.id ?? globalThis.crypto.randomUUID();
150
+ const id = r.id ?? randomUUID();
199
151
  assertValidRecordId(id);
200
152
  if (id in recordCids) throw new Error(`record id ${id} staged twice`);
201
153
  const block = await encodeBlock({
@@ -211,13 +163,14 @@ var FangornEngine = class {
211
163
  for (const node of mst.nodes) if (!old.nodeCids.has(node.cid.toString())) carBlocks.push(node);
212
164
  for (const block of carBlocks) this.putLocal(block);
213
165
  const carBytes = await packCar(mst.root, carBlocks);
214
- const car = fileCid(await this.storage.putFile(carBytes, `${mst.root.toString()}.car`));
166
+ const carUri = await this.storage.putFile(carBytes, `${mst.root.toString()}.car`);
215
167
  const commitBlock = await encodeBlock({
216
168
  epoch: await this.registry.epoch(),
217
169
  parent: base,
218
170
  root: mst.root,
219
171
  repo,
220
- car
172
+ message: opts.message,
173
+ car: carUri
221
174
  });
222
175
  await this.storage.putBlock(commitBlock);
223
176
  this.putLocal(commitBlock);
@@ -232,37 +185,25 @@ var FangornEngine = class {
232
185
  * Settle a local commit as the on-chain head of one repo — the `update` of
233
186
  * WHITEPAPER_1 §3.2. The commit and its CAR are already durable in storage
234
187
  * (createCommit uploads them), so this is just the compare-and-swap.
188
+ *
189
+ * The commit's parent must BE the current head, not merely an ancestor of
190
+ * it: then the parent chain and the registry's log are one history
191
+ * (Theorem 3). Today's contract only checks the old root, so the parent is
192
+ * checked here.
235
193
  */
236
194
  async pushCommit(publisher, repo, commitCid) {
237
- const update = await this.settleable(publisher, repo, commitCid);
238
- return {
239
- txHash: await this.registry.update(update),
240
- onChainTip: commitCid
241
- };
242
- }
243
- /** `pushCommit`, signed by this registry's wallet for anyone to submit with `relayCommit`. */
244
- async signCommit(publisher, repo, commitCid) {
245
- const update = await this.settleable(publisher, repo, commitCid);
246
- return {
195
+ const head = await this.registry.head({
247
196
  publisher,
248
- update,
249
- signature: await this.registry.signUpdate(update)
250
- };
251
- }
252
- relayCommit(s) {
253
- return this.registry.updateBySig(s.publisher, s.update, s.signature);
254
- }
255
- /** The update for `commitCid`, once it is one the registry will take as the next head. */
256
- async settleable(publisher, repo, commitCid) {
197
+ repo
198
+ });
257
199
  const commit = await this.getCommit(commitCid);
200
+ const parentHex = commit.parent ? rootHexFromCid(commit.parent) : ZERO_BYTES32;
258
201
  if (commit.repo !== repo) throw new Error(`commit ${commitCid.toString()} is a commit of ${commit.repo}, not ${repo}`);
259
- const update = commitUpdate(commit);
260
- if (update.parent !== await this.registry.head({
261
- publisher,
262
- repo
263
- })) throw new Error("stale parent: this commit's parent is not the on-chain head — rebuild it onto the current head.");
264
- if (await commitHead(update) !== rootHexFromCid(commitCid)) throw new Error(`commit ${commitCid.toString()} is not in the current commit format — rebuild it`);
265
- return update;
202
+ if (parentHex !== head) throw new Error("stale parent: this commit's parent is not the on-chain head — rebuild it onto the current head.");
203
+ return {
204
+ txHash: await this.registry.update(repo, head, rootHexFromCid(commitCid)),
205
+ onChainTip: commitCid
206
+ };
266
207
  }
267
208
  /** Walk the parent chain from `tip` toward the root, newest first. */
268
209
  async *walkParents(tip, max) {
@@ -303,20 +244,21 @@ var FangornEngine = class {
303
244
  * Diffing root-to-root (rather than walking commits) is deliberately robust:
304
245
  * it yields the correct net delta even across a force-push / non-fast-forward
305
246
  * reorg — exactly what an index builder needs (what to (re)embed, what to evict).
306
- * Reads only the nodes on changed paths (`diffMst`): 10 new records in a
307
- * 50k-record repo cost ~10 × depth nodes, not two 50k-key walks.
247
+ * ponytail: reads both whole trees; skip subtrees with equal CIDs if diffs
248
+ * of large repos get slow.
308
249
  */
309
250
  async repoDiff(oldRootHex, newRootHex) {
310
251
  const newCommit = newRootHex === ZERO_BYTES32 ? null : this.hexToRootCid(newRootHex);
311
252
  const oldCommit = oldRootHex && oldRootHex !== ZERO_BYTES32 ? this.hexToRootCid(oldRootHex) : null;
312
- const root = async (c) => c ? (await this.getCommit(c)).root : null;
313
- const diff = await diffMst(await root(oldCommit), await root(newCommit), (cid) => this.getBlock(cid, oldCommit), (cid) => this.getBlock(cid, newCommit));
253
+ const after = newCommit ? (await this.stateAt(newCommit)).state : /* @__PURE__ */ new Map();
254
+ const before = oldCommit ? (await this.stateAt(oldCommit)).state : /* @__PURE__ */ new Map();
314
255
  const puts = [];
315
- if (newCommit) for (const [id, cid] of diff.puts) puts.push(await this.decodeRecord(id, cid, newCommit));
256
+ if (newCommit) {
257
+ for (const [id, cid] of after) if (before.get(id)?.toString() !== cid.toString()) puts.push(await this.decodeRecord(id, cid, newCommit));
258
+ }
316
259
  return {
317
260
  puts,
318
- deletes: diff.deletes,
319
- superseded: [...diff.before.values()].map(String)
261
+ deletes: [...before.keys()].filter((id) => !after.has(id))
320
262
  };
321
263
  }
322
264
  /** Diff a commit against its parent. */
@@ -335,6 +277,7 @@ var FangornEngine = class {
335
277
  }
336
278
  for (const id of par.keys()) if (!child.has(id)) removed.push(`${ns}/${id}`);
337
279
  return {
280
+ message: commit.message,
338
281
  parent: commit.parent?.toString() ?? null,
339
282
  epoch: commit.epoch,
340
283
  repo: ns,
@@ -374,4 +317,4 @@ var FangornEngine = class {
374
317
  };
375
318
 
376
319
  //#endregion
377
- export { FangornEngine, MAX_REPO_NAME_LENGTH, assertValidRepoName, commitHead, commitUpdate, fileCid, rootHexFromCid };
320
+ export { FangornEngine, MAX_REPO_NAME_LENGTH, assertValidRepoName, rootHexFromCid };
@@ -37,10 +37,12 @@ type Ref = {
37
37
  * Commit block: γ = (e, parent, root) of WHITEPAPER_1 §3.1, whose name is the
38
38
  * on-chain head. `root` is the repo's MST root.
39
39
  *
40
- * Two more fields:
40
+ * Three more fields, until the registry computes heads from (e, parent, root)
41
+ * itself (docs/REGISTRY_SPEC.md):
41
42
  * • `repo`: the repo's name, which events carry only as keccak256(name);
42
- * • `car`: the CID of the CAR file holding every block this commit
43
- * introduced. The commit block itself is stored OUTSIDE that CAR
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
44
46
  * (individually addressable), so a reader goes digest → commit → CAR with
45
47
  * no index lookups.
46
48
  */
@@ -49,7 +51,8 @@ interface CommitBlock {
49
51
  parent: CID | null;
50
52
  root: CID;
51
53
  repo: string;
52
- car: CID;
54
+ message: string;
55
+ car: string;
53
56
  }
54
57
  /** Upper bound on a record id's length. */
55
58
  declare const MAX_RECORD_ID_LENGTH = 256;
@@ -96,20 +99,6 @@ declare function readMst(root: CID, getBlock: (cid: CID) => Promise<Uint8Array>)
96
99
  state: Map<string, CID>;
97
100
  nodeCids: Set<string>;
98
101
  }>;
99
- /**
100
- * What changed from one tree to another: `puts` (new keys, and keys with a new value) and `deletes`
101
- * (keys gone), each with its old value in `before`. Both trees are read as in-order streams of entries
102
- * and unopened subtrees; a subtree with the same CID at the head of both streams holds the same keys
103
- * and values, so it is dropped unread. Only nodes on changed paths are fetched: ~changes × depth,
104
- * not the size of either tree.
105
- * ponytail: unlike readMst, does not prove either tree canonical (that needs every key). Blocks are
106
- * still hash-checked by `getBlock`; a publisher's malformed tree can only misreport its own records.
107
- */
108
- declare function diffMst(oldRoot: CID | null, newRoot: CID | null, getOld: (cid: CID) => Promise<Uint8Array>, getNew: (cid: CID) => Promise<Uint8Array>): Promise<{
109
- puts: Map<string, CID>;
110
- deletes: string[];
111
- before: Map<string, CID>;
112
- }>;
113
102
  /**
114
103
  * The record CID stored under `key`, or null if absent — one node per level.
115
104
  * ponytail: trusts node ordering on the path, so a malformed tree can hide a
@@ -118,4 +107,4 @@ declare function diffMst(oldRoot: CID | null, newRoot: CID | null, getOld: (cid:
118
107
  */
119
108
  declare function lookupMst(root: CID, key: string, getBlock: (cid: CID) => Promise<Uint8Array>): Promise<CID | null>;
120
109
  //#endregion
121
- export { Block, CommitBlock, MAX_RECORD_ID_LENGTH, MST_BITS, MstEntry, MstNode, RecordBlock, Ref, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, diffMst, encodeBlock, keyLevel, lookupMst, readMst };
110
+ export { Block, CommitBlock, MAX_RECORD_ID_LENGTH, MST_BITS, MstEntry, MstNode, RecordBlock, Ref, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, keyLevel, lookupMst, readMst };
@@ -1,14 +1,13 @@
1
1
  import { CID } from "multiformats/cid";
2
2
  import * as dagCbor from "@ipld/dag-cbor";
3
- import { sha256 } from "@noble/hashes/sha2.js";
4
- import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js";
5
- import { sha256 as sha256$1 } from "multiformats/hashes/sha2";
3
+ import { createHash } from "crypto";
4
+ import { sha256 } from "multiformats/hashes/sha2";
6
5
 
7
6
  //#region src/engine/records.ts
8
7
  /** Encode any value as canonical dag-cbor and derive its CIDv1 (sha256). */
9
8
  async function encodeBlock(obj) {
10
9
  const bytes = dagCbor.encode(obj);
11
- const hash = await sha256$1.digest(bytes);
10
+ const hash = await sha256.digest(bytes);
12
11
  return {
13
12
  cid: CID.createV1(dagCbor.code, hash),
14
13
  bytes
@@ -23,7 +22,8 @@ function decodeBlock(bytes) {
23
22
  }
24
23
  /** Throw unless `bytes` hash to `cid`. Hosts are untrusted; every fetched block goes through this. */
25
24
  function assertBlockMatches(cid, bytes) {
26
- if (bytesToHex(cid.multihash.digest) !== bytesToHex(sha256(bytes))) throw new Error(`block ${cid.toString()} does not match its content hash`);
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
27
  }
28
28
  /** Upper bound on a record id's length. */
29
29
  const MAX_RECORD_ID_LENGTH = 256;
@@ -41,7 +41,7 @@ const MST_BITS = 4;
41
41
  const MAX_DEPTH = 256 / MST_BITS + 1;
42
42
  /** level(k) = ⌊leading zero bits of sha256(k) / b⌋ */
43
43
  function keyLevel(key) {
44
- const digest = sha256(utf8ToBytes(key));
44
+ const digest = createHash("sha256").update(key).digest();
45
45
  let zeros = 0;
46
46
  for (const byte of digest) {
47
47
  if (byte !== 0) {
@@ -141,80 +141,6 @@ async function readMst(root, getBlock) {
141
141
  };
142
142
  }
143
143
  /**
144
- * What changed from one tree to another: `puts` (new keys, and keys with a new value) and `deletes`
145
- * (keys gone), each with its old value in `before`. Both trees are read as in-order streams of entries
146
- * and unopened subtrees; a subtree with the same CID at the head of both streams holds the same keys
147
- * and values, so it is dropped unread. Only nodes on changed paths are fetched: ~changes × depth,
148
- * not the size of either tree.
149
- * ponytail: unlike readMst, does not prove either tree canonical (that needs every key). Blocks are
150
- * still hash-checked by `getBlock`; a publisher's malformed tree can only misreport its own records.
151
- */
152
- async function diffMst(oldRoot, newRoot, getOld, getNew) {
153
- const a = oldRoot ? [{
154
- sub: oldRoot,
155
- level: Infinity
156
- }] : [];
157
- const b = newRoot ? [{
158
- sub: newRoot,
159
- level: Infinity
160
- }] : [];
161
- const open = async (s, get) => {
162
- const { sub } = s.pop();
163
- const node = decodeBlock(await get(sub));
164
- const items = node.left ? [{
165
- sub: node.left,
166
- level: node.level - 1
167
- }] : [];
168
- for (const e of node.entries) {
169
- items.push({
170
- key: e.key,
171
- value: e.value
172
- });
173
- if (e.right) items.push({
174
- sub: e.right,
175
- level: node.level - 1
176
- });
177
- }
178
- s.push(...items.reverse());
179
- };
180
- const puts = /* @__PURE__ */ new Map(), before = /* @__PURE__ */ new Map(), deletes = [];
181
- for (let steps = 0; a.length || b.length; steps++) {
182
- if (steps > 1e8) throw new Error("MST diff did not terminate");
183
- const x = a.at(-1), y = b.at(-1);
184
- if (x && y && "sub" in x && "sub" in y) if (x.sub.equals(y.sub)) {
185
- a.pop();
186
- b.pop();
187
- } else if (x.level > y.level) await open(a, getOld);
188
- else if (y.level > x.level) await open(b, getNew);
189
- else {
190
- await open(a, getOld);
191
- await open(b, getNew);
192
- }
193
- else if (x && "sub" in x) await open(a, getOld);
194
- else if (y && "sub" in y) await open(b, getNew);
195
- else if (x && (!y || x.key < y.key)) {
196
- a.pop();
197
- deletes.push(x.key);
198
- before.set(x.key, x.value);
199
- } else if (y && (!x || y.key < x.key)) {
200
- b.pop();
201
- puts.set(y.key, y.value);
202
- } else if (x && y) {
203
- a.pop();
204
- b.pop();
205
- if (!x.value.equals(y.value)) {
206
- puts.set(y.key, y.value);
207
- before.set(x.key, x.value);
208
- }
209
- }
210
- }
211
- return {
212
- puts,
213
- deletes,
214
- before
215
- };
216
- }
217
- /**
218
144
  * The record CID stored under `key`, or null if absent — one node per level.
219
145
  * ponytail: trusts node ordering on the path, so a malformed tree can hide a
220
146
  * record (never forge one: the caller checks the record's id). Run `readMst`
@@ -236,4 +162,4 @@ async function lookupMst(root, key, getBlock) {
236
162
  }
237
163
 
238
164
  //#endregion
239
- export { MAX_RECORD_ID_LENGTH, MST_BITS, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, diffMst, encodeBlock, keyLevel, lookupMst, readMst };
165
+ export { MAX_RECORD_ID_LENGTH, MST_BITS, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, keyLevel, lookupMst, readMst };
@@ -20,7 +20,7 @@ declare class MemStorage implements MetadataStorage {
20
20
  /** Remote fetches performed (getRawBlock + getFile). */
21
21
  reads: number;
22
22
  putBlock(block: RawBlock): Promise<void>;
23
- putFile(bytes: Uint8Array): Promise<string>;
23
+ putFile(bytes: Uint8Array, name: string): Promise<string>;
24
24
  getFile(uri: string): Promise<Uint8Array>;
25
25
  getRawBlock(uri: string): Promise<Uint8Array>;
26
26
  put(_data: unknown, _meta?: StorageMeta): Promise<string>;
@@ -33,6 +33,7 @@ declare class MemStorage implements MetadataStorage {
33
33
  }
34
34
  /** The paper's registry in memory, signing as PUBLISHER. */
35
35
  declare const PUBLISHER: Hex;
36
+ declare const APP: Hex;
36
37
  declare const newRegistry: () => MemRegistry;
37
38
  //#endregion
38
- export { MemStorage, PUBLISHER, ZERO_BYTES32, newRegistry, rootHex };
39
+ export { APP, MemStorage, PUBLISHER, ZERO_BYTES32, newRegistry, rootHex };
@@ -1,7 +1,4 @@
1
1
  import { MemRegistry } from "../test/mem-registry.js";
2
- import { CID } from "multiformats/cid";
3
- import { sha256 } from "multiformats/hashes/sha2";
4
- import * as raw from "multiformats/codecs/raw";
5
2
 
6
3
  //#region src/engine/test-helpers.ts
7
4
  const ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
@@ -24,11 +21,11 @@ var MemStorage = class {
24
21
  this.blocks.set(block.cid.toString(), block.bytes);
25
22
  return Promise.resolve();
26
23
  }
27
- async putFile(bytes) {
24
+ putFile(bytes, name) {
28
25
  this.writes++;
29
- const uri = CID.createV1(raw.code, await sha256.digest(bytes)).toString();
26
+ const uri = `mem://${this.files.size.toString()}/${name}`;
30
27
  this.files.set(uri, bytes);
31
- return uri;
28
+ return Promise.resolve(uri);
32
29
  }
33
30
  getFile(uri) {
34
31
  const bytes = this.files.get(uri);
@@ -57,7 +54,8 @@ var MemStorage = class {
57
54
  };
58
55
  /** The paper's registry in memory, signing as PUBLISHER. */
59
56
  const PUBLISHER = "0x0000000000000000000000000000000000000001";
57
+ const APP = `0x${"a".repeat(64)}`;
60
58
  const newRegistry = () => new MemRegistry(PUBLISHER);
61
59
 
62
60
  //#endregion
63
- export { MemStorage, PUBLISHER, ZERO_BYTES32, newRegistry, rootHex };
61
+ export { APP, MemStorage, PUBLISHER, ZERO_BYTES32, newRegistry, rootHex };
package/lib/erc8004.js CHANGED
@@ -2,7 +2,10 @@ import { parseAbi, parseEventLogs } from "viem";
2
2
 
3
3
  //#region src/erc8004.ts
4
4
  /** The ERC-8004 identity registry by chain id */
5
- const IDENTITY_REGISTRY = { 84532: "0x8004A818BFB912233c491871b3d84c89A494BD9e" };
5
+ const IDENTITY_REGISTRY = {
6
+ 84532: "0x8004A818BFB912233c491871b3d84c89A494BD9e",
7
+ 421614: "0x8004A818BFB912233c491871b3d84c89A494BD9e"
8
+ };
6
9
  const ABI = parseAbi([
7
10
  "function register(string agentURI, (string metadataKey, bytes metadataValue)[] metadata) returns (uint256 agentId)",
8
11
  "function setAgentURI(uint256 agentId, string newURI)",
@@ -11,12 +14,12 @@ const ABI = parseAbi([
11
14
  /**
12
15
  * Get the agent card URL based on the access worker if defined, else return the carUrl
13
16
  * Both params are optional, but at least one must be defined.
14
- *
17
+ *
15
18
  * Agent cards must live at accessWorker/.well-known/agent-card.json
16
- *
19
+ *
17
20
  * @param cardUrl The cardUrl (passthrough when defined)
18
- * @param accessWorker The (optional) access worker url
19
- * @returns
21
+ * @param accessWorker The (optional) access worker url
22
+ * @returns
20
23
  */
21
24
  function agentCardUrl(cardUrl, accessWorker) {
22
25
  if (cardUrl) return cardUrl;
@@ -48,8 +51,8 @@ async function mcpTools(endpoint) {
48
51
  }
49
52
  }
50
53
  /**
51
- * Build a registration file
52
- * Since it desfines its own agent id,
54
+ * Build a registration file
55
+ * Since it desfines its own agent id,
53
56
  * first registration mints without an agent URI and must be set retroactively.
54
57
  */
55
58
  function registrationFile(opts) {