@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/lib/cli/cli.js CHANGED
@@ -29,6 +29,7 @@ function loadConfig() {
29
29
  const pinataJwt = process.env.PINATA_JWT;
30
30
  const pinataGateway = process.env.PINATA_GATEWAY;
31
31
  const chainName = process.env.CHAIN_NAME;
32
+ const workerUrl = process.env.WORKER_URL;
32
33
  if (privateKey || pinataJwt || pinataGateway || chainName) {
33
34
  const missing = [];
34
35
  if (!privateKey) missing.push("DELEGATOR_ETH_PRIVATE_KEY");
@@ -40,7 +41,8 @@ function loadConfig() {
40
41
  privateKey,
41
42
  cfg: resolveAppConfig(chainName ?? ""),
42
43
  pinataJwt: pinataJwt ?? "",
43
- pinataGateway: pinataGateway ?? ""
44
+ pinataGateway: pinataGateway ?? "",
45
+ workerUrl: workerUrl ?? ""
44
46
  };
45
47
  return _config;
46
48
  }
@@ -50,7 +52,8 @@ function loadConfig() {
50
52
  privateKey: stored.privateKey,
51
53
  cfg: resolveAppConfig(stored.chainName),
52
54
  pinataJwt: stored.pinataJwt,
53
- pinataGateway: stored.pinataGateway
55
+ pinataGateway: stored.pinataGateway,
56
+ workerUrl: stored.workerUrl
54
57
  };
55
58
  return _config;
56
59
  }
@@ -61,31 +64,25 @@ function getAccount() {
61
64
  _account = privateKeyToAccount(loadConfig().privateKey);
62
65
  return _account;
63
66
  }
64
- async function getFangorn() {
67
+ function getFangorn() {
65
68
  if (_fangorn) return _fangorn;
66
69
  const cfg = loadConfig();
67
70
  const agentConfig = {
68
71
  privateKey: cfg.privateKey,
69
72
  pinataJwt: cfg.pinataJwt
70
73
  };
71
- _fangorn = await Fangorn.create({
74
+ _fangorn = Fangorn.create({
72
75
  privateKey: cfg.privateKey,
73
76
  storage: { pinata: {
74
77
  jwt: cfg.pinataJwt,
75
78
  gateway: cfg.pinataGateway
76
79
  } },
77
- encryption: { lit: true },
78
80
  domain: "localhost",
79
81
  config: cfg.cfg,
80
82
  agentConfig
81
83
  });
82
84
  return _fangorn;
83
85
  }
84
- /**
85
- * Resolves a schema name or bytes32 ID to an on-chain schemaId.
86
- * Accepts either a human-readable name (e.g. "fangorn.music.v1") or a
87
- * 0x-prefixed 32-byte hex string — the SchemaRegistry handles both.
88
- */
89
86
  async function resolveSchemaId(fangorn, schemaNameOrId) {
90
87
  try {
91
88
  return await fangorn.getSchemaRegistry().schemaId(/^0x[0-9a-fA-F]{64}$/.test(schemaNameOrId) ? schemaNameOrId : schemaNameOrId);
@@ -132,11 +129,20 @@ program.command("init").description("Configure your Fangorn credentials").action
132
129
  }
133
130
  });
134
131
  handleCancel(pinataGateway);
132
+ const workerUrl = await text({
133
+ message: "Fangorn access worker URL:",
134
+ placeholder: "https://fangorn-access-worker.your-subdomain.workers.dev",
135
+ validate: (v) => {
136
+ if (!v) return "Required";
137
+ }
138
+ });
139
+ handleCancel(workerUrl);
135
140
  const stored = {
136
141
  privateKey,
137
142
  chainName,
138
143
  pinataJwt,
139
- pinataGateway
144
+ pinataGateway,
145
+ workerUrl
140
146
  };
141
147
  if (!existsSync(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
142
148
  writeFileSync(CONFIG_PATH, JSON.stringify(stored, null, 2), "utf-8");
@@ -144,12 +150,11 @@ program.command("init").description("Configure your Fangorn credentials").action
144
150
  outro(`Config saved to ${CONFIG_PATH}`);
145
151
  });
146
152
  const schemaCmd = program.command("schema").description("Schema registry operations");
147
- schemaCmd.command("register").description("Register an agent identity and/or a schema on-chain").argument("<name>", "Schema / agent name").action(async (name) => {
153
+ schemaCmd.command("register").description("Register a schema on-chain").argument("<name>", "Schema name").action(async (name) => {
148
154
  try {
149
155
  intro("Chain selection");
150
156
  outro(`Selected chain: ${(await selectChain()).name}`);
151
- const fangorn = await getFangorn();
152
- const datasourceAgentId = "";
157
+ const fangorn = getFangorn();
153
158
  intro("Schema Registration");
154
159
  const schemaFilePath = await text({
155
160
  message: "Path to your JSON schema file:",
@@ -161,8 +166,6 @@ schemaCmd.command("register").description("Register an agent identity and/or a s
161
166
  }
162
167
  });
163
168
  handleCancel(schemaFilePath);
164
- const schemaName = name;
165
- handleCancel(schemaName);
166
169
  let definition;
167
170
  try {
168
171
  definition = JSON.parse(readFileSync(schemaFilePath, "utf-8"));
@@ -179,9 +182,9 @@ schemaCmd.command("register").description("Register an agent identity and/or a s
179
182
  const s = spinner();
180
183
  s.start("Registering schema...");
181
184
  const { schemaId, schemaCid } = await fangorn.schema.register({
182
- name: schemaName,
185
+ name,
183
186
  definition,
184
- agentId: datasourceAgentId
187
+ agentId: ""
185
188
  });
186
189
  s.stop();
187
190
  note(`Schema ID: ${schemaId}\nCID: ${schemaCid}`, "Schema registered");
@@ -191,9 +194,9 @@ schemaCmd.command("register").description("Register an agent identity and/or a s
191
194
  process.exit(1);
192
195
  }
193
196
  });
194
- schemaCmd.command("get").description("Fetch a registered schema by name").argument("<name>", "Schema name (e.g. fangorn.music.v1)").action(async (name) => {
197
+ schemaCmd.command("get").description("Fetch a registered schema by name").argument("<name>", "Schema name").action(async (name) => {
195
198
  try {
196
- const fangorn = await getFangorn();
199
+ const fangorn = getFangorn();
197
200
  const s = spinner();
198
201
  s.start(`Fetching schema "${name}"...`);
199
202
  const schema = await fangorn.schema.get(name);
@@ -210,53 +213,24 @@ schemaCmd.command("get").description("Fetch a registered schema by name").argume
210
213
  }
211
214
  });
212
215
  const publishCmd = program.command("publish").description("Publisher operations");
213
- publishCmd.command("upload").description("Encrypt and publish file(s) under a schema").argument("<files...>", "File path(s) to upload").requiredOption("-s, --schema <schemaName>", "Schema name (e.g. fangorn.music.v1) or bytes32 ID").option("-p, --price <USDC>", "Resource price in USDC (smallest unit)", "0").option("-o, --overwrite", "Overwrite existing entries instead of merging", false).action(async (files, options) => {
216
+ publishCmd.command("upload").description("Publish file handle(s) under a schema").argument("<files...>", "JSON file(s) containing PublishRecord(s)").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").option("-p, --price <USDC>", "Resource price in USDC (smallest unit)", "0").option("-o, --overwrite", "Overwrite existing entries instead of merging", false).action(async (files, options) => {
214
217
  try {
215
- const fangorn = await getFangorn();
216
- const cfg = loadConfig();
218
+ const fangorn = getFangorn();
217
219
  const price = BigInt(options.price);
218
220
  const s = spinner();
219
221
  const records = files.flatMap((filepath) => {
220
222
  const data = readFileSync(filepath, "utf-8");
221
- const parsed = JSON.parse(data, (key, value) => {
222
- if (key === "data") {
223
- if (Array.isArray(value)) return new Uint8Array(value.map((v) => Number(v)));
224
- if (typeof value === "object" && value !== null) {
225
- const obj = value;
226
- return new Uint8Array(Object.values(obj).map((v) => Number(v)));
227
- }
228
- }
229
- return value;
230
- });
223
+ const parsed = JSON.parse(data);
231
224
  return Array.isArray(parsed) ? parsed : [parsed];
232
225
  });
233
- s.start("Encrypting and publishing...");
226
+ s.start("Publishing...");
234
227
  const result = await fangorn.publisher.upload({
235
228
  records,
236
229
  schemaName: options.schema,
237
- gateway: cfg.pinataGateway,
238
230
  options: { overwrite: options.overwrite }
239
231
  }, price);
240
232
  s.stop();
241
- note(`Manifest CID: ${result.manifestCid}\nEntries: ${result.entryCount.toString()}\nSchema: ${result.schemaId}`, "Upload complete");
242
- process.exit(0);
243
- } catch (err) {
244
- console.error("Failed:", err.message);
245
- process.exit(1);
246
- }
247
- });
248
- publishCmd.command("list").description("List your manifest entries for a schema").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").action(async (options) => {
249
- try {
250
- const fangorn = await getFangorn();
251
- const schemaId = await resolveSchemaId(fangorn, options.schema);
252
- const manifest = await fangorn.publisher.getManifest(schemaId, options.schema);
253
- if (!manifest) {
254
- log.warn("No manifest found. Upload with `fangorn publish upload <files> -s <schema>`.");
255
- process.exit(0);
256
- }
257
- console.log(`Schema: ${options.schema}`);
258
- console.log(`Entries (${manifest.entries.length.toString()}):`);
259
- for (const entry of manifest.entries) console.log(` - ${entry.name}`);
233
+ note(`Manifest URI: ${result.manifestUri}\nEntries: ${result.entryCount.toString()}\nSchema: ${result.schemaId}`, "Upload complete");
260
234
  process.exit(0);
261
235
  } catch (err) {
262
236
  console.error("Failed:", err.message);
@@ -265,7 +239,7 @@ publishCmd.command("list").description("List your manifest entries for a schema"
265
239
  });
266
240
  publishCmd.command("entry").description("Show details for one of your manifest entries").argument("<tag>", "Entry tag / name").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").action(async (tag, options) => {
267
241
  try {
268
- const fangorn = await getFangorn();
242
+ const fangorn = getFangorn();
269
243
  const schemaId = await resolveSchemaId(fangorn, options.schema);
270
244
  const entry = await fangorn.publisher.getEntry(schemaId, tag);
271
245
  note(JSON.stringify(entry, null, 2), `Entry: ${tag}`);
@@ -278,7 +252,7 @@ publishCmd.command("entry").description("Show details for one of your manifest e
278
252
  const consumeCmd = program.command("consume").description("Consumer operations");
279
253
  consumeCmd.command("list").description("List a publisher's manifest entries").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").requiredOption("--owner <address>", "Publisher address").action(async (options) => {
280
254
  try {
281
- const fangorn = await getFangorn();
255
+ const fangorn = getFangorn();
282
256
  const schemaId = await resolveSchemaId(fangorn, options.schema);
283
257
  const manifest = await fangorn.consumer.getManifest(options.owner, schemaId, options.schema);
284
258
  if (!manifest) {
@@ -297,7 +271,7 @@ consumeCmd.command("list").description("List a publisher's manifest entries").re
297
271
  });
298
272
  consumeCmd.command("entry").description("Show a publisher's manifest entry").argument("<tag>", "Entry tag / name").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").requiredOption("--owner <address>", "Publisher address").action(async (tag, options) => {
299
273
  try {
300
- const fangorn = await getFangorn();
274
+ const fangorn = getFangorn();
301
275
  const schemaId = await resolveSchemaId(fangorn, options.schema);
302
276
  const entry = await fangorn.consumer.getEntry(options.owner, schemaId, tag);
303
277
  note(JSON.stringify(entry, null, 2), `Entry: ${tag}`);
@@ -307,15 +281,9 @@ consumeCmd.command("entry").description("Show a publisher's manifest entry").arg
307
281
  process.exit(1);
308
282
  }
309
283
  });
310
- /**
311
- * consume purchase
312
- *
313
- * Phase 1 — sign ERC-3009 authorization + join Semaphore group.
314
- * Prints the identity export string; the user must save it for `claim` + `decrypt`.
315
- */
316
284
  consumeCmd.command("purchase").description("Phase 1: pay and join the access group").argument("<owner>", "Publisher address").argument("<name>", "Entry name").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").requiredOption("--burner-key <hex>", "Burner wallet private key (pays USDC)").requiredOption("--amount <usdc>", "USDC amount in smallest unit").requiredOption("--usdc <address>", "USDC contract address").action(async (owner, name, options) => {
317
285
  try {
318
- const fangorn = await getFangorn();
286
+ const fangorn = getFangorn();
319
287
  const cfg = loadConfig();
320
288
  const schemaId = await resolveSchemaId(fangorn, options.schema);
321
289
  const relayerKey = cfg.privateKey;
@@ -351,7 +319,7 @@ consumeCmd.command("purchase").description("Phase 1: pay and join the access gro
351
319
  note([
352
320
  `Tx: ${txHash}`,
353
321
  ``,
354
- `⚠️ Save this identity string — you will need it for \`claim\` and \`decrypt\`:`,
322
+ `⚠️ Save this identity string — you will need it for \`claim\`:`,
355
323
  ``,
356
324
  ` ${exported}`,
357
325
  ``,
@@ -367,15 +335,9 @@ consumeCmd.command("purchase").description("Phase 1: pay and join the access gro
367
335
  process.exit(1);
368
336
  }
369
337
  });
370
- /**
371
- * consume claim
372
- *
373
- * Phase 2 — generate Groth16 ZK proof of group membership and fire the
374
- * SettlementRegistry hook. Prints the nullifier for use in `decrypt`.
375
- */
376
338
  consumeCmd.command("claim").description("Phase 2: prove membership and claim access").argument("<owner>", "Publisher address").argument("<name>", "Entry name").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").requiredOption("--identity <string>", "Exported identity from `consume purchase`").requiredOption("--stealth <address>", "Stealth address to receive the access token").action(async (owner, name, options) => {
377
339
  try {
378
- const fangorn = await getFangorn();
340
+ const fangorn = getFangorn();
379
341
  const schemaId = await resolveSchemaId(fangorn, options.schema);
380
342
  const relayerKey = loadConfig().privateKey;
381
343
  const identity = new Identity(options.identity);
@@ -401,7 +363,7 @@ consumeCmd.command("claim").description("Phase 2: prove membership and claim acc
401
363
  `Nullifier: ${nullifier.toString()}`,
402
364
  ``,
403
365
  `Next:`,
404
- ` fangorn consume decrypt ${owner} ${name} \\`,
366
+ ` fangorn consume fetch ${owner} ${name} \\`,
405
367
  ` -s ${options.schema} \\`,
406
368
  ` -f <field> \\`,
407
369
  ` --nullifier ${nullifier.toString()} \\`,
@@ -414,17 +376,11 @@ consumeCmd.command("claim").description("Phase 2: prove membership and claim acc
414
376
  process.exit(1);
415
377
  }
416
378
  });
417
- /**
418
- * consume decrypt
419
- *
420
- * Decrypt a field after a completed purchase + claim cycle.
421
- * The stealth wallet is used to create the Lit auth context.
422
- */
423
- consumeCmd.command("decrypt").description("Decrypt a field after purchase and claim").argument("<owner>", "Publisher address").argument("<name>", "Entry name").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").requiredOption("-f, --field <field>", "Field name to decrypt").requiredOption("--nullifier <bigint>", "Nullifier from `consume claim`").requiredOption("--stealth-key <hex>", "Private key of the stealth address").option("--identity <string>", "Exported identity (required unless --skip-settlement-check)").option("--skip-settlement-check", "Skip on-chain registration verification").option("-o, --output <path>", "Write decrypted output to file (default: stdout)").action(async (owner, name, options) => {
379
+ consumeCmd.command("fetch").description("Fetch a field after purchase and claim").argument("<owner>", "Publisher address").argument("<name>", "Entry name").requiredOption("-s, --schema <schemaName>", "Schema name or bytes32 ID").requiredOption("-f, --field <field>", "Field name to fetch").requiredOption("--nullifier <bigint>", "Nullifier from `consume claim`").requiredOption("--stealth-key <hex>", "Private key of the stealth address").option("-o, --output <path>", "Write output to file (default: stdout)").action(async (owner, name, options) => {
424
380
  try {
425
381
  const cfg = loadConfig();
426
382
  const chain = getChain(cfg.cfg.chainName);
427
- const fangorn = await getFangorn();
383
+ const fangorn = getFangorn();
428
384
  const schemaId = await resolveSchemaId(fangorn, options.schema);
429
385
  const s = spinner();
430
386
  const walletClient = createWalletClient({
@@ -432,24 +388,14 @@ consumeCmd.command("decrypt").description("Decrypt a field after purchase and cl
432
388
  chain,
433
389
  transport: http(cfg.cfg.rpcUrl)
434
390
  });
435
- const identity = options.identity ? new Identity(options.identity) : void 0;
436
- s.start("Decrypting...");
437
- const decrypted = await fangorn.consumer.decrypt({
438
- owner,
439
- walletClient,
440
- schemaId,
441
- nullifierHash: BigInt(options.nullifier),
442
- name,
443
- field: options.field,
444
- identity,
445
- skipSettlementCheck: !!options.skipSettlementCheck
446
- });
391
+ s.start("Fetching...");
392
+ const { data } = await fangorn.consumer.fetchField(owner, schemaId, name, options.field, options.nullifier, walletClient);
447
393
  s.stop();
448
394
  if (options.output) {
449
- writeFileSync(options.output, Buffer.from(decrypted));
395
+ writeFileSync(options.output, Buffer.from(data));
450
396
  console.log(`Saved to: ${options.output}`);
451
397
  } else {
452
- process.stdout.write(Buffer.from(decrypted));
398
+ process.stdout.write(Buffer.from(data));
453
399
  process.stdout.write("\n");
454
400
  }
455
401
  process.exit(0);
@@ -462,7 +408,7 @@ program.command("datasource").description("Data source registry queries").comman
462
408
  try {
463
409
  const self = getAccount().address;
464
410
  const owner = options.owner ?? self;
465
- const fangorn = await getFangorn();
411
+ const fangorn = getFangorn();
466
412
  const schemaId = await resolveSchemaId(fangorn, options.schema);
467
413
  const ds = await fangorn.getDatasourceRegistry().get(owner, schemaId, options.schema);
468
414
  console.log(`Owner: ${owner}`);