@fangorn-network/sdk 0.1.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 (102) hide show
  1. package/LICENSE.md +20 -0
  2. package/README.md +80 -0
  3. package/lib/cli/cli.d.ts +2 -0
  4. package/lib/cli/cli.js +571 -0
  5. package/lib/cli/index.d.ts +10 -0
  6. package/lib/cli/index.js +18 -0
  7. package/lib/cli/sell.d.ts +28 -0
  8. package/lib/cli/sell.js +95 -0
  9. package/lib/cli/stage.d.ts +19 -0
  10. package/lib/cli/stage.js +94 -0
  11. package/lib/config.d.ts +697 -0
  12. package/lib/config.js +45 -0
  13. package/lib/contracts/data-registry/abi.d.ts +442 -0
  14. package/lib/contracts/data-registry/abi.js +508 -0
  15. package/lib/contracts/data-registry/index.d.ts +131 -0
  16. package/lib/contracts/data-registry/index.js +183 -0
  17. package/lib/contracts/index.d.ts +6 -0
  18. package/lib/contracts/index.js +6 -0
  19. package/lib/contracts/logs.d.ts +17 -0
  20. package/lib/contracts/logs.js +28 -0
  21. package/lib/contracts/settlement-registry/abi.d.ts +810 -0
  22. package/lib/contracts/settlement-registry/abi.js +962 -0
  23. package/lib/contracts/settlement-registry/index.d.ts +143 -0
  24. package/lib/contracts/settlement-registry/index.js +213 -0
  25. package/lib/contracts/write.d.ts +59 -0
  26. package/lib/contracts/write.js +102 -0
  27. package/lib/crypto/aes.d.ts +19 -0
  28. package/lib/crypto/aes.js +26 -0
  29. package/lib/crypto/encryption.d.ts +38 -0
  30. package/lib/crypto/encryption.js +68 -0
  31. package/lib/crypto/paid.d.ts +104 -0
  32. package/lib/crypto/paid.js +147 -0
  33. package/lib/crypto/rand.d.ts +5 -0
  34. package/lib/crypto/rand.js +16 -0
  35. package/lib/crypto/types.d.ts +23 -0
  36. package/lib/crypto/types.js +1 -0
  37. package/lib/engine/car.d.ts +14 -0
  38. package/lib/engine/car.js +42 -0
  39. package/lib/engine/index.d.ts +206 -0
  40. package/lib/engine/index.js +377 -0
  41. package/lib/engine/records.d.ts +121 -0
  42. package/lib/engine/records.js +239 -0
  43. package/lib/engine/test-helpers.d.ts +38 -0
  44. package/lib/engine/test-helpers.js +63 -0
  45. package/lib/erc8004.d.ts +56 -0
  46. package/lib/erc8004.js +140 -0
  47. package/lib/fangorn.d.ts +342 -0
  48. package/lib/fangorn.js +642 -0
  49. package/lib/feed.d.ts +34 -0
  50. package/lib/feed.js +67 -0
  51. package/lib/index.d.ts +17 -0
  52. package/lib/index.js +16 -0
  53. package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
  54. package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
  55. package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
  56. package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
  57. package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
  58. package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
  59. package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
  60. package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
  61. package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
  62. package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
  63. package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
  64. package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
  65. package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
  66. package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
  67. package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
  68. package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
  69. package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
  70. package/lib/node_modules/chai/index.js +2874 -0
  71. package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
  72. package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
  73. package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
  74. package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
  75. package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
  76. package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
  77. package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
  78. package/lib/providers/storage/car.d.ts +36 -0
  79. package/lib/providers/storage/car.js +81 -0
  80. package/lib/providers/storage/index.d.ts +5 -0
  81. package/lib/providers/storage/index.js +5 -0
  82. package/lib/providers/storage/pinata.d.ts +53 -0
  83. package/lib/providers/storage/pinata.js +178 -0
  84. package/lib/providers/storage/signed-url.d.ts +54 -0
  85. package/lib/providers/storage/signed-url.js +122 -0
  86. package/lib/providers/storage/types.d.ts +62 -0
  87. package/lib/providers/storage/types.js +1 -0
  88. package/lib/providers/storage/utils.d.ts +30 -0
  89. package/lib/providers/storage/utils.js +107 -0
  90. package/lib/repo/index.d.ts +56 -0
  91. package/lib/repo/index.js +73 -0
  92. package/lib/test/key-server.d.ts +12 -0
  93. package/lib/test/key-server.js +30 -0
  94. package/lib/test/mem-registry.d.ts +46 -0
  95. package/lib/test/mem-registry.js +114 -0
  96. package/lib/test/sdk-suite.d.ts +10 -0
  97. package/lib/test/sdk-suite.js +155 -0
  98. package/lib/test/testbed.d.ts +16786 -0
  99. package/lib/test/testbed.js +175 -0
  100. package/lib/types/index.d.ts +37 -0
  101. package/lib/types/index.js +1 -0
  102. package/package.json +87 -0
