@fangorn-network/sdk 2026.4.10 → 2026.4.15

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 (42) hide show
  1. package/README.md +118 -135
  2. package/lib/cli/cli.js +42 -96
  3. package/lib/cli/index.d.ts +159 -159
  4. package/lib/fangorn.d.ts +3 -17
  5. package/lib/fangorn.js +12 -37
  6. package/lib/index.d.ts +3 -7
  7. package/lib/index.js +3 -5
  8. package/lib/modules/encryption/aes.d.ts +3 -3
  9. package/lib/modules/encryption/index.d.ts +1 -28
  10. package/lib/modules/encryption/index.js +1 -3
  11. package/lib/modules/encryption/types.d.ts +1 -48
  12. package/lib/providers/storage/index.d.ts +4 -21
  13. package/lib/providers/storage/index.js +3 -49
  14. package/lib/providers/storage/pinata.d.ts +8 -5
  15. package/lib/providers/storage/pinata.js +16 -8
  16. package/lib/providers/storage/types.d.ts +37 -0
  17. package/lib/providers/storage/utils.d.ts +12 -0
  18. package/lib/providers/storage/utils.js +50 -0
  19. package/lib/registries/datasource-registry/index.d.ts +2 -2
  20. package/lib/registries/schema-registry/index.d.ts +2 -2
  21. package/lib/registries/settlement-registry/index.d.ts +2 -2
  22. package/lib/roles/consumer/index.d.ts +14 -18
  23. package/lib/roles/consumer/index.js +73 -24
  24. package/lib/roles/consumer/types.d.ts +13 -32
  25. package/lib/roles/publisher/index.d.ts +17 -32
  26. package/lib/roles/publisher/index.js +37 -74
  27. package/lib/roles/publisher/types.d.ts +36 -41
  28. package/lib/roles/schema/index.d.ts +4 -25
  29. package/lib/roles/schema/index.js +5 -30
  30. package/lib/roles/schema/types.d.ts +16 -2
  31. package/lib/test/testbed.d.ts +6 -5
  32. package/lib/test/testbed.js +16 -26
  33. package/lib/types/index.d.ts +12 -45
  34. package/package.json +1 -6
  35. package/lib/modules/encryption/lit.d.ts +0 -20
  36. package/lib/modules/encryption/lit.js +0 -121
  37. package/lib/modules/gadgets/index.d.ts +0 -2
  38. package/lib/modules/gadgets/index.js +0 -1
  39. package/lib/modules/gadgets/settledGadget.d.ts +0 -24
  40. package/lib/modules/gadgets/settledGadget.js +0 -63
  41. package/lib/modules/gadgets/types.d.ts +0 -27
  42. /package/lib/{modules/gadgets → providers/storage}/types.js +0 -0
package/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Fangorn SDK
2
2
 
3
- Intent-bound encrypted data for the agentic web.
3
+ Programmable access control for the agentic web.
4
4
 
5
- Fangorn lets you publish data encrypted under programmable access conditions using **gadgets**. A gadget defines an NP-relation that must must be provably satisfied in order to recover the plaintext (decrypt). Data is organized by **schemas**, enabling agent-based discovery across any number of publishers.
5
+ Fangorn lets you publish data under programmable access conditions. Access is enforced on-chain through settlement verification, so your content is only retrievable by those who have provably paid for it. Data is organized by schemas, enabling agent-based discovery across any number of publishers.
6
+ Content is stored in your own storage backend (Cloudflare R2, IPFS, or any compatible future backend). The Fangorn protocol coordinates access without ever touching your content directly.
6
7
 
7
8
  ## Supported Networks
8
9
 
@@ -27,28 +28,30 @@ npm i -g @fangorn-network/sdk
27
28
  fangorn init
28
29
  ```
29
30
 
30
- - `fangorn init` prompts for a wallet private key, Pinata JWT, Pinata gateway URL, and default chain. Config is written to `~/.fangorn/config.json`.
31
- - You can also configure via environment variables instead of `fangorn init`:
31
+ `fangorn init` prompts for:
32
+ - Wallet private key
33
+ - Pinata JWT + gateway URL (for schema/manifest storage)
34
+ - Fangorn access worker URL (for content retrieval)
35
+ - Default chain
36
+
37
+ Config is written to `~/.fangorn/config.json`.
38
+
39
+ You can also configure via environment variables:
32
40
 
33
41
  ```sh
34
42
  DELEGATOR_ETH_PRIVATE_KEY=0x...
35
- # if you use pinata over storacha
36
43
  PINATA_JWT=...
37
44
  PINATA_GATEWAY=https://your-gateway.mypinata.cloud
