@fangorn-network/sdk 2026.5.10 → 2026.6.16
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 +87 -37
- package/lib/ crypto/constants.d.ts +4 -0
- package/lib/ crypto/constants.js +5 -0
- package/lib/ crypto/merkle.d.ts +10 -0
- package/lib/ crypto/merkle.js +34 -0
- package/lib/ crypto/utils.d.ts +1 -0
- package/lib/ crypto/utils.js +1 -0
- package/lib/cli/cli.js +61 -156
- package/lib/cli/index.d.ts +171 -170
- package/lib/config.d.ts +258 -257
- package/lib/config.js +3 -3
- package/lib/fangorn.js +2 -2
- package/lib/index.d.ts +1 -3
- package/lib/index.js +1 -3
- package/lib/modules/encryption/rand.d.ts +1 -1
- package/lib/providers/storage/pinata.d.ts +5 -1
- package/lib/providers/storage/pinata.js +24 -3
- package/lib/providers/storage/types.d.ts +8 -0
- package/lib/providers/storage/utils.js +2 -1
- package/lib/registries/datasource-registry/abi.d.ts +34 -4
- package/lib/registries/datasource-registry/abi.js +40 -5
- package/lib/registries/datasource-registry/index.d.ts +22 -13
- package/lib/registries/datasource-registry/index.js +76 -76
- package/lib/registries/index.d.ts +1 -1
- package/lib/registries/schema-registry/index.d.ts +3 -3
- package/lib/registries/schema-registry/index.js +1 -1
- package/lib/registries/settlement-registry/index.d.ts +2 -2
- package/lib/roles/consumer/index.d.ts +3 -3
- package/lib/roles/consumer/index.js +19 -12
- package/lib/roles/publisher/builders/bundle.d.ts +32 -0
- package/lib/roles/publisher/builders/bundle.js +137 -0
- package/lib/roles/publisher/builders/record-set.d.ts +17 -0
- package/lib/roles/publisher/builders/record-set.js +55 -0
- package/lib/roles/publisher/builders/types.d.ts +38 -0
- package/lib/roles/publisher/builders/types.js +1 -0
- package/lib/roles/publisher/builders/utils.d.ts +9 -0
- package/lib/roles/publisher/builders/utils.js +29 -0
- package/lib/roles/publisher/encryption.d.ts +18 -0
- package/lib/roles/publisher/encryption.js +38 -0
- package/lib/roles/publisher/index.d.ts +54 -36
- package/lib/roles/publisher/index.js +137 -174
- package/lib/roles/publisher/types.d.ts +61 -46
- package/lib/roles/schema/index.d.ts +30 -4
- package/lib/roles/schema/index.js +81 -47
- package/lib/roles/schema/types.d.ts +59 -37
- package/lib/roles/schema/validate.d.ts +9 -0
- package/lib/roles/schema/validate.js +128 -0
- package/lib/test/cleanup-embeddings-testdata.d.ts +1 -0
- package/lib/test/cleanup-embeddings-testdata.js +104 -0
- package/lib/test/publish_mb_bundle.d.ts +1 -0
- package/lib/test/publish_mb_bundle.js +296 -0
- package/lib/test/setup-embeddings-testdata.d.ts +13 -0
- package/lib/test/setup-embeddings-testdata.js +209 -0
- package/lib/test/testbed.d.ts +14 -8
- package/lib/test/testbed.js +28 -24
- package/package.json +16 -2
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { validate } from "./validate.js";
|
|
2
|
+
|
|
1
3
|
//#region src/roles/schema/index.ts
|
|
2
4
|
var SchemaRole = class {
|
|
3
5
|
constructor(schemaRegistry, storage, walletClient) {
|
|
@@ -7,80 +9,106 @@ var SchemaRole = class {
|
|
|
7
9
|
}
|
|
8
10
|
async register(params) {
|
|
9
11
|
const owner = this.requireAccount();
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
+
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
13
|
+
let blob;
|
|
14
|
+
if (params.kind === "bundle") {
|
|
15
|
+
const bundle = await this.resolveBundle(params.bundle);
|
|
16
|
+
blob = {
|
|
17
|
+
kind: "bundle",
|
|
18
|
+
name: params.name,
|
|
19
|
+
owner,
|
|
20
|
+
createdAt,
|
|
21
|
+
bundle
|
|
22
|
+
};
|
|
23
|
+
} else blob = {
|
|
24
|
+
kind: "resolver",
|
|
12
25
|
name: params.name,
|
|
13
26
|
owner,
|
|
14
|
-
|
|
27
|
+
createdAt,
|
|
15
28
|
definition: params.definition,
|
|
16
|
-
|
|
29
|
+
types: params.types
|
|
17
30
|
};
|
|
18
31
|
const schemaCid = await this.storage.put(blob, { name: `schema:${params.name}` });
|
|
19
|
-
|
|
20
|
-
|
|
32
|
+
let schemaId;
|
|
33
|
+
try {
|
|
34
|
+
({schemaId} = await this.schemaRegistry.registerSchema(params.name, schemaCid));
|
|
35
|
+
} catch (err) {
|
|
36
|
+
if (!isSchemaAlreadyExists(err)) throw err;
|
|
37
|
+
schemaId = await this.schemaRegistry.schemaId(params.name);
|
|
38
|
+
}
|
|
39
|
+
const base = {
|
|
21
40
|
schemaId,
|
|
22
41
|
schemaCid,
|
|
23
|
-
definition: params.definition,
|
|
24
42
|
name: params.name,
|
|
25
|
-
agentId: params.agentId,
|
|
26
43
|
owner
|
|
27
44
|
};
|
|
45
|
+
return blob.kind === "bundle" ? {
|
|
46
|
+
kind: "bundle",
|
|
47
|
+
...base,
|
|
48
|
+
bundle: blob.bundle
|
|
49
|
+
} : {
|
|
50
|
+
kind: "resolver",
|
|
51
|
+
...base,
|
|
52
|
+
definition: blob.definition,
|
|
53
|
+
types: blob.types
|
|
54
|
+
};
|
|
28
55
|
}
|
|
29
56
|
async get(nameOrId) {
|
|
30
57
|
try {
|
|
31
|
-
const schemaId = await this.schemaRegistry.schemaId(
|
|
58
|
+
const schemaId = await this.schemaRegistry.schemaId(nameOrId);
|
|
32
59
|
const record = await this.schemaRegistry.getSchema(nameOrId);
|
|
33
60
|
if (!record.specCid) return void 0;
|
|
34
61
|
const blob = await this.storage.get(record.specCid);
|
|
35
|
-
|
|
62
|
+
const base = {
|
|
36
63
|
schemaId,
|
|
37
64
|
schemaCid: record.specCid,
|
|
38
|
-
definition: blob.definition,
|
|
39
65
|
name: blob.name,
|
|
40
|
-
agentId: blob.agentId,
|
|
41
66
|
owner: blob.owner
|
|
42
67
|
};
|
|
68
|
+
if (blob.kind === "resolver") return {
|
|
69
|
+
kind: "resolver",
|
|
70
|
+
...base,
|
|
71
|
+
definition: blob.definition,
|
|
72
|
+
types: blob.types
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
kind: "bundle",
|
|
76
|
+
...base,
|
|
77
|
+
bundle: blob.bundle
|
|
78
|
+
};
|
|
43
79
|
} catch (e) {
|
|
44
80
|
console.error(e);
|
|
45
81
|
return;
|
|
46
82
|
}
|
|
47
83
|
}
|
|
48
|
-
|
|
84
|
+
/** Resolve node refs → registered resolver schemaIds and enforce edge closure. */
|
|
85
|
+
async resolveBundle(input) {
|
|
49
86
|
const errors = [];
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
const items = fieldDef.items;
|
|
78
|
-
value.forEach((item, i) => errors.push(...this.validate({ [`${field}[${i.toString()}]`]: item }, { [`${field}[${i.toString()}]`]: items })));
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return errors;
|
|
87
|
+
if (Object.keys(input.nodes).length === 0) errors.push("bundle declares no node types");
|
|
88
|
+
const nodes = {};
|
|
89
|
+
await Promise.all(Object.entries(input.nodes).map(async ([typeName, ref]) => {
|
|
90
|
+
const existing = await this.get(ref);
|
|
91
|
+
if (!existing) errors.push(`node "${typeName}" → unknown schema "${ref}"`);
|
|
92
|
+
else if (existing.kind !== "resolver") errors.push(`node "${typeName}" → "${ref}" is a bundle; nodes must be resolver schemas`);
|
|
93
|
+
else nodes[typeName] = existing.schemaId;
|
|
94
|
+
}));
|
|
95
|
+
input.edges.forEach((e, i) => {
|
|
96
|
+
if (!e.rel) errors.push(`edge[${i.toString()}] missing rel`);
|
|
97
|
+
if (!(e.from in input.nodes)) errors.push(`edge[${i.toString()}] "${e.rel}" from undeclared type "${e.from}"`);
|
|
98
|
+
if (!(e.to in input.nodes)) errors.push(`edge[${i.toString()}] "${e.rel}" to undeclared type "${e.to}"`);
|
|
99
|
+
const min = e.min ?? 0;
|
|
100
|
+
const max = e.max ?? null;
|
|
101
|
+
if (min < 0) errors.push(`edge[${i.toString()}] "${e.rel}" min < 0`);
|
|
102
|
+
if (max !== null && max < min) errors.push(`edge[${i.toString()}] "${e.rel}" max(${max.toString()}) < min(${min.toString()})`);
|
|
103
|
+
});
|
|
104
|
+
if (errors.length) throw new Error("Invalid bundle shape:\n" + errors.map((e) => ` - ${e}`).join("\n"));
|
|
105
|
+
return {
|
|
106
|
+
nodes,
|
|
107
|
+
edges: input.edges
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
validate(data, definition) {
|
|
111
|
+
return validate(data, definition);
|
|
84
112
|
}
|
|
85
113
|
requireAccount() {
|
|
86
114
|
const address = this.walletClient.account?.address;
|
|
@@ -88,6 +116,12 @@ var SchemaRole = class {
|
|
|
88
116
|
return address;
|
|
89
117
|
}
|
|
90
118
|
};
|
|
119
|
+
function isSchemaAlreadyExists(err) {
|
|
120
|
+
if (!(err instanceof Error)) return false;
|
|
121
|
+
const causeData = err.cause?.data;
|
|
122
|
+
const errorData = err.data;
|
|
123
|
+
return (causeData ?? errorData)?.errorName === "SchemaAlreadyExists";
|
|
124
|
+
}
|
|
91
125
|
|
|
92
126
|
//#endregion
|
|
93
127
|
export { SchemaRole };
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Hex } from "viem";
|
|
2
2
|
|
|
3
3
|
//#region src/roles/schema/types.d.ts
|
|
4
|
+
type SchemaKind = "resolver" | "bundle";
|
|
5
|
+
type SchemaDefinition = Record<string, FieldDefinition>;
|
|
4
6
|
type ScalarType = "string" | "number" | "boolean" | "bytes";
|
|
5
7
|
type NullableType = `${ScalarType} | null` | "array | null" | "object | null";
|
|
6
8
|
type FieldType = ScalarType | "array" | "object" | NullableType;
|
|
@@ -15,9 +17,7 @@ type PlainField = {
|
|
|
15
17
|
};
|
|
16
18
|
interface EncryptedField {
|
|
17
19
|
"@type": "encrypted";
|
|
18
|
-
/** Gadget identifier — resolved against the GadgetRegistry at publish time */
|
|
19
20
|
gadget: string;
|
|
20
|
-
/** Populated by a publisher after encryption, not defined by the schema owner */
|
|
21
21
|
handle?: {
|
|
22
22
|
cid: string;
|
|
23
23
|
gateway: string;
|
|
@@ -26,54 +26,76 @@ interface EncryptedField {
|
|
|
26
26
|
interface HandleField {
|
|
27
27
|
"@type": "handle";
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
type Constraint = {
|
|
30
|
+
kind: "regex";
|
|
31
|
+
pattern: string;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "enum";
|
|
34
|
+
values: (string | number | boolean)[];
|
|
35
|
+
} | {
|
|
36
|
+
kind: "range";
|
|
37
|
+
min?: number;
|
|
38
|
+
max?: number;
|
|
39
|
+
exclusive?: boolean;
|
|
40
|
+
} | {
|
|
41
|
+
kind: "length";
|
|
42
|
+
min?: number;
|
|
43
|
+
max?: number;
|
|
44
|
+
} | {
|
|
45
|
+
kind: "ref";
|
|
46
|
+
type: string;
|
|
47
|
+
};
|
|
48
|
+
interface TypeDefinition {
|
|
49
|
+
shape: Record<string, FieldDefinition>;
|
|
50
|
+
constraints?: Constraint[];
|
|
36
51
|
}
|
|
37
52
|
interface FieldDefinition {
|
|
38
53
|
"@type": string;
|
|
39
54
|
"@description"?: string;
|
|
55
|
+
constraints?: Constraint[];
|
|
40
56
|
items?: FieldDefinition | Record<string, FieldDefinition>;
|
|
41
57
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
description: string;
|
|
46
|
-
/** Optional A2A agent card URL e.g. https://example.com/.well-known/agentcard.json */
|
|
47
|
-
a2aUrl?: string;
|
|
48
|
-
/** Optional MCP endpoint URL */
|
|
49
|
-
mcpEndpoint?: string;
|
|
50
|
-
/** Optional ENS name */
|
|
51
|
-
ens?: string;
|
|
58
|
+
interface SchemaDoc {
|
|
59
|
+
types?: Record<string, TypeDefinition>;
|
|
60
|
+
fields: SchemaDefinition;
|
|
52
61
|
}
|
|
53
|
-
interface
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
interface EdgeShape {
|
|
63
|
+
rel: string;
|
|
64
|
+
from: string;
|
|
65
|
+
to: string;
|
|
66
|
+
min?: number;
|
|
67
|
+
max?: number | null;
|
|
57
68
|
}
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
/** Author-facing input: node refs are schema names OR ids. */
|
|
70
|
+
interface BundleInput {
|
|
71
|
+
nodes: Record<string, string>;
|
|
72
|
+
edges: EdgeShape[];
|
|
60
73
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
/** Committed form: node refs resolved + pinned to schemaIds. */
|
|
75
|
+
interface ResolvedBundle {
|
|
76
|
+
nodes: Record<string, Hex>;
|
|
77
|
+
edges: EdgeShape[];
|
|
78
|
+
}
|
|
79
|
+
/** The on-storage bundle shape (what specCid points to for a bundle). */
|
|
80
|
+
interface BundleShape {
|
|
81
|
+
"@kind": "bundle";
|
|
82
|
+
bundle: ResolvedBundle;
|
|
68
83
|
}
|
|
69
|
-
interface
|
|
70
|
-
version: 1;
|
|
84
|
+
interface SchemaBlobBase {
|
|
71
85
|
name: string;
|
|
72
86
|
owner: Hex;
|
|
73
|
-
agentId: string;
|
|
74
|
-
definition: SchemaDefinition;
|
|
75
87
|
createdAt: string;
|
|
76
88
|
}
|
|
89
|
+
interface ResolverSchemaBlob extends SchemaBlobBase {
|
|
90
|
+
kind: "resolver";
|
|
91
|
+
definition: SchemaDefinition;
|
|
92
|
+
types?: Record<string, TypeDefinition>;
|
|
93
|
+
}
|
|
94
|
+
interface BundleSchemaBlob extends SchemaBlobBase {
|
|
95
|
+
kind: "bundle";
|
|
96
|
+
bundle: ResolvedBundle;
|
|
97
|
+
}
|
|
98
|
+
type SchemaBlob = ResolverSchemaBlob | BundleSchemaBlob;
|
|
77
99
|
interface SchemaRoleConfig {
|
|
78
100
|
chainId: number;
|
|
79
101
|
rpcUrl: string;
|
|
@@ -88,4 +110,4 @@ interface SchemaRoleConfig {
|
|
|
88
110
|
subgraphOverrides?: Record<number, string>;
|
|
89
111
|
}
|
|
90
112
|
//#endregion
|
|
91
|
-
export { EncryptedField, FieldDefinition, FieldType, HandleField, NullableType, PlainField,
|
|
113
|
+
export { BundleInput, BundleSchemaBlob, BundleShape, Constraint, EdgeShape, EncryptedField, FieldDefinition, FieldType, HandleField, NullableType, PlainField, ResolvedBundle, ResolverSchemaBlob, ScalarType, SchemaBlob, SchemaDefinition, SchemaDoc, SchemaKind, SchemaRoleConfig, TypeDefinition };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Constraint, SchemaDefinition, SchemaDoc } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/roles/schema/validate.d.ts
|
|
4
|
+
/** Accept either the explicit document shape or a bare flat field map. */
|
|
5
|
+
declare function normalize(schema: SchemaDoc | SchemaDefinition): SchemaDoc;
|
|
6
|
+
declare function validate(data: Record<string, unknown>, schema: SchemaDoc | SchemaDefinition): string[];
|
|
7
|
+
declare function applyConstraints(value: unknown, constraints: Constraint[], doc: SchemaDoc, path: string): string[];
|
|
8
|
+
//#endregion
|
|
9
|
+
export { applyConstraints, normalize, validate };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//#region src/roles/schema/validate.ts
|
|
2
|
+
const BUILT_INS = new Set([
|
|
3
|
+
"string",
|
|
4
|
+
"number",
|
|
5
|
+
"boolean",
|
|
6
|
+
"bytes",
|
|
7
|
+
"handle",
|
|
8
|
+
"array",
|
|
9
|
+
"object",
|
|
10
|
+
"encrypted"
|
|
11
|
+
]);
|
|
12
|
+
/** Accept either the explicit document shape or a bare flat field map. */
|
|
13
|
+
function normalize(schema) {
|
|
14
|
+
if ("fields" in schema && typeof schema.fields === "object") return schema;
|
|
15
|
+
return { fields: schema };
|
|
16
|
+
}
|
|
17
|
+
function validate(data, schema) {
|
|
18
|
+
const doc = normalize(schema);
|
|
19
|
+
const errors = [];
|
|
20
|
+
for (const [field, def] of Object.entries(doc.fields)) errors.push(...validateValue(data[field], def, doc, field));
|
|
21
|
+
return errors;
|
|
22
|
+
}
|
|
23
|
+
function validateValue(value, def, doc, path) {
|
|
24
|
+
const rawType = def["@type"];
|
|
25
|
+
const nullable = rawType.endsWith("| null");
|
|
26
|
+
const baseType = nullable ? rawType.replace("| null", "").trim() : rawType;
|
|
27
|
+
if (value === void 0 || value === null) return nullable ? [] : [`${path}: missing required field`];
|
|
28
|
+
const errors = [];
|
|
29
|
+
if (BUILT_INS.has(baseType)) errors.push(...validateBuiltIn(value, baseType, def, doc, path));
|
|
30
|
+
else {
|
|
31
|
+
const customType = doc.types?.[baseType];
|
|
32
|
+
if (!customType) {
|
|
33
|
+
errors.push(`${path}: unknown @type "${rawType}"`);
|
|
34
|
+
return errors;
|
|
35
|
+
}
|
|
36
|
+
errors.push(...validateCustomType(value, customType, doc, path));
|
|
37
|
+
}
|
|
38
|
+
if (def.constraints) errors.push(...applyConstraints(value, def.constraints, doc, path));
|
|
39
|
+
return errors;
|
|
40
|
+
}
|
|
41
|
+
function validateBuiltIn(value, baseType, def, doc, path) {
|
|
42
|
+
const errors = [];
|
|
43
|
+
switch (baseType) {
|
|
44
|
+
case "string":
|
|
45
|
+
if (typeof value !== "string") errors.push(`${path}: must be a string, got ${typeof value}`);
|
|
46
|
+
break;
|
|
47
|
+
case "number":
|
|
48
|
+
if (typeof value !== "number") errors.push(`${path}: must be a number, got ${typeof value}`);
|
|
49
|
+
break;
|
|
50
|
+
case "boolean":
|
|
51
|
+
if (typeof value !== "boolean") errors.push(`${path}: must be a boolean, got ${typeof value}`);
|
|
52
|
+
break;
|
|
53
|
+
case "bytes":
|
|
54
|
+
if (!(value instanceof Uint8Array) && !ArrayBuffer.isView(value)) errors.push(`${path}: must be bytes (Uint8Array)`);
|
|
55
|
+
break;
|
|
56
|
+
case "handle":
|
|
57
|
+
if (typeof value?.uri !== "string") errors.push(`${path}: is a handle — expected { uri: string }`);
|
|
58
|
+
break;
|
|
59
|
+
case "array": {
|
|
60
|
+
if (!Array.isArray(value)) {
|
|
61
|
+
errors.push(`${path}: must be an array, got ${typeof value}`);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
const items = def.items;
|
|
65
|
+
if (items) value.forEach((item, i) => errors.push(...validateValue(item, items, doc, `${path}[${i}]`)));
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case "object": {
|
|
69
|
+
if (typeof value !== "object" || value === null) {
|
|
70
|
+
errors.push(`${path}: must be an object, got ${typeof value}`);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
const items = def.items;
|
|
74
|
+
if (items) {
|
|
75
|
+
const obj = value;
|
|
76
|
+
for (const [k, sub] of Object.entries(items)) errors.push(...validateValue(obj[k], sub, doc, `${path}.${k}`));
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "encrypted": break;
|
|
81
|
+
}
|
|
82
|
+
return errors;
|
|
83
|
+
}
|
|
84
|
+
function validateCustomType(value, type, doc, path) {
|
|
85
|
+
if (typeof value !== "object" || value === null) return [`${path}: expected object for custom type`];
|
|
86
|
+
const obj = value;
|
|
87
|
+
const errors = [];
|
|
88
|
+
for (const [k, subDef] of Object.entries(type.shape)) errors.push(...validateValue(obj[k], subDef, doc, `${path}.${k}`));
|
|
89
|
+
if (type.constraints) errors.push(...applyConstraints(value, type.constraints, doc, path));
|
|
90
|
+
return errors;
|
|
91
|
+
}
|
|
92
|
+
function applyConstraints(value, constraints, doc, path) {
|
|
93
|
+
const errors = [];
|
|
94
|
+
for (const c of constraints) {
|
|
95
|
+
const err = applyOne(value, c, doc, path);
|
|
96
|
+
if (err) errors.push(err);
|
|
97
|
+
}
|
|
98
|
+
return errors;
|
|
99
|
+
}
|
|
100
|
+
function applyOne(value, c, doc, path) {
|
|
101
|
+
switch (c.kind) {
|
|
102
|
+
case "regex":
|
|
103
|
+
if (typeof value !== "string") return `${path}: regex constraint requires a string`;
|
|
104
|
+
return new RegExp(c.pattern).test(value) ? null : `${path}: must match /${c.pattern}/`;
|
|
105
|
+
case "enum": return c.values.includes(value) ? null : `${path}: must be one of ${JSON.stringify(c.values)}`;
|
|
106
|
+
case "range":
|
|
107
|
+
if (typeof value !== "number") return `${path}: range constraint requires a number`;
|
|
108
|
+
if (c.min !== void 0 && (c.exclusive ? value <= c.min : value < c.min)) return `${path}: must be ${c.exclusive ? ">" : ">="} ${c.min}`;
|
|
109
|
+
if (c.max !== void 0 && (c.exclusive ? value >= c.max : value > c.max)) return `${path}: must be ${c.exclusive ? "<" : "<="} ${c.max}`;
|
|
110
|
+
return null;
|
|
111
|
+
case "length": {
|
|
112
|
+
const len = typeof value === "string" ? value.length : Array.isArray(value) ? value.length : value instanceof Uint8Array ? value.length : -1;
|
|
113
|
+
if (len < 0) return `${path}: length constraint requires a string, array, or bytes`;
|
|
114
|
+
if (c.min !== void 0 && len < c.min) return `${path}: length must be >= ${c.min}`;
|
|
115
|
+
if (c.max !== void 0 && len > c.max) return `${path}: length must be <= ${c.max}`;
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
case "ref": {
|
|
119
|
+
const target = doc.types?.[c.type];
|
|
120
|
+
if (!target) return `${path}: ref constraint references unknown type "${c.type}"`;
|
|
121
|
+
const subErrors = validateCustomType(value, target, doc, path);
|
|
122
|
+
return subErrors.length === 0 ? null : subErrors.join("\n");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
export { applyConstraints, normalize, validate };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { TestBed } from "./testbed.js";
|
|
2
|
+
import { LEDGER_FILE } from "./setup-embeddings-testdata.js";
|
|
3
|
+
import { arbitrumSepolia } from "viem/chains";
|
|
4
|
+
import { createWalletClient, http } from "viem";
|
|
5
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
|
|
8
|
+
//#region src/test/cleanup-embeddings-testdata.ts
|
|
9
|
+
/**
|
|
10
|
+
* Cleanup counterpart to setup-embeddings-testdata.ts.
|
|
11
|
+
*
|
|
12
|
+
* Reads the ledger written by the setup script and unpins, for every recorded
|
|
13
|
+
* run, the v3 bundle manifest plus all of its node + edge chunk CIDs from
|
|
14
|
+
* Pinata. Schema registrations are on-chain and cannot be removed; this only
|
|
15
|
+
* reclaims the IPFS pins the test data created.
|
|
16
|
+
*
|
|
17
|
+
* Run it:
|
|
18
|
+
*
|
|
19
|
+
* pnpm cleanup:embeddings
|
|
20
|
+
* # or: pnpm dotenvx run -f .env -- tsx src/test/cleanup-embeddings-testdata.ts
|
|
21
|
+
*
|
|
22
|
+
* The ledger is deleted only if every CID unpinned cleanly.
|
|
23
|
+
*/
|
|
24
|
+
const SK = process.env.DELEGATOR_ETH_PRIVATE_KEY;
|
|
25
|
+
const RPC_URL = process.env.RPC_URL ?? process.env.CHAIN_RPC_URL ?? "https://sepolia-rollup.arbitrum.io/rpc";
|
|
26
|
+
const WORKER_URL = process.env.WORKER_URL ?? "http://localhost:8787";
|
|
27
|
+
const PINATA_JWT = process.env.PINATA_JWT ?? "";
|
|
28
|
+
const SETTLEMENT_REGISTRY_ADDRESS = process.env.SETTLEMENT_REGISTRY_ADDRESS;
|
|
29
|
+
const DATA_SOURCE_REGISTRY_ADDRESS = process.env.DATA_SOURCE_REGISTRY_ADDRESS;
|
|
30
|
+
const SCHEMA_REGISTRY_ADDRESS = process.env.SCHEMA_REGISTRY_ADDRESS;
|
|
31
|
+
const USDC_ADDRESS = process.env.USDC_ADDRESS ?? process.env.USDC_CONTRACT_ADDRESS ?? "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d";
|
|
32
|
+
const USDC_DOMAIN = "USD Coin";
|
|
33
|
+
const CHAIN = arbitrumSepolia;
|
|
34
|
+
function makeWallet(key) {
|
|
35
|
+
return createWalletClient({
|
|
36
|
+
account: privateKeyToAccount(key),
|
|
37
|
+
chain: CHAIN,
|
|
38
|
+
transport: http(RPC_URL)
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async function unpinFromPinata(cid) {
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch(`https://api.pinata.cloud/pinning/unpin/${cid}`, {
|
|
44
|
+
method: "DELETE",
|
|
45
|
+
headers: { Authorization: `Bearer ${PINATA_JWT}` }
|
|
46
|
+
});
|
|
47
|
+
if (response.ok) {
|
|
48
|
+
console.log(` ✓ unpinned ${cid}`);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
if (response.status === 404) {
|
|
52
|
+
console.log(` - ${cid} not pinned (already gone)`);
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
console.warn(` ✗ ${cid}: ${response.status.toString()} ${response.statusText}`);
|
|
56
|
+
return false;
|
|
57
|
+
} catch (err) {
|
|
58
|
+
console.warn(` ✗ ${cid}: request failed`, err);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function main() {
|
|
63
|
+
if (!PINATA_JWT) throw new Error("PINATA_JWT is required to unpin.");
|
|
64
|
+
if (!existsSync(LEDGER_FILE)) {
|
|
65
|
+
console.log(`[cleanup] no ledger at ${LEDGER_FILE} — nothing to clean up.`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const entries = JSON.parse(readFileSync(LEDGER_FILE, "utf8"));
|
|
69
|
+
if (entries.length === 0) {
|
|
70
|
+
console.log("[cleanup] ledger is empty — nothing to clean up.");
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const publisher = TestBed.init(makeWallet(SK), DATA_SOURCE_REGISTRY_ADDRESS, SCHEMA_REGISTRY_ADDRESS, SETTLEMENT_REGISTRY_ADDRESS, USDC_ADDRESS, USDC_DOMAIN, RPC_URL, "arbitrumSepolia", CHAIN.id, WORKER_URL).getDelegatorFangorn().publisher;
|
|
74
|
+
let allOk = true;
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
console.log(`\n[cleanup] ${entry.bundleName} (manifest ${entry.manifestUri})`);
|
|
77
|
+
const cids = [];
|
|
78
|
+
try {
|
|
79
|
+
const manifest = await publisher.getBundleManifestByCid(entry.manifestUri);
|
|
80
|
+
if (manifest) {
|
|
81
|
+
for (const c of manifest.nodeChunks) cids.push(c.dataCid);
|
|
82
|
+
if (manifest.edgeChunk.dataCid) cids.push(manifest.edgeChunk.dataCid);
|
|
83
|
+
} else console.warn(" (could not read manifest — unpinning manifest CID only)");
|
|
84
|
+
} catch (err) {
|
|
85
|
+
console.warn(" (failed to read manifest — unpinning manifest CID only)", err);
|
|
86
|
+
}
|
|
87
|
+
cids.push(entry.manifestUri);
|
|
88
|
+
for (const cid of cids) {
|
|
89
|
+
const ok = await unpinFromPinata(cid);
|
|
90
|
+
allOk = allOk && ok;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (allOk) {
|
|
94
|
+
writeFileSync(LEDGER_FILE, "[]");
|
|
95
|
+
console.log(`\n✅ Cleanup complete. Cleared ${LEDGER_FILE}.`);
|
|
96
|
+
} else console.log(`\n⚠️ Cleanup finished with some failures — ledger left in place (${LEDGER_FILE}) so you can re-run.`);
|
|
97
|
+
}
|
|
98
|
+
main().catch((err) => {
|
|
99
|
+
console.error("\n[cleanup] failed:", err);
|
|
100
|
+
process.exit(1);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|