@@ -0,0 +1,342 @@
1
+ import { PreparedTx } from "./contracts/write.js";
2
+ import { Registry } from "./contracts/data-registry/index.js";
3
+ import { SettlementRegistryClient } from "./contracts/settlement-registry/index.js";
4
+ import "./contracts/index.js";
5
+ import { MetadataStorage } from "./providers/storage/types.js";
6
+ import { CommitDiff, FangornEngine, RecordInput, RepoContents, RepoDiff, SignedUpdate } from "./engine/index.js";
7
+ import { NetworkConfig } from "./config.js";
8
+ import { FangornCreateOptions, StorageConfig } from "./types/index.js";
9
+ import { AgentCard as AgentCard$1 } from "./erc8004.js";
10
+ import { CommitFeed } from "./feed.js";
11
+ import { Hash, Hex, WalletClient } from "viem";
12
+ import { CID } from "multiformats/cid";
13
+
14
+ //#region src/fangorn.d.ts
15
+ interface RepoChange extends RepoDiff {
16
+ repo: string;
17
+ owner: Hex;
18
+ commitCid: string;
19
+ oldRoot: Hex;
20
+ newRoot: Hex;
21
+ blockNumber: bigint;
22
+ }
23
+ interface SubscribeOptions {
24
+ repo: string;
25
+ /** Publisher whose repo to watch (defaults to this wallet's address). */
26
+ owner?: Hex;
27
+ /**
28
+ * Replay `StateCommitted` events from this block before going live (the resume
29
+ * cursor). Omit to start live from the current chain tip — seed initial state
30
+ * separately via `inspectRepo`.
31
+ */
32
+ fromBlock?: bigint;
33
+ /** Poll interval (ms) for the live watch over an HTTP transport. */
34
+ pollingInterval?: number;
35
+ /** Abort to stop the subscription and release the watch. */
36
+ signal?: AbortSignal;
37
+ }
38
+ /**
39
+ * The A2A extension a repo's agent card carries. Its params name the repo the
40
+ * card speaks for: `{ chainId, dataRegistry, publisher, repo }`.
41
+ */
42
+ declare const FANGORN_REPO_EXTENSION = "https://fangorn.network/a2a/repo/v1";
43
+ /**
44
+ * The record that binds a repo to its agent card: `{ url }`, at this id in the
45
+ * repo. Only the publisher can commit it, so it plays the part an on-chain
46
+ * pointer would.
47
+ */
48
+ declare const CARD_RECORD = "@card";
49
+ /**
50
+ * The part of an A2A agent card `discoverCard` reads. A card's other fields
51
+ * (skills, endpoints and so on) pass through untouched.
52
+ */
53
+ interface AgentCard {
54
+ name?: string;
55
+ description?: string;
56
+ url?: string;
57
+ capabilities?: {
58
+ extensions?: {
59
+ uri: string;
60
+ params?: Record<string, unknown>;
61
+ }[];
62
+ };
63
+ [key: string]: unknown;
64
+ }
65
+ declare class Fangorn {
66
+ private readonly ctx;
67
+ private _engine;
68
+ private _feed;
69
+ private _settlement;
70
+ /**
71
+ * The repo cache
72
+ * Insertion order = LRU order
73
+ */
74
+ private readonly nsCache;
75
+ private constructor();
76
+ get engine(): FangornEngine;
77
+ /**
78
+ * Static creation of a new Fangorn client
79
+ *
80
+ * @param options
81
+ * @returns the client
82
+ */
83
+ static create(options: FangornCreateOptions): Fangorn;
84
+ /**
85
+ * Empties a repo: one commit of the empty state on top of the head. History
86
+ * stays (a head never goes back to zero), and so do the old records: the new
87
+ * head still names them as history.
88
+ */
89
+ reset(repo: string): Promise<{
90
+ txHash: Hash;
91
+ onChainTip: CID;
92
+ }>;
93
+ /** Stage records into one commit on top of `base`. */
94
+ private createCommit;
95
+ /**
96
+ * Allocates a repo in the publisher's root map.
97
+ * A no-op (no on-chain tx) if the repo is already initialized.
98
+ */
99
+ initRepo(repoName: string): Promise<{
100
+ cid: undefined;
101
+ commitCid: undefined;
102
+ txHash: undefined;
103
+ alreadyInitialized: true;
104
+ } | {
105
+ cid: string;
106
+ commitCid: string;
107
+ txHash: `0x${string}`;
108
+ alreadyInitialized: false;
109
+ }>;
110
+ /**
111
+ * Put one record and settle it on-chain in a single shot. `id` defaults to
112
+ * a random one, so repeated calls add records; pass an id to update one.
113
+ */
114
+ upload(repoName: string, payload: unknown, type: string, id?: string): Promise<{
115
+ id: string;
116
+ payloadCid: string;
117
+ commitCid: string;
118
+ txHash: `0x${string}`;
119
+ }>;
120
+ /**
121
+ * Puts many records (and deletes some, by id) under one repo in a
122
+ * single commit, instead of one tx per record. Built in memory and persisted
123
+ * as ONE CAR upload plus one commit block, then settled in a single
124
+ * fast-forward tx. Relations are records too: give them `src`/`dst` `Ref`s.
125
+ */
126
+ uploadBatch(repoName: string, records: RecordInput[], deletes?: string[]): Promise<{
127
+ commitCid: string;
128
+ txHash: `0x${string}`;
129
+ recordCids: Record<string, string>;
130
+ }>;
131
+ /**
132
+ * Build a new commit (no on-chain tx) that puts/deletes records in
133
+ * `repo` on top of `parent` (a commit CID string, or undefined for a
134
+ * root commit). The commit and its CAR are uploaded to storage, so a later
135
+ * `push` — even in a separate process — can settle it.
136
+ */
137
+ commit(opts: {
138
+ repo: string;
139
+ records?: RecordInput[];
140
+ deletes?: string[];
141
+ parent?: string;
142
+ /** Snapshot semantics: the staged records replace the repo's previous contents. */
143
+ replace?: boolean;
144
+ }): Promise<{
145
+ commitCid: string;
146
+ parent: string | null;
147
+ root: string;
148
+ recordCids: Record<string, string>;
149
+ }>;
150
+ /**
151
+ * Build a commit on top of `owner`'s current on-chain head and return it
152
+ * together with the UNSIGNED settlement tx — the self-custodial publish path.
153
+ *
154
+ * `commit` + `push` assume the caller holds the publishing key. A relay server
155
+ * doesn't: it builds and pins the commit with a keyless service wallet, then
156
+ * hands the tx to the user's browser wallet to sign. The head only moves when
157
+ * the publisher themselves signs, so the server is never a trusted party.
158
+ *
159
+ * Nothing is on-chain when this returns. The commit and its CAR are durable in
160
+ * storage, so if the user never signs, the result is an orphaned commit — junk
161
+ * in IPFS, not a corrupted timeline.
162
+ */
163
+ prepareCommit(opts: {
164
+ /** The publisher whose head advances — NOT this client's service wallet. */
165
+ owner: Hex;
166
+ repo: string;
167
+ records?: RecordInput[];
168
+ deletes?: string[];
169
+ /** Snapshot semantics: the staged records replace the repo's contents. */
170
+ replace?: boolean;
171
+ }): Promise<{
172
+ commitCid: string;
173
+ oldRoot: Hex;
174
+ newRoot: Hex;
175
+ staged: {
176
+ records: number;
177
+ deletes: number;
178
+ };
179
+ tx: PreparedTx;
180
+ recordCids: Record<string, string>;
181
+ }>;
182
+ /**
183
+ * Settle a local commit as the on-chain head of `repo`. Its parent must be
184
+ * the current head (see `FangornEngine.pushCommit`). Each repo has its own
185
+ * timeline, so pushes to different repos never contend.
186
+ */
187
+ push(repo: string, commitCid: string): Promise<{
188
+ txHash: string;
189
+ onChainTip: string;
190
+ }>;
191
+ /**
192
+ * `push`, signed instead of sent: anyone holding the result can settle it with
193
+ * `relayPush`, paying the gas.
194
+ */
195
+ signPush(repo: string, commitCid: string): Promise<SignedUpdate>;
196
+ relayPush(signed: SignedUpdate): Promise<Hash>;
197
+ /** The owner's current on-chain tip for a repo, or null if they have none. */
198
+ onChainTip(owner: Hex, repo: string): Promise<string | null>;
199
+ /**
200
+ * A publisher's repo at its latest head, cached against that head.
201
+ *
202
+ * Reading a repo walks its whole record tree from the root. The tip is one
203
+ * cheap contract read and only moves on publish, so it doubles as the cache
204
+ * key: no TTL to tune, no invalidation to remember after a settle, and never
205
+ * stale.
206
+ *
207
+ * LRU-bounded so an always-on process can't accumulate every repo it
208
+ * ever touched. The durable copy is on-chain — an evicted entry re-walks.
209
+ * ponytail: fixed entry count, not byte-aware; switch to a size budget if a
210
+ * few huge repos blow memory before the count cap bites.
211
+ */
212
+ readRepo(owner: Hex, repo: string): Promise<{
213
+ tip: string | null;
214
+ contents: RepoContents;
215
+ }>;
216
+ /** The commit at a repo's head, or null for a repo never written. */
217
+ private headCommit;
218
+ /**
219
+ * Every (publisher, repo) at its latest head, from one scan of the update log.
220
+ * Narrow it with `repo` (every publisher's `docs`) or `owner`, filtered on-chain.
221
+ *
222
+ * ponytail: scans the whole log from `fromBlock`; an indexer's sub-log once
223
+ * that is too slow.
224
+ */
225
+ repos(opts?: {
226
+ repo?: string;
227
+ owner?: Hex;
228
+ fromBlock?: bigint;
229
+ }): Promise<{
230
+ owner: Hex;
231
+ repo: string;
232
+ root: Hex;
233
+ blockNumber: bigint;
234
+ }[]>;
235
+ /**
236
+ * Resolve a repo from its agent card URL alone, and prove the card speaks for it.
237
+ *
238
+ * The card names its repo in the `FANGORN_REPO_EXTENSION` A2A extension, but
239
+ * anyone can serve a card that names any repo. So the card is trusted only if
240
+ * the repo's `@card` record, which only its publisher can commit, points
241
+ * back at this exact URL. The comparison is exact string equality, so a
242
+ * trailing slash or a different host counts as a different card.
243
+ */
244
+ discoverCard(cardUrl: string): Promise<{
245
+ card: AgentCard;
246
+ publisher: Hex;
247
+ repo: string;
248
+ }>;
249
+ /** The card URL a repo's `@card` record points at, or undefined for none. */
250
+ cardOf(publisher: Hex, repo: string): Promise<string | undefined>;
251
+ /**
252
+ * Point one of your repos at its agent card, by committing its `@card` record.
253
+ * Returns the transaction, or undefined if it already points there.
254
+ */
255
+ setCard(repo: string, cardUrl: string): Promise<Hash | undefined>;
256
+ /**
257
+ * `subscribeAll` as ONE shared, ref-counted subscription (see `CommitFeed`), for
258
+ * anywhere more than one consumer wants the same stream.
259
+ */
260
+ feed(opts?: {
261
+ pollingInterval?: number;
262
+ }): CommitFeed;
263
+ /** Walk commit history from `head` (newest first). */
264
+ log(head: string, max?: number): AsyncGenerator<{
265
+ cid: string;
266
+ parent: string | null;
267
+ epoch: number;
268
+ root: string;
269
+ }>;
270
+ /** Show a commit and what it changed vs. its parent. */
271
+ show(commitCid: string): Promise<CommitDiff>;
272
+ /**
273
+ * Lists every record currently committed under a repo, resolved
274
+ * from the publisher's on-chain root — useful for verifying what actually
275
+ * made it into the repository, as opposed to what a given call staged.
276
+ */
277
+ inspectRepo(repoName: string): Promise<RepoContents>;
278
+ /**
279
+ * Stream changes to one publisher's repo. Optionally replays from
280
+ * `fromBlock` first (resume cursor), then watches live until the `signal`
281
+ * aborts. Each yielded change carries `blockNumber` — persist it to resume
282
+ * exactly where you left off.
283
+ */
284
+ subscribe(opts: SubscribeOptions): AsyncGenerator<RepoChange>;
285
+ /**
286
+ * Stream changes to every repo of every publisher. Narrow it with `repo`
287
+ * (every publisher's `docs`) or `owner` (one publisher's repos).
288
+ */
289
+ subscribeAll(opts?: Omit<SubscribeOptions, "repo"> & {
290
+ repo?: string;
291
+ }): AsyncGenerator<RepoChange>;
292
+ /** Shared catch-up-then-live loop behind `subscribe` and `subscribeAll`. */
293
+ private streamCommits;
294
+ /**
295
+ * Resolve a log entry into a repo change, or null if the repo's records did
296
+ * not change.
297
+ *
298
+ * The commit itself names its repo, which must be the one the entry was
299
+ * logged under, or the publisher settled a commit for one repo against
300
+ * another's timeline.
301
+ *
302
+ * The diff runs from the last head this stream saw for the repo, not from
303
+ * the entry's parent. If an entry was dropped (an RPC that left out a log),
304
+ * the two differ, and diffing from the entry's parent would skip what the
305
+ * dropped entry changed.
306
+ */
307
+ private toRepoChange;
308
+ getConfig(): NetworkConfig;
309
+ /** Heads and the update log: the registry of WHITEPAPER_1 §3.2. */
310
+ getRegistry(): Registry;
311
+ getSettlementRegistry(): SettlementRegistryClient;
312
+ /**
313
+ * Register a repo as an ERC-8004 agent from its card, and point the repo at
314
+ * the card (`setCard`). The registration file is pinned with this client's storage, so a
315
+ * browser wallet on signed-URL storage needs no Pinata JWT.
316
+ *
317
+ * `update` re-files an agent already minted (its token id) from the card as it is
318
+ * now, minting nothing. `skipRegister` only binds the card. A binding that already
319
+ * points at `cardUrl` is left alone.
320
+ */
321
+ registerAgent(cardUrl: string, opts: {
322
+ repo: string;
323
+ mcp?: string;
324
+ update?: bigint;
325
+ skipRegister?: boolean;
326
+ }): Promise<{
327
+ card: AgentCard$1;
328
+ agentId?: bigint;
329
+ txHash?: Hash;
330
+ }>;
331
+ getStorage(): MetadataStorage;
332
+ /**
333
+ * Swap the storage backend at runtime — e.g. move from access-worker signed
334
+ * URLs to your own Pinata JWT (or back). Rebuilds the engine on next use so it
335
+ * picks up the new backend.
336
+ */
337
+ setStorage(storage: StorageConfig): void;
338
+ getWalletClient(): WalletClient;
339
+ getAddress(): Hex;
340
+ }
341
+ //#endregion
342
+ export { AgentCard, CARD_RECORD, FANGORN_REPO_EXTENSION, Fangorn, RepoChange, SubscribeOptions };