38
- # else email for storacha
39
- STORACHA_EMAIL=...
45
+ WORKER_URL=https://your-worker.workers.dev
40
46
  CHAIN_NAME=arbitrumSepolia
41
47
  ```
42
48
 
43
49
  ### Register a Schema
44
50
 
45
51
  ```sh
46
- # Registers an ERC-8004 agent identity and a schema
52
+ # Register a schema on-chain
47
53
  fangorn schema register <name>
48
54
 
49
- # Skip agent registration
50
- fangorn schema register <name> --skip-erc
51
-
52
55
  # Fetch a registered schema by name
53
56
  fangorn schema get schema.name.v1
54
57
  ```
@@ -56,40 +59,50 @@ fangorn schema get schema.name.v1
56
59
  ### Publish Data
57
60
 
58
61
  ```sh
59
- # Encrypt and publish files under a schema, priced at 1 USDC unit
60
- fangorn publish upload file.ext -s schema.name.v1 -p 1
61
-
62
- # List your manifest entries for a schema
63
- fangorn publish list -s schema.name.v1
62
+ # Publish records under a schema, priced at 1 USDC unit
63
+ fangorn publish upload records.json -s schema.name.v1 -p 1
64
64
 
65
65
  # Inspect a specific entry
66
66
  fangorn publish entry track1 -s schema.name.v1
67
67
  ```
68
68
 
69
+ Records are JSON files containing `PublishRecord` objects. Handle fields point to content already uploaded to your storage backend:
70
+
71
+ ```json
72
+ {
73
+ "name": "track1",
74
+ "fields": {
75
+ "title": "Locura",
76
+ "artist": "Alice",
77
+ "audio": { "@type": "handle", "uri": "r2://my-dir/locura.mp3" }
78
+ }
79
+ }
80
+ ```
81
+
69
82
  > A price of `1` equals the smallest USDC unit (0.000001 USDC).
70
83
 
71
84
  ### Consume Data
72
85
 
73
- The consumer flow is three phases: **purchase => claim => decrypt**.
86
+ The consumer flow is three phases: **purchase → claim → fetch**.
74
87
 
75
88
  ```sh
76
- # Phase 1: pay and join the sempaphore group group
77
- fangorn consume purchase <owner> <tag> \
89
+ # Phase 1: pay and join the Semaphore group
90
+ fangorn consume purchase <owner> <name> \
78
91
  -s schema.name.v1 \
79
92
  --burner-key 0x... \
80
93
  --amount 1 \
81
94
  --usdc 0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d
82
95
 
83
- # Save the identity string printed by purchase — required for the next steps.
96
+ # Save the identity string printed by purchase — required for claim.
84
97
 
85
98
  # Phase 2: prove membership and claim access (generates a Groth16 ZK proof)
86
- fangorn consume claim <owner> <tag> \
99
+ fangorn consume claim <owner> <name> \
87
100
  -s schema.name.v1 \
88
101
  --identity '<identity-string>' \
89
102
  --stealth <stealth-address>
90
103
 
91
- # Phase 3: decrypt a specific field
92
- fangorn consume decrypt <owner> <tag> \
104
+ # Phase 3: fetch content via the access worker
105
+ fangorn consume fetch <owner> <name> \
93
106
  -s schema.name.v1 \
94
107
  -f audio \
95
108
  --nullifier <nullifier> \
@@ -115,7 +128,7 @@ import { Fangorn, FangornConfig } from "@fangorn-network/sdk";
115
128
  const fangorn = await Fangorn.create({
116
129
  privateKey: "0x...",
117
130
  storage: { pinata: { jwt: "...", gateway: "https://your-gateway.mypinata.cloud" } },
118
- encryption: { lit: true },
131
+ workerUrl: "https://your-worker.workers.dev",
119
132
  config: FangornConfig.ArbitrumSepolia,
120
133
  domain: "localhost",
121
134
  });
