@fangorn-network/sdk 2026.4.7 → 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 (61) hide show
  1. package/README.md +137 -150
  2. package/lib/cli/cli.js +134 -226
  3. package/lib/cli/index.d.ts +170 -170
  4. package/lib/config.d.ts +260 -257
  5. package/lib/config.js +7 -5
  6. package/lib/fangorn.d.ts +3 -20
  7. package/lib/fangorn.js +15 -52
  8. package/lib/index.d.ts +3 -7
  9. package/lib/index.js +4 -7
  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 -2
  16. package/lib/providers/storage/pinata.d.ts +14 -0
  17. package/lib/providers/storage/pinata.js +32 -0
  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/abi.d.ts +116 -43
  22. package/lib/registries/datasource-registry/abi.js +190 -12
  23. package/lib/registries/datasource-registry/index.d.ts +12 -6
  24. package/lib/registries/datasource-registry/index.js +96 -20
  25. package/lib/registries/index.js +1 -1
  26. package/lib/registries/schema-registry/abi.d.ts +157 -66
  27. package/lib/registries/schema-registry/abi.js +242 -15
  28. package/lib/registries/schema-registry/index.d.ts +8 -3
  29. package/lib/registries/schema-registry/index.js +82 -17
  30. package/lib/registries/settlement-registry/abi.d.ts +199 -145
  31. package/lib/registries/settlement-registry/abi.js +331 -25
  32. package/lib/registries/settlement-registry/index.d.ts +36 -39
  33. package/lib/registries/settlement-registry/index.js +125 -91
  34. package/lib/registries/settlement-registry/types.d.ts +6 -3
  35. package/lib/roles/consumer/index.d.ts +17 -42
  36. package/lib/roles/consumer/index.js +87 -58
  37. package/lib/roles/consumer/types.d.ts +15 -34
  38. package/lib/roles/publisher/index.d.ts +22 -40
  39. package/lib/roles/publisher/index.js +69 -113
  40. package/lib/roles/publisher/types.d.ts +40 -41
  41. package/lib/roles/schema/index.d.ts +4 -30
  42. package/lib/roles/schema/index.js +7 -60
  43. package/lib/roles/schema/types.d.ts +16 -2
  44. package/lib/test/testbed.d.ts +12 -36
  45. package/lib/test/testbed.js +35 -68
  46. package/lib/types/index.d.ts +14 -57
  47. package/package.json +1 -6
  48. package/lib/modules/encryption/lit.d.ts +0 -21
  49. package/lib/modules/encryption/lit.js +0 -127
  50. package/lib/modules/gadgets/index.d.ts +0 -2
  51. package/lib/modules/gadgets/index.js +0 -1
  52. package/lib/modules/gadgets/settledGadget.d.ts +0 -31
  53. package/lib/modules/gadgets/settledGadget.js +0 -75
  54. package/lib/modules/gadgets/types.d.ts +0 -27
  55. package/lib/providers/storage/pinata/index.d.ts +0 -13
  56. package/lib/providers/storage/pinata/index.js +0 -41
  57. package/lib/providers/storage/storacha/index.d.ts +0 -13
  58. package/lib/providers/storage/storacha/index.js +0 -49
  59. package/lib/providers/storage/storacha/readOnly.d.ts +0 -8
  60. package/lib/providers/storage/storacha/readOnly.js +0 -16
  61. /package/lib/{modules/gadgets → providers/storage}/types.js +0 -0
@@ -1,14 +1,12 @@
1
- import { PrepareSettleResult, RegisterParams, SettleParams, TransferWithAuthPayload } from "../../registries/settlement-registry/types.js";
2
- import { AuthContext } from "../../modules/encryption/index.js";
3
1
  import { ManifestEntry } from "../publisher/types.js";
2
+ import { PrepareSettleResult, TransferWithAuthPayload } from "../../registries/settlement-registry/types.js";
4
3
  import { Address, Hex, WalletClient } from "viem";
5
- import { Identity } from "@semaphore-protocol/identity";
6
4
 
7
5
  //#region src/roles/consumer/types.d.ts
8
6
  interface PurchaseParams {
9
7
  owner: Address;
10
8
  schemaId: Hex;
11
- tag: string;
9
+ name: string;
12
10
  identityCommitment: bigint;
13
11
  relayerPrivateKey: Hex;
14
12
  preparedRegister: TransferWithAuthPayload;
@@ -16,38 +14,10 @@ interface PurchaseParams {
16
14
  interface ClaimParams {
17
15
  owner: Address;
18
16
  schemaId: Hex;
19
- tag: string;
17
+ name: string;
20
18
  relayerPrivateKey: Hex;
21
19
  preparedSettle: PrepareSettleResult;
22
20
  }
23
- interface DecryptParams {
24
- owner: Address;
25
- walletClient: WalletClient;
26
- schemaId: Hex;
27
- nullifierHash: bigint;
28
- /** The record tag — maps to the resourceId */
29
- tag: string;
30
- /** The specific encrypted field within the record to decrypt */
31
- field: string;
32
- identity?: Identity;
33
- /**
34
- * When true, skips settlement verification.
35
- * Use for owner self-access or out-of-band flows only.
36
- */
37
- skipSettlementCheck?: boolean;
38
- authContext?: AuthContext;
39
- }
40
- interface AccessParams {
41
- owner: Address;
42
- schemaId: Hex;
43
- tag: string;
44
- /** The encrypted field to decrypt after settling */
45
- field: string;
46
- identity: Identity;
47
- payment: Omit<RegisterParams, "resourceId">;
48
- proof: Omit<SettleParams, "resourceId">;
49
- authContext?: AuthContext;
50
- }
51
21
  interface PurchaseResult {
52
22
  txHash: Hex;
53
23
  resourceId: Hex;
@@ -62,5 +32,16 @@ interface AccessResult {
62
32
  resourceId: Hex;
63
33
  entry: ManifestEntry;
64
34
  }
35
+ interface FetchParams {
36
+ nullifier: string;
37
+ resourceId: Hex;
38
+ objectKey: string;
39
+ workerUrl: string;
40
+ walletClient: WalletClient;
41
+ }
42
+ interface FetchResult {
43
+ data: Uint8Array;
44
+ contentType: string;
45
+ }
65
46
  //#endregion
66
- export { AccessParams, AccessResult, ClaimParams, ClaimResult, DecryptParams, PurchaseParams, PurchaseResult };
47
+ export { AccessResult, ClaimParams, ClaimResult, FetchParams, FetchResult, PurchaseParams, PurchaseResult };
@@ -1,76 +1,58 @@
1
1
  import { AppConfig } from "../../config.js";
2
2
  import { SchemaRegistry } from "../../registries/schema-registry/index.js";
3
- import { WritableStorage } from "../../providers/storage/index.js";
3
+ import { MetadataStorage } from "../../providers/storage/types.js";
4
4
  import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
5
- import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
6
- import { EncryptionService } from "../../modules/encryption/index.js";
7
- import { CommitResult, EncryptedFieldInput, FieldInput, Manifest, ManifestEntry, PublishRecord, ResolvedEncryptedField, ResolvedField, ResolvedPlainField, UploadParams } from "./types.js";
5
+ import { CommitResult, FieldInput, HandleFieldInput, Manifest, ManifestEntry, PublishRecord, ResolvedField, ResolvedHandleField, ResolvedPlainField, UploadParams } from "./types.js";
8
6
  import { Hex, WalletClient } from "viem";
9
7
 
10
8
  //#region src/roles/publisher/index.d.ts
11
9
  declare class PublisherRole {
12
10
  private readonly dataSourceRegistry;
13
- private readonly settlementRegistry;
14
11
  private readonly schemaRegistry;
15
12
  private readonly storage;
16
- private readonly encryptionService;
17
13
  private readonly walletClient;
18
14
  private readonly config;
19
15
  private pendingEntries;
20
16
  private readonly schemaCache;
21
- constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry, schemaRegistry: SchemaRegistry, storage: WritableStorage<unknown>, encryptionService: EncryptionService, walletClient: WalletClient, config: AppConfig);
17
+ constructor(dataSourceRegistry: DataSourceRegistry, schemaRegistry: SchemaRegistry, storage: MetadataStorage, walletClient: WalletClient, config: AppConfig);
22
18
  /**
23
- * Encrypt and Upload data with Fangorn
24
- * Validate records against the schema, encrypt encrypted
25
- * fields, stage all resolved entries, then commit to IPFS + on-chain.
19
+ * Validate and stage schema-conformant records, then commit to storage
20
+ * and publish on-chain.
26
21
  *
27
- * Each encrypted field within a record is stored as a separate IPFS object
28
- * (the ciphertext). The manifest entry stores the handle (CID + gateway)
29
- * alongside the plain fields. Consumers can read plain fields freely and
30
- * only need to purchase + decrypt for encrypted fields.
22
+ * Fields with a HandleFieldInput are written to the manifest as-is.
23
+ * The content is assumed to already exist at the supplied URI (e.g. pre-uploaded to R2).
24
+ *
25
+ * Plain fields are stored inline in the manifest.
31
26
  */
32
27
  upload(params: UploadParams, price: bigint): Promise<CommitResult>;
33
28
  stage(entry: ManifestEntry): void;
34
- remove(tag: string): boolean;
29
+ remove(name: string): boolean;
35
30
  getPending(): ManifestEntry[];
36
31
  clearPending(): void;
37
32
  /**
38
- * Serialize all staged entries into a manifest, create SettlementRegistry
39
- * resources for each entry, pin the manifest to IPFS, and publish on-chain.
40
- *
41
- * @param schemaId: The unique id of the schema
42
- * @param price: The USDC price required to register with the sempaphore group
43
- * @param overwrite: If true, overwrite manifest data, else append (default: false)
44
- *
33
+ * Serialize all staged entries into a manifest, publish each entry
34
+ * on-chain via the DataSourceRegistry, and record the manifest URI on-chain.
45
35
  */
46
36
  commit(schemaId: Hex, price: bigint, overwrite: boolean): Promise<CommitResult>;
47
- getManifest(schemaId: Hex): Promise<Manifest | undefined>;
48
- getEntry(schemaId: Hex, tag: string): Promise<ManifestEntry>;
37
+ getManifest(schemaId: Hex, name: string): Promise<Manifest | undefined>;
38
+ getEntry(schemaId: Hex, name: string): Promise<ManifestEntry>;
49
39
  private requireAccount;
50
40
  /**
51
- * Resolve the schema def and id by name
41
+ * Fetch the schema by name from the schema registry/cache
52
42
  * @param name The name of the schema
53
- * @returns The schema and id if they exist
43
+ * @returns The schema and schema id, if they exist
54
44
  */
55
45
  private resolveSchema;
56
46
  /**
57
- * Resolve a PublishRecord into a ManifestEntry by processing each field
58
- * according to the schema definition:
59
- * - plain fields are stored as-is
60
- * - encrypted fields are AES+Lit encrypted, pinned to IPFS, and replaced with a handle + gadgetDescriptor
47
+ * Resolve a PublishRecord into a ManifestEntry.
48
+ *
49
+ * Handle fields are passed through directly
50
+ * Plain fields are stored inline.
61
51
  */
62
52
  private resolveRecord;
63
- /**
64
- * Validate that a record's fields match the schema before touching IPFS.
65
- * Catches type mismatches and missing required fields early.
66
- */
67
53
  private validateRecord;
68
54
  private validateField;
69
- /**
70
- * Load the existing manifest into pending entries for merge behaviour.
71
- * Unpins the old manifest CID
72
- */
73
- private loadExistingManifest;
55
+ private loadExistingEntries;
74
56
  }
75
57
  //#endregion
76
- export { CommitResult, EncryptedFieldInput, FieldInput, Manifest, ManifestEntry, PublishRecord, PublisherRole, ResolvedEncryptedField, ResolvedField, ResolvedPlainField, UploadParams };
58
+ export { CommitResult, FieldInput, HandleFieldInput, Manifest, ManifestEntry, PublishRecord, PublisherRole, ResolvedField, ResolvedHandleField, ResolvedPlainField, UploadParams };
@@ -1,51 +1,38 @@
1
- import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
2
- import { makeSettledGadgetFactory } from "../../modules/gadgets/settledGadget.js";
3
-
4
1
  //#region src/roles/publisher/index.ts
5
2
  var PublisherRole = class {
6
3
  pendingEntries = /* @__PURE__ */ new Map();
7
4
  schemaCache = /* @__PURE__ */ new Map();
8
- constructor(dataSourceRegistry, settlementRegistry, schemaRegistry, storage, encryptionService, walletClient, config) {
5
+ constructor(dataSourceRegistry, schemaRegistry, storage, walletClient, config) {
9
6
  this.dataSourceRegistry = dataSourceRegistry;
10
- this.settlementRegistry = settlementRegistry;
11
7
  this.schemaRegistry = schemaRegistry;
12
8
  this.storage = storage;
13
- this.encryptionService = encryptionService;
14
9
  this.walletClient = walletClient;
15
10
  this.config = config;
16
11
  }
17
12
  /**
18
- * Encrypt and Upload data with Fangorn
19
- * Validate records against the schema, encrypt encrypted
20
- * fields, stage all resolved entries, then commit to IPFS + on-chain.
13
+ * Validate and stage schema-conformant records, then commit to storage
14
+ * and publish on-chain.
15
+ *
16
+ * Fields with a HandleFieldInput are written to the manifest as-is.
17
+ * The content is assumed to already exist at the supplied URI (e.g. pre-uploaded to R2).
21
18
  *
22
- * Each encrypted field within a record is stored as a separate IPFS object
23
- * (the ciphertext). The manifest entry stores the handle (CID + gateway)
24
- * alongside the plain fields. Consumers can read plain fields freely and
25
- * only need to purchase + decrypt for encrypted fields.
19
+ * Plain fields are stored inline in the manifest.
26
20
  */
27
21
  async upload(params, price) {
28
- const { records, schemaName, gateway, options } = params;
29
- const address = this.requireAccount();
22
+ const { records, schemaName, options } = params;
30
23
  const { schema, schemaId } = await this.resolveSchema(schemaName);
31
- console.log("got the schemaId " + schemaId);
32
- console.log("got the schema " + JSON.stringify(schema));
33
- const gadgetFactory = params.gadgetFactory ?? ((resourceId) => {
34
- return makeSettledGadgetFactory(this.config)(resourceId);
35
- });
36
24
  for (const record of records) {
37
25
  this.validateRecord(record, schema);
38
- const gadget = await gadgetFactory(SettlementRegistry.deriveResourceId(address, schemaId, record.tag));
39
- const entry = await this.resolveRecord(record, schema, gadget, gateway);
40
- this.pendingEntries.set(record.tag, entry);
26
+ const entry = this.resolveRecord(record, schema, price);
27
+ this.pendingEntries.set(record.name, entry);
41
28
  }
42
29
  return this.commit(schemaId, price, options?.overwrite ?? false);
43
30
  }
44
31
  stage(entry) {
45
- this.pendingEntries.set(entry.tag, entry);
32
+ this.pendingEntries.set(entry.name, entry);
46
33
  }
47
- remove(tag) {
48
- return this.pendingEntries.delete(tag);
34
+ remove(name) {
35
+ return this.pendingEntries.delete(name);
49
36
  }
50
37
  getPending() {
51
38
  return Array.from(this.pendingEntries.values());
@@ -54,61 +41,47 @@ var PublisherRole = class {
54
41
  this.pendingEntries.clear();
55
42
  }
56
43
  /**
57
- * Serialize all staged entries into a manifest, create SettlementRegistry
58
- * resources for each entry, pin the manifest to IPFS, and publish on-chain.
59
- *
60
- * @param schemaId: The unique id of the schema
61
- * @param price: The USDC price required to register with the sempaphore group
62
- * @param overwrite: If true, overwrite manifest data, else append (default: false)
63
- *
44
+ * Serialize all staged entries into a manifest, publish each entry
45
+ * on-chain via the DataSourceRegistry, and record the manifest URI on-chain.
64
46
  */
65
47
  async commit(schemaId, price, overwrite) {
66
48
  const owner = this.requireAccount();
67
49
  if (this.pendingEntries.size === 0) throw new Error("Nothing to commit. Stage at least one record.");
68
50
  let entries = Array.from(this.pendingEntries.values());
69
- for (const entry of entries) {
70
- const resourceId = SettlementRegistry.deriveResourceId(owner, schemaId, entry.tag);
71
- try {
72
- await this.settlementRegistry.createResource(resourceId, price);
73
- } catch (e) {
74
- const msg = e instanceof Error ? e.message : String(e);
75
- if (!msg.includes("ResourceAlreadyExists") && !msg.includes("AlreadyExists")) console.warn("Failed to create the resource: already exists!");
76
- }
77
- }
78
51
  if (!overwrite) {
79
- const vals = (await this.loadExistingManifest(owner, schemaId)).values();
80
- entries = [...Array.from(vals), ...entries];
52
+ const existingEntriesMap = await this.loadExistingEntries(owner, schemaId, entries);
53
+ entries = [...Array.from(existingEntriesMap.values()), ...entries];
81
54
  }
82
55
  const manifest = {
83
- version: 1,
56
+ version: 2,
84
57
  schemaId,
85
58
  entries
86
59
  };
87
- const manifestCid = await this.storage.store(manifest, { metadata: { name: `manifest:${schemaId}` } });
88
- await this.dataSourceRegistry.publishManifest(manifestCid, schemaId);
60
+ const manifestUri = await this.storage.put(manifest, { name: `manifest:${schemaId}` });
61
+ for (const entry of entries) await this.dataSourceRegistry.publish(manifestUri, schemaId, entry.name, price);
89
62
  this.pendingEntries.clear();
90
63
  return {
91
- manifestCid,
64
+ manifestUri,
92
65
  schemaId,
93
66
  owner,
94
67
  entryCount: entries.length
95
68
  };
96
69
  }
97
- async getManifest(schemaId) {
70
+ async getManifest(schemaId, name) {
98
71
  const owner = this.requireAccount();
99
72
  try {
100
- const ds = await this.dataSourceRegistry.getManifest(owner, schemaId);
73
+ const ds = await this.dataSourceRegistry.get(owner, schemaId, name);
101
74
  if (!ds.manifestCid || ds.manifestCid === "") return void 0;
102
- return await this.storage.retrieve(ds.manifestCid);
75
+ return await this.storage.get(ds.manifestCid);
103
76
  } catch {
104
77
  return;
105
78
  }
106
79
  }
107
- async getEntry(schemaId, tag) {
108
- const manifest = await this.getManifest(schemaId);
109
- if (!manifest) throw new Error(`No manifest found for schemaId ${schemaId}`);
110
- const entry = manifest.entries.find((e) => e.tag === tag);
111
- if (!entry) throw new Error(`Entry not found: "${tag}"`);
80
+ async getEntry(schemaId, name) {
81
+ const manifest = await this.getManifest(schemaId, name);
82
+ if (!manifest) throw new Error(`No manifest found for schemaId ${schemaId} / name ${name}`);
83
+ const entry = manifest.entries.find((e) => e.name === name);
84
+ if (!entry) throw new Error(`Entry not found: "${name}"`);
112
85
  return entry;
113
86
  }
114
87
  requireAccount() {
@@ -117,15 +90,15 @@ var PublisherRole = class {
117
90
  return address;
118
91
  }
119
92
  /**
120
- * Resolve the schema def and id by name
93
+ * Fetch the schema by name from the schema registry/cache
121
94
  * @param name The name of the schema
122
- * @returns The schema and id if they exist
95
+ * @returns The schema and schema id, if they exist
123
96
  */
124
97
  resolveSchema(name) {
125
98
  if (!this.schemaCache.has(name)) {
126
- const p = Promise.all([this.schemaRegistry.getSchema(name), this.schemaRegistry.schemaId(name)]).then(async ([{ cid }, schemaId]) => {
99
+ const p = Promise.all([this.schemaRegistry.getSchema(name), this.schemaRegistry.schemaId(name)]).then(async ([{ specCid }, schemaId]) => {
127
100
  return {
128
- schema: (await this.storage.retrieve(cid)).definition,
101
+ schema: (await this.storage.get(specCid)).definition,
129
102
  schemaId
130
103
  };
131
104
  }).catch((err) => {
@@ -134,57 +107,41 @@ var PublisherRole = class {
134
107
  });
135
108
  this.schemaCache.set(name, p);
136
109
  }
137
- return this.schemaCache.get(name);
110
+ const cached = this.schemaCache.get(name);
111
+ if (!cached) throw new Error(`Schema not found in cache: ${name}`);
112
+ return cached;
138
113
  }
139
114
  /**
140
- * Resolve a PublishRecord into a ManifestEntry by processing each field
141
- * according to the schema definition:
142
- * - plain fields are stored as-is
143
- * - encrypted fields are AES+Lit encrypted, pinned to IPFS, and replaced with a handle + gadgetDescriptor
115
+ * Resolve a PublishRecord into a ManifestEntry.
116
+ *
117
+ * Handle fields are passed through directly
118
+ * Plain fields are stored inline.
144
119
  */
145
- async resolveRecord(record, schema, gadget, gateway) {
120
+ resolveRecord(record, schema, price) {
146
121
  const resolvedFields = {};
147
- for (const [fieldName, fieldDef] of Object.entries(schema)) {
122
+ for (const [fieldName] of Object.entries(schema)) {
148
123
  const value = record.fields[fieldName];
149
- if (fieldDef["@type"] === "encrypted") {
150
- const input = value;
151
- const encrypted = await this.encryptionService.encrypt({
152
- tag: `${record.tag}:${fieldName}`,
153
- data: input.data,
154
- extension: input.extension ?? "",
155
- fileType: input.fileType ?? "application/octet-stream"
156
- }, gadget);
157
- const cid = await this.storage.store(encrypted, { metadata: { name: `${record.tag}:${fieldName}` } });
158
- const gadgetDescriptor = gadget.toDescriptor();
159
- resolvedFields[fieldName] = {
160
- "@type": "encrypted",
161
- handle: {
162
- cid,
163
- gateway
164
- },
165
- gadgetDescriptor
166
- };
167
- } else resolvedFields[fieldName] = value;
124
+ if (isHandleFieldInput(value)) resolvedFields[fieldName] = {
125
+ "@type": "handle",
126
+ uri: value.uri,
127
+ workerUrl: value.workerUrl,
128
+ price: price.toString()
129
+ };
130
+ else resolvedFields[fieldName] = value;
168
131
  }
169
132
  return {
170
- tag: record.tag,
133
+ name: record.name,
171
134
  fields: resolvedFields
172
135
  };
173
136
  }
174
- /**
175
- * Validate that a record's fields match the schema before touching IPFS.
176
- * Catches type mismatches and missing required fields early.
177
- */
178
137
  validateRecord(record, schema) {
179
138
  const errors = [];
180
- for (const [fieldName, fieldDef] of Object.entries(schema)) {
181
- const value = record.fields[fieldName];
182
- this.validateField(record.tag, fieldName, fieldDef, value, errors);
183
- }
184
- for (const fieldName of Object.keys(record.fields)) if (!(fieldName in schema)) console.warn(`Record "${record.tag}": field "${fieldName}" is not in the schema and will be ignored`);
185
- if (errors.length > 0) throw new Error(`Validation failed for record "${record.tag}":\n` + errors.map((e) => ` • ${e}`).join("\n"));
139
+ for (const [fieldName, fieldDef] of Object.entries(schema)) this.validateField(record.name, fieldName, fieldDef, record.fields[fieldName], errors);
140
+ for (const fieldName of Object.keys(record.fields)) if (!(fieldName in schema)) console.warn(`Record "${record.name}": field "${fieldName}" is not in the schema and will be ignored`);
141
+ if (errors.length > 0) throw new Error(`Validation failed for record "${record.name}":\n` + errors.map((e) => ` • ${e}`).join("\n"));
186
142
  }
187
- validateField(tag, fieldName, fieldDef, value, errors) {
143
+ validateField(name, fieldName, fieldDef, value, errors) {
144
+ if (isHandleFieldInput(value)) return;
188
145
  switch (fieldDef["@type"]) {
189
146
  case "string":
190
147
  if (typeof value !== "string") errors.push(`"${fieldName}" must be a string, got ${typeof value}`);
@@ -198,31 +155,30 @@ var PublisherRole = class {
198
155
  case "bytes":
199
156
  if (!(value instanceof Uint8Array)) errors.push(`"${fieldName}" must be Uint8Array`);
200
157
  break;
201
- case "encrypted":
202
- if (typeof value !== "object" || !("data" in value) || !(value.data instanceof Uint8Array)) errors.push(`"${fieldName}" is encrypted — expected EncryptedFieldInput { data: Uint8Array }`);
203
- break;
204
158
  }
205
159
  }
206
- /**
207
- * Load the existing manifest into pending entries for merge behaviour.
208
- * Unpins the old manifest CID
209
- */
210
- async loadExistingManifest(owner, schemaId) {
211
- const entries = /* @__PURE__ */ new Map();
160
+ async loadExistingEntries(owner, schemaId, pendingEntries) {
161
+ const existingMap = /* @__PURE__ */ new Map();
162
+ const pendingNames = new Set(pendingEntries.map((e) => e.name));
163
+ const firstName = pendingEntries[0]?.name;
164
+ if (!firstName) return existingMap;
212
165
  try {
213
- const ds = await this.dataSourceRegistry.getManifest(owner, schemaId);
214
- if (!ds.manifestCid) return entries;
215
- const manifest = await this.storage.retrieve(ds.manifestCid);
216
- for (const entry of manifest.entries) entries.set(entry.tag, entry);
166
+ const ds = await this.dataSourceRegistry.get(owner, schemaId, firstName);
167
+ if (!ds.manifestCid) return existingMap;
168
+ const manifest = await this.storage.get(ds.manifestCid);
169
+ for (const entry of manifest.entries) if (!pendingNames.has(entry.name)) existingMap.set(entry.name, entry);
217
170
  try {
218
171
  await this.storage.delete(ds.manifestCid);
219
172
  } catch (e) {
220
- console.warn(`Failed to unpin old manifest ${ds.manifestCid}:`, e);
173
+ console.warn(`Failed to delete old manifest ${ds.manifestCid}:`, e);
221
174
  }
222
175
  } catch {}
223
- return entries;
176
+ return existingMap;
224
177
  }
225
178
  };
179
+ function isHandleFieldInput(value) {
180
+ return typeof value === "object" && "@type" in value && value["@type"] === "handle";
181
+ }
226
182
 
227
183
  //#endregion
228
184
  export { PublisherRole };
@@ -1,87 +1,86 @@
1
- import { Gadget, GadgetDescriptor } from "../../modules/gadgets/types.js";
2
- import "../../modules/gadgets/index.js";
3
1
  import { Address, Hex } from "viem";
4
2
 
5
3
  //#region src/roles/publisher/types.d.ts
6
4
 
7
5
  /**
8
- * A field value the publisher wants to store encrypted.
9
- * The `data` bytes are what get AES-encrypted and threshold-keyed via Lit.
6
+ * A single field value supplied by the publisher.
7
+ * If the value is already stored externally, use a HandleFieldInput instead.
10
8
  */
11
- interface EncryptedFieldInput {
12
- data: Uint8Array;
13
- fileType?: string;
14
- extension?: string;
15
- }
9
+ type FieldInput = string | number | boolean | Uint8Array | HandleFieldInput;
16
10
  /**
17
- * A single field value supplied by the publisher.
18
- * Plain types are stored as-is; EncryptedFieldInput goes through encryption.
11
+ * A field whose content already lives in a storage backend.
12
+ * The publisher supplies the URI directly — no encrypt/store step occurs.
13
+ * Examples:
14
+ * "r2://tracks/song.mp3"
15
+ * "ipfs://QmXyz..." [future]
19
16
  */
20
- type FieldInput = string | number | boolean | Uint8Array | EncryptedFieldInput;
17
+ interface HandleFieldInput {
18
+ "@type": "handle";
19
+ uri: string;
20
+ workerUrl: string;
21
+ }
21
22
  /**
22
23
  * One schema-conformant record to publish.
23
- * `tag` uniquely identifies this record within (owner, schemaId) —
24
+ * `name` uniquely identifies this record within (owner, schemaId) —
24
25
  * it maps to the resourceId in the SettlementRegistry.
25
26
  */
26
27
  interface PublishRecord {
27
- tag: string;
28
+ name: string;
28
29
  fields: Record<string, FieldInput>;
29
30
  }
30
- /** A resolved encrypted field — what actually gets stored in the manifest entry */
31
- interface ResolvedEncryptedField {
32
- "@type": "encrypted";
33
- handle: {
34
- cid: string;
35
- gateway: string;
36
- };
37
- gadgetDescriptor: GadgetDescriptor;
31
+ /**
32
+ * A resolved handle field — points to content in a storage backend.
33
+ * The URI scheme identifies the backend:
34
+ * r2:// → Fangorn access worker (requires settlement proof)
35
+ * ipfs:// → public IPFS gateway [future]
36
+ */
37
+ interface ResolvedHandleField {
38
+ "@type": "handle";
39
+ uri: string;
40
+ workerUrl: string;
41
+ price: string;
38
42
  }
39
- /** A resolved plain field value — stored as-is */
43
+ /** A resolved plain field, stored inline in the manifest */
40
44
  type ResolvedPlainField = string | number | boolean | Uint8Array;
41
- type ResolvedField = ResolvedPlainField | ResolvedEncryptedField;
45
+ type ResolvedField = ResolvedPlainField | ResolvedHandleField;
42
46
  /**
43
- * A manifest entry — one schema-conformant record with all fields resolved.
44
- * Plain fields are readable by anyone. Encrypted fields carry a handle
45
- * pointing to the ciphertext in IPFS and the gadget descriptor describing
46
- * the access condition.
47
+ * A manifest entry; one schema-conformant record with all fields resolved.
48
+ * Plain fields are readable by anyone directly from the manifest.
49
+ * Handle fields require the consumer to fetch via the appropriate backend.
47
50
  */
48
51
  interface ManifestEntry {
49
- tag: string;
52
+ name: string;
50
53
  fields: Record<string, ResolvedField>;
51
54
  }
52
55
  interface Manifest {
53
- version: 1;
56
+ version: 2;
54
57
  schemaId: Hex;
55
58
  entries: ManifestEntry[];
56
59
  }
57
60
  interface UploadParams {
58
61
  records: PublishRecord[];
59
- schemaName: string;
60
62
  /**
61
- * Called once per record to produce the gadget for that record's encrypted
62
- * fields. All encrypted fields within a record share the same gadget —
63
- * and therefore the same resourceId and access condition.
63
+ * The unique name of the schema the records must conform to.
64
64
  */
65
- gadgetFactory?: (tag: string) => Gadget | Promise<Gadget>;
65
+ schemaName: string;
66
66
  /**
67
- * IPFS gateway URL written into each encrypted field handle so consumers
68
- * can retrieve ciphertexts without knowing the storage provider.
67
+ * Configurable gas for the on-chain publish call.
69
68
  */
70
- gateway: string;
69
+ gas?: bigint;
71
70
  options?: {
72
71
  /**
73
72
  * When false (default) the existing manifest for this (owner, schemaId)
74
- * is loaded and merged — existing records not in this upload are kept.
73
+ * is loaded and merged. Existing records not in this upload are kept.
75
74
  * When true the manifest is fully replaced.
76
75
  */
77
76
  overwrite?: boolean;
78
77
  };
79
78
  }
80
79
  interface CommitResult {
81
- manifestCid: string;
80
+ manifestUri: string;
82
81
  schemaId: Hex;
83
82
  owner: Address;
84
83
  entryCount: number;
85
84
  }
86
85
  //#endregion
87
- export { CommitResult, EncryptedFieldInput, FieldInput, Manifest, ManifestEntry, PublishRecord, ResolvedEncryptedField, ResolvedField, ResolvedPlainField, UploadParams };
86
+ export { CommitResult, FieldInput, HandleFieldInput, Manifest, ManifestEntry, PublishRecord, ResolvedField, ResolvedHandleField, ResolvedPlainField, UploadParams };
@@ -1,6 +1,6 @@
1
1
  import { SchemaRegistry } from "../../registries/schema-registry/index.js";
2
- import { WritableStorage } from "../../providers/storage/index.js";
3
- import { EncryptedField, FieldDefinition, PlainField, RegisterAgentParams, RegisterSchemaParams, RegisteredAgent, RegisteredSchema, SchemaBlobV1, SchemaDefinition, SchemaRoleConfig } from "./types.js";
2
+ import { MetadataStorage } from "../../providers/storage/types.js";
3
+ import { EncryptedField, FieldDefinition, HandleField, PlainField, RegisterAgentParams, RegisterSchemaParams, RegisteredAgent, RegisteredSchema, SchemaBlobV1, SchemaDefinition, SchemaRoleConfig } from "./types.js";
4
4
  import { WalletClient } from "viem";
5
5
 
6
6
  //#region src/roles/schema/index.d.ts
@@ -8,37 +8,11 @@ declare class SchemaRole {
8
8
  private readonly schemaRegistry;
9
9
  private readonly storage;
10
10
  private readonly walletClient;
11
- private readonly agent0;
12
- constructor(schemaRegistry: SchemaRegistry, storage: WritableStorage<unknown>, walletClient: WalletClient, config?: SchemaRoleConfig);
13
- /**
14
- * Register an agent identity via the agent0-sdk / ERC-8004.
15
- */
16
- registerAgent(params: RegisterAgentParams): Promise<RegisteredAgent>;
17
- /**
18
- * Register a schema on-chain, tied to an existing agent identity.
19
- *
20
- * Uploads a versioned schema blob to IPFS (definition + metadata), then
21
- * calls SchemaRegistry.registerSchema(name, schemaCid, agentId).
22
- */
11
+ constructor(schemaRegistry: SchemaRegistry, storage: MetadataStorage, walletClient: WalletClient);
23
12
  register(params: RegisterSchemaParams): Promise<RegisteredSchema>;
24
- /**
25
- * Fetch a registered schema. Accepts either a schema name or a bytes32 id.
26
- * Returns undefined if not registered.
27
- */
28
13
  get(nameOrId: string): Promise<RegisteredSchema | undefined>;
29
- /**
30
- * Validate that a data object structurally conforms to a schema definition.
31
- *
32
- * Shallow type-level check — catches missing fields and type mismatches
33
- * before a publisher wastes gas on a bad commit. Encrypted fields require
34
- * a `handle` object; ciphertext validity is enforced by the encryption
35
- * service at upload time.
36
- *
37
- * Returns a list of validation errors, or an empty array if valid.
38
- */
39
14
  validate(data: Record<string, unknown>, definition: SchemaDefinition): string[];
40
15
  private requireAccount;
41
- private requireAgent0;
42
16
  }
43
17
  //#endregion
44
- export { EncryptedField, FieldDefinition, PlainField, RegisterAgentParams, RegisterSchemaParams, RegisteredAgent, RegisteredSchema, SchemaBlobV1, SchemaDefinition, SchemaRole, SchemaRoleConfig };
18
+ export { EncryptedField, FieldDefinition, HandleField, PlainField, RegisterAgentParams, RegisterSchemaParams, RegisteredAgent, RegisteredSchema, SchemaBlobV1, SchemaDefinition, SchemaRole, SchemaRoleConfig };