@fengce/sdk 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/abi.d.ts +142 -0
- package/dist/abi.js +21 -0
- package/dist/artifacts.d.ts +5341 -0
- package/dist/artifacts.js +53 -0
- package/dist/catalog.d.ts +41 -0
- package/dist/catalog.js +19 -0
- package/dist/cli/quote.d.ts +2 -0
- package/dist/cli/quote.js +74 -0
- package/dist/evaluate.d.ts +124 -0
- package/dist/evaluate.js +221 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/install.d.ts +80 -0
- package/dist/install.js +115 -0
- package/dist/netlist.d.ts +18 -0
- package/dist/netlist.js +44 -0
- package/dist/quote.d.ts +94 -0
- package/dist/quote.js +344 -0
- package/dist/schema.d.ts +77 -0
- package/dist/schema.js +170 -0
- package/dist/tiers.d.ts +12 -0
- package/dist/tiers.js +18 -0
- package/dist/tiers.json +4 -0
- package/package.json +54 -4
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type Hex } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Schema file format "fengce-schema/1": the typed input layout of one circuit.
|
|
4
|
+
*
|
|
5
|
+
* {
|
|
6
|
+
* "format": "fengce-schema/1",
|
|
7
|
+
* "netlistHash": "0x…", // keccak256 of the circuit netlist the layout is bound to
|
|
8
|
+
* "nIn": 2, // circuit input count
|
|
9
|
+
* "fields": [
|
|
10
|
+
* { "name": "overLimit", "kind": "bool", "offset": 0, "width": 1 },
|
|
11
|
+
* { "name": "one", "kind": "flag", "offset": 1, "width": 1, "value": 1 }
|
|
12
|
+
* ],
|
|
13
|
+
* "bindings": [ { "field": "overLimit", "source": "AmountGt", "param": "100" } ] // optional, FengceGuard only
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* Kinds: "uint" (1..256 bits, value < 2^width), "bool" (1 bit, 0 or 1), "flag" (1 bit fixed by the
|
|
17
|
+
* schema in "value"; takes no caller value). Bits are packed least significant first (TAP-02).
|
|
18
|
+
* Every input bit must belong to exactly one field.
|
|
19
|
+
*
|
|
20
|
+
* schemaHash = keccak256(abi.encode(netlistHash, nIn, fields)) with fields as
|
|
21
|
+
* (uint8 kind, uint32 offset, uint32 width, uint256 fixedValue)[], kind uint=0 bool=1 flag=2.
|
|
22
|
+
* This equals Schema.hash in src/core/Schema.sol and is what PinRegistry stores as schemaHash.
|
|
23
|
+
* Field names and bindings are not part of the hash.
|
|
24
|
+
*/
|
|
25
|
+
export type FieldKind = "uint" | "bool" | "flag";
|
|
26
|
+
export interface SchemaField {
|
|
27
|
+
name: string;
|
|
28
|
+
kind: FieldKind;
|
|
29
|
+
offset: number;
|
|
30
|
+
width: number;
|
|
31
|
+
value?: number | string;
|
|
32
|
+
}
|
|
33
|
+
/** FengceGuard fact sources (TxFacts.Source in src/core/TxFacts.sol), in enum order. */
|
|
34
|
+
export declare const BINDING_SOURCES: readonly ["To", "Value", "Token", "Amount", "Kind", "AmountGt", "ToIs", "TokenIs", "Selector", "Actor", "ActorIs", "TokenOut", "MinAmountOut", "Recipient", "RecipientIsForeign", "Deadline", "Action", "Outflow", "OutflowGt", "AllowanceIncrease", "AllowanceIncreaseGt"];
|
|
35
|
+
export type BindingSource = (typeof BINDING_SOURCES)[number];
|
|
36
|
+
export interface SchemaBinding {
|
|
37
|
+
field: string;
|
|
38
|
+
source: BindingSource;
|
|
39
|
+
param?: string | number;
|
|
40
|
+
}
|
|
41
|
+
export interface SchemaFile {
|
|
42
|
+
format: "fengce-schema/1";
|
|
43
|
+
netlistHash: Hex;
|
|
44
|
+
nIn: number;
|
|
45
|
+
fields: SchemaField[];
|
|
46
|
+
bindings?: SchemaBinding[];
|
|
47
|
+
}
|
|
48
|
+
/** The on-chain Schema.Layout struct, ready for viem calls. */
|
|
49
|
+
export interface Layout {
|
|
50
|
+
netlistHash: Hex;
|
|
51
|
+
nIn: number;
|
|
52
|
+
fields: {
|
|
53
|
+
kind: number;
|
|
54
|
+
offset: number;
|
|
55
|
+
width: number;
|
|
56
|
+
fixedValue: bigint;
|
|
57
|
+
}[];
|
|
58
|
+
}
|
|
59
|
+
export declare class SchemaError extends Error {
|
|
60
|
+
}
|
|
61
|
+
/** Parses and validates a schema (same rules as Schema.validate on-chain). Throws SchemaError. */
|
|
62
|
+
export declare function parseSchema(input: unknown): SchemaFile;
|
|
63
|
+
/** The on-chain Schema.Layout for this schema. */
|
|
64
|
+
export declare function toLayout(schema: SchemaFile): Layout;
|
|
65
|
+
/** keccak256(abi.encode(netlistHash, nIn, fields)), equal to Schema.hash on-chain. */
|
|
66
|
+
export declare function schemaHash(schema: SchemaFile | Layout): Hex;
|
|
67
|
+
/**
|
|
68
|
+
* Strict encoder: one value per non-flag field (by name), packed LSB first into exactly ceil(nIn/8)
|
|
69
|
+
* bytes. Throws on a missing or unknown name, a value wider than its field, or a bool that is not 0/1.
|
|
70
|
+
* It never truncates.
|
|
71
|
+
*/
|
|
72
|
+
export declare function encodeInputs(schema: SchemaFile, values: Record<string, bigint | number | boolean | string>): Hex;
|
|
73
|
+
/**
|
|
74
|
+
* What a TapeOut processor actually reads from a value written into a field: only the low `width`
|
|
75
|
+
* bits (TAP-02 reads input leniently). Exposed to show the truncation that encodeInputs refuses.
|
|
76
|
+
*/
|
|
77
|
+
export declare function lenientFieldValue(value: bigint, width: number): bigint;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { encodeAbiParameters, isHex, keccak256, toHex } from "viem";
|
|
2
|
+
/** FengceGuard fact sources (TxFacts.Source in src/core/TxFacts.sol), in enum order. */
|
|
3
|
+
export const BINDING_SOURCES = [
|
|
4
|
+
"To",
|
|
5
|
+
"Value",
|
|
6
|
+
"Token",
|
|
7
|
+
"Amount",
|
|
8
|
+
"Kind",
|
|
9
|
+
"AmountGt",
|
|
10
|
+
"ToIs",
|
|
11
|
+
"TokenIs",
|
|
12
|
+
"Selector",
|
|
13
|
+
// policy context version 1 (docs/POLICY-INTERFACE.md)
|
|
14
|
+
"Actor",
|
|
15
|
+
"ActorIs",
|
|
16
|
+
"TokenOut",
|
|
17
|
+
"MinAmountOut",
|
|
18
|
+
"Recipient",
|
|
19
|
+
"RecipientIsForeign",
|
|
20
|
+
"Deadline",
|
|
21
|
+
"Action",
|
|
22
|
+
// policy context version 2: outcome facts (FengceInspector)
|
|
23
|
+
"Outflow",
|
|
24
|
+
"OutflowGt",
|
|
25
|
+
"AllowanceIncrease",
|
|
26
|
+
"AllowanceIncreaseGt",
|
|
27
|
+
];
|
|
28
|
+
export class SchemaError extends Error {
|
|
29
|
+
}
|
|
30
|
+
const KIND_ID = { uint: 0, bool: 1, flag: 2 };
|
|
31
|
+
const MAX_IN = 65_536;
|
|
32
|
+
/** Parses and validates a schema (same rules as Schema.validate on-chain). Throws SchemaError. */
|
|
33
|
+
export function parseSchema(input) {
|
|
34
|
+
const s = (typeof input === "string" ? JSON.parse(input) : input);
|
|
35
|
+
if (!s || s.format !== "fengce-schema/1")
|
|
36
|
+
throw new SchemaError('format must be "fengce-schema/1"');
|
|
37
|
+
if (!isHex(s.netlistHash) || s.netlistHash.length !== 66)
|
|
38
|
+
throw new SchemaError("netlistHash must be a 32-byte hex string");
|
|
39
|
+
if (!Number.isInteger(s.nIn) || s.nIn < 0 || s.nIn > MAX_IN)
|
|
40
|
+
throw new SchemaError(`nIn must be an integer in 0..${MAX_IN}`);
|
|
41
|
+
if (!Array.isArray(s.fields))
|
|
42
|
+
throw new SchemaError("fields must be an array");
|
|
43
|
+
const names = new Set();
|
|
44
|
+
const taken = new Uint8Array(s.nIn);
|
|
45
|
+
let sum = 0;
|
|
46
|
+
s.fields.forEach((f, k) => {
|
|
47
|
+
if (typeof f.name !== "string" || !f.name)
|
|
48
|
+
throw new SchemaError(`field ${k}: name required`);
|
|
49
|
+
if (names.has(f.name))
|
|
50
|
+
throw new SchemaError(`field ${k}: duplicate name "${f.name}"`);
|
|
51
|
+
names.add(f.name);
|
|
52
|
+
if (!(f.kind in KIND_ID))
|
|
53
|
+
throw new SchemaError(`field ${f.name}: unknown kind "${f.kind}"`);
|
|
54
|
+
if (!Number.isInteger(f.offset) || f.offset < 0)
|
|
55
|
+
throw new SchemaError(`field ${f.name}: bad offset`);
|
|
56
|
+
if (f.kind === "uint") {
|
|
57
|
+
if (!Number.isInteger(f.width) || f.width < 1 || f.width > 256)
|
|
58
|
+
throw new SchemaError(`field ${f.name}: uint width must be 1..256`);
|
|
59
|
+
if (f.value !== undefined)
|
|
60
|
+
throw new SchemaError(`field ${f.name}: only flags have a value`);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
if (f.width !== 1)
|
|
64
|
+
throw new SchemaError(`field ${f.name}: ${f.kind} width must be 1`);
|
|
65
|
+
if (f.kind === "bool" && f.value !== undefined)
|
|
66
|
+
throw new SchemaError(`field ${f.name}: only flags have a value`);
|
|
67
|
+
if (f.kind === "flag" && !(BigInt(f.value ?? -1) === 0n || BigInt(f.value ?? -1) === 1n)) {
|
|
68
|
+
throw new SchemaError(`field ${f.name}: flag value must be 0 or 1`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (f.offset + f.width > s.nIn)
|
|
72
|
+
throw new SchemaError(`field ${f.name}: bits ${f.offset}..${f.offset + f.width - 1} are outside nIn ${s.nIn}`);
|
|
73
|
+
for (let b = f.offset; b < f.offset + f.width; b++) {
|
|
74
|
+
if (taken[b])
|
|
75
|
+
throw new SchemaError(`field ${f.name}: bit ${b} overlaps another field`);
|
|
76
|
+
taken[b] = 1;
|
|
77
|
+
}
|
|
78
|
+
sum += f.width;
|
|
79
|
+
});
|
|
80
|
+
if (sum < s.nIn)
|
|
81
|
+
throw new SchemaError(`unmapped bits: fields cover ${sum} of ${s.nIn} inputs`);
|
|
82
|
+
if (sum > s.nIn)
|
|
83
|
+
throw new SchemaError(`width mismatch: fields cover ${sum} bits, nIn is ${s.nIn}`);
|
|
84
|
+
for (const b of s.bindings ?? []) {
|
|
85
|
+
const f = s.fields.find((x) => x.name === b.field);
|
|
86
|
+
if (!f)
|
|
87
|
+
throw new SchemaError(`binding: unknown field "${b.field}"`);
|
|
88
|
+
if (f.kind === "flag")
|
|
89
|
+
throw new SchemaError(`binding: flag "${b.field}" takes no binding`);
|
|
90
|
+
if (!BINDING_SOURCES.includes(b.source))
|
|
91
|
+
throw new SchemaError(`binding ${b.field}: unknown source "${b.source}"`);
|
|
92
|
+
}
|
|
93
|
+
if (s.bindings) {
|
|
94
|
+
const bound = new Set(s.bindings.map((b) => b.field));
|
|
95
|
+
const need = s.fields.filter((f) => f.kind !== "flag");
|
|
96
|
+
if (bound.size !== s.bindings.length || need.some((f) => !bound.has(f.name)) || bound.size !== need.length) {
|
|
97
|
+
throw new SchemaError("bindings must cover every non-flag field exactly once");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return s;
|
|
101
|
+
}
|
|
102
|
+
/** The on-chain Schema.Layout for this schema. */
|
|
103
|
+
export function toLayout(schema) {
|
|
104
|
+
return {
|
|
105
|
+
netlistHash: schema.netlistHash,
|
|
106
|
+
nIn: schema.nIn,
|
|
107
|
+
fields: schema.fields.map((f) => ({
|
|
108
|
+
kind: KIND_ID[f.kind],
|
|
109
|
+
offset: f.offset,
|
|
110
|
+
width: f.width,
|
|
111
|
+
fixedValue: f.kind === "flag" ? BigInt(f.value) : 0n,
|
|
112
|
+
})),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const FIELDS_TYPE = {
|
|
116
|
+
type: "tuple[]",
|
|
117
|
+
components: [
|
|
118
|
+
{ name: "kind", type: "uint8" },
|
|
119
|
+
{ name: "offset", type: "uint32" },
|
|
120
|
+
{ name: "width", type: "uint32" },
|
|
121
|
+
{ name: "fixedValue", type: "uint256" },
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
/** keccak256(abi.encode(netlistHash, nIn, fields)), equal to Schema.hash on-chain. */
|
|
125
|
+
export function schemaHash(schema) {
|
|
126
|
+
const l = "format" in schema ? toLayout(parseSchema(schema)) : schema;
|
|
127
|
+
return keccak256(encodeAbiParameters([{ type: "bytes32" }, { type: "uint32" }, FIELDS_TYPE], [l.netlistHash, l.nIn, l.fields]));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Strict encoder: one value per non-flag field (by name), packed LSB first into exactly ceil(nIn/8)
|
|
131
|
+
* bytes. Throws on a missing or unknown name, a value wider than its field, or a bool that is not 0/1.
|
|
132
|
+
* It never truncates.
|
|
133
|
+
*/
|
|
134
|
+
export function encodeInputs(schema, values) {
|
|
135
|
+
const s = parseSchema(schema);
|
|
136
|
+
const bytes = new Uint8Array(Math.ceil(s.nIn / 8));
|
|
137
|
+
const known = new Set(s.fields.filter((f) => f.kind !== "flag").map((f) => f.name));
|
|
138
|
+
for (const k of Object.keys(values))
|
|
139
|
+
if (!known.has(k))
|
|
140
|
+
throw new SchemaError(`unknown or fixed field "${k}"`);
|
|
141
|
+
for (const f of s.fields) {
|
|
142
|
+
let v;
|
|
143
|
+
if (f.kind === "flag")
|
|
144
|
+
v = BigInt(f.value);
|
|
145
|
+
else {
|
|
146
|
+
if (!(f.name in values))
|
|
147
|
+
throw new SchemaError(`missing value for "${f.name}"`);
|
|
148
|
+
const raw = values[f.name];
|
|
149
|
+
v = typeof raw === "boolean" ? (raw ? 1n : 0n) : BigInt(raw);
|
|
150
|
+
if (v < 0n)
|
|
151
|
+
throw new SchemaError(`"${f.name}": negative value`);
|
|
152
|
+
if (v >> BigInt(f.width) !== 0n)
|
|
153
|
+
throw new SchemaError(`"${f.name}": value ${v} does not fit in ${f.width} bit(s)`);
|
|
154
|
+
}
|
|
155
|
+
for (let b = 0; b < f.width; b++) {
|
|
156
|
+
if ((v >> BigInt(b)) & 1n) {
|
|
157
|
+
const bit = f.offset + b;
|
|
158
|
+
bytes[bit >> 3] |= 1 << (bit & 7);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return toHex(bytes);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* What a TapeOut processor actually reads from a value written into a field: only the low `width`
|
|
166
|
+
* bits (TAP-02 reads input leniently). Exposed to show the truncation that encodeInputs refuses.
|
|
167
|
+
*/
|
|
168
|
+
export function lenientFieldValue(value, width) {
|
|
169
|
+
return value & ((1n << BigInt(width)) - 1n);
|
|
170
|
+
}
|
package/dist/tiers.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Budget tiers in gas. The thresholds come from src/tiers.json, the same file the Catalog contract's
|
|
3
|
+
* constants are checked against (test/unit/TiersShared.t.sol), so the SDK and the chain agree.
|
|
4
|
+
* Quote applies them to the full Safe -> guard -> circuit path; the Catalog applies them to the gas of
|
|
5
|
+
* the circuit evaluation (pin checks included) measured on-chain at listing.
|
|
6
|
+
*/
|
|
7
|
+
export declare const TIERS: {
|
|
8
|
+
readonly greenMaxGas: bigint;
|
|
9
|
+
readonly amberMaxGas: bigint;
|
|
10
|
+
};
|
|
11
|
+
export type Tier = "Green" | "Amber" | "Red";
|
|
12
|
+
export declare function tierFor(gas: bigint): Tier;
|
package/dist/tiers.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import shared from "./tiers.json" with { type: "json" };
|
|
2
|
+
/**
|
|
3
|
+
* Budget tiers in gas. The thresholds come from src/tiers.json, the same file the Catalog contract's
|
|
4
|
+
* constants are checked against (test/unit/TiersShared.t.sol), so the SDK and the chain agree.
|
|
5
|
+
* Quote applies them to the full Safe -> guard -> circuit path; the Catalog applies them to the gas of
|
|
6
|
+
* the circuit evaluation (pin checks included) measured on-chain at listing.
|
|
7
|
+
*/
|
|
8
|
+
export const TIERS = {
|
|
9
|
+
greenMaxGas: BigInt(shared.greenMaxGas),
|
|
10
|
+
amberMaxGas: BigInt(shared.amberMaxGas),
|
|
11
|
+
};
|
|
12
|
+
export function tierFor(gas) {
|
|
13
|
+
if (gas <= TIERS.greenMaxGas)
|
|
14
|
+
return "Green";
|
|
15
|
+
if (gas <= TIERS.amberMaxGas)
|
|
16
|
+
return "Amber";
|
|
17
|
+
return "Red";
|
|
18
|
+
}
|
package/dist/tiers.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fengce/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript SDK for Fengce: TapeOut circuit schemas, Quote certificates and Safe guard install calldata",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"fengce-quote": "dist/cli/quote.js"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=20"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/pgreyy/fengce.git",
|
|
29
|
+
"directory": "sdk"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"tapeout",
|
|
33
|
+
"x-layer",
|
|
34
|
+
"safe",
|
|
35
|
+
"guard",
|
|
36
|
+
"policy",
|
|
37
|
+
"circuit"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p .",
|
|
41
|
+
"typecheck": "tsc -p . --noEmit",
|
|
42
|
+
"test": "node --import tsx --test test/*.test.ts",
|
|
43
|
+
"quote": "tsx src/cli/quote.ts",
|
|
44
|
+
"sync-artifacts": "tsx scripts/sync-artifacts.ts",
|
|
45
|
+
"prepack": "npm run build && cp ../LICENSE ./LICENSE",
|
|
46
|
+
"test:live": "tsx scripts/differential.ts"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"viem": "^2.58.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^22.20.5",
|
|
53
|
+
"tsx": "^4.23.15",
|
|
54
|
+
"typescript": "^5.9.3"
|
|
55
|
+
}
|
|
56
|
+
}
|