@@ -128,101 +141,77 @@ const fangorn = await Fangorn.create({
128
141
  | `{ pinata: { jwt, gateway } }` | Read + write |
129
142
  | undefined | Read only |
130
143
 
144
+ Storage is used for schemas and manifests only. Content itself lives in your storage backend (R2 etc.) and is never handled by the SDK directly.
145
+
131
146
  ### Schemas
132
147
 
133
- A `SchemaDefinition` is a JSON object where each field declares its type. Fields marked `@type: "encrypted"` are automatically encrypted by the SDK at publish time. All other fields are stored in plaintext. The `gadget` hint on an encrypted field tells the SDK which access condition to apply.
148
+ A `SchemaDefinition` is a JSON object where each field declares its type. Fields marked `@type: "handle"` point to content in an external storage backend — access is enforced by the Fangorn access worker. All other fields are stored inline in the manifest and are publicly readable.
134
149
 
135
150
  ```ts
136
- // Define a schema
137
151
  const definition: SchemaDefinition = {
138
152
  title: { "@type": "string" },
139
153
  artist: { "@type": "string" },
140
- audio: { "@type": "encrypted", gadget: "settled" }, // field-level encryption
141
- cover: { "@type": "file" }, // plaintext
154
+ audio: { "@type": "handle" }, // content lives in R2, access-controlled by worker
142
155
  };
143
156
 
144
- // Register an ERC-8004 agent identity
145
- const { agentId } = await fangorn.schema.registerAgent({
146
- name: "schema.agent.name.v1",
147
- description: "Music streaming data source agent",
148
- });
149
-
150
157
  // Register the schema on-chain
151
158
  const { schemaId, schemaCid } = await fangorn.schema.register({
152
159
  name: "schema.name.v1",
153
160
  definition,
154
- agentId,
161
+ agentId: "",
155
162
  });
156
163
 
157
164
  // Fetch a schema by name
158
165
  const schema = await fangorn.schema.get("schema.name.v1");
159
166
  ```
160
167
 
161
- When a record conforming to this schema is published, Fangorn encrypts each `@type: "encrypted"` field and replaces its value with a ciphertext handle and a gadget descriptor:
162
-
163
- ```json
164
- // Input record
165
- {
166
- "tag": "track1",
167
- "fields": {
168
- "title": "Cassini Division",
169
- "artist": "Arca",
170
- "audio": { "data": "<bytes>", "fileType": "audio/mp3" }
171
- }
172
- }
173
-
174
- // Stored manifest entry (audio field encrypted)
175
- {
176
- "tag": "track1",
177
- "fields": {
178
- "title": "Cassini Division",
179
- "artist": "Arca",
180
- "audio": {
181
- "@type": "encrypted",
182
- "handle": {
183
- "cid": "bafkrei...",
184
- "gateway": "your-gateway.mypinata.cloud"
185
- },
186
- "gadgetDescriptor": {
187
- "type": "settled",
188
- "description": "Settlement-gated: SettlementRegistry.isSettled(resourceId, caller)",
189
- "params": {
190
- "resourceId": "0xce16c0...",
191
- "settlementRegistryAddress": "0x4536881306ee355c2f18ae81658771c4488139a3",
192
- "chainName": "arbitrumSepolia"
193
- }
194
- }
195
- }
196
- }
197
- }
198
- ```
199
-
200
- The `gadgetDescriptor` is human- and agent-readable: it describes exactly what a consumer must do to unlock the field. Plaintext fields (`title`, `artist`) remain directly readable in the manifest without any purchase flow.
201
-
202
168
  ### Publishing
203
169
 
204
- Each upload encrypts files via the gadget returned by `gadgetFactory`, pins the manifest to IPFS, and commits the new CID on-chain. Subsequent uploads merge with the existing manifest unless `overwrite` is set.
170
+ Upload content to your R2 bucket out-of-band (via Cloudflare dashboard, wrangler CLI, or your own tooling), then publish a manifest pointing at it. The SDK stores the manifest on IPFS and commits the CID on-chain.
205
171
 
206
172
  ```ts
207
- import { SettledGadget } from "@fangorn-network/sdk/gadgets";
208
- import { SettlementRegistry } from "@fangorn-network/sdk/registries";
209
-
210
- const owner = fangorn.getAddress();
211
-
212
- // Default gadget = payment settled
213
173
  await fangorn.publisher.upload(
214
174
  {
215
175
  records: [
216
- { tag: "track1", field: "audio", data: audioBytes, extension: ".mp3", fileType: "audio/mpeg" },
217
- { tag: "track1", field: "cover", data: imageBytes, extension: ".png", fileType: "image/png" },
176
+ {
177
+ name: "track1",
178
+ fields: {
179
+ title: "Locura",
180
+ artist: "Alice",
181
+ audio: { "@type": "handle", uri: "r2://my-dir/locura.mp3" },
182
+ },
183
+ },
218
184
  ],
219
- schemaName: 'schema.name.v1'
220
- gateway: "https://your-gateway.mypinata.cloud",
185
+ schemaName: "schema.name.v1",
221
186
  },
222
187
  1n, // price in smallest USDC units
223
188
  );
224
189
  ```
225
190
 
191
+ The manifest stored on-chain looks like:
192
+
193
+ ```json
194
+ {
195
+ "version": 2,
196
+ "schemaId": "0x...",
197
+ "entries": [
198
+ {
199
+ "name": "track1",
200
+ "fields": {
201
+ "title": "Locura",
202
+ "artist": "Alice",
203
+ "audio": {
204
+ "@type": "handle",
205
+ "uri": "r2://my-dir/locura.mp3"
206
+ }
207
+ }
208
+ }
209
+ ]
210
+ }
211
+ ```
212
+
213
+ Plain fields (`title`, `artist`) are readable by anyone directly from the manifest. Handle fields require a valid on-chain settlement to retrieve via the access worker.
214
+
226
215
  ### Consuming
227
216
 
228
217
  #### Phase 1: Purchase
@@ -232,7 +221,6 @@ import { Identity } from "@semaphore-protocol/identity";
232
221
 
233
222
  const identity = new Identity();
234
223
 
235
- // Sign ERC-3009 authorization with the burner wallet
236
224
  const preparedRegister = await fangorn.consumer.prepareRegister({
237
225
  walletClient,
238
226
  paymentRecipient: ownerAddress,
@@ -242,76 +230,80 @@ const preparedRegister = await fangorn.consumer.prepareRegister({
242
230
  usdcDomainVersion: "2",
243
231
  });
244
232
 
245
- // Submit payment and join the Semaphore group
246
233
  const { txHash } = await fangorn.consumer.register({
247
234
  owner: ownerAddress,
248
235
  schemaId,
249
- tag: "track1",
236
+ name: "track1",
250
237
  identityCommitment: identity.commitment,
251
238
  relayerPrivateKey: "0x...",
252
239
  preparedRegister,
253
240
  });
254
241
 
255
- // Save identity.export(). Required for Phase 2 and 3
242
+ // Save identity.export() — required for Phase 2
256
243
  ```
257
244
 
258
245
  #### Phase 2: Claim
259
246
 
260
247
  ```ts
261
- // Generate Groth16 ZK proof of group membership
262
248
  const preparedSettle = await fangorn.consumer.prepareSettle({
263
- resourceId: SettlementRegistry.deriveResourceId(ownerAddress, schemaId, "track1"),
249
+ resourceId: DataSourceRegistry.resourceIdLocal(ownerAddress, schemaId, "track1"),
264
250
  identity,
265
251
  stealthAddress: "0x...",
266
252
  });
267
253
 
268
- // Submit the proof and trigger the hook call (if configured)
269
254
  const { txHash, nullifier } = await fangorn.consumer.claim({
270
255
  owner: ownerAddress,
271
256
  schemaId,
272
- tag: "track1",
257
+ name: "track1",
273
258
  relayerPrivateKey: "0x...",
274
259
  preparedSettle,
275
260
  });
276
261
 
277
- // Store nullifier. required for Phase 3.
262
+ // Save nullifier — required for Phase 3
278
263
  ```
279
264
 
280
- #### Phase 3: Decrypt
265
+ #### Phase 3: Fetch
281
266
 
282
267
  ```ts
283
268
  import { createWalletClient, http } from "viem";
284
269
  import { privateKeyToAccount } from "viem/accounts";
285
270
  import { arbitrumSepolia } from "viem/chains";
286
271
 
287
- const walletClient = createWalletClient({
272
+ const stealthWalletClient = createWalletClient({
288
273
  account: privateKeyToAccount(stealthPrivateKey),
289
274
  chain: arbitrumSepolia,
290
275
  transport: http(rpcUrl),
291
276
  });
292
277
 
293
- const plaintext = await fangorn.consumer.decrypt({
294
- owner: ownerAddress,
295
- walletClient,
278
+ const { data, contentType } = await fangorn.consumer.fetchField(
279
+ ownerAddress,
296
280
  schemaId,
297
- nullifierHash: nullifier,
298
- tag: "track1",
299
- field: "audio",
300
- identity,
301
- });
281
+ "track1",
282
+ "audio",
283
+ nullifier.toString(),
284
+ stealthWalletClient,
285
+ );
302
286
  ```
303
287
 
304
- ### Gadgets
288
+ The consumer signs `{ nullifier, resourceId, objectKey, timestamp }` with their stealth key. The access worker verifies the signature, checks `is_settled()` on-chain, and proxies the content bytes directly from R2. The content URL is never exposed to the client.
289
+
290
+ ---
291
+
292
+ ## Access Worker
305
293
 
306
- Gadgets define the access control condition baked into encryption. Built-in gadgets:
294
+ The Fangorn access worker is a Cloudflare Worker that gates R2 content behind on-chain settlement verification. Publishers deploy their own worker, with one worker per R2 bucket.
307
295
 
308
- | Gadget | Condition |
309
- | --------------- | --------------------------------------------------- |
310
- | `SettledGadget` | Caller must complete a USDC payment + ZK claim flow |
296
+ ### How it works
311
297
 
312
- More coming soon ;)
298
+ 1. Consumer signs `{ nullifier, resourceId, objectKey, timestamp }` with their stealth address private key
299
+ 2. Worker recovers the stealth address from the signature
300
+ 3. Worker calls `is_settled(stealthAddress, resourceId)` on the Settlement Registry
301
+ 4. If settled → bytes proxied directly from R2
302
+ 5. If not → 401
313
303
 
314
- See the [gadgets](./src/modules/gadgets/README.md) docs for details on implementing your own gadgets.
304
+ The worker is stateless, open-source, and has no logging. Its only capability is verifying settlement and proxying bytes. The content URL is never exposed to the consumer.
305
+
306
+ See the [fangorn-access-worker](https://github.com/fangorn-network/webworker) repo for deployment instructions.
315
307
 
316
308
  ---
317
309
 
@@ -337,8 +329,6 @@ pnpm test
337
329
 
338
330
  ### E2E Tests
339
331
 
340
- Copy the example env and fill in values:
341
-
342
332
  ```sh
343
333
  cp env.example .env
344
334
  pnpm test:e2e
@@ -352,35 +342,28 @@ Required variables:
352
342
  | `DELEGATEE_ETH_PRIVATE_KEY` | Consumer private key |
353
343
  | `PINATA_JWT` | Pinata API JWT |
354
344
  | `PINATA_GATEWAY` | Pinata gateway URL |
345
+ | `WORKER_URL` | Access worker URL (optional for Phase 3) |
355
346
  | `CHAIN_NAME` | `arbitrumSepolia` |
356
347
  | `CAIP2` | `421614` |
357
- | `CHAIN_RPC_URL` | RPC endpoint |
358
- | `USDC_CONTRACT_ADDRESS` | USDC contract address |
348
+ | `RPC_URL` | RPC endpoint |
349
+ | `USDC_ADDRESS` | USDC contract address |
359
350
  | `DATA_SOURCE_REGISTRY_ADDRESS` | DataSourceRegistry address |
360
351
  | `SCHEMA_REGISTRY_ADDRESS` | SchemaRegistry address |
361
- | `SETTLEMENT_REGISTRY_ADDRESS` | SettlementTracker address |
352
+ | `SETTLEMENT_REGISTRY_ADDRESS` | SettlementRegistry address |
362
353
 
363
- Sample `.env` for Arbitrum Sepolia:
364
-
365
- ```sh
366
- CHAIN_NAME=arbitrumSepolia
367
- CAIP2=421614
368
- CHAIN_RPC_URL=https://sepolia-rollup.arbitrum.io/rpc
369
- USDC_CONTRACT_ADDRESS=0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d
370
- SETTLEMENT_REGISTRY_ADDRESS=0x6aff8212e126ed3232958fd228bc58a202b8f590
371
- SCHEMA_REGISTRY_ADDRESS=0x35b67934f9c75bfef6ff3f4d61ff406d81420066
372
- DATA_SOURCE_REGISTRY_ADDRESS=0xdb82c131a9d51f6e7695e744bb2bd7774cbb224c
373
- ```
374
-
375
- E2E tests deploy any contracts not defined in `.env`, register a test schema, publish manifests, and verify the full purchase → claim → decrypt cycle end-to-end.
354
+ Phase 3 tests are skipped unless `WORKER_URL` is set. Run the access worker locally with `wrangler dev --local` and set `WORKER_URL=http://localhost:8787` to enable them.
376
355
 
377
356
  ---
378
357
 
379
- ## Limitations/Future Work
358
+ ## Limitations / Future Work
359
+
360
+ - Schema validation is client-side only — no on-chain enforcement.
361
+ - The access worker is a trusted component. Future versions will replace it with a TEE or protocol-native verification layer.
362
+ - One worker per R2 bucket. Multi-bucket support is planned.
363
+ - Purchase ledger is in-memory only. Persistent ledger backed by IPFS is in progress.
380
364
 
381
- - Schema validation is **client-side** only, meaning there is no on-chain validation at all, instead operating on a 'trust me' basis. This will be addressed in the future.
382
- - Schema validation only works with Uint8Array inputs currently.
365
+ ---
383
366
 
384
367
  ## License
385
368
 
386
- MIT
369
+ MITSonnet 4.6Claude is AI and can make mistakes. Please double-check responses.