@fangorn-network/sdk 2026.4.10 → 2026.4.22

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 (44) hide show
  1. package/README.md +126 -144
  2. package/lib/cli/cli.js +42 -96
  3. package/lib/cli/index.d.ts +170 -170
  4. package/lib/config.d.ts +257 -257
  5. package/lib/config.js +3 -3
  6. package/lib/fangorn.d.ts +3 -17
  7. package/lib/fangorn.js +12 -37
  8. package/lib/index.d.ts +3 -7
  9. package/lib/index.js +3 -5
  10. package/lib/modules/encryption/aes.d.ts +3 -3
  11. package/lib/modules/encryption/index.d.ts +1 -28
  12. package/lib/modules/encryption/index.js +1 -3
  13. package/lib/modules/encryption/types.d.ts +1 -48
  14. package/lib/providers/storage/index.d.ts +4 -21
  15. package/lib/providers/storage/index.js +3 -49
  16. package/lib/providers/storage/pinata.d.ts +8 -5
  17. package/lib/providers/storage/pinata.js +16 -8
  18. package/lib/providers/storage/types.d.ts +37 -0
  19. package/lib/providers/storage/utils.d.ts +12 -0
  20. package/lib/providers/storage/utils.js +50 -0
  21. package/lib/registries/datasource-registry/index.d.ts +2 -2
  22. package/lib/registries/schema-registry/index.d.ts +2 -2
  23. package/lib/registries/settlement-registry/index.d.ts +2 -2
  24. package/lib/roles/consumer/index.d.ts +14 -18
  25. package/lib/roles/consumer/index.js +73 -24
  26. package/lib/roles/consumer/types.d.ts +13 -32
  27. package/lib/roles/publisher/index.d.ts +17 -32
  28. package/lib/roles/publisher/index.js +37 -74
  29. package/lib/roles/publisher/types.d.ts +36 -41
  30. package/lib/roles/schema/index.d.ts +4 -25
  31. package/lib/roles/schema/index.js +5 -30
  32. package/lib/roles/schema/types.d.ts +16 -2
  33. package/lib/test/testbed.d.ts +6 -5
  34. package/lib/test/testbed.js +16 -26
  35. package/lib/types/index.d.ts +12 -45
  36. package/package.json +1 -6
  37. package/lib/modules/encryption/lit.d.ts +0 -20
  38. package/lib/modules/encryption/lit.js +0 -121
  39. package/lib/modules/gadgets/index.d.ts +0 -2
  40. package/lib/modules/gadgets/index.js +0 -1
  41. package/lib/modules/gadgets/settledGadget.d.ts +0 -24
  42. package/lib/modules/gadgets/settledGadget.js +0 -63
  43. package/lib/modules/gadgets/types.d.ts +0 -27
  44. /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> \
@@ -114,115 +127,90 @@ import { Fangorn, FangornConfig } from "@fangorn-network/sdk";
114
127
 
115
128
  const fangorn = await Fangorn.create({
116
129
  privateKey: "0x...",
117
- storage: { pinata: { jwt: "...", gateway: "https://your-gateway.mypinata.cloud" } },
118
- encryption: { lit: true },
130
+ workerUrl: "https://your-worker.workers.dev",
119
131
  config: FangornConfig.ArbitrumSepolia,
120
132
  domain: "localhost",
121
133
  });
122
134
  ```
123
135
 
124
- **Storage options:**
136
+ ### Storage
125
137
 
126
- | Config | Mode |
127
- | ------------------------------ | ------------ |
128
- | `{ pinata: { jwt, gateway } }` | Read + write |
129
- | undefined | Read only |
138
+ Fangorn operates on a 'Bring Your Own Storage' basis.
139
+
140
+ - Schema definitions and schema-conformant data sets live in IPFS using Pinata.
141
+ - Storage of data that should be guarded via Fangorn can live in any database desired, be it S3 or within IPFS. At present, the implementation only supports Cloudflare R2.
142
+
143
+ 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.
130
144
 
131
145
  ### Schemas
132
146
 
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.
147
+ 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
148
 
135
149
  ```ts
136
- // Define a schema
137
150
  const definition: SchemaDefinition = {
138
151
  title: { "@type": "string" },
139
152
  artist: { "@type": "string" },
140
- audio: { "@type": "encrypted", gadget: "settled" }, // field-level encryption
141
- cover: { "@type": "file" }, // plaintext
153
+ audio: { "@type": "handle" }, // content lives in R2, access-controlled by worker
142
154
  };
143
155
 
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
156
  // Register the schema on-chain
151
157
  const { schemaId, schemaCid } = await fangorn.schema.register({
152
158
  name: "schema.name.v1",
153
159
  definition,
154
- agentId,
160
+ agentId: "",
155
161
  });
156
162
 
157
163
  // Fetch a schema by name
158
164
  const schema = await fangorn.schema.get("schema.name.v1");
159
165
  ```
160
166
 
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
167
  ### Publishing
203
168
 
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.
169
+ 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
170
 
206
171
  ```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
172
  await fangorn.publisher.upload(
214
173
  {
215
174
  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" },
175
+ {
176
+ name: "track1",
177
+ fields: {
178
+ title: "Locura",
179
+ artist: "Alice",
180
+ audio: { "@type": "handle", uri: "r2://my-dir/locura.mp3" },
181
+ },
182
+ },
218
183
  ],
219
- schemaName: 'schema.name.v1'
220
- gateway: "https://your-gateway.mypinata.cloud",
184
+ schemaName: "schema.name.v1",
221
185
  },
222
186
  1n, // price in smallest USDC units
223
187
  );
224
188
  ```
225
189
 
190
+ The manifest stored on-chain looks like:
191
+
192
+ ```json
193
+ {
194
+ "version": 2,
195
+ "schemaId": "0x...",
196
+ "entries": [
197
+ {
198
+ "name": "track1",
199
+ "fields": {
200
+ "title": "Locura",
201
+ "artist": "Alice",
202
+ "audio": {
203
+ "@type": "handle",
204
+ "uri": "r2://my-dir/locura.mp3"
205
+ }
206
+ }
207
+ }
208
+ ]
209
+ }
210
+ ```
211
+
212
+ 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.
213
+
226
214
  ### Consuming
227
215
 
228
216
  #### Phase 1: Purchase
@@ -232,7 +220,6 @@ import { Identity } from "@semaphore-protocol/identity";
232
220
 
233
221
  const identity = new Identity();
234
222
 
235
- // Sign ERC-3009 authorization with the burner wallet
236
223
  const preparedRegister = await fangorn.consumer.prepareRegister({
237
224
  walletClient,
238
225
  paymentRecipient: ownerAddress,
@@ -242,76 +229,80 @@ const preparedRegister = await fangorn.consumer.prepareRegister({
242
229
  usdcDomainVersion: "2",
243
230
  });
244
231
 
245
- // Submit payment and join the Semaphore group
246
232
  const { txHash } = await fangorn.consumer.register({
247
233
  owner: ownerAddress,
248
234
  schemaId,
249
- tag: "track1",
235
+ name: "track1",
250
236
  identityCommitment: identity.commitment,
251
237
  relayerPrivateKey: "0x...",
252
238
  preparedRegister,
253
239
  });
254
240
 
255
- // Save identity.export(). Required for Phase 2 and 3
241
+ // Save identity.export() — required for Phase 2
256
242
  ```
257
243
 
258
244
  #### Phase 2: Claim
259
245
 
260
246
  ```ts
261
- // Generate Groth16 ZK proof of group membership
262
247
  const preparedSettle = await fangorn.consumer.prepareSettle({
263
- resourceId: SettlementRegistry.deriveResourceId(ownerAddress, schemaId, "track1"),
248
+ resourceId: DataSourceRegistry.resourceIdLocal(ownerAddress, schemaId, "track1"),
264
249
  identity,
265
250
  stealthAddress: "0x...",
266
251
  });
267
252
 
268
- // Submit the proof and trigger the hook call (if configured)
269
253
  const { txHash, nullifier } = await fangorn.consumer.claim({
270
254
  owner: ownerAddress,
271
255
  schemaId,
272
- tag: "track1",
256
+ name: "track1",
273
257
  relayerPrivateKey: "0x...",
274
258
  preparedSettle,
275
259
  });
276
260
 
277
- // Store nullifier. required for Phase 3.
261
+ // Save nullifier — required for Phase 3
278
262
  ```
279
263
 
280
- #### Phase 3: Decrypt
264
+ #### Phase 3: Fetch
281
265
 
282
266
  ```ts
283
267
  import { createWalletClient, http } from "viem";
284
268
  import { privateKeyToAccount } from "viem/accounts";
285
269
  import { arbitrumSepolia } from "viem/chains";
286
270
 
287
- const walletClient = createWalletClient({
271
+ const stealthWalletClient = createWalletClient({
288
272
  account: privateKeyToAccount(stealthPrivateKey),
289
273
  chain: arbitrumSepolia,
290
274
  transport: http(rpcUrl),
291
275
  });
292
276
 
293
- const plaintext = await fangorn.consumer.decrypt({
294
- owner: ownerAddress,
295
- walletClient,
277
+ const { data, contentType } = await fangorn.consumer.fetchField(
278
+ ownerAddress,
296
279
  schemaId,
297
- nullifierHash: nullifier,
298
- tag: "track1",
299
- field: "audio",
300
- identity,
301
- });
280
+ "track1",
281
+ "audio",
282
+ nullifier.toString(),
283
+ stealthWalletClient,
284
+ );
302
285
  ```
303
286
 
304
- ### Gadgets
287
+ 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.
288
+
289
+ ---
290
+
291
+ ## Access Worker
292
+
293
+ 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.
305
294
 
306
- Gadgets define the access control condition baked into encryption. Built-in gadgets:
295
+ ### How it works
307
296
 
308
- | Gadget | Condition |
309
- | --------------- | --------------------------------------------------- |
310
- | `SettledGadget` | Caller must complete a USDC payment + ZK claim flow |
297
+ 1. Consumer signs `{ nullifier, resourceId, objectKey, timestamp }` with their stealth address private key
298
+ 2. Worker recovers the stealth address from the signature
299
+ 3. Worker calls `is_settled(stealthAddress, resourceId)` on the Settlement Registry
300
+ 4. If settled → bytes proxied directly from R2
301
+ 5. If not → 401
311
302
 
312
- More coming soon ;)
303
+ 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.
313
304
 
314
- See the [gadgets](./src/modules/gadgets/README.md) docs for details on implementing your own gadgets.
305
+ See the [fangorn-access-worker](https://github.com/fangorn-network/webworker) repo for deployment instructions.
315
306
 
316
307
  ---
317
308
 
@@ -321,9 +312,9 @@ See the [gadgets](./src/modules/gadgets/README.md) docs for details on implement
321
312
 
322
313
  | Contract | Address |
323
314
  | ------------------- | -------------------------------------------- |
324
- | DataSource Registry | `0xdb82c131a9d51f6e7695e744bb2bd7774cbb224c` |
325
- | Schema Registry | `0x35b67934f9c75bfef6ff3f4d61ff406d81420066` |
326
- | Settlement Registry | `0x6aff8212e126ed3232958fd228bc58a202b8f590` |
315
+ | DataSource Registry | `0x0e61ca2dbba225580aeab641ab43d623bf5c7e5f` |
316
+ | Schema Registry | `0x4615b03ccaf5e834490a94211e129e6ee8ec6604` |
317
+ | Settlement Registry | `0x0309df3ccb232023934b68c9ed068dec74be42cc` |
327
318
 
328
319
  ---
329
320
 
@@ -337,8 +328,6 @@ pnpm test
337
328
 
338
329
  ### E2E Tests
339
330
 
340
- Copy the example env and fill in values:
341
-
342
331
  ```sh
343
332
  cp env.example .env
344
333
  pnpm test:e2e
@@ -352,35 +341,28 @@ Required variables:
352
341
  | `DELEGATEE_ETH_PRIVATE_KEY` | Consumer private key |
353
342
  | `PINATA_JWT` | Pinata API JWT |
354
343
  | `PINATA_GATEWAY` | Pinata gateway URL |
344
+ | `WORKER_URL` | Access worker URL (optional for Phase 3) |
355
345
  | `CHAIN_NAME` | `arbitrumSepolia` |
356
346
  | `CAIP2` | `421614` |
357
- | `CHAIN_RPC_URL` | RPC endpoint |
358
- | `USDC_CONTRACT_ADDRESS` | USDC contract address |
347
+ | `RPC_URL` | RPC endpoint |
348
+ | `USDC_ADDRESS` | USDC contract address |
359
349
  | `DATA_SOURCE_REGISTRY_ADDRESS` | DataSourceRegistry address |
360
350
  | `SCHEMA_REGISTRY_ADDRESS` | SchemaRegistry address |
361
- | `SETTLEMENT_REGISTRY_ADDRESS` | SettlementTracker address |
362
-
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
- ```
351
+ | `SETTLEMENT_REGISTRY_ADDRESS` | SettlementRegistry address |
374
352
 
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.
353
+ 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
354
 
377
355
  ---
378
356
 
379
- ## Limitations/Future Work
357
+ ## Limitations / Future Work
380
358
 
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.
359
+ - Schema validation is client-side only — no on-chain enforcement.
360
+ - The access worker is a trusted component. Future versions will replace it with a TEE or protocol-native verification layer.
361
+ - One worker per R2 bucket. Multi-bucket support is planned.
362
+ - Purchase ledger is in-memory only. Persistent ledger backed by IPFS is in progress.
363
+
364
+ ---
383
365
 
384
366
  ## License
385
367
 
386
- MIT
368
+ MITSonnet 4.6Claude is AI and can make mistakes. Please double-check responses.