@fangorn-network/sdk 2026.3.11 → 2026.3.28
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.
- package/README.md +311 -198
- package/lib/builders/a2aCardBuilder.js +1 -0
- package/lib/cli/cli.d.ts +0 -0
- package/lib/cli/cli.js +399 -380
- package/lib/cli/index.d.ts +170 -170
- package/lib/cli/index.js +1 -4
- package/lib/config.d.ts +543 -163
- package/lib/config.js +11 -8
- package/lib/fangorn.d.ts +31 -43
- package/lib/fangorn.js +101 -117
- package/lib/index.d.ts +8 -5
- package/lib/index.js +8 -7
- package/lib/modules/encryption/index.d.ts +4 -3
- package/lib/modules/encryption/lit.d.ts +3 -18
- package/lib/modules/encryption/lit.js +20 -49
- package/lib/modules/encryption/rand.js +2 -2
- package/lib/modules/encryption/types.d.ts +23 -8
- package/lib/modules/gadgets/settledGadget.d.ts +31 -0
- package/lib/modules/gadgets/settledGadget.js +75 -0
- package/lib/modules/gadgets/types.d.ts +16 -4
- package/lib/providers/storage/index.d.ts +15 -4
- package/lib/providers/storage/pinata/index.d.ts +5 -4
- package/lib/providers/storage/pinata/index.js +17 -2
- package/lib/providers/storage/storacha/index.d.ts +13 -0
- package/lib/providers/storage/storacha/index.js +49 -0
- package/lib/providers/storage/storacha/readOnly.d.ts +8 -0
- package/lib/providers/storage/storacha/readOnly.js +16 -0
- package/lib/{interface → registries}/datasource-registry/abi.d.ts +29 -43
- package/lib/registries/datasource-registry/abi.js +16 -0
- package/lib/registries/datasource-registry/index.d.ts +24 -0
- package/lib/{interface/datasource-registry/dataSourceRegistry.js → registries/datasource-registry/index.js} +35 -34
- package/lib/registries/index.d.ts +3 -0
- package/lib/registries/index.js +4 -0
- package/lib/registries/schema-registry/abi.d.ts +127 -0
- package/lib/registries/schema-registry/abi.js +19 -0
- package/lib/registries/schema-registry/index.d.ts +29 -0
- package/lib/registries/schema-registry/index.js +114 -0
- package/lib/registries/settlement-registry/abi.d.ts +228 -0
- package/lib/registries/settlement-registry/abi.js +29 -0
- package/lib/registries/settlement-registry/index.d.ts +72 -0
- package/lib/registries/settlement-registry/index.js +316 -0
- package/lib/registries/settlement-registry/types.d.ts +51 -0
- package/lib/roles/consumer/index.d.ts +64 -0
- package/lib/roles/consumer/index.js +114 -0
- package/lib/roles/consumer/types.d.ts +66 -0
- package/lib/roles/index.js +1 -0
- package/lib/roles/publisher/index.d.ts +68 -0
- package/lib/roles/publisher/index.js +205 -0
- package/lib/roles/publisher/types.d.ts +93 -0
- package/lib/roles/publisher/types.js +1 -0
- package/lib/roles/schema/index.d.ts +44 -0
- package/lib/roles/schema/index.js +137 -0
- package/lib/roles/schema/types.d.ts +67 -0
- package/lib/roles/schema/types.js +1 -0
- package/lib/test/index.d.ts +5 -2
- package/lib/test/index.js +5 -3
- package/lib/test/testbed.d.ts +52 -32
- package/lib/test/testbed.js +138 -137
- package/lib/types/index.d.ts +65 -21
- package/lib/types/index.js +1 -12
- package/lib/utils/index.d.ts +2 -3
- package/lib/utils/index.js +4 -18
- package/package.json +85 -81
- package/lib/cli/registry.d.ts +0 -29
- package/lib/cli/registry.js +0 -53
- package/lib/deployContract.d.ts +0 -23
- package/lib/deployContract.js +0 -78
- package/lib/interface/datasource-registry/abi.js +0 -110
- package/lib/interface/datasource-registry/dataSourceRegistry.d.ts +0 -24
- package/lib/interface/index.d.ts +0 -3
- package/lib/interface/index.js +0 -4
- package/lib/interface/manifest.d.ts +0 -7
- package/lib/interface/manifest.js +0 -10
- package/lib/interface/settlement-tracker/abi.d.ts +0 -26
- package/lib/interface/settlement-tracker/abi.js +0 -88
- package/lib/interface/settlement-tracker/settlementTracker.d.ts +0 -32
- package/lib/interface/settlement-tracker/settlementTracker.js +0 -64
- package/lib/modules/gadgets/acc/gadgets.d.ts +0 -7
- package/lib/modules/gadgets/acc/gadgets.js +0 -10
- package/lib/modules/gadgets/acc.d.ts +0 -15
- package/lib/modules/gadgets/acc.js +0 -24
- package/lib/modules/gadgets/builder.d.ts +0 -1
- package/lib/modules/gadgets/payment.d.ts +0 -23
- package/lib/modules/gadgets/payment.js +0 -88
- package/lib/test/test-gadget.d.ts +0 -7
- package/lib/test/test-gadget.js +0 -10
- /package/lib/{modules/gadgets/builder.js → registries/settlement-registry/types.js} +0 -0
- /package/lib/{providers/storage/s3/index.js → roles/consumer/types.js} +0 -0
- /package/lib/{providers/storage/s3 → roles}/index.d.ts +0 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
|
|
2
|
+
import { makeSettledGadgetFactory } from "../../modules/gadgets/settledGadget.js";
|
|
3
|
+
|
|
4
|
+
//#region src/roles/publisher/index.ts
|
|
5
|
+
var PublisherRole = class {
|
|
6
|
+
pendingEntries = /* @__PURE__ */ new Map();
|
|
7
|
+
constructor(dataSourceRegistry, settlementRegistry, storage, encryptionService, walletClient, config) {
|
|
8
|
+
this.dataSourceRegistry = dataSourceRegistry;
|
|
9
|
+
this.settlementRegistry = settlementRegistry;
|
|
10
|
+
this.storage = storage;
|
|
11
|
+
this.encryptionService = encryptionService;
|
|
12
|
+
this.walletClient = walletClient;
|
|
13
|
+
this.config = config;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Encrypt and Upload data with Fangorn
|
|
17
|
+
* Validate records against the schema, encrypt encrypted
|
|
18
|
+
* fields, stage all resolved entries, then commit to IPFS + on-chain.
|
|
19
|
+
*
|
|
20
|
+
* Each encrypted field within a record is stored as a separate IPFS object
|
|
21
|
+
* (the ciphertext). The manifest entry stores the handle (CID + gateway)
|
|
22
|
+
* alongside the plain fields. Consumers can read plain fields freely and
|
|
23
|
+
* only need to purchase + decrypt for encrypted fields.
|
|
24
|
+
*/
|
|
25
|
+
async upload(params, price) {
|
|
26
|
+
const { records, schema, schemaId, gateway, options } = params;
|
|
27
|
+
const address = this.requireAccount();
|
|
28
|
+
const gadgetFactory = params.gadgetFactory ?? ((tag) => {
|
|
29
|
+
const resourceId = SettlementRegistry.deriveResourceId(address, schemaId, tag);
|
|
30
|
+
return makeSettledGadgetFactory(this.config)(resourceId);
|
|
31
|
+
});
|
|
32
|
+
for (const record of records) {
|
|
33
|
+
this.validateRecord(record, schema);
|
|
34
|
+
const gadget = await gadgetFactory(SettlementRegistry.deriveResourceId(address, schemaId, record.tag));
|
|
35
|
+
const entry = await this.resolveRecord(record, schema, gadget, gateway);
|
|
36
|
+
this.pendingEntries.set(record.tag, entry);
|
|
37
|
+
}
|
|
38
|
+
return this.commit(schemaId, price, options?.overwrite ?? false);
|
|
39
|
+
}
|
|
40
|
+
stage(entry) {
|
|
41
|
+
this.pendingEntries.set(entry.tag, entry);
|
|
42
|
+
}
|
|
43
|
+
remove(tag) {
|
|
44
|
+
return this.pendingEntries.delete(tag);
|
|
45
|
+
}
|
|
46
|
+
getPending() {
|
|
47
|
+
return Array.from(this.pendingEntries.values());
|
|
48
|
+
}
|
|
49
|
+
clearPending() {
|
|
50
|
+
this.pendingEntries.clear();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Serialize all staged entries into a manifest, create SettlementRegistry
|
|
54
|
+
* resources for each entry, pin the manifest to IPFS, and publish on-chain.
|
|
55
|
+
*
|
|
56
|
+
* @param schemaId: The unique id of the schema
|
|
57
|
+
* @param price: The USDC price required to register with the sempaphore group
|
|
58
|
+
* @param overwrite: If true, overwrite manifest data, else append (default: false)
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
async commit(schemaId, price, overwrite) {
|
|
62
|
+
const owner = this.requireAccount();
|
|
63
|
+
if (this.pendingEntries.size === 0) throw new Error("Nothing to commit. Stage at least one record.");
|
|
64
|
+
let entries = Array.from(this.pendingEntries.values());
|
|
65
|
+
for (const entry of entries) {
|
|
66
|
+
const resourceId = SettlementRegistry.deriveResourceId(owner, schemaId, entry.tag);
|
|
67
|
+
try {
|
|
68
|
+
await this.settlementRegistry.createResource(resourceId, price);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
71
|
+
if (!msg.includes("ResourceAlreadyExists") && !msg.includes("AlreadyExists")) console.warn("Failed to create the resource: already exists!");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (!overwrite) {
|
|
75
|
+
const vals = (await this.loadExistingManifest(owner, schemaId)).values();
|
|
76
|
+
entries = [...Array.from(vals), ...entries];
|
|
77
|
+
}
|
|
78
|
+
const manifest = {
|
|
79
|
+
version: 1,
|
|
80
|
+
schemaId,
|
|
81
|
+
entries
|
|
82
|
+
};
|
|
83
|
+
const manifestCid = await this.storage.store(manifest, { metadata: { name: `manifest:${schemaId}` } });
|
|
84
|
+
await this.dataSourceRegistry.publishManifest(manifestCid, schemaId);
|
|
85
|
+
this.pendingEntries.clear();
|
|
86
|
+
return {
|
|
87
|
+
manifestCid,
|
|
88
|
+
schemaId,
|
|
89
|
+
owner,
|
|
90
|
+
entryCount: entries.length
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
async getManifest(schemaId) {
|
|
94
|
+
const owner = this.requireAccount();
|
|
95
|
+
try {
|
|
96
|
+
const ds = await this.dataSourceRegistry.getManifest(owner, schemaId);
|
|
97
|
+
if (!ds.manifestCid || ds.manifestCid === "") return void 0;
|
|
98
|
+
return await this.storage.retrieve(ds.manifestCid);
|
|
99
|
+
} catch {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async getEntry(schemaId, tag) {
|
|
104
|
+
const manifest = await this.getManifest(schemaId);
|
|
105
|
+
if (!manifest) throw new Error(`No manifest found for schemaId ${schemaId}`);
|
|
106
|
+
const entry = manifest.entries.find((e) => e.tag === tag);
|
|
107
|
+
if (!entry) throw new Error(`Entry not found: "${tag}"`);
|
|
108
|
+
return entry;
|
|
109
|
+
}
|
|
110
|
+
requireAccount() {
|
|
111
|
+
const address = this.walletClient.account?.address;
|
|
112
|
+
if (!address) throw new Error("No account connected to wallet client");
|
|
113
|
+
return address;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Resolve a PublishRecord into a ManifestEntry by processing each field
|
|
117
|
+
* according to the schema definition:
|
|
118
|
+
* - plain fields are stored as-is
|
|
119
|
+
* - encrypted fields are AES+Lit encrypted, pinned to IPFS, and replaced
|
|
120
|
+
* with a handle + gadgetDescriptor
|
|
121
|
+
*/
|
|
122
|
+
async resolveRecord(record, schema, gadget, gateway) {
|
|
123
|
+
const resolvedFields = {};
|
|
124
|
+
for (const [fieldName, fieldDef] of Object.entries(schema)) {
|
|
125
|
+
const value = record.fields[fieldName];
|
|
126
|
+
if (fieldDef["@type"] === "encrypted") {
|
|
127
|
+
const input = value;
|
|
128
|
+
const encrypted = await this.encryptionService.encrypt({
|
|
129
|
+
tag: `${record.tag}:${fieldName}`,
|
|
130
|
+
data: input.data,
|
|
131
|
+
extension: input.extension ?? "",
|
|
132
|
+
fileType: input.fileType ?? "application/octet-stream"
|
|
133
|
+
}, gadget);
|
|
134
|
+
const cid = await this.storage.store(encrypted, { metadata: { name: `${record.tag}:${fieldName}` } });
|
|
135
|
+
const gadgetDescriptor = gadget.toDescriptor();
|
|
136
|
+
resolvedFields[fieldName] = {
|
|
137
|
+
"@type": "encrypted",
|
|
138
|
+
handle: {
|
|
139
|
+
cid,
|
|
140
|
+
gateway
|
|
141
|
+
},
|
|
142
|
+
gadgetDescriptor
|
|
143
|
+
};
|
|
144
|
+
} else resolvedFields[fieldName] = value;
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
tag: record.tag,
|
|
148
|
+
fields: resolvedFields
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Validate that a record's fields match the schema before touching IPFS.
|
|
153
|
+
* Catches type mismatches and missing required fields early.
|
|
154
|
+
*/
|
|
155
|
+
validateRecord(record, schema) {
|
|
156
|
+
const errors = [];
|
|
157
|
+
for (const [fieldName, fieldDef] of Object.entries(schema)) {
|
|
158
|
+
const value = record.fields[fieldName];
|
|
159
|
+
this.validateField(record.tag, fieldName, fieldDef, value, errors);
|
|
160
|
+
}
|
|
161
|
+
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`);
|
|
162
|
+
if (errors.length > 0) throw new Error(`Validation failed for record "${record.tag}":\n` + errors.map((e) => ` • ${e}`).join("\n"));
|
|
163
|
+
}
|
|
164
|
+
validateField(tag, fieldName, fieldDef, value, errors) {
|
|
165
|
+
switch (fieldDef["@type"]) {
|
|
166
|
+
case "string":
|
|
167
|
+
if (typeof value !== "string") errors.push(`"${fieldName}" must be a string, got ${typeof value}`);
|
|
168
|
+
break;
|
|
169
|
+
case "number":
|
|
170
|
+
if (typeof value !== "number") errors.push(`"${fieldName}" must be a number, got ${typeof value}`);
|
|
171
|
+
break;
|
|
172
|
+
case "boolean":
|
|
173
|
+
if (typeof value !== "boolean") errors.push(`"${fieldName}" must be a boolean, got ${typeof value}`);
|
|
174
|
+
break;
|
|
175
|
+
case "bytes":
|
|
176
|
+
if (!(value instanceof Uint8Array)) errors.push(`"${fieldName}" must be Uint8Array`);
|
|
177
|
+
break;
|
|
178
|
+
case "encrypted":
|
|
179
|
+
if (typeof value !== "object" || !("data" in value) || !(value.data instanceof Uint8Array)) errors.push(`"${fieldName}" is encrypted — expected EncryptedFieldInput { data: Uint8Array }`);
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Load the existing manifest into pending entries for merge behaviour.
|
|
185
|
+
* Unpins the old manifest CID
|
|
186
|
+
*/
|
|
187
|
+
async loadExistingManifest(owner, schemaId) {
|
|
188
|
+
const entries = /* @__PURE__ */ new Map();
|
|
189
|
+
try {
|
|
190
|
+
const ds = await this.dataSourceRegistry.getManifest(owner, schemaId);
|
|
191
|
+
if (!ds.manifestCid) return entries;
|
|
192
|
+
const manifest = await this.storage.retrieve(ds.manifestCid);
|
|
193
|
+
for (const entry of manifest.entries) entries.set(entry.tag, entry);
|
|
194
|
+
try {
|
|
195
|
+
await this.storage.delete(ds.manifestCid);
|
|
196
|
+
} catch (e) {
|
|
197
|
+
console.warn(`Failed to unpin old manifest ${ds.manifestCid}:`, e);
|
|
198
|
+
}
|
|
199
|
+
} catch {}
|
|
200
|
+
return entries;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
//#endregion
|
|
205
|
+
export { PublisherRole };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { SchemaDefinition } from "../schema/types.js";
|
|
2
|
+
import { Gadget, GadgetDescriptor } from "../../modules/gadgets/types.js";
|
|
3
|
+
import "../../modules/gadgets/index.js";
|
|
4
|
+
import { Address, Hex } from "viem";
|
|
5
|
+
|
|
6
|
+
//#region src/roles/publisher/types.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A field value the publisher wants to store encrypted.
|
|
10
|
+
* The `data` bytes are what get AES-encrypted and threshold-keyed via Lit.
|
|
11
|
+
*/
|
|
12
|
+
interface EncryptedFieldInput {
|
|
13
|
+
data: Uint8Array;
|
|
14
|
+
fileType?: string;
|
|
15
|
+
extension?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A single field value supplied by the publisher.
|
|
19
|
+
* Plain types are stored as-is; EncryptedFieldInput goes through encryption.
|
|
20
|
+
*/
|
|
21
|
+
type FieldInput = string | number | boolean | Uint8Array | EncryptedFieldInput;
|
|
22
|
+
/**
|
|
23
|
+
* One schema-conformant record to publish.
|
|
24
|
+
* `tag` uniquely identifies this record within (owner, schemaId) —
|
|
25
|
+
* it maps to the resourceId in the SettlementRegistry.
|
|
26
|
+
*/
|
|
27
|
+
interface PublishRecord {
|
|
28
|
+
tag: string;
|
|
29
|
+
fields: Record<string, FieldInput>;
|
|
30
|
+
}
|
|
31
|
+
/** A resolved encrypted field — what actually gets stored in the manifest entry */
|
|
32
|
+
interface ResolvedEncryptedField {
|
|
33
|
+
"@type": "encrypted";
|
|
34
|
+
handle: {
|
|
35
|
+
cid: string;
|
|
36
|
+
gateway: string;
|
|
37
|
+
};
|
|
38
|
+
gadgetDescriptor: GadgetDescriptor;
|
|
39
|
+
}
|
|
40
|
+
/** A resolved plain field value — stored as-is */
|
|
41
|
+
type ResolvedPlainField = string | number | boolean | Uint8Array;
|
|
42
|
+
type ResolvedField = ResolvedPlainField | ResolvedEncryptedField;
|
|
43
|
+
/**
|
|
44
|
+
* A manifest entry — one schema-conformant record with all fields resolved.
|
|
45
|
+
* Plain fields are readable by anyone. Encrypted fields carry a handle
|
|
46
|
+
* pointing to the ciphertext in IPFS and the gadget descriptor describing
|
|
47
|
+
* the access condition.
|
|
48
|
+
*/
|
|
49
|
+
interface ManifestEntry {
|
|
50
|
+
tag: string;
|
|
51
|
+
fields: Record<string, ResolvedField>;
|
|
52
|
+
}
|
|
53
|
+
interface Manifest {
|
|
54
|
+
version: 1;
|
|
55
|
+
schemaId: Hex;
|
|
56
|
+
entries: ManifestEntry[];
|
|
57
|
+
}
|
|
58
|
+
interface UploadParams {
|
|
59
|
+
records: PublishRecord[];
|
|
60
|
+
/**
|
|
61
|
+
* The schema the records must conform to. Required — field-level encryption
|
|
62
|
+
* decisions are driven by the schema definition.
|
|
63
|
+
*/
|
|
64
|
+
schema: SchemaDefinition;
|
|
65
|
+
schemaId: Hex;
|
|
66
|
+
/**
|
|
67
|
+
* Called once per record to produce the gadget for that record's encrypted
|
|
68
|
+
* fields. All encrypted fields within a record share the same gadget —
|
|
69
|
+
* and therefore the same resourceId and access condition.
|
|
70
|
+
*/
|
|
71
|
+
gadgetFactory?: (tag: string) => Gadget | Promise<Gadget>;
|
|
72
|
+
/**
|
|
73
|
+
* IPFS gateway URL written into each encrypted field handle so consumers
|
|
74
|
+
* can retrieve ciphertexts without knowing the storage provider.
|
|
75
|
+
*/
|
|
76
|
+
gateway: string;
|
|
77
|
+
options?: {
|
|
78
|
+
/**
|
|
79
|
+
* When false (default) the existing manifest for this (owner, schemaId)
|
|
80
|
+
* is loaded and merged — existing records not in this upload are kept.
|
|
81
|
+
* When true the manifest is fully replaced.
|
|
82
|
+
*/
|
|
83
|
+
overwrite?: boolean;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
interface CommitResult {
|
|
87
|
+
manifestCid: string;
|
|
88
|
+
schemaId: Hex;
|
|
89
|
+
owner: Address;
|
|
90
|
+
entryCount: number;
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
export { CommitResult, EncryptedFieldInput, FieldInput, Manifest, ManifestEntry, PublishRecord, ResolvedEncryptedField, ResolvedField, ResolvedPlainField, UploadParams };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,44 @@
|
|
|
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";
|
|
4
|
+
import { WalletClient } from "viem";
|
|
5
|
+
|
|
6
|
+
//#region src/roles/schema/index.d.ts
|
|
7
|
+
declare class SchemaRole {
|
|
8
|
+
private readonly schemaRegistry;
|
|
9
|
+
private readonly storage;
|
|
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
|
+
*/
|
|
23
|
+
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
|
+
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
|
+
validate(data: Record<string, unknown>, definition: SchemaDefinition): string[];
|
|
40
|
+
private requireAccount;
|
|
41
|
+
private requireAgent0;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { EncryptedField, FieldDefinition, PlainField, RegisterAgentParams, RegisterSchemaParams, RegisteredAgent, RegisteredSchema, SchemaBlobV1, SchemaDefinition, SchemaRole, SchemaRoleConfig };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { SDK } from "agent0-sdk";
|
|
2
|
+
|
|
3
|
+
//#region src/roles/schema/index.ts
|
|
4
|
+
var SchemaRole = class {
|
|
5
|
+
agent0;
|
|
6
|
+
constructor(schemaRegistry, storage, walletClient, config) {
|
|
7
|
+
this.schemaRegistry = schemaRegistry;
|
|
8
|
+
this.storage = storage;
|
|
9
|
+
this.walletClient = walletClient;
|
|
10
|
+
this.agent0 = config ? new SDK({
|
|
11
|
+
chainId: config.chainId,
|
|
12
|
+
rpcUrl: config.rpcUrl,
|
|
13
|
+
privateKey: config.privateKey,
|
|
14
|
+
ipfs: "pinata",
|
|
15
|
+
pinataJwt: config.pinataJwt,
|
|
16
|
+
...config.registryOverrides && { registryOverrides: config.registryOverrides },
|
|
17
|
+
...config.subgraphOverrides && { subgraphOverrides: config.subgraphOverrides }
|
|
18
|
+
}) : null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Register an agent identity via the agent0-sdk / ERC-8004.
|
|
22
|
+
*/
|
|
23
|
+
async registerAgent(params) {
|
|
24
|
+
const agent = this.requireAgent0().createAgent(params.name, params.description);
|
|
25
|
+
if (params.a2aUrl) await agent.setA2A(params.a2aUrl);
|
|
26
|
+
if (params.mcpEndpoint) await agent.setMCP(params.mcpEndpoint);
|
|
27
|
+
if (params.ens) agent.setENS(params.ens);
|
|
28
|
+
agent.setActive(true);
|
|
29
|
+
agent.setX402Support(true);
|
|
30
|
+
const { result: registrationFile } = await (await agent.registerIPFS()).waitConfirmed();
|
|
31
|
+
const agentId = registrationFile.agentId;
|
|
32
|
+
if (!agentId) throw new Error("ERC-8004 registration did not return an agentId");
|
|
33
|
+
return { agentId };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Register a schema on-chain, tied to an existing agent identity.
|
|
37
|
+
*
|
|
38
|
+
* Uploads a versioned schema blob to IPFS (definition + metadata), then
|
|
39
|
+
* calls SchemaRegistry.registerSchema(name, schemaCid, agentId).
|
|
40
|
+
*/
|
|
41
|
+
async register(params) {
|
|
42
|
+
const owner = this.requireAccount();
|
|
43
|
+
const blob = {
|
|
44
|
+
version: 1,
|
|
45
|
+
name: params.name,
|
|
46
|
+
owner,
|
|
47
|
+
agentId: params.agentId,
|
|
48
|
+
definition: params.definition,
|
|
49
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
50
|
+
};
|
|
51
|
+
const schemaCid = await this.storage.store(blob, { metadata: { name: `schema:${params.name}` } });
|
|
52
|
+
const { schemaId } = await this.schemaRegistry.registerSchema(params.name, schemaCid, params.agentId);
|
|
53
|
+
return {
|
|
54
|
+
schemaId,
|
|
55
|
+
schemaCid,
|
|
56
|
+
definition: params.definition,
|
|
57
|
+
name: params.name,
|
|
58
|
+
agentId: params.agentId,
|
|
59
|
+
owner
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Fetch a registered schema. Accepts either a schema name or a bytes32 id.
|
|
64
|
+
* Returns undefined if not registered.
|
|
65
|
+
*/
|
|
66
|
+
async get(nameOrId) {
|
|
67
|
+
try {
|
|
68
|
+
const schemaId = await this.schemaRegistry.schemaId(typeof nameOrId === "string" && !/^0x[0-9a-fA-F]{64}$/.test(nameOrId) ? nameOrId : nameOrId);
|
|
69
|
+
const record = await this.schemaRegistry.getSchema(nameOrId);
|
|
70
|
+
if (!record.cid) return void 0;
|
|
71
|
+
const blob = await this.storage.retrieve(record.cid);
|
|
72
|
+
return {
|
|
73
|
+
schemaId,
|
|
74
|
+
schemaCid: record.cid,
|
|
75
|
+
definition: blob.definition,
|
|
76
|
+
name: blob.name,
|
|
77
|
+
agentId: blob.agentId,
|
|
78
|
+
owner: blob.owner
|
|
79
|
+
};
|
|
80
|
+
} catch (e) {
|
|
81
|
+
console.error(e);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Validate that a data object structurally conforms to a schema definition.
|
|
87
|
+
*
|
|
88
|
+
* Shallow type-level check — catches missing fields and type mismatches
|
|
89
|
+
* before a publisher wastes gas on a bad commit. Encrypted fields require
|
|
90
|
+
* a `handle` object; ciphertext validity is enforced by the encryption
|
|
91
|
+
* service at upload time.
|
|
92
|
+
*
|
|
93
|
+
* Returns a list of validation errors, or an empty array if valid.
|
|
94
|
+
*/
|
|
95
|
+
validate(data, definition) {
|
|
96
|
+
const errors = [];
|
|
97
|
+
for (const [field, fieldDef] of Object.entries(definition)) {
|
|
98
|
+
const value = data[field];
|
|
99
|
+
if (value === void 0 || value === null) {
|
|
100
|
+
errors.push(`Missing required field: "${field}"`);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
switch (fieldDef["@type"]) {
|
|
104
|
+
case "string":
|
|
105
|
+
if (typeof value !== "string") errors.push(`Field "${field}" must be a string, got ${typeof value}`);
|
|
106
|
+
break;
|
|
107
|
+
case "number":
|
|
108
|
+
if (typeof value !== "number") errors.push(`Field "${field}" must be a number, got ${typeof value}`);
|
|
109
|
+
break;
|
|
110
|
+
case "boolean":
|
|
111
|
+
if (typeof value !== "boolean") errors.push(`Field "${field}" must be a boolean, got ${typeof value}`);
|
|
112
|
+
break;
|
|
113
|
+
case "bytes":
|
|
114
|
+
if (!(value instanceof Uint8Array) && !ArrayBuffer.isView(value)) errors.push(`Field "${field}" must be bytes (Uint8Array)`);
|
|
115
|
+
break;
|
|
116
|
+
case "encrypted": {
|
|
117
|
+
const asObj = value;
|
|
118
|
+
if (!asObj.handle || typeof asObj.handle !== "object") errors.push(`Field "${field}" is encrypted — expected a { handle: { cid, gateway } } shape`);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return errors;
|
|
124
|
+
}
|
|
125
|
+
requireAccount() {
|
|
126
|
+
const address = this.walletClient.account?.address;
|
|
127
|
+
if (!address) throw new Error("No account connected to wallet client");
|
|
128
|
+
return address;
|
|
129
|
+
}
|
|
130
|
+
requireAgent0() {
|
|
131
|
+
if (!this.agent0) throw new Error("registerAgent() requires AgentConfig (privateKey + pinataJwt). Pass agentConfig to Fangorn.init() to enable ERC-8004 registration.");
|
|
132
|
+
return this.agent0;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
//#endregion
|
|
137
|
+
export { SchemaRole };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Hex } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/roles/schema/types.d.ts
|
|
4
|
+
interface PlainField {
|
|
5
|
+
"@type": "string" | "number" | "boolean" | "bytes";
|
|
6
|
+
}
|
|
7
|
+
interface EncryptedField {
|
|
8
|
+
"@type": "encrypted";
|
|
9
|
+
/** Gadget identifier — resolved against the GadgetRegistry at publish time */
|
|
10
|
+
gadget: string;
|
|
11
|
+
/** Populated by a publisher after encryption, not defined by the schema owner */
|
|
12
|
+
handle?: {
|
|
13
|
+
cid: string;
|
|
14
|
+
gateway: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
type FieldDefinition = PlainField | EncryptedField;
|
|
18
|
+
type SchemaDefinition = Record<string, FieldDefinition>;
|
|
19
|
+
interface RegisterAgentParams {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
/** Optional A2A agent card URL e.g. https://example.com/.well-known/agentcard.json */
|
|
23
|
+
a2aUrl?: string;
|
|
24
|
+
/** Optional MCP endpoint URL */
|
|
25
|
+
mcpEndpoint?: string;
|
|
26
|
+
/** Optional ENS name */
|
|
27
|
+
ens?: string;
|
|
28
|
+
}
|
|
29
|
+
interface RegisterSchemaParams {
|
|
30
|
+
definition: SchemaDefinition;
|
|
31
|
+
agentId: string;
|
|
32
|
+
name: string;
|
|
33
|
+
}
|
|
34
|
+
interface RegisteredAgent {
|
|
35
|
+
agentId: string;
|
|
36
|
+
}
|
|
37
|
+
interface RegisteredSchema {
|
|
38
|
+
schemaId: Hex;
|
|
39
|
+
schemaCid: string;
|
|
40
|
+
definition: SchemaDefinition;
|
|
41
|
+
name: string;
|
|
42
|
+
agentId: string;
|
|
43
|
+
owner: Hex;
|
|
44
|
+
}
|
|
45
|
+
interface SchemaBlobV1 {
|
|
46
|
+
version: 1;
|
|
47
|
+
name: string;
|
|
48
|
+
owner: Hex;
|
|
49
|
+
agentId: string;
|
|
50
|
+
definition: SchemaDefinition;
|
|
51
|
+
createdAt: string;
|
|
52
|
+
}
|
|
53
|
+
interface SchemaRoleConfig {
|
|
54
|
+
chainId: number;
|
|
55
|
+
rpcUrl: string;
|
|
56
|
+
privateKey: Hex;
|
|
57
|
+
pinataJwt: string;
|
|
58
|
+
/** Optional — override ERC-8004 registry addresses per chainId */
|
|
59
|
+
registryOverrides?: Record<number, {
|
|
60
|
+
IDENTITY: string;
|
|
61
|
+
REPUTATION: string;
|
|
62
|
+
}>;
|
|
63
|
+
/** Optional — override subgraph URLs per chainId */
|
|
64
|
+
subgraphOverrides?: Record<number, string>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { EncryptedField, FieldDefinition, PlainField, RegisterAgentParams, RegisterSchemaParams, RegisteredAgent, RegisteredSchema, SchemaBlobV1, SchemaDefinition, SchemaRoleConfig };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/lib/test/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { TestBed } from "./testbed.js";
|
|
2
|
+
|
|
1
3
|
//#region src/test/index.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Uploads any data to IPFS via Pinata
|
|
4
7
|
*/
|
|
5
|
-
declare function uploadToPinata(name: string, data:
|
|
8
|
+
declare function uploadToPinata(name: string, data: unknown, pinataJwt?: string): Promise<string>;
|
|
6
9
|
//#endregion
|
|
7
|
-
export { uploadToPinata };
|
|
10
|
+
export { TestBed, uploadToPinata };
|
package/lib/test/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { TestBed } from "./testbed.js";
|
|
2
|
+
|
|
1
3
|
//#region src/test/index.ts
|
|
2
4
|
/**
|
|
3
5
|
* Uploads any data to IPFS via Pinata
|
|
4
6
|
*/
|
|
5
7
|
async function uploadToPinata(name, data, pinataJwt) {
|
|
6
|
-
const jwt = pinataJwt
|
|
8
|
+
const jwt = pinataJwt ?? process.env.PINATA_JWT;
|
|
7
9
|
if (!jwt) throw new Error("PINATA_JWT is required");
|
|
8
10
|
const content = typeof data === "object" && !(data instanceof Blob) ? JSON.stringify(data) : data;
|
|
9
11
|
const contentType = typeof data === "object" ? "application/json" : "text/plain";
|
|
@@ -16,10 +18,10 @@ async function uploadToPinata(name, data, pinataJwt) {
|
|
|
16
18
|
});
|
|
17
19
|
if (!response.ok) {
|
|
18
20
|
const text = await response.text();
|
|
19
|
-
throw new Error(`Upload failed: ${response.status} - ${text}`);
|
|
21
|
+
throw new Error(`Upload failed: ${String(response.status)} - ${text}`);
|
|
20
22
|
}
|
|
21
23
|
return (await response.json()).IpfsHash;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
//#endregion
|
|
25
|
-
export { uploadToPinata };
|
|
27
|
+
export { TestBed, uploadToPinata };
|