@crediolabs/policy-synth 0.3.0 → 0.4.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/dist/install/authority-overlap.d.ts +101 -0
- package/dist/install/authority-overlap.js +227 -0
- package/dist/install/index.d.ts +1 -0
- package/dist/install/index.js +4 -0
- package/dist/record/freshness.d.ts +17 -11
- package/dist/record/freshness.js +28 -18
- package/dist/record/index.d.ts +10 -0
- package/dist/record/index.js +32 -1
- package/dist/record/rpc.d.ts +4 -0
- package/dist/record/rpc.js +4 -1
- package/dist/registry/identify.d.ts +10 -1
- package/dist/registry/identify.js +4 -1
- package/dist/registry/on-chain-spec.d.ts +37 -0
- package/dist/registry/on-chain-spec.js +152 -0
- package/dist/run/index.d.ts +25 -4
- package/dist/run/index.js +65 -4
- package/dist/run/schemas.d.ts +313 -0
- package/dist/run/schemas.js +52 -0
- package/dist/synth/declare.d.ts +30 -0
- package/dist/synth/declare.js +98 -0
- package/dist/synth/index.d.ts +1 -0
- package/dist/synth/index.js +1 -0
- package/dist-cjs/install/authority-overlap.d.ts +101 -0
- package/dist-cjs/install/authority-overlap.js +236 -0
- package/dist-cjs/install/index.d.ts +1 -0
- package/dist-cjs/install/index.js +13 -2
- package/dist-cjs/record/freshness.d.ts +17 -11
- package/dist-cjs/record/freshness.js +28 -18
- package/dist-cjs/record/index.d.ts +10 -0
- package/dist-cjs/record/index.js +31 -0
- package/dist-cjs/record/rpc.d.ts +4 -0
- package/dist-cjs/record/rpc.js +7 -3
- package/dist-cjs/registry/identify.d.ts +10 -1
- package/dist-cjs/registry/identify.js +4 -0
- package/dist-cjs/registry/on-chain-spec.d.ts +37 -0
- package/dist-cjs/registry/on-chain-spec.js +159 -0
- package/dist-cjs/run/index.d.ts +25 -4
- package/dist-cjs/run/index.js +65 -2
- package/dist-cjs/run/schemas.d.ts +313 -0
- package/dist-cjs/run/schemas.js +53 -1
- package/dist-cjs/synth/declare.d.ts +30 -0
- package/dist-cjs/synth/declare.js +101 -0
- package/dist-cjs/synth/index.d.ts +1 -0
- package/dist-cjs/synth/index.js +3 -1
- package/package.json +2 -2
- package/src/install/authority-overlap.ts +312 -0
- package/src/install/index.ts +20 -0
- package/src/record/freshness.ts +29 -18
- package/src/record/index.ts +59 -2
- package/src/record/rpc.ts +4 -1
- package/src/registry/identify.ts +4 -1
- package/src/registry/on-chain-spec.ts +168 -0
- package/src/run/index.ts +79 -2
- package/src/run/schemas.ts +57 -0
- package/src/synth/declare.ts +157 -0
- package/src/synth/index.ts +5 -0
- 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
package/dist-cjs/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-cjs/ir/types.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/ir/types.ts - the PolicyIR ("Policy Tree").
|
|
3
|
-
//
|
|
4
|
-
// `PolicyIR` is the custody-agnostic "Policy Tree" hub (the diagram's Policy
|
|
5
|
-
// Tree) that every CustodyAdapter compiles FROM. It generalizes the NEAR-V2
|
|
6
|
-
// policy schema (roles / scope filter / guard / constraint / comparison leaves /
|
|
7
|
-
// default behaviour) into one chain-neutral shape and extends it with the
|
|
8
|
-
// single IR can serve every backend. NEAR-V2 has no stateful spend window,
|
|
9
|
-
// below and are only lowered by adapters that declare support for them.
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/mandate/index.ts - re-export the Mandate source.
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.mandateToPolicyIR = void 0;
|
|
5
|
-
var to_ir_ts_1 = require("./to-ir.js");
|
|
6
|
-
Object.defineProperty(exports, "mandateToPolicyIR", { enumerable: true, get: function () { return to_ir_ts_1.mandateToPolicyIR; } });
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/mandate/to-ir.ts - deterministic lowering of a MandateSpec to a PolicyIR.
|
|
3
|
-
//
|
|
4
|
-
// `mandateToPolicyIR` is pure and total: the same spec always lowers to a
|
|
5
|
-
// byte-identical PolicyIR. No decoding, no inference, no clock. Each mandate
|
|
6
|
-
// field maps to exactly one IR construct:
|
|
7
|
-
// contract/method -> rule.scope
|
|
8
|
-
// spendingLimit -> a `window_spent(token,window) <= limit` compare
|
|
9
|
-
// approvalThreshold-> rule.approval.threshold
|
|
10
|
-
// recipients -> an `in` condition on the recipient arg (flagged as not
|
|
11
|
-
// covered by the OZ adapter; that is expected)
|
|
12
|
-
// expiry -> rule.expiry
|
|
13
|
-
// The top-level default is `deny_all` (OZ context rules are deny-by-default).
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.mandateToPolicyIR = mandateToPolicyIR;
|
|
16
|
-
/** Arg index the recipient allowlist constrains. Pinned to the SEP-41
|
|
17
|
-
* `transfer(from, to, amount)` convention where `to` is arg 1. This condition
|
|
18
|
-
* is flagged as not covered by the OZ adapter (no built-in primitive expresses
|
|
19
|
-
* an arg allowlist), so the index only needs to be deterministic in week-1. */
|
|
20
|
-
const RECIPIENT_ARG_INDEX = 1;
|
|
21
|
-
function mandateToPolicyIR(spec) {
|
|
22
|
-
const scope = { contract: spec.contract };
|
|
23
|
-
if (spec.method !== undefined)
|
|
24
|
-
scope.method = spec.method;
|
|
25
|
-
const constraints = [];
|
|
26
|
-
if (spec.spendingLimit) {
|
|
27
|
-
constraints.push({
|
|
28
|
-
op: 'compare',
|
|
29
|
-
compare: {
|
|
30
|
-
selector: {
|
|
31
|
-
kind: 'window_spent',
|
|
32
|
-
token: spec.spendingLimit.token,
|
|
33
|
-
windowSeconds: spec.spendingLimit.windowSeconds,
|
|
34
|
-
},
|
|
35
|
-
operator: 'lte',
|
|
36
|
-
value: spec.spendingLimit.limit,
|
|
37
|
-
},
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
if (spec.recipients && spec.recipients.length > 0) {
|
|
41
|
-
const selector = {
|
|
42
|
-
kind: 'arg',
|
|
43
|
-
argIndex: RECIPIENT_ARG_INDEX,
|
|
44
|
-
scalarType: 'address',
|
|
45
|
-
};
|
|
46
|
-
constraints.push({ op: 'in', selector, values: [...spec.recipients] });
|
|
47
|
-
}
|
|
48
|
-
const rule = { roles: [], scope, constraints };
|
|
49
|
-
if (spec.approvalThreshold !== undefined) {
|
|
50
|
-
rule.approval = { kind: 'threshold', threshold: spec.approvalThreshold };
|
|
51
|
-
}
|
|
52
|
-
if (spec.expiry) {
|
|
53
|
-
const expiry = {};
|
|
54
|
-
if (spec.expiry.validUntilLedger !== undefined) {
|
|
55
|
-
expiry.validUntilLedger = spec.expiry.validUntilLedger;
|
|
56
|
-
}
|
|
57
|
-
if (spec.expiry.validUntilUnixSeconds !== undefined) {
|
|
58
|
-
expiry.validUntilUnixSeconds = spec.expiry.validUntilUnixSeconds;
|
|
59
|
-
}
|
|
60
|
-
rule.expiry = expiry;
|
|
61
|
-
}
|
|
62
|
-
return { chain: spec.chain, defaultBehavior: 'deny_all', rules: [rule] };
|
|
63
|
-
}
|
|
@@ -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
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/mandate/types.ts - the deterministic Mandate source.
|
|
3
|
-
//
|
|
4
|
-
// A MandateSpec is a declarative policy statement: "this account may call this
|
|
5
|
-
// contract, spending at most L per window, requiring M approvals, to these
|
|
6
|
-
// recipients, until this expiry". It carries NO transaction to decode, NO
|
|
7
|
-
// parseConfidence, and NO inference - it lowers deterministically to a
|
|
8
|
-
// PolicyIR. It is the clean end-to-end demo path, co-equal with the recorder.
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type { ChainDecoder, CompileResult, CustodyAdapter, CustodyCapabilities, CustodyMode, PolicySource, SimulationResult, } from './types.ts';
|
package/dist-cjs/seams/index.js
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-cjs/seams/types.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// src/seams/types.ts - the three custody seams (contracts, not implementations).
|
|
3
|
-
//
|
|
4
|
-
// The synthesizer is decoupled from its edges by three seams:
|
|
5
|
-
// - PolicySource (INPUT): produces a normalized RecordedTransaction. The
|
|
6
|
-
// recorder is the reference source; a MandateSpec
|
|
7
|
-
// lowering is a second, deterministic source.
|
|
8
|
-
// - ChainDecoder (DECODE): chain-specific call data -> normalized facts.
|
|
9
|
-
// - CustodyAdapter (OUTPUT): compile a PolicyIR to a backend-native policy +
|
|
10
|
-
// verify/export it (OZ is the first adapter).
|
|
11
|
-
// These are INTERFACES only. Implementations live in their own modules.
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -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;
|
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OVERPERMISSIVE_DIMENSIONS = void 0;
|
|
4
|
-
exports.generateCases = generateCases;
|
|
5
|
-
exports.cloneScVal = cloneScVal;
|
|
6
|
-
const types_ts_1 = require("../types.js");
|
|
7
|
-
// Deterministic XLM/USDC adjacency fixture; the shared registry can replace this boundary later.
|
|
8
|
-
const OVERPERMISSIVE_DIMENSIONS = ['argument_reorder'];
|
|
9
|
-
exports.OVERPERMISSIVE_DIMENSIONS = OVERPERMISSIVE_DIMENSIONS;
|
|
10
|
-
/** Build deterministic model-evaluated alternatives without mutating the intended call.
|
|
11
|
-
*
|
|
12
|
-
* @param predicate - the synthesized predicate
|
|
13
|
-
* @param permitCtx - EvalContext for the intended (permitted) call
|
|
14
|
-
* @param dimensions - optional whitelist of dimension names to emit; when omitted
|
|
15
|
-
* all known dimensions (including over-permissiveness mutations)
|
|
16
|
-
* are emitted. */
|
|
17
|
-
function generateCases(predicate, permitCtx, dimensions) {
|
|
18
|
-
const facts = inspectPredicate(predicate);
|
|
19
|
-
const denies = [];
|
|
20
|
-
// arg_bound: set a constrained arg to an opaque ScVal.
|
|
21
|
-
// The reason depends on the predicate shape:
|
|
22
|
-
// - an `eq(call_arg[i], literal)` denies with ARG_MISMATCH
|
|
23
|
-
// - an `in(call_arg[i], [literals])` denies with NOT_IN_ALLOWLIST
|
|
24
|
-
// Both are correct in the TS evaluator; the reason is predicate-dependent
|
|
25
|
-
// so we cannot pin a single canonical reason here.
|
|
26
|
-
const argumentConstraints = [];
|
|
27
|
-
for (const comparison of facts.comparisons) {
|
|
28
|
-
if (comparison.op === 'eq' &&
|
|
29
|
-
comparison.left.kind === 'call_arg' &&
|
|
30
|
-
comparison.right.kind !== 'literal_vec') {
|
|
31
|
-
argumentConstraints.push({ index: comparison.left.index });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
for (const membership of facts.memberships) {
|
|
35
|
-
if (membership.needle.kind === 'call_arg') {
|
|
36
|
-
argumentConstraints.push({ index: membership.needle.index });
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
for (const constraint of argumentConstraints) {
|
|
40
|
-
const ctx = cloneContext(permitCtx);
|
|
41
|
-
ctx.args[constraint.index] = { type: 'other', value: 'deny-case-opaque-argument' };
|
|
42
|
-
denies.push({ dimension: 'arg_bound', ctx });
|
|
43
|
-
}
|
|
44
|
-
// soroswap_allowed_path: replace a bound vec arg with a different vector.
|
|
45
|
-
// Still valid for eq comparisons on literal_vec leaves.
|
|
46
|
-
for (const comparison of facts.comparisons) {
|
|
47
|
-
if (comparison.op !== 'eq' ||
|
|
48
|
-
comparison.left.kind !== 'call_arg' ||
|
|
49
|
-
comparison.right.kind !== 'literal_vec') {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
const ctx = cloneContext(permitCtx);
|
|
53
|
-
ctx.args[comparison.left.index] = differentVector(ctx.args[comparison.left.index]);
|
|
54
|
-
denies.push({ dimension: 'soroswap_allowed_path', ctx });
|
|
55
|
-
}
|
|
56
|
-
// argument_reorder: swap first two address args.
|
|
57
|
-
// Skipped when dimensions filter is active so the synth pipeline can emit a policy;
|
|
58
|
-
// the over-permissiveness harness then tests this mutation as a FINDING.
|
|
59
|
-
const constrainedArgIndices = new Set();
|
|
60
|
-
for (const comparison of facts.comparisons) {
|
|
61
|
-
if (comparison.left.kind === 'call_arg')
|
|
62
|
-
constrainedArgIndices.add(comparison.left.index);
|
|
63
|
-
}
|
|
64
|
-
for (const membership of facts.memberships) {
|
|
65
|
-
if (membership.needle.kind === 'call_arg')
|
|
66
|
-
constrainedArgIndices.add(membership.needle.index);
|
|
67
|
-
}
|
|
68
|
-
const hasConstrainedArg = constrainedArgIndices.size > 0;
|
|
69
|
-
if ((!dimensions || dimensions.includes('argument_reorder')) &&
|
|
70
|
-
hasConstrainedArg &&
|
|
71
|
-
permitCtx.args.length >= 2 &&
|
|
72
|
-
permitCtx.args[0]?.type === 'address' &&
|
|
73
|
-
permitCtx.args[1]?.type === 'address' &&
|
|
74
|
-
permitCtx.args[0].value !==
|
|
75
|
-
permitCtx.args[1].value) {
|
|
76
|
-
const ctx = cloneContext(permitCtx);
|
|
77
|
-
// Guard above guarantees args[0] and args[1] exist and are address-typed.
|
|
78
|
-
const a0 = ctx.args[0];
|
|
79
|
-
const a1 = ctx.args[1];
|
|
80
|
-
ctx.args[0] = a1;
|
|
81
|
-
ctx.args[1] = a0;
|
|
82
|
-
denies.push({ dimension: 'argument_reorder', ctx });
|
|
83
|
-
}
|
|
84
|
-
// map_field_flip: flip a bound map field to a different valid value of
|
|
85
|
-
// the same type. OPT-IN only, never ORIGINAL.
|
|
86
|
-
// Targets each `call_arg_field` leaf and produces a ctx whose vec element
|
|
87
|
-
// has a different value of the recorded field. ---
|
|
88
|
-
if (!dimensions || dimensions.includes('map_field_flip')) {
|
|
89
|
-
for (const comparison of facts.comparisons) {
|
|
90
|
-
const sel = comparison.left;
|
|
91
|
-
if (sel.kind !== 'call_arg_field')
|
|
92
|
-
continue;
|
|
93
|
-
const arg = permitCtx.args[sel.index];
|
|
94
|
-
if (arg?.type !== 'vec')
|
|
95
|
-
continue;
|
|
96
|
-
const element = arg.value[sel.element];
|
|
97
|
-
if (element?.type !== 'map' || !Array.isArray(element.value))
|
|
98
|
-
continue;
|
|
99
|
-
const entry = element.value.find((e) => e.key === sel.field);
|
|
100
|
-
if (!entry)
|
|
101
|
-
continue;
|
|
102
|
-
const flipped = flipFieldValue(entry.val);
|
|
103
|
-
if (flipped === null)
|
|
104
|
-
continue;
|
|
105
|
-
const ctx = cloneContext(permitCtx);
|
|
106
|
-
const clonedVec = arg.value.map(cloneScVal);
|
|
107
|
-
const clonedElement = clonedVec[sel.element];
|
|
108
|
-
if (clonedElement?.type !== 'map' || !Array.isArray(clonedElement.value))
|
|
109
|
-
continue;
|
|
110
|
-
clonedElement.value = clonedElement.value.map((e) => e.key === sel.field ? { key: e.key, val: flipped } : e);
|
|
111
|
-
clonedVec[sel.element] = clonedElement;
|
|
112
|
-
ctx.args[sel.index] = { type: 'vec', value: clonedVec };
|
|
113
|
-
denies.push({ dimension: 'map_field_flip', ctx });
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
// vec_append: append a new element to a bound vec. OPT-IN only, never ORIGINAL.
|
|
117
|
-
// Targets every `call_arg_len` leaf; without the length pin a caller can
|
|
118
|
-
// append an extra element to defeat per-element binds. ---
|
|
119
|
-
if (!dimensions || dimensions.includes('vec_append')) {
|
|
120
|
-
for (const comparison of facts.comparisons) {
|
|
121
|
-
const sel = comparison.left;
|
|
122
|
-
if (sel.kind !== 'call_arg_len')
|
|
123
|
-
continue;
|
|
124
|
-
const arg = permitCtx.args[sel.index];
|
|
125
|
-
if (arg?.type !== 'vec')
|
|
126
|
-
continue;
|
|
127
|
-
const ctx = cloneContext(permitCtx);
|
|
128
|
-
ctx.args[sel.index] = {
|
|
129
|
-
type: 'vec',
|
|
130
|
-
value: [...arg.value, { type: 'other', value: 'deny-case-vec-append' }],
|
|
131
|
-
};
|
|
132
|
-
denies.push({ dimension: 'vec_append', ctx });
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return { permit: cloneContext(permitCtx), denies };
|
|
136
|
-
}
|
|
137
|
-
function inspectPredicate(predicate) {
|
|
138
|
-
const facts = { comparisons: [], memberships: [] };
|
|
139
|
-
visit(predicate, facts);
|
|
140
|
-
return facts;
|
|
141
|
-
}
|
|
142
|
-
function visit(node, facts) {
|
|
143
|
-
switch (node.op) {
|
|
144
|
-
case 'and':
|
|
145
|
-
for (const child of node.children)
|
|
146
|
-
visit(child, facts);
|
|
147
|
-
return;
|
|
148
|
-
case 'in':
|
|
149
|
-
facts.memberships.push(node);
|
|
150
|
-
return;
|
|
151
|
-
case 'eq':
|
|
152
|
-
case 'lte':
|
|
153
|
-
facts.comparisons.push(node);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
function differentVector(actual) {
|
|
157
|
-
if (actual?.type !== 'vec')
|
|
158
|
-
return { type: 'vec', value: [] };
|
|
159
|
-
if (actual.value.length === 0) {
|
|
160
|
-
return { type: 'vec', value: [{ type: 'other', value: 'deny-case-extra-hop' }] };
|
|
161
|
-
}
|
|
162
|
-
if (actual.value.length > 1) {
|
|
163
|
-
const reversed = actual.value.map(cloneScVal).reverse();
|
|
164
|
-
if (JSON.stringify(reversed) !== JSON.stringify(actual.value)) {
|
|
165
|
-
return { type: 'vec', value: reversed };
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
const value = actual.value.map(cloneScVal);
|
|
169
|
-
value[0] = { type: 'other', value: 'deny-case-different-hop' };
|
|
170
|
-
return { type: 'vec', value };
|
|
171
|
-
}
|
|
172
|
-
function cloneContext(ctx) {
|
|
173
|
-
return {
|
|
174
|
-
contract: ctx.contract,
|
|
175
|
-
fn: ctx.fn,
|
|
176
|
-
args: ctx.args.map(cloneScVal),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
/** Deep-copy an ScVal so a mutation cannot alias the recorded call.
|
|
180
|
-
* Exported so the permit-context builder shares this one implementation. */
|
|
181
|
-
function cloneScVal(value, depth = 0) {
|
|
182
|
-
if (value.type === 'vec') {
|
|
183
|
-
if (depth >= types_ts_1.MAX_SCVAL_CLONE_DEPTH) {
|
|
184
|
-
throw cloneDepthError(value);
|
|
185
|
-
}
|
|
186
|
-
return { type: 'vec', value: value.value.map((v) => cloneScVal(v, depth + 1)) };
|
|
187
|
-
}
|
|
188
|
-
return { ...value };
|
|
189
|
-
}
|
|
190
|
-
function cloneDepthError(value) {
|
|
191
|
-
const err = new Error(`ScVal clone depth exceeds MAX_SCVAL_CLONE_DEPTH (${types_ts_1.MAX_SCVAL_CLONE_DEPTH})`);
|
|
192
|
-
err.code = 'SYNTHESIS_ERROR';
|
|
193
|
-
err.severity = 'error';
|
|
194
|
-
err.retryable = false;
|
|
195
|
-
err.depthContext = value.type;
|
|
196
|
-
throw err;
|
|
197
|
-
}
|
|
198
|
-
/** Build a value of the SAME ScVal type that differs from the recorded one,
|
|
199
|
-
* used by the `map_field_flip` mutation to defeat a per-element pin without
|
|
200
|
-
* changing the wire type. Returns null when the ScVal type has no obvious
|
|
201
|
-
* different value (e.g. opaque/other). */
|
|
202
|
-
function flipFieldValue(val) {
|
|
203
|
-
switch (val.type) {
|
|
204
|
-
case 'address': {
|
|
205
|
-
const a = 'GBFKRGJYZXLTDEI36ZCQEIM225NMOCR2VDBOIHJTXJ54FEFFVL2FKALE';
|
|
206
|
-
const b = 'GD6XSMQJ47EHHJOWXQOND5YDVZC37JWZJHYHBKE6QJFSLLJ5KQXM5QS5';
|
|
207
|
-
return { type: 'address', value: val.value === a ? b : a };
|
|
208
|
-
}
|
|
209
|
-
case 'i128':
|
|
210
|
-
case 'u32':
|
|
211
|
-
return { type: val.type, value: String(BigInt(val.value) + 1n) };
|
|
212
|
-
case 'symbol':
|
|
213
|
-
return { type: 'symbol', value: `${val.value}x` };
|
|
214
|
-
default:
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { PredicateNode, ScVal } from '../types.ts';
|
|
2
|
-
export interface EvalContext {
|
|
3
|
-
/** Contract the interpreter is asked to enforce against. */
|
|
4
|
-
contract: string;
|
|
5
|
-
/** Function name on that contract. */
|
|
6
|
-
fn: string;
|
|
7
|
-
/** Decoded `ScVal[]` of the top-level authorized call. */
|
|
8
|
-
args: ScVal[];
|
|
9
|
-
}
|
|
10
|
-
export type EvalResult = {
|
|
11
|
-
permit: true;
|
|
12
|
-
} | {
|
|
13
|
-
permit: false;
|
|
14
|
-
reason: string;
|
|
15
|
-
};
|
|
16
|
-
/** Evaluate a `PredicateNode` against the candidate call described by `ctx`.
|
|
17
|
-
* Pure function. Returns `{ permit: true }` or `{ permit: false; reason }`. */
|
|
18
|
-
export declare function evaluate(predicate: PredicateNode, ctx: EvalContext): EvalResult;
|