@fengce/sdk 0.0.0-stage → 0.2.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/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ - `check(client, safe, tx)`: runs the Safe's guard on a transaction (one `eth_call`, nothing sent) and returns
6
+ `{ allowed, reason, code?, limit?, guard, refusedTo?, detail? }`. Reason codes for FengceGuard v2 and FengceGuardV3:
7
+ `MAX_PER_BUY`, `CIRCUIT`, `TRADES_PER_HOUR`, `QUIET_HOURS`, `HOLD_LOCK`, `TILT_SHIELD`, `AGENT_CAP`,
8
+ `NOT_CONFIGURED`, `DELEGATECALL`, `ADMIN`, `OTHER`. Exports `V3_REASONS` and `MAX_BUY_PIN`.
9
+ - README: the app is getfengce.vercel.app.
10
+
11
+ ## 0.1.1
12
+
13
+ - `readCatalog` reads every Catalog entry in parallel (one round of calls; a batching transport sends them together)
14
+ instead of one call after another. Same result, in listing order.
15
+ - README: links to the app (Okun, getokun.vercel.app) and its engine page, and to this changelog.
16
+
17
+ ## 0.1.0
18
+
19
+ - First release: schemas and schema hashes, Quote certificates, Safe guard install calldata, local evaluation and
20
+ `evaluateTrace`, the `fengce-quote` CLI.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pgreyy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,118 @@
1
- # Temporary Holding Version
1
+ # @fengce/sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ TypeScript tools for [Fengce](https://github.com/pgreyy/fengce): check a Safe transaction against its on-chain limits
4
+ before you send it, TapeOut circuit schemas, Quote certificates and Safe guard install calldata. Built on
5
+ [viem](https://viem.sh). Node 20 or later.
6
+
7
+ App: [getfengce.vercel.app](https://getfengce.vercel.app) (limits on your Safe) and the [docs](https://getfengce.vercel.app/docs/). Changes: [CHANGELOG.md](CHANGELOG.md).
8
+
9
+ ## Install
10
+
11
+ On npm as [@fengce/sdk](https://www.npmjs.com/package/@fengce/sdk):
12
+
13
+ ```sh
14
+ npm install @fengce/sdk
15
+ npx fengce-quote <processor> <circuitId> --out quote.json # the Quote CLI
16
+ ```
17
+
18
+ From the repository instead: `cd sdk && npm ci && npm run build`.
19
+
20
+ ## Check a transaction before you send it
21
+
22
+ ```ts
23
+ import { createPublicClient, http } from "viem";
24
+ import { check } from "@fengce/sdk";
25
+
26
+ const client = createPublicClient({ transport: http("https://rpc.xlayer.tech") });
27
+ const r = await check(client, safe, { to: USDT0, data: transferCalldata });
28
+ // { allowed: false, reason: "MAX_PER_BUY", limit: 30000000n, guard: "0x9D2A…8E6e", refusedTo: "0x779D…3736" }
29
+ ```
30
+
31
+ `check` runs the Safe's own guard on the transaction (one `eth_call` with the Safe as sender; nothing is sent and no
32
+ signature is needed), so the answer is the one the Safe would give. Batches: pass the MultiSendCallOnly call with
33
+ `operation: 1`. A Safe without a guard returns `{ allowed: true, guard: null }`.
34
+
35
+ | `reason` | Meaning |
36
+ |---|---|
37
+ | `MAX_PER_BUY` | the max-per-buy circuit (NAND #2) refused: over the cap, paying USDT0; `limit` is the cap |
38
+ | `CIRCUIT` | another pinned policy circuit refused |
39
+ | `TRADES_PER_HOUR` | counter circuit #7: too many swaps this clock hour (UTC) |
40
+ | `QUIET_HOURS`, `HOLD_LOCK`, `TILT_SHIELD`, `AGENT_CAP` | FengceGuardV3 rules (`code` 3, 4, 5, 6) |
41
+ | `NOT_CONFIGURED` | a Fengce guard is installed but has no policy for this Safe |
42
+ | `DELEGATECALL` | a delegatecall other than MultiSendCallOnly |
43
+ | `ADMIN` | a Safe admin change that must wait for the timelock or recovery |
44
+ | `OTHER` | anything else (`detail` has the decoded error) |
45
+
46
+ ## Schemas
47
+
48
+ ```ts
49
+ import { parseSchema, schemaHash, encodeInputs, toLayout } from "@fengce/sdk";
50
+
51
+ const schema = parseSchema(readFileSync("nand-limit.schema.json", "utf8")); // validates like Schema.validate
52
+ schemaHash(schema); // equals Schema.hash on-chain; pin this
53
+ encodeInputs(schema, { overLimit: true }); // "0x03": strict, LSB first, never truncates
54
+ toLayout(schema); // the Schema.Layout struct for contract calls
55
+ ```
56
+
57
+ File format and binding sources: see `contracts/src/schema.ts` and `docs/INTEGRATION.md`. Examples:
58
+ `examples/nand-limit.schema.json`, `examples/comparator16.schema.json`, `examples/encode.ts`.
59
+
60
+ ## Install calldata for a Safe
61
+
62
+ ```ts
63
+ import { buildInstall } from "@fengce/sdk";
64
+
65
+ const { calls, multiSend } = await buildInstall(publicClient, {
66
+ safe, guard, pinId, schema,
67
+ recoveryDelay: 172_800n, recoveryWindow: 172_800n, adminDelay: 86_400n,
68
+ });
69
+ ```
70
+
71
+ Returns `configure`, `setGuard`, `setModuleGuard` (in that order) and the same three as one MultiSendCallOnly
72
+ batch. Throws `InstallError` if the Safe has modules enabled. It only builds calldata. Example:
73
+ `examples/install-calldata.ts`.
74
+
75
+ ## Quote
76
+
77
+ ```sh
78
+ fengce-quote <processor> <circuitId> [--rpc URL] [--port N] [--out FILE] [--input 0xBYTES] [--schema FILE --values JSON]
79
+ ```
80
+
81
+ Needs `anvil` (Foundry) on PATH. Starts a local fork of X Layer, deploys PinRegistry and FengceGuard on it,
82
+ measures gas on the full Safe path (Safe execution overhead, pre-check and outcome check, reported separately),
83
+ assigns a tier from the full Safe path (`contracts/src/tiers.json`: Green up to 1M gas, Amber up to 3.5M, Red above), pins the circuit on the fork with the resulting `certificateHash` and checks what
84
+ the registry stored. Nothing is sent to the real chain. Programmatic use: `quote(env, processor, id, options)`.
85
+
86
+ The certificate hash is keccak256 of the certificate's canonical JSON (`canonicalJson`: keys sorted, no
87
+ whitespace). Certificates are not signed.
88
+
89
+ Other exports: `parseRefs` (netlist REF records), `refClosure` (dependency closure hash, as PinRegistry computes
90
+ it), `tierFor`, `PinRegistryArtifact` and `FengceGuardArtifact` (ABI and bytecode).
91
+
92
+ ## Local evaluation
93
+
94
+ ```ts
95
+ import { evaluateLocally, evaluateTrace, preSign } from "@fengce/sdk";
96
+
97
+ await evaluateLocally(client, processor, circuitId, "0x02"); // { output, newState }, from the on-chain netlist
98
+ await evaluateTrace(client, processor, circuitId, "0x02"); // also every gate's output and topological level (REF nested)
99
+ await preSign(client, { processor, circuitId, schema }, safe, { to, value, data }); // facts -> input -> "allow" | "deny"
100
+ ```
101
+
102
+ The interpreter follows TAP-02 (NAND, LATCH with state, REF sub-circuits) on the netlist bytes read from chain.
103
+ `scripts/differential.ts` compares it with on-chain `eval` on random inputs (`npm run test:live`);
104
+ `scripts/trace-check.ts` compares `evaluateTrace` with on-chain `eval` (`npx tsx scripts/trace-check.ts`).
105
+ `preSign` covers native transfers, ERC-20 transfer and approve offline; use the app's [Try it](https://getfengce.vercel.app/try/) page for real calls through a Safe.
106
+
107
+ ## Development
108
+
109
+ | Command | What |
110
+ |---|---|
111
+ | `npm test` | Unit tests |
112
+ | `npm run typecheck` | Type check |
113
+ | `npm run sync-artifacts` | Refresh `contracts/src/artifacts.ts` after `forge build` in the repository root |
114
+ | `npx tsx scripts/gen-vectors.ts` | Refresh `contracts/test/vectors/hashes.json` (read-only RPC calls) |
115
+
116
+ ## License
117
+
118
+ MIT
package/dist/abi.d.ts ADDED
@@ -0,0 +1,142 @@
1
+ export declare const processorAbi: readonly [{
2
+ readonly name: "circuitInfo";
3
+ readonly type: "function";
4
+ readonly stateMutability: "view";
5
+ readonly inputs: readonly [{
6
+ readonly type: "uint256";
7
+ readonly name: "id";
8
+ }];
9
+ readonly outputs: readonly [{
10
+ readonly type: "uint32";
11
+ readonly name: "nIn";
12
+ }, {
13
+ readonly type: "uint32";
14
+ readonly name: "nOut";
15
+ }, {
16
+ readonly type: "uint32";
17
+ readonly name: "nState";
18
+ }, {
19
+ readonly type: "uint32";
20
+ readonly name: "gateCount";
21
+ }];
22
+ }, {
23
+ readonly name: "netlist";
24
+ readonly type: "function";
25
+ readonly stateMutability: "view";
26
+ readonly inputs: readonly [{
27
+ readonly type: "uint256";
28
+ readonly name: "id";
29
+ }];
30
+ readonly outputs: readonly [{
31
+ readonly type: "bytes";
32
+ }];
33
+ }, {
34
+ readonly name: "eval";
35
+ readonly type: "function";
36
+ readonly stateMutability: "view";
37
+ readonly inputs: readonly [{
38
+ readonly type: "uint256";
39
+ readonly name: "id";
40
+ }, {
41
+ readonly type: "bytes";
42
+ readonly name: "inputs";
43
+ }];
44
+ readonly outputs: readonly [{
45
+ readonly type: "bytes";
46
+ }];
47
+ }];
48
+ export declare const safeAbi: readonly [{
49
+ readonly name: "setup";
50
+ readonly type: "function";
51
+ readonly stateMutability: "nonpayable";
52
+ readonly inputs: readonly [{
53
+ readonly type: "address[]";
54
+ readonly name: "owners";
55
+ }, {
56
+ readonly type: "uint256";
57
+ readonly name: "threshold";
58
+ }, {
59
+ readonly type: "address";
60
+ readonly name: "to";
61
+ }, {
62
+ readonly type: "bytes";
63
+ readonly name: "data";
64
+ }, {
65
+ readonly type: "address";
66
+ readonly name: "fallbackHandler";
67
+ }, {
68
+ readonly type: "address";
69
+ readonly name: "paymentToken";
70
+ }, {
71
+ readonly type: "uint256";
72
+ readonly name: "payment";
73
+ }, {
74
+ readonly type: "address";
75
+ readonly name: "paymentReceiver";
76
+ }];
77
+ readonly outputs: readonly [];
78
+ }, {
79
+ readonly name: "execTransaction";
80
+ readonly type: "function";
81
+ readonly stateMutability: "payable";
82
+ readonly inputs: readonly [{
83
+ readonly type: "address";
84
+ readonly name: "to";
85
+ }, {
86
+ readonly type: "uint256";
87
+ readonly name: "value";
88
+ }, {
89
+ readonly type: "bytes";
90
+ readonly name: "data";
91
+ }, {
92
+ readonly type: "uint8";
93
+ readonly name: "operation";
94
+ }, {
95
+ readonly type: "uint256";
96
+ readonly name: "safeTxGas";
97
+ }, {
98
+ readonly type: "uint256";
99
+ readonly name: "baseGas";
100
+ }, {
101
+ readonly type: "uint256";
102
+ readonly name: "gasPrice";
103
+ }, {
104
+ readonly type: "address";
105
+ readonly name: "gasToken";
106
+ }, {
107
+ readonly type: "address";
108
+ readonly name: "refundReceiver";
109
+ }, {
110
+ readonly type: "bytes";
111
+ readonly name: "signatures";
112
+ }];
113
+ readonly outputs: readonly [{
114
+ readonly type: "bool";
115
+ }];
116
+ }];
117
+ export declare const proxyFactoryAbi: readonly [{
118
+ readonly name: "createProxyWithNonce";
119
+ readonly type: "function";
120
+ readonly stateMutability: "nonpayable";
121
+ readonly inputs: readonly [{
122
+ readonly type: "address";
123
+ readonly name: "singleton";
124
+ }, {
125
+ readonly type: "bytes";
126
+ readonly name: "initializer";
127
+ }, {
128
+ readonly type: "uint256";
129
+ readonly name: "saltNonce";
130
+ }];
131
+ readonly outputs: readonly [{
132
+ readonly type: "address";
133
+ }];
134
+ }];
135
+ export declare const XLAYER: {
136
+ readonly chainId: 196;
137
+ readonly rpc: "https://rpc.xlayer.tech";
138
+ readonly tapeoutFactory: "0x1f09DAeFA827f02CBb40967cc91b259763760761";
139
+ readonly safeProxyFactory: "0x14F2982D601c9458F93bd70B218933A6f8165e7b";
140
+ readonly safeL2: "0xEdd160fEBBD92E350D4D398fb636302fccd67C7e";
141
+ readonly fallbackHandler: "0x3EfCBb83A4A7AfcB4F68D501E2c2203a38be77f4";
142
+ };
package/dist/abi.js ADDED
@@ -0,0 +1,21 @@
1
+ import { parseAbi } from "viem";
2
+ export const processorAbi = parseAbi([
3
+ "function circuitInfo(uint256 id) view returns (uint32 nIn, uint32 nOut, uint32 nState, uint32 gateCount)",
4
+ "function netlist(uint256 id) view returns (bytes)",
5
+ "function eval(uint256 id, bytes inputs) view returns (bytes)",
6
+ ]);
7
+ export const safeAbi = parseAbi([
8
+ "function setup(address[] owners, uint256 threshold, address to, bytes data, address fallbackHandler, address paymentToken, uint256 payment, address paymentReceiver)",
9
+ "function execTransaction(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 baseGas, uint256 gasPrice, address gasToken, address refundReceiver, bytes signatures) payable returns (bool)",
10
+ ]);
11
+ export const proxyFactoryAbi = parseAbi([
12
+ "function createProxyWithNonce(address singleton, bytes initializer, uint256 saltNonce) returns (address)",
13
+ ]);
14
+ export const XLAYER = {
15
+ chainId: 196,
16
+ rpc: "https://rpc.xlayer.tech",
17
+ tapeoutFactory: "0x1f09DAeFA827f02CBb40967cc91b259763760761",
18
+ safeProxyFactory: "0x14F2982D601c9458F93bd70B218933A6f8165e7b",
19
+ safeL2: "0xEdd160fEBBD92E350D4D398fb636302fccd67C7e",
20
+ fallbackHandler: "0x3EfCBb83A4A7AfcB4F68D501E2c2203a38be77f4",
21
+ };