@crediolabs/policy-synth 0.3.0 → 0.3.1
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/dist/record/freshness.d.ts +17 -11
- package/dist/record/freshness.js +28 -18
- package/dist-cjs/record/freshness.d.ts +17 -11
- package/dist-cjs/record/freshness.js +28 -18
- package/package.json +2 -2
- package/src/record/freshness.ts +29 -18
- package/dist/adapters/interpreter/index.d.ts +0 -1
- package/dist/adapters/interpreter/index.js +0 -2
- package/dist/adapters/oz/adapter.d.ts +0 -20
- package/dist/adapters/oz/adapter.js +0 -281
- package/dist/adapters/oz/index.d.ts +0 -1
- package/dist/adapters/oz/index.js +0 -2
- package/dist/codegen/compile-gate.d.ts +0 -33
- package/dist/codegen/compile-gate.js +0 -124
- package/dist/codegen/index.d.ts +0 -2
- package/dist/codegen/index.js +0 -8
- package/dist/codegen/template.d.ts +0 -18
- package/dist/codegen/template.js +0 -148
- package/dist/ir/index.d.ts +0 -1
- package/dist/ir/index.js +0 -2
- package/dist/ir/types.d.ts +0 -71
- package/dist/ir/types.js +0 -9
- package/dist/mandate/index.d.ts +0 -2
- package/dist/mandate/index.js +0 -2
- package/dist/mandate/to-ir.d.ts +0 -3
- package/dist/mandate/to-ir.js +0 -60
- package/dist/mandate/types.d.ts +0 -20
- package/dist/mandate/types.js +0 -8
- package/dist/seams/index.d.ts +0 -1
- package/dist/seams/index.js +0 -2
- package/dist/seams/types.d.ts +0 -64
- package/dist/seams/types.js +0 -11
- package/dist/synth/deny-cases.d.ts +0 -28
- package/dist/synth/deny-cases.js +0 -212
- package/dist/synth/evaluate.d.ts +0 -18
- package/dist/synth/evaluate.js +0 -271
- package/dist/synth/harness.d.ts +0 -28
- package/dist/synth/harness.js +0 -47
- package/dist/synth/minimize.d.ts +0 -4
- package/dist/synth/minimize.js +0 -38
- package/dist/synth/permit-context.d.ts +0 -8
- package/dist/synth/permit-context.js +0 -35
- package/dist/synth/predicate-literals.d.ts +0 -5
- package/dist/synth/predicate-literals.js +0 -25
- package/dist/synth/synthesize-from-mandate.d.ts +0 -20
- package/dist/synth/synthesize-from-mandate.js +0 -59
- package/dist/verify/envelope.d.ts +0 -15
- package/dist/verify/envelope.js +0 -22
- package/dist/verify/index.d.ts +0 -3
- package/dist/verify/index.js +0 -3
- package/dist/verify/simulate.d.ts +0 -18
- package/dist/verify/simulate.js +0 -172
- package/dist/verify/verify.d.ts +0 -15
- package/dist/verify/verify.js +0 -181
- package/dist-cjs/adapters/interpreter/index.d.ts +0 -1
- package/dist-cjs/adapters/interpreter/index.js +0 -7
- package/dist-cjs/adapters/oz/adapter.d.ts +0 -20
- package/dist-cjs/adapters/oz/adapter.js +0 -286
- package/dist-cjs/adapters/oz/index.d.ts +0 -1
- package/dist-cjs/adapters/oz/index.js +0 -8
- package/dist-cjs/codegen/compile-gate.d.ts +0 -33
- package/dist-cjs/codegen/compile-gate.js +0 -128
- package/dist-cjs/codegen/index.d.ts +0 -2
- package/dist-cjs/codegen/index.js +0 -14
- package/dist-cjs/codegen/template.d.ts +0 -18
- package/dist-cjs/codegen/template.js +0 -151
- package/dist-cjs/ir/index.d.ts +0 -1
- package/dist-cjs/ir/index.js +0 -3
- package/dist-cjs/ir/types.d.ts +0 -71
- package/dist-cjs/ir/types.js +0 -10
- package/dist-cjs/mandate/index.d.ts +0 -2
- package/dist-cjs/mandate/index.js +0 -6
- package/dist-cjs/mandate/to-ir.d.ts +0 -3
- package/dist-cjs/mandate/to-ir.js +0 -63
- package/dist-cjs/mandate/types.d.ts +0 -20
- package/dist-cjs/mandate/types.js +0 -9
- package/dist-cjs/seams/index.d.ts +0 -1
- package/dist-cjs/seams/index.js +0 -3
- package/dist-cjs/seams/types.d.ts +0 -64
- package/dist-cjs/seams/types.js +0 -12
- package/dist-cjs/synth/deny-cases.d.ts +0 -28
- package/dist-cjs/synth/deny-cases.js +0 -217
- package/dist-cjs/synth/evaluate.d.ts +0 -18
- package/dist-cjs/synth/evaluate.js +0 -274
- package/dist-cjs/synth/harness.d.ts +0 -28
- package/dist-cjs/synth/harness.js +0 -50
- package/dist-cjs/synth/minimize.d.ts +0 -4
- package/dist-cjs/synth/minimize.js +0 -41
- package/dist-cjs/synth/permit-context.d.ts +0 -8
- package/dist-cjs/synth/permit-context.js +0 -38
- package/dist-cjs/synth/predicate-literals.d.ts +0 -5
- package/dist-cjs/synth/predicate-literals.js +0 -28
- package/dist-cjs/synth/synthesize-from-mandate.d.ts +0 -20
- package/dist-cjs/synth/synthesize-from-mandate.js +0 -62
- package/dist-cjs/verify/envelope.d.ts +0 -15
- package/dist-cjs/verify/envelope.js +0 -23
- package/dist-cjs/verify/index.d.ts +0 -3
- package/dist-cjs/verify/index.js +0 -8
- package/dist-cjs/verify/simulate.d.ts +0 -18
- package/dist-cjs/verify/simulate.js +0 -175
- package/dist-cjs/verify/verify.d.ts +0 -15
- package/dist-cjs/verify/verify.js +0 -184
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export type CompileGateResult = {
|
|
2
|
-
code: 'COMPILE_OK';
|
|
3
|
-
} | {
|
|
4
|
-
code: 'COMPILE_GATE_FAILED';
|
|
5
|
-
stderr: string;
|
|
6
|
-
} | {
|
|
7
|
-
code: 'SKIPPED';
|
|
8
|
-
reason: string;
|
|
9
|
-
};
|
|
10
|
-
export interface CompileGateOpts {
|
|
11
|
-
/** Caller-controlled abort signal; aborting before toolchain probe resolves
|
|
12
|
-
* the gate to `SKIPPED` rather than starting a cargo invocation. */
|
|
13
|
-
signal?: AbortSignal;
|
|
14
|
-
/** Override the `cargo` binary (mostly for tests). */
|
|
15
|
-
cargoBin?: string;
|
|
16
|
-
/** Override the target directory (mostly for tests). */
|
|
17
|
-
tempDir?: string;
|
|
18
|
-
}
|
|
19
|
-
/** Probe the host for the Rust toolchain the gate requires. We require BOTH
|
|
20
|
-
* `cargo` AND `stellar` (the Soroban CLI) because a real gate run invokes
|
|
21
|
-
* `stellar contract build`; absent either, the gate returns `SKIPPED`. */
|
|
22
|
-
export declare function hasRustToolchain(): Promise<boolean>;
|
|
23
|
-
/** Compile-check a Rust source string against the pinned Soroban SDK.
|
|
24
|
-
*
|
|
25
|
-
* - Toolchain absent -> `{ code: 'SKIPPED', reason }` (NOT a failure).
|
|
26
|
-
* - Toolchain present, source compiles -> `{ code: 'COMPILE_OK' }`.
|
|
27
|
-
* - Toolchain present, source broken -> `{ code: 'COMPILE_GATE_FAILED', stderr }`.
|
|
28
|
-
*
|
|
29
|
-
* The gate writes the source to a temporary crate and runs `cargo check`
|
|
30
|
-
* against it; the crate is cleaned up whether or not compilation succeeds.
|
|
31
|
-
* The caller is expected to surface `COMPILE_GATE_FAILED.stderr` to the
|
|
32
|
-
* user (the LLM agent) so the user can iterate. */
|
|
33
|
-
export declare function compileCheck(rustSource: string, opts?: CompileGateOpts): Promise<CompileGateResult>;
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
// src/codegen/compile-gate.ts - toolchain-gated `cargo check` for the
|
|
2
|
-
// codegen escape hatch.
|
|
3
|
-
//
|
|
4
|
-
// The escape hatch ships as the `policy-builder escape-hatch` CLI subcommand
|
|
5
|
-
// (a later phase). This gate is its safety net, NOT the happy path. Property:
|
|
6
|
-
// generated Rust compiles against the pinned OZ crate; broken source returns
|
|
7
|
-
// `COMPILE_GATE_FAILED` with `stderr`; an absent toolchain returns `SKIPPED`
|
|
8
|
-
// (does NOT fail - we cannot make build tools a hard runtime requirement for
|
|
9
|
-
// a TypeScript package).
|
|
10
|
-
import { spawn } from 'node:child_process';
|
|
11
|
-
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
12
|
-
import { tmpdir } from 'node:os';
|
|
13
|
-
import { join } from 'node:path';
|
|
14
|
-
/** Probe the host for the Rust toolchain the gate requires. We require BOTH
|
|
15
|
-
* `cargo` AND `stellar` (the Soroban CLI) because a real gate run invokes
|
|
16
|
-
* `stellar contract build`; absent either, the gate returns `SKIPPED`. */
|
|
17
|
-
export async function hasRustToolchain() {
|
|
18
|
-
const probe = await probeToolchain();
|
|
19
|
-
return probe.present;
|
|
20
|
-
}
|
|
21
|
-
async function probeToolchain() {
|
|
22
|
-
const cargo = await which('cargo');
|
|
23
|
-
if (!cargo)
|
|
24
|
-
return { present: false, reason: 'cargo not found on PATH' };
|
|
25
|
-
const stellar = await which('stellar');
|
|
26
|
-
if (!stellar)
|
|
27
|
-
return { present: false, reason: 'stellar not found on PATH' };
|
|
28
|
-
return { present: true, reason: `${cargo} + ${stellar}` };
|
|
29
|
-
}
|
|
30
|
-
async function which(bin) {
|
|
31
|
-
try {
|
|
32
|
-
const out = await runProcess('sh', ['-c', `command -v ${bin}`], { capture: true });
|
|
33
|
-
const path = out.stdout.trim();
|
|
34
|
-
return path.length > 0 ? path : null;
|
|
35
|
-
}
|
|
36
|
-
catch {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
function runProcess(cmd, args, opts) {
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
if (opts.signal?.aborted) {
|
|
43
|
-
reject(new Error('aborted'));
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
47
|
-
let stdout = '';
|
|
48
|
-
let stderr = '';
|
|
49
|
-
child.stdout.on('data', (b) => {
|
|
50
|
-
stdout += b.toString('utf8');
|
|
51
|
-
});
|
|
52
|
-
child.stderr.on('data', (b) => {
|
|
53
|
-
stderr += b.toString('utf8');
|
|
54
|
-
});
|
|
55
|
-
const onAbort = () => {
|
|
56
|
-
child.kill('SIGTERM');
|
|
57
|
-
reject(new Error('aborted'));
|
|
58
|
-
};
|
|
59
|
-
opts.signal?.addEventListener('abort', onAbort, { once: true });
|
|
60
|
-
child.on('error', (e) => {
|
|
61
|
-
opts.signal?.removeEventListener('abort', onAbort);
|
|
62
|
-
reject(e);
|
|
63
|
-
});
|
|
64
|
-
child.on('close', (code) => {
|
|
65
|
-
opts.signal?.removeEventListener('abort', onAbort);
|
|
66
|
-
resolve({ stdout, stderr, code: code ?? -1 });
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
const CARGO_MANIFEST = `[package]
|
|
71
|
-
name = "escape-hatch-gate"
|
|
72
|
-
version = "0.0.0"
|
|
73
|
-
edition = "2021"
|
|
74
|
-
publish = false
|
|
75
|
-
|
|
76
|
-
[lib]
|
|
77
|
-
crate-type = ["cdylib"]
|
|
78
|
-
# The gate writes the source beside the manifest rather than under src/, so
|
|
79
|
-
# the path is stated. Without it cargo looks for src/lib.rs and fails to
|
|
80
|
-
# parse the manifest before it ever compiles anything, which reads as a
|
|
81
|
-
# broken generator rather than a broken scaffold.
|
|
82
|
-
path = "lib.rs"
|
|
83
|
-
|
|
84
|
-
[dependencies]
|
|
85
|
-
soroban-sdk = "22"
|
|
86
|
-
|
|
87
|
-
[workspace]
|
|
88
|
-
`;
|
|
89
|
-
/** Compile-check a Rust source string against the pinned Soroban SDK.
|
|
90
|
-
*
|
|
91
|
-
* - Toolchain absent -> `{ code: 'SKIPPED', reason }` (NOT a failure).
|
|
92
|
-
* - Toolchain present, source compiles -> `{ code: 'COMPILE_OK' }`.
|
|
93
|
-
* - Toolchain present, source broken -> `{ code: 'COMPILE_GATE_FAILED', stderr }`.
|
|
94
|
-
*
|
|
95
|
-
* The gate writes the source to a temporary crate and runs `cargo check`
|
|
96
|
-
* against it; the crate is cleaned up whether or not compilation succeeds.
|
|
97
|
-
* The caller is expected to surface `COMPILE_GATE_FAILED.stderr` to the
|
|
98
|
-
* user (the LLM agent) so the user can iterate. */
|
|
99
|
-
export async function compileCheck(rustSource, opts = {}) {
|
|
100
|
-
if (opts.signal?.aborted) {
|
|
101
|
-
return { code: 'SKIPPED', reason: 'aborted before probe' };
|
|
102
|
-
}
|
|
103
|
-
const probe = await probeToolchain();
|
|
104
|
-
if (!probe.present) {
|
|
105
|
-
return { code: 'SKIPPED', reason: probe.reason };
|
|
106
|
-
}
|
|
107
|
-
let dir = null;
|
|
108
|
-
try {
|
|
109
|
-
dir = await mkdtemp(join(opts.tempDir ?? tmpdir(), 'codegen-gate-'));
|
|
110
|
-
await writeFile(join(dir, 'Cargo.toml'), CARGO_MANIFEST);
|
|
111
|
-
await writeFile(join(dir, 'lib.rs'), rustSource);
|
|
112
|
-
const runOpts = opts.signal ? { signal: opts.signal } : {};
|
|
113
|
-
const result = await runProcess(opts.cargoBin ?? 'cargo', ['check', '--quiet', '--offline', '--manifest-path', join(dir, 'Cargo.toml')], runOpts).catch((e) => ({ stdout: '', stderr: e.message, code: -1 }));
|
|
114
|
-
if (result.code === 0) {
|
|
115
|
-
return { code: 'COMPILE_OK' };
|
|
116
|
-
}
|
|
117
|
-
return { code: 'COMPILE_GATE_FAILED', stderr: result.stderr || result.stdout };
|
|
118
|
-
}
|
|
119
|
-
finally {
|
|
120
|
-
if (dir) {
|
|
121
|
-
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
package/dist/codegen/index.d.ts
DELETED
package/dist/codegen/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// src/codegen/index.ts - re-export the codegen escape-hatch surface.
|
|
2
|
-
//
|
|
3
|
-
// The escape hatch is OUT of the audited happy path: the synthesiser never
|
|
4
|
-
// calls `generateRust` itself; the CLI subcommand (a later phase) is the only
|
|
5
|
-
// entry point. Keeping the surface in one file makes that boundary visible
|
|
6
|
-
// in import statements.
|
|
7
|
-
export { compileCheck, hasRustToolchain, } from "./compile-gate.js";
|
|
8
|
-
export { generateRust, } from "./template.js";
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/** A small typed description of one uncovered constraint the v1 DSL refused
|
|
2
|
-
* to express. The synthesiser surfaces these from `CompileResult.uncovered`
|
|
3
|
-
* and the codegen escape hatch wraps them into a hand-auditable skeleton. */
|
|
4
|
-
export interface EscapeHatchSpec {
|
|
5
|
-
/** Address of the policy contract the skeleton is generated for. */
|
|
6
|
-
contract: string;
|
|
7
|
-
/** The trait method the predicate lives inside (default: 'enforce'). */
|
|
8
|
-
fnName?: string;
|
|
9
|
-
/** Human-readable description of each construct the DSL could not cover. */
|
|
10
|
-
uncovered: string[];
|
|
11
|
-
}
|
|
12
|
-
/** Generate a deterministic OZ `Policy` skeleton for an uncovered constraint.
|
|
13
|
-
*
|
|
14
|
-
* The skeleton is NOT a finished policy; the user fills in the predicate
|
|
15
|
-
* logic in the marked TODO(user) holes and audits the result themselves.
|
|
16
|
-
*
|
|
17
|
-
* Same `(spec, name)` always produces byte-identical output. */
|
|
18
|
-
export declare function generateRust(spec: EscapeHatchSpec, name: string): string;
|
package/dist/codegen/template.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
// src/codegen/template.ts - deterministic Rust source generator for the OZ
|
|
2
|
-
// `Policy` escape-hatch skeleton.
|
|
3
|
-
//
|
|
4
|
-
// SUPERSEDED. The architecture this belongs to no longer exists. A deployed
|
|
5
|
-
// policy involves exactly two contracts, the OZ smart account and one
|
|
6
|
-
// immutable interpreter, and the synthesiser emits policy DATA rather than
|
|
7
|
-
// code. Nothing on the synthesis path reaches this module, and no install can
|
|
8
|
-
// produce a contract from it. When a constraint falls outside the grammar the
|
|
9
|
-
// answer is to version the synthesiser and the interpreter together, not to
|
|
10
|
-
// put unaudited Rust on chain per policy.
|
|
11
|
-
//
|
|
12
|
-
// Deleting it is a decision nobody has taken yet, not a dependency question.
|
|
13
|
-
// `compile-gate.ts` takes a Rust string and knows nothing about this module,
|
|
14
|
-
// and its tests compile a hardcoded fixture, so the gate keeps working either
|
|
15
|
-
// way. What deleting this would remove is the only in-repo producer of policy
|
|
16
|
-
// Rust for the gate to check. Do not read its presence as the product
|
|
17
|
-
// generating contracts, and do not wire it into the synthesis path.
|
|
18
|
-
//
|
|
19
|
-
// What follows describes the abandoned design.
|
|
20
|
-
//
|
|
21
|
-
// This is the LAST-RESORT tool for constraints the v1 DSL cannot express. It
|
|
22
|
-
// lives OUT of the audited happy-path surface; the synthesiser never emits it
|
|
23
|
-
// automatically. It mirrors Zodiac Roles' `Custom` operator: a hand-written
|
|
24
|
-
// (here: scaffolded) policy the operator assumes responsibility for.
|
|
25
|
-
//
|
|
26
|
-
// The skeleton carries five storage invariants the user MUST preserve when
|
|
27
|
-
// filling in the predicate logic:
|
|
28
|
-
// 1. `smart_account.require_auth()` is invoked INSIDE `enforce`.
|
|
29
|
-
// 2. Storage is keyed by the composite `(smart_account, rule_id)`.
|
|
30
|
-
// 3. Chain reads use `unwrap_or_default()` so a missing key is a permitted
|
|
31
|
-
// default, not a panic.
|
|
32
|
-
// 4. Storage is `persistent()` (NOT `instance()`) so the policy survives
|
|
33
|
-
// contract instance lifecycle changes.
|
|
34
|
-
// 5. `uninstall` removes the stored state.
|
|
35
|
-
//
|
|
36
|
-
// Determinism: same `(spec, name)` -> byte-identical output. No clock, no RNG,
|
|
37
|
-
// no environment reads.
|
|
38
|
-
/** Format a name for use as a Rust identifier. The input is constrained by
|
|
39
|
-
* the caller (a kebab-case policy name + alphanumeric addresses); this is a
|
|
40
|
-
* belt-and-braces sanitiser that maps anything non-identifier to '_'. */
|
|
41
|
-
function asRustIdent(s) {
|
|
42
|
-
let out = '';
|
|
43
|
-
for (let i = 0; i < s.length; i++) {
|
|
44
|
-
const ch = s.charCodeAt(i);
|
|
45
|
-
const ok = (ch >= 48 && ch <= 57) || // 0-9
|
|
46
|
-
(ch >= 65 && ch <= 90) || // A-Z
|
|
47
|
-
(ch >= 97 && ch <= 122) || // a-z
|
|
48
|
-
ch === 95; // _
|
|
49
|
-
out += ok ? s[i] : '_';
|
|
50
|
-
}
|
|
51
|
-
// Identifiers may not start with a digit.
|
|
52
|
-
if (out.length > 0 && out.charCodeAt(0) >= 48 && out.charCodeAt(0) <= 57) {
|
|
53
|
-
out = `_${out}`;
|
|
54
|
-
}
|
|
55
|
-
return out || 'policy';
|
|
56
|
-
}
|
|
57
|
-
/** Render the `uncovered` list as a `//` comment block (one line per item). */
|
|
58
|
-
function renderUncovered(uncovered) {
|
|
59
|
-
if (uncovered.length === 0) {
|
|
60
|
-
return ' // (no specific uncovered constructs reported)';
|
|
61
|
-
}
|
|
62
|
-
return uncovered.map((line) => ` // - ${line}`).join('\n');
|
|
63
|
-
}
|
|
64
|
-
/** Generate a deterministic OZ `Policy` skeleton for an uncovered constraint.
|
|
65
|
-
*
|
|
66
|
-
* The skeleton is NOT a finished policy; the user fills in the predicate
|
|
67
|
-
* logic in the marked TODO(user) holes and audits the result themselves.
|
|
68
|
-
*
|
|
69
|
-
* Same `(spec, name)` always produces byte-identical output. */
|
|
70
|
-
export function generateRust(spec, name) {
|
|
71
|
-
const _contract = spec.contract;
|
|
72
|
-
const _fnName = spec.fnName ?? 'enforce';
|
|
73
|
-
const structName = asRustIdent(name);
|
|
74
|
-
const _dataKeyName = `${asRustIdent(name)}_data`;
|
|
75
|
-
const uncoveredBlock = renderUncovered(spec.uncovered);
|
|
76
|
-
return `// Auto-generated by the OZ Policy Builder codegen escape hatch.
|
|
77
|
-
// THIS OUTPUT IS THE USER'S TO COMPLETE AND AUDIT.
|
|
78
|
-
// The synthesiser never emits it on the happy path; reach for it only when
|
|
79
|
-
// a constraint cannot be expressed in the v1 DSL grammar (same shape as
|
|
80
|
-
// Zodiac Roles' \`Custom\` operator).
|
|
81
|
-
|
|
82
|
-
#![no_std]
|
|
83
|
-
|
|
84
|
-
use soroban_sdk::{contract, contractimpl, contracttype, Address, Env, Vec};
|
|
85
|
-
|
|
86
|
-
#[contracttype]
|
|
87
|
-
pub enum DataKey {
|
|
88
|
-
/// Composite key \`(smart_account, rule_id)\` so multiple policies on
|
|
89
|
-
/// one smart account coexist without collision.
|
|
90
|
-
State(Address, u32),
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
#[contract]
|
|
94
|
-
pub struct ${structName};
|
|
95
|
-
|
|
96
|
-
#[contractimpl]
|
|
97
|
-
impl ${structName} {
|
|
98
|
-
pub fn install(e: &Env, smart_account: Address, rule_id: u32) {
|
|
99
|
-
// TODO(user): initialise policy state for this (smart_account, rule_id).
|
|
100
|
-
// The skeleton leaves the stored value empty; the predicate body
|
|
101
|
-
// (in \`enforce\`) decides what to read.
|
|
102
|
-
e.storage().persistent().set(
|
|
103
|
-
&DataKey::State(smart_account.clone(), rule_id),
|
|
104
|
-
&(),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
pub fn enforce(
|
|
109
|
-
e: &Env,
|
|
110
|
-
smart_account: Address,
|
|
111
|
-
rule_id: u32,
|
|
112
|
-
_signers: Vec<Address>,
|
|
113
|
-
) {
|
|
114
|
-
// Invariant 1: authorise the smart account BEFORE any state read or
|
|
115
|
-
// predicate evaluation. Removing this call opens the policy to
|
|
116
|
-
// impersonation by any caller with a valid rule_id.
|
|
117
|
-
smart_account.require_auth();
|
|
118
|
-
|
|
119
|
-
// Invariant 2 + 4: read stored state through the composite key on
|
|
120
|
-
// \`persistent()\` storage. \`unwrap_or_default()\` (invariant 3)
|
|
121
|
-
// makes an empty key a permitted default rather than a panic.
|
|
122
|
-
let _state: () = e
|
|
123
|
-
.storage()
|
|
124
|
-
.persistent()
|
|
125
|
-
.get(&DataKey::State(smart_account.clone(), rule_id))
|
|
126
|
-
.unwrap_or_default();
|
|
127
|
-
|
|
128
|
-
// --- uncovered constraints the v1 DSL refused to express ---
|
|
129
|
-
${uncoveredBlock}
|
|
130
|
-
|
|
131
|
-
// TODO(user): replace the placeholder below with the real predicate
|
|
132
|
-
// body. The recorded flow's \`(contract, fn_name, args)\` is the
|
|
133
|
-
// single authorised call \`Policy::enforce\` receives; match against
|
|
134
|
-
// \`e\` here.
|
|
135
|
-
let _ = (_state, &e);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
pub fn uninstall(e: &Env, smart_account: Address, rule_id: u32) {
|
|
139
|
-
// Invariant 5: \`uninstall\` removes the stored state so a subsequent
|
|
140
|
-
// install starts from a clean slate (and the nonce returns to its
|
|
141
|
-
// post-uninstall value).
|
|
142
|
-
e.storage()
|
|
143
|
-
.persistent()
|
|
144
|
-
.remove(&DataKey::State(smart_account, rule_id));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
`;
|
|
148
|
-
}
|
package/dist/ir/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { IRCompare, IRCompOp, IRCondition, IRLogic, IRPolicyRule, IRScalarType, IRSelector, PolicyIR, } from './types.ts';
|
package/dist/ir/index.js
DELETED
package/dist/ir/types.d.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/** Comparison operator. Mirrors NEAR-V2 `CompOp`. */
|
|
2
|
-
export type IRCompOp = 'eq' | 'lte';
|
|
3
|
-
/** Boolean combinator. Mirrors NEAR-V2 `LogicOp`. */
|
|
4
|
-
export type IRLogic = 'and';
|
|
5
|
-
/** Scalar value type. The subset the predicate grammar has a literal for. */
|
|
6
|
-
export type IRScalarType = 'address' | 'i128' | 'u32' | 'symbol';
|
|
7
|
-
/** WHERE in the authorized call a value is read. NEAR-V2 selectors first, OZ
|
|
8
|
-
* extensions after (an adapter that cannot express an extension flags it). */
|
|
9
|
-
export type IRSelector = {
|
|
10
|
-
kind: 'arg';
|
|
11
|
-
argIndex: number;
|
|
12
|
-
scalarType: IRScalarType;
|
|
13
|
-
} | {
|
|
14
|
-
kind: 'arg_len';
|
|
15
|
-
argIndex: number;
|
|
16
|
-
} | {
|
|
17
|
-
kind: 'arg_field';
|
|
18
|
-
argIndex: number;
|
|
19
|
-
element: number;
|
|
20
|
-
field: string;
|
|
21
|
-
scalarType: IRScalarType;
|
|
22
|
-
};
|
|
23
|
-
/** A single comparison leaf. `value` is a decimal/hex string (i128-safe;
|
|
24
|
-
* never a JS number). */
|
|
25
|
-
export interface IRCompare {
|
|
26
|
-
selector: IRSelector;
|
|
27
|
-
operator: IRCompOp;
|
|
28
|
-
value: string;
|
|
29
|
-
}
|
|
30
|
-
/** Condition tree. NEAR-V2 guard/constraint are flat And/Or; the IR allows
|
|
31
|
-
* nesting + `not` + `in` + `eq_seq` so the same IR can later lower to the OZ
|
|
32
|
-
* predicate DSL. Week-1 adapters only lower the flat supported subset and flag
|
|
33
|
-
* the rest. */
|
|
34
|
-
export type IRCondition = {
|
|
35
|
-
op: 'and';
|
|
36
|
-
children: IRCondition[];
|
|
37
|
-
} | {
|
|
38
|
-
op: 'compare';
|
|
39
|
-
compare: IRCompare;
|
|
40
|
-
} | {
|
|
41
|
-
op: 'in';
|
|
42
|
-
selector: IRSelector;
|
|
43
|
-
values: string[];
|
|
44
|
-
}
|
|
45
|
-
/** Exact ordered sequence equality. The IR-level selector value MUST EQUAL
|
|
46
|
-
* `values` as an ORDERED sequence (e.g. a swap hop `path`). `in` is pure
|
|
47
|
-
* set membership and cannot express order; `eq_seq` is the construct that
|
|
48
|
-
* does. Adapters that cannot express an exact ordered vector (OZ built-ins)
|
|
49
|
-
* flag this as `uncovered` rather than silently dropping it. */
|
|
50
|
-
| {
|
|
51
|
-
op: 'eq_seq';
|
|
52
|
-
selector: IRSelector;
|
|
53
|
-
values: string[];
|
|
54
|
-
};
|
|
55
|
-
export interface IRPolicyRule {
|
|
56
|
-
/** NEAR-V2 scope filter; each field optional (absent = wildcard). */
|
|
57
|
-
scope: {
|
|
58
|
-
contract?: string;
|
|
59
|
-
method?: string;
|
|
60
|
-
};
|
|
61
|
-
/** NEAR-V2 constraint (AND; reject the transaction when any fails). */
|
|
62
|
-
constraints: IRCondition[];
|
|
63
|
-
/** OZ context-rule expiry, by ledger sequence - the only clock the OZ
|
|
64
|
-
* account model exposes to a rule. */
|
|
65
|
-
expiry?: {
|
|
66
|
-
validUntilLedger?: number;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
export interface PolicyIR {
|
|
70
|
-
rules: IRPolicyRule[];
|
|
71
|
-
}
|
package/dist/ir/types.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// src/ir/types.ts - the PolicyIR ("Policy Tree").
|
|
2
|
-
//
|
|
3
|
-
// `PolicyIR` is the custody-agnostic "Policy Tree" hub (the diagram's Policy
|
|
4
|
-
// Tree) that every CustodyAdapter compiles FROM. It generalizes the NEAR-V2
|
|
5
|
-
// policy schema (roles / scope filter / guard / constraint / comparison leaves /
|
|
6
|
-
// default behaviour) into one chain-neutral shape and extends it with the
|
|
7
|
-
// single IR can serve every backend. NEAR-V2 has no stateful spend window,
|
|
8
|
-
// below and are only lowered by adapters that declare support for them.
|
|
9
|
-
export {};
|
package/dist/mandate/index.d.ts
DELETED
package/dist/mandate/index.js
DELETED
package/dist/mandate/to-ir.d.ts
DELETED
package/dist/mandate/to-ir.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
// src/mandate/to-ir.ts - deterministic lowering of a MandateSpec to a PolicyIR.
|
|
2
|
-
//
|
|
3
|
-
// `mandateToPolicyIR` is pure and total: the same spec always lowers to a
|
|
4
|
-
// byte-identical PolicyIR. No decoding, no inference, no clock. Each mandate
|
|
5
|
-
// field maps to exactly one IR construct:
|
|
6
|
-
// contract/method -> rule.scope
|
|
7
|
-
// spendingLimit -> a `window_spent(token,window) <= limit` compare
|
|
8
|
-
// approvalThreshold-> rule.approval.threshold
|
|
9
|
-
// recipients -> an `in` condition on the recipient arg (flagged as not
|
|
10
|
-
// covered by the OZ adapter; that is expected)
|
|
11
|
-
// expiry -> rule.expiry
|
|
12
|
-
// The top-level default is `deny_all` (OZ context rules are deny-by-default).
|
|
13
|
-
/** Arg index the recipient allowlist constrains. Pinned to the SEP-41
|
|
14
|
-
* `transfer(from, to, amount)` convention where `to` is arg 1. This condition
|
|
15
|
-
* is flagged as not covered by the OZ adapter (no built-in primitive expresses
|
|
16
|
-
* an arg allowlist), so the index only needs to be deterministic in week-1. */
|
|
17
|
-
const RECIPIENT_ARG_INDEX = 1;
|
|
18
|
-
export function mandateToPolicyIR(spec) {
|
|
19
|
-
const scope = { contract: spec.contract };
|
|
20
|
-
if (spec.method !== undefined)
|
|
21
|
-
scope.method = spec.method;
|
|
22
|
-
const constraints = [];
|
|
23
|
-
if (spec.spendingLimit) {
|
|
24
|
-
constraints.push({
|
|
25
|
-
op: 'compare',
|
|
26
|
-
compare: {
|
|
27
|
-
selector: {
|
|
28
|
-
kind: 'window_spent',
|
|
29
|
-
token: spec.spendingLimit.token,
|
|
30
|
-
windowSeconds: spec.spendingLimit.windowSeconds,
|
|
31
|
-
},
|
|
32
|
-
operator: 'lte',
|
|
33
|
-
value: spec.spendingLimit.limit,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
if (spec.recipients && spec.recipients.length > 0) {
|
|
38
|
-
const selector = {
|
|
39
|
-
kind: 'arg',
|
|
40
|
-
argIndex: RECIPIENT_ARG_INDEX,
|
|
41
|
-
scalarType: 'address',
|
|
42
|
-
};
|
|
43
|
-
constraints.push({ op: 'in', selector, values: [...spec.recipients] });
|
|
44
|
-
}
|
|
45
|
-
const rule = { roles: [], scope, constraints };
|
|
46
|
-
if (spec.approvalThreshold !== undefined) {
|
|
47
|
-
rule.approval = { kind: 'threshold', threshold: spec.approvalThreshold };
|
|
48
|
-
}
|
|
49
|
-
if (spec.expiry) {
|
|
50
|
-
const expiry = {};
|
|
51
|
-
if (spec.expiry.validUntilLedger !== undefined) {
|
|
52
|
-
expiry.validUntilLedger = spec.expiry.validUntilLedger;
|
|
53
|
-
}
|
|
54
|
-
if (spec.expiry.validUntilUnixSeconds !== undefined) {
|
|
55
|
-
expiry.validUntilUnixSeconds = spec.expiry.validUntilUnixSeconds;
|
|
56
|
-
}
|
|
57
|
-
rule.expiry = expiry;
|
|
58
|
-
}
|
|
59
|
-
return { chain: spec.chain, defaultBehavior: 'deny_all', rules: [rule] };
|
|
60
|
-
}
|
package/dist/mandate/types.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export interface MandateSpec {
|
|
2
|
-
chain: 'stellar';
|
|
3
|
-
contract: string;
|
|
4
|
-
method?: string;
|
|
5
|
-
/** -> OZ `spending_limit` primitive. `limit` is an i128 decimal string. */
|
|
6
|
-
spendingLimit?: {
|
|
7
|
-
token: string;
|
|
8
|
-
limit: string;
|
|
9
|
-
windowSeconds: number;
|
|
10
|
-
};
|
|
11
|
-
/** -> OZ `simple_threshold` / `weighted_threshold` primitive. */
|
|
12
|
-
approvalThreshold?: number;
|
|
13
|
-
/** Recipient allowlist -> an `in` arg condition (not covered by an OZ built-in). */
|
|
14
|
-
recipients?: string[];
|
|
15
|
-
/** -> OZ context-rule expiry. */
|
|
16
|
-
expiry?: {
|
|
17
|
-
validUntilLedger?: number;
|
|
18
|
-
validUntilUnixSeconds?: number;
|
|
19
|
-
};
|
|
20
|
-
}
|
package/dist/mandate/types.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// src/mandate/types.ts - the deterministic Mandate source.
|
|
2
|
-
//
|
|
3
|
-
// A MandateSpec is a declarative policy statement: "this account may call this
|
|
4
|
-
// contract, spending at most L per window, requiring M approvals, to these
|
|
5
|
-
// recipients, until this expiry". It carries NO transaction to decode, NO
|
|
6
|
-
// parseConfidence, and NO inference - it lowers deterministically to a
|
|
7
|
-
// PolicyIR. It is the clean end-to-end demo path, co-equal with the recorder.
|
|
8
|
-
export {};
|
package/dist/seams/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { ChainDecoder, CompileResult, CustodyAdapter, CustodyCapabilities, CustodyMode, PolicySource, SimulationResult, } from './types.ts';
|
package/dist/seams/index.js
DELETED
package/dist/seams/types.d.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { ToolResponse } from '../errors.ts';
|
|
2
|
-
import type { PolicyIR } from '../ir/types.ts';
|
|
3
|
-
import type { Network, ProposedPolicy, RecordedTransaction, ScVal } from '../types.ts';
|
|
4
|
-
/** INPUT: something that produces a normalized RecordedTransaction. */
|
|
5
|
-
export interface PolicySource {
|
|
6
|
-
readonly name: string;
|
|
7
|
-
capture(input: {
|
|
8
|
-
hash?: string;
|
|
9
|
-
xdr?: string;
|
|
10
|
-
network: Network;
|
|
11
|
-
}): Promise<ToolResponse<RecordedTransaction>>;
|
|
12
|
-
}
|
|
13
|
-
/** DECODE: chain-specific call data -> the normalized facts the synth reasons
|
|
14
|
-
* over. */
|
|
15
|
-
export interface ChainDecoder {
|
|
16
|
-
readonly chain: 'stellar' | 'evm';
|
|
17
|
-
decodeInvocation(tx: RecordedTransaction): {
|
|
18
|
-
contract: string;
|
|
19
|
-
method: string;
|
|
20
|
-
args: ScVal[];
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
/** OZ = enforce; `log_only` is reserved for shadow mode (out of scope). */
|
|
24
|
-
export type CustodyMode = 'enforce' | 'log_only';
|
|
25
|
-
/** What a backend can express. A construct needing a false flag is flagged
|
|
26
|
-
* `uncovered` by the adapter, never silently dropped. */
|
|
27
|
-
export interface CustodyCapabilities {
|
|
28
|
-
supportsSpendWindow: boolean;
|
|
29
|
-
supportsTimeExpiry: boolean;
|
|
30
|
-
supportsThreshold: boolean;
|
|
31
|
-
supportsGeneralPredicate: boolean;
|
|
32
|
-
}
|
|
33
|
-
/** The result of compiling a PolicyIR for one backend. */
|
|
34
|
-
export interface CompileResult {
|
|
35
|
-
/** false => some IR construct this backend cannot express (see `uncovered`). */
|
|
36
|
-
covered: boolean;
|
|
37
|
-
/** Human-readable list of unsupported constructs. */
|
|
38
|
-
uncovered: string[];
|
|
39
|
-
/** The backend-native installable policy, assembled when a rule lowered. */
|
|
40
|
-
proposed?: ProposedPolicy;
|
|
41
|
-
}
|
|
42
|
-
/** Result of a simulate() dry-run. `ts-model` is the off-chain TS evaluator.
|
|
43
|
-
* Real permit/deny semantics wiring is a later phase; week-1 returns a
|
|
44
|
-
* clearly-marked stub (empty `evaluations`, `permitted: null`). */
|
|
45
|
-
export interface SimulationResult {
|
|
46
|
-
backend: 'ts-model';
|
|
47
|
-
/** Whether the permit tx would be allowed; null until real semantics land. */
|
|
48
|
-
permitted: boolean | null;
|
|
49
|
-
/** Per-construct evaluation trace; empty placeholder in this slice. */
|
|
50
|
-
evaluations: unknown[];
|
|
51
|
-
/** Diagnostics; marks the stub explicitly. */
|
|
52
|
-
notes: string[];
|
|
53
|
-
}
|
|
54
|
-
/** OUTPUT: compile a PolicyIR to a backend-specific installable policy. */
|
|
55
|
-
export interface CustodyAdapter {
|
|
56
|
-
readonly name: string;
|
|
57
|
-
readonly mode: CustodyMode;
|
|
58
|
-
capabilities(): CustodyCapabilities;
|
|
59
|
-
compile(ir: PolicyIR): CompileResult;
|
|
60
|
-
/** Phase-03 wiring; a clearly-marked stub in week-1. */
|
|
61
|
-
simulate(ir: PolicyIR, permitTx: RecordedTransaction): SimulationResult;
|
|
62
|
-
/** Canonical JSON of the IR (portability / audit). */
|
|
63
|
-
export(ir: PolicyIR): string;
|
|
64
|
-
}
|
package/dist/seams/types.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// src/seams/types.ts - the three custody seams (contracts, not implementations).
|
|
2
|
-
//
|
|
3
|
-
// The synthesizer is decoupled from its edges by three seams:
|
|
4
|
-
// - PolicySource (INPUT): produces a normalized RecordedTransaction. The
|
|
5
|
-
// recorder is the reference source; a MandateSpec
|
|
6
|
-
// lowering is a second, deterministic source.
|
|
7
|
-
// - ChainDecoder (DECODE): chain-specific call data -> normalized facts.
|
|
8
|
-
// - CustodyAdapter (OUTPUT): compile a PolicyIR to a backend-native policy +
|
|
9
|
-
// verify/export it (OZ is the first adapter).
|
|
10
|
-
// These are INTERFACES only. Implementations live in their own modules.
|
|
11
|
-
export {};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode, ScVal } from '../types.ts';
|
|
2
|
-
import type { EvalContext } from './evaluate.ts';
|
|
3
|
-
export interface DenyCase {
|
|
4
|
-
dimension: string;
|
|
5
|
-
ctx: EvalContext;
|
|
6
|
-
/** Canonical reason the Rust interpreter returns for this dimension.
|
|
7
|
-
* Optional: when set, `runHarness` asserts the TS evaluator emits this
|
|
8
|
-
* exact string, so a future TS/Rust reason divergence fails CI. When
|
|
9
|
-
* absent the case is only checked on the boolean decision. */
|
|
10
|
-
expectedReason?: string;
|
|
11
|
-
}
|
|
12
|
-
export interface GeneratedCases {
|
|
13
|
-
permit: EvalContext;
|
|
14
|
-
denies: DenyCase[];
|
|
15
|
-
}
|
|
16
|
-
declare const OVERPERMISSIVE_DIMENSIONS: readonly ["argument_reorder"];
|
|
17
|
-
export { OVERPERMISSIVE_DIMENSIONS };
|
|
18
|
-
/** Build deterministic model-evaluated alternatives without mutating the intended call.
|
|
19
|
-
*
|
|
20
|
-
* @param predicate - the synthesized predicate
|
|
21
|
-
* @param permitCtx - EvalContext for the intended (permitted) call
|
|
22
|
-
* @param dimensions - optional whitelist of dimension names to emit; when omitted
|
|
23
|
-
* all known dimensions (including over-permissiveness mutations)
|
|
24
|
-
* are emitted. */
|
|
25
|
-
export declare function generateCases(predicate: PredicateNode, permitCtx: EvalContext, dimensions?: string[]): GeneratedCases;
|
|
26
|
-
/** Deep-copy an ScVal so a mutation cannot alias the recorded call.
|
|
27
|
-
* Exported so the permit-context builder shares this one implementation. */
|
|
28
|
-
export declare function cloneScVal(value: ScVal, depth?: number): ScVal;
|