@crediolabs/policy-synth 0.2.0 → 0.3.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/README.md +4 -4
- package/dist/adapters/interpreter/adapter.d.ts +19 -25
- package/dist/adapters/interpreter/adapter.js +54 -487
- package/dist/adapters/interpreter/index.d.ts +1 -1
- package/dist/adapters/interpreter/index.js +1 -1
- package/dist/adapters/oz/adapter.js +1 -15
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.js +1 -4
- package/dist/install/build-add-context-rule.d.ts +3 -19
- package/dist/install/build-add-context-rule.js +3 -98
- package/dist/install/build-install-policy.d.ts +4 -53
- package/dist/install/build-install-policy.js +44 -123
- package/dist/install/index.d.ts +0 -2
- package/dist/install/index.js +0 -7
- package/dist/ir/index.d.ts +1 -1
- package/dist/ir/types.d.ts +7 -76
- package/dist/ir/types.js +0 -2
- package/dist/predicate/decode.d.ts +1 -1
- package/dist/predicate/decode.js +2 -70
- package/dist/predicate/encode.js +45 -229
- package/dist/predicate/from-json.js +1 -42
- package/dist/record/decode.js +3 -8
- package/dist/review-card/builder.d.ts +4 -3
- package/dist/review-card/builder.js +18 -162
- package/dist/review-card/cross-check.js +2 -105
- package/dist/review-card/render-leaf.d.ts +4 -0
- package/dist/review-card/render-leaf.js +47 -0
- package/dist/run/index.d.ts +36 -139
- package/dist/run/index.js +125 -437
- package/dist/run/schemas.d.ts +168 -1929
- package/dist/run/schemas.js +40 -269
- package/dist/seams/types.d.ts +0 -2
- package/dist/simulate/deny-cases.d.ts +26 -0
- package/dist/simulate/deny-cases.js +282 -0
- package/dist/simulate/evaluate.d.ts +18 -0
- package/dist/simulate/evaluate.js +271 -0
- package/dist/simulate/index.d.ts +4 -0
- package/dist/simulate/index.js +8 -0
- package/dist/synth/compose-from-recording.d.ts +24 -51
- package/dist/synth/compose-from-recording.js +120 -282
- package/dist/synth/deny-cases.d.ts +5 -7
- package/dist/synth/deny-cases.js +23 -373
- package/dist/synth/evaluate.d.ts +0 -21
- package/dist/synth/evaluate.js +56 -336
- package/dist/synth/index.d.ts +0 -5
- package/dist/synth/index.js +0 -5
- package/dist/synth/permit-context.d.ts +2 -9
- package/dist/synth/permit-context.js +1 -82
- package/dist/synth/synthesize-from-recording.d.ts +1 -29
- package/dist/synth/synthesize-from-recording.js +41 -356
- package/dist/types.d.ts +8 -60
- package/dist/types.js +5 -2
- package/dist/verify/simulate.d.ts +1 -14
- package/dist/verify/simulate.js +2 -88
- package/dist/verify/verify.d.ts +1 -7
- package/dist/verify/verify.js +0 -8
- package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
- package/dist-cjs/adapters/interpreter/adapter.js +56 -489
- package/dist-cjs/adapters/interpreter/index.d.ts +1 -1
- package/dist-cjs/adapters/interpreter/index.js +1 -2
- package/dist-cjs/adapters/oz/adapter.js +1 -15
- package/dist-cjs/errors.d.ts +1 -1
- package/dist-cjs/index.d.ts +1 -4
- package/dist-cjs/index.js +1 -4
- package/dist-cjs/install/build-add-context-rule.d.ts +3 -19
- package/dist-cjs/install/build-add-context-rule.js +1 -97
- package/dist-cjs/install/build-install-policy.d.ts +4 -53
- package/dist-cjs/install/build-install-policy.js +44 -122
- package/dist-cjs/install/index.d.ts +0 -2
- package/dist-cjs/install/index.js +2 -23
- package/dist-cjs/ir/index.d.ts +1 -1
- package/dist-cjs/ir/types.d.ts +7 -76
- package/dist-cjs/ir/types.js +0 -2
- package/dist-cjs/predicate/decode.d.ts +1 -1
- package/dist-cjs/predicate/decode.js +2 -70
- package/dist-cjs/predicate/encode.js +45 -229
- package/dist-cjs/predicate/from-json.js +1 -42
- package/dist-cjs/record/decode.js +3 -8
- package/dist-cjs/review-card/builder.d.ts +4 -3
- package/dist-cjs/review-card/builder.js +23 -167
- package/dist-cjs/review-card/cross-check.js +7 -110
- package/dist-cjs/review-card/render-leaf.d.ts +4 -0
- package/dist-cjs/review-card/render-leaf.js +52 -0
- package/dist-cjs/run/index.d.ts +36 -139
- package/dist-cjs/run/index.js +125 -444
- package/dist-cjs/run/schemas.d.ts +168 -1929
- package/dist-cjs/run/schemas.js +41 -270
- package/dist-cjs/seams/types.d.ts +0 -2
- package/dist-cjs/simulate/deny-cases.d.ts +26 -0
- package/dist-cjs/simulate/deny-cases.js +286 -0
- package/dist-cjs/simulate/evaluate.d.ts +18 -0
- package/dist-cjs/simulate/evaluate.js +274 -0
- package/dist-cjs/simulate/index.d.ts +4 -0
- package/dist-cjs/simulate/index.js +13 -0
- package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
- package/dist-cjs/synth/compose-from-recording.js +120 -282
- package/dist-cjs/synth/deny-cases.d.ts +5 -7
- package/dist-cjs/synth/deny-cases.js +23 -374
- package/dist-cjs/synth/evaluate.d.ts +0 -21
- package/dist-cjs/synth/evaluate.js +57 -337
- package/dist-cjs/synth/index.d.ts +0 -5
- package/dist-cjs/synth/index.js +1 -11
- package/dist-cjs/synth/permit-context.d.ts +2 -9
- package/dist-cjs/synth/permit-context.js +1 -82
- package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
- package/dist-cjs/synth/synthesize-from-recording.js +39 -354
- package/dist-cjs/types.d.ts +8 -60
- package/dist-cjs/types.js +6 -3
- package/dist-cjs/verify/simulate.d.ts +1 -14
- package/dist-cjs/verify/simulate.js +2 -88
- package/dist-cjs/verify/verify.d.ts +1 -7
- package/dist-cjs/verify/verify.js +0 -8
- package/package.json +4 -4
- package/src/adapters/interpreter/adapter.ts +76 -572
- package/src/errors.ts +0 -19
- package/src/index.ts +1 -4
- package/src/install/build-add-context-rule.ts +5 -139
- package/src/install/build-install-policy.ts +93 -214
- package/src/install/index.ts +0 -34
- package/src/predicate/decode.ts +2 -70
- package/src/predicate/encode.ts +49 -261
- package/src/predicate/from-json.ts +1 -43
- package/src/record/decode.ts +3 -8
- package/src/review-card/builder.ts +19 -168
- package/src/review-card/cross-check.ts +3 -105
- package/src/review-card/render-leaf.ts +48 -0
- package/src/run/index.ts +144 -572
- package/src/run/schemas.ts +42 -291
- package/src/simulate/deny-cases.ts +334 -0
- package/src/simulate/evaluate.ts +284 -0
- package/src/simulate/index.ts +11 -0
- package/src/synth/compose-from-recording.ts +148 -347
- package/src/synth/index.ts +0 -13
- package/src/synth/synthesize-from-recording.ts +43 -456
- package/src/types.ts +13 -49
- package/dist/install/authority-overlap.d.ts +0 -134
- package/dist/install/authority-overlap.js +0 -0
- package/dist/install/build-install-predicate.d.ts +0 -96
- package/dist/install/build-install-predicate.js +0 -444
- package/dist/install/build-merge-policy.d.ts +0 -70
- package/dist/install/build-merge-policy.js +0 -130
- package/dist/install/plan-merge-policy.d.ts +0 -49
- package/dist/install/plan-merge-policy.js +0 -86
- package/dist/install/read-account-rules.d.ts +0 -100
- package/dist/install/read-account-rules.js +0 -283
- package/dist-cjs/install/authority-overlap.d.ts +0 -134
- package/dist-cjs/install/authority-overlap.js +0 -0
- package/dist-cjs/install/build-install-predicate.d.ts +0 -96
- package/dist-cjs/install/build-install-predicate.js +0 -479
- package/dist-cjs/install/build-merge-policy.d.ts +0 -70
- package/dist-cjs/install/build-merge-policy.js +0 -134
- package/dist-cjs/install/plan-merge-policy.d.ts +0 -49
- package/dist-cjs/install/plan-merge-policy.js +0 -90
- package/dist-cjs/install/read-account-rules.d.ts +0 -100
- package/dist-cjs/install/read-account-rules.js +0 -296
- package/src/adapters/interpreter/index.ts +0 -8
- package/src/adapters/oz/adapter.ts +0 -376
- package/src/adapters/oz/index.ts +0 -9
- package/src/codegen/compile-gate.ts +0 -167
- package/src/codegen/index.ts +0 -17
- package/src/codegen/template.ts +0 -165
- package/src/install/authority-overlap.ts +0 -0
- package/src/install/build-merge-policy.ts +0 -219
- package/src/install/plan-merge-policy.ts +0 -133
- package/src/install/read-account-rules.ts +0 -376
- package/src/ir/index.ts +0 -13
- package/src/ir/types.ts +0 -132
- package/src/mandate/index.ts +0 -4
- package/src/mandate/to-ir.ts +0 -71
- package/src/mandate/types.ts +0 -21
- package/src/seams/index.ts +0 -11
- package/src/seams/types.ts +0 -81
- package/src/synth/deny-cases.ts +0 -663
- package/src/synth/evaluate.ts +0 -613
- package/src/synth/harness.ts +0 -68
- package/src/synth/minimize.ts +0 -48
- package/src/synth/permit-context.ts +0 -136
- package/src/synth/predicate-literals.ts +0 -27
- package/src/synth/synthesize-from-mandate.ts +0 -82
- package/src/verify/envelope.ts +0 -28
- package/src/verify/index.ts +0 -5
- package/src/verify/simulate.ts +0 -311
- package/src/verify/verify.ts +0 -243
|
@@ -1,376 +0,0 @@
|
|
|
1
|
-
// src/adapters/oz/adapter.ts - the OZ Accounts CustodyAdapter.
|
|
2
|
-
//
|
|
3
|
-
// Compiles a PolicyIR to an OZ `ProposedPolicy` using OZ built-in policy
|
|
4
|
-
// primitives. Only the constructs OZ can express natively are lowered:
|
|
5
|
-
// scope.contract -> ContextRuleType.call_contract (else default)
|
|
6
|
-
// expiry.validUntilLedger -> ContextRuleDraft.validUntilLedger
|
|
7
|
-
// window_spent(t,w) <= L -> `spending_limit` primitive
|
|
8
|
-
// approval.threshold -> `simple_threshold` / `weighted_threshold`
|
|
9
|
-
// Anything needing a capability this backend lacks (oracle price, invocation
|
|
10
|
-
// count, per-arg comparison/allowlist, guard, nested boolean predicate) is NOT
|
|
11
|
-
// emitted: it is named in `uncovered` and `covered` is set false. Nothing is
|
|
12
|
-
// silently dropped.
|
|
13
|
-
//
|
|
14
|
-
// OZ built-in policy instance addresses are per-network deploy artifacts we do
|
|
15
|
-
// not have yet (install is a later phase). They are injected via config; week-1
|
|
16
|
-
// ships a clearly-labelled [VERIFY] placeholder so nothing invents an address.
|
|
17
|
-
|
|
18
|
-
import type { IRCondition, IRPolicyRule, IRSelector, PolicyIR } from '../../ir/types.ts'
|
|
19
|
-
import type {
|
|
20
|
-
CompileResult,
|
|
21
|
-
CustodyAdapter,
|
|
22
|
-
CustodyCapabilities,
|
|
23
|
-
SimulationResult,
|
|
24
|
-
} from '../../seams/types.ts'
|
|
25
|
-
import type {
|
|
26
|
-
ContextRuleDraft,
|
|
27
|
-
Network,
|
|
28
|
-
ParseConfidence,
|
|
29
|
-
PolicyRef,
|
|
30
|
-
ProposedPolicy,
|
|
31
|
-
} from '../../types.ts'
|
|
32
|
-
import { OZ_LIMITS, SOROBAN_LIMITS } from '../../types.ts'
|
|
33
|
-
|
|
34
|
-
/** OZ built-in policy contract instance addresses, per primitive. */
|
|
35
|
-
export interface OzPrimitiveInstances {
|
|
36
|
-
spending_limit: string
|
|
37
|
-
simple_threshold: string
|
|
38
|
-
weighted_threshold: string
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Per-network config for the OZ adapter. */
|
|
42
|
-
export interface OzAdapterConfig {
|
|
43
|
-
network: Network
|
|
44
|
-
instances: OzPrimitiveInstances
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** [VERIFY] NOT real deployed addresses. The OZ built-in policy instances are
|
|
48
|
-
* per-network deploy artifacts we do not have yet; install is a later phase.
|
|
49
|
-
* Injected so the adapter never invents a Stellar contract address. */
|
|
50
|
-
export const PLACEHOLDER_OZ_INSTANCES: OzPrimitiveInstances = {
|
|
51
|
-
spending_limit: 'VERIFY-oz-spending-limit-instance-address',
|
|
52
|
-
simple_threshold: 'VERIFY-oz-simple-threshold-instance-address',
|
|
53
|
-
weighted_threshold: 'VERIFY-oz-weighted-threshold-instance-address',
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Week-1 OZ adapter config with placeholder instance addresses. */
|
|
57
|
-
export function placeholderOzConfig(network: Network): OzAdapterConfig {
|
|
58
|
-
return { network, instances: PLACEHOLDER_OZ_INSTANCES }
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Parse confidence for a deterministic (non-decoded) input: full (1.0). A
|
|
62
|
-
* mandate needs no decoding, so the gate is not applicable. */
|
|
63
|
-
const FULL_PARSE_CONFIDENCE: ParseConfidence = {
|
|
64
|
-
overall: 1,
|
|
65
|
-
knownContracts: [],
|
|
66
|
-
unknownContracts: [],
|
|
67
|
-
opaqueScVals: [],
|
|
68
|
-
thresholdUsed: 1,
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const CAPABILITIES: CustodyCapabilities = {
|
|
72
|
-
supportsSpendWindow: true,
|
|
73
|
-
supportsThreshold: true,
|
|
74
|
-
supportsTimeExpiry: true,
|
|
75
|
-
supportsOraclePrice: false,
|
|
76
|
-
supportsInvocationCount: false,
|
|
77
|
-
supportsGeneralPredicate: false,
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function createOzAdapter(config: OzAdapterConfig): CustodyAdapter {
|
|
81
|
-
return {
|
|
82
|
-
name: 'oz-accounts',
|
|
83
|
-
mode: 'enforce',
|
|
84
|
-
capabilities: () => ({ ...CAPABILITIES }),
|
|
85
|
-
compile: (ir) => compile(ir, config),
|
|
86
|
-
simulate: () => simulateStub(),
|
|
87
|
-
export: (ir) => canonicalStringify(ir),
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function compile(ir: PolicyIR, config: OzAdapterConfig): CompileResult {
|
|
92
|
-
const uncovered: string[] = []
|
|
93
|
-
|
|
94
|
-
const firstRule = ir.rules[0]
|
|
95
|
-
if (!firstRule) {
|
|
96
|
-
return { covered: false, uncovered: ['empty PolicyIR (no rules to compile)'] }
|
|
97
|
-
}
|
|
98
|
-
if (ir.rules.length > 1) {
|
|
99
|
-
uncovered.push(
|
|
100
|
-
`multi-rule PolicyIR: ${ir.rules.length - 1} rule(s) beyond the first are not compiled (a ProposedPolicy carries a single context rule in this slice)`
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const lowered = lowerRule(firstRule, config)
|
|
105
|
-
uncovered.push(...lowered.uncovered)
|
|
106
|
-
|
|
107
|
-
const result: CompileResult = { covered: uncovered.length === 0, uncovered }
|
|
108
|
-
if (!lowered.capExceeded) {
|
|
109
|
-
const proposed: ProposedPolicy = {
|
|
110
|
-
contextRule: lowered.contextRule,
|
|
111
|
-
policyDocuments: [],
|
|
112
|
-
policyRefs: lowered.policyRefs,
|
|
113
|
-
parseConfidence: { ...FULL_PARSE_CONFIDENCE },
|
|
114
|
-
warnings: [],
|
|
115
|
-
ambiguities: [],
|
|
116
|
-
}
|
|
117
|
-
result.proposed = proposed
|
|
118
|
-
}
|
|
119
|
-
return result
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
interface LoweredRule {
|
|
123
|
-
contextRule: ContextRuleDraft
|
|
124
|
-
policyRefs: PolicyRef[]
|
|
125
|
-
uncovered: string[]
|
|
126
|
-
capExceeded: boolean
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function lowerRule(rule: IRPolicyRule, config: OzAdapterConfig): LoweredRule {
|
|
130
|
-
const uncovered: string[] = []
|
|
131
|
-
const policyRefs: PolicyRef[] = []
|
|
132
|
-
|
|
133
|
-
// scope -> context rule type. OZ scopes by contract (CallContract); a
|
|
134
|
-
// method-level restriction is flagged as not covered because CallContract
|
|
135
|
-
// alone permits other methods on the same contract (e.g. an unbounded approve
|
|
136
|
-
// alongside a capped transfer).
|
|
137
|
-
const contextRuleType: ContextRuleDraft['contextRuleType'] =
|
|
138
|
-
rule.scope.contract !== undefined
|
|
139
|
-
? { kind: 'call_contract', contract: rule.scope.contract }
|
|
140
|
-
: { kind: 'default' }
|
|
141
|
-
|
|
142
|
-
if (rule.scope.method !== undefined && rule.scope.contract !== undefined) {
|
|
143
|
-
uncovered.push(
|
|
144
|
-
`per-method scoping to \`${rule.scope.method}\` (OZ CallContract scopes by contract only; requires the interpreter predicate)`
|
|
145
|
-
)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (rule.scope.chainId !== undefined) {
|
|
149
|
-
uncovered.push(
|
|
150
|
-
`chainId \`${rule.scope.chainId}\` not bindable by the Stellar OZ adapter (network is per-context, not per-rule)`
|
|
151
|
-
)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (rule.roles.length > 0) {
|
|
155
|
-
uncovered.push(
|
|
156
|
-
`roles [${rule.roles.join(', ')}] dropped (role-to-signer mapping is a later phase; OZ signers carry addresses, not role names)`
|
|
157
|
-
)
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// expiry -> validUntilLedger. Unix-timestamp expiry cannot be expressed by an
|
|
161
|
-
// OZ context rule (it expires by ledger sequence), so flag it.
|
|
162
|
-
let validUntilLedger: number | null = null
|
|
163
|
-
if (rule.expiry) {
|
|
164
|
-
if (rule.expiry.validUntilLedger !== undefined) {
|
|
165
|
-
validUntilLedger = rule.expiry.validUntilLedger
|
|
166
|
-
} else if (rule.expiry.validUntilUnixSeconds !== undefined) {
|
|
167
|
-
uncovered.push(
|
|
168
|
-
'time expiry given as a unix timestamp (OZ context rules expire by ledger sequence; supply expiry.validUntilLedger)'
|
|
169
|
-
)
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// guard -> not covered (applicability predicates are not an OZ built-in).
|
|
174
|
-
if (rule.guard) {
|
|
175
|
-
uncovered.push(`guard: ${describeCondition(rule.guard)}`)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// constraints -> spending_limit where they match; else not covered. The OZ
|
|
179
|
-
// spending_limit takes `{ spending_limit: i128, period_ledgers: u32 }` and
|
|
180
|
-
// has NO token param: it only accepts a CallContract context rule
|
|
181
|
-
// (OnlyCallContractAllowed) and limits transfers of that context's contract,
|
|
182
|
-
// so the spent token must equal the scope contract, and the window is a
|
|
183
|
-
// ledger count (~5s/ledger), not seconds.
|
|
184
|
-
for (const c of rule.constraints) {
|
|
185
|
-
const spend = matchSpendingLimit(c)
|
|
186
|
-
if (!spend) {
|
|
187
|
-
uncovered.push(describeCondition(c))
|
|
188
|
-
continue
|
|
189
|
-
}
|
|
190
|
-
if (contextRuleType.kind !== 'call_contract' || spend.token !== contextRuleType.contract) {
|
|
191
|
-
uncovered.push(
|
|
192
|
-
`spending_limit on token ${spend.token} needs a CallContract context scoped to that token (OZ pins the limit to the context contract, not a token param)`
|
|
193
|
-
)
|
|
194
|
-
continue
|
|
195
|
-
}
|
|
196
|
-
policyRefs.push({
|
|
197
|
-
kind: 'oz_builtin',
|
|
198
|
-
primitive: {
|
|
199
|
-
primitive: 'spending_limit',
|
|
200
|
-
params: {
|
|
201
|
-
spending_limit: spend.limit,
|
|
202
|
-
period_ledgers: secondsToLedgers(spend.windowSeconds),
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
instanceAddress: config.instances.spending_limit,
|
|
206
|
-
})
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// approval.threshold -> simple/weighted threshold primitive. A threshold < 1
|
|
210
|
-
// is not a real M-of-N gate (0 approvals authorises everything), so refuse to
|
|
211
|
-
// emit a no-op primitive and flag it as not covered.
|
|
212
|
-
if (rule.approval) {
|
|
213
|
-
if (!Number.isInteger(rule.approval.threshold) || rule.approval.threshold < 1) {
|
|
214
|
-
uncovered.push(
|
|
215
|
-
`approval threshold ${rule.approval.threshold} is not a positive integer (a 0 or negative threshold is not an M-of-N gate)`
|
|
216
|
-
)
|
|
217
|
-
} else {
|
|
218
|
-
const weights = rule.approval.weights
|
|
219
|
-
if (weights && Object.keys(weights).length > 0) {
|
|
220
|
-
policyRefs.push({
|
|
221
|
-
kind: 'oz_builtin',
|
|
222
|
-
primitive: {
|
|
223
|
-
primitive: 'weighted_threshold',
|
|
224
|
-
params: { threshold: rule.approval.threshold, weights },
|
|
225
|
-
},
|
|
226
|
-
instanceAddress: config.instances.weighted_threshold,
|
|
227
|
-
})
|
|
228
|
-
} else {
|
|
229
|
-
policyRefs.push({
|
|
230
|
-
kind: 'oz_builtin',
|
|
231
|
-
primitive: {
|
|
232
|
-
primitive: 'simple_threshold',
|
|
233
|
-
params: { threshold: rule.approval.threshold },
|
|
234
|
-
},
|
|
235
|
-
instanceAddress: config.instances.simple_threshold,
|
|
236
|
-
})
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const capExceeded = policyRefs.length > OZ_LIMITS.maxPoliciesPerRule
|
|
242
|
-
if (capExceeded) {
|
|
243
|
-
uncovered.push(
|
|
244
|
-
`policy count ${policyRefs.length} exceeds OZ maxPoliciesPerRule (${OZ_LIMITS.maxPoliciesPerRule})`
|
|
245
|
-
)
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const contextRule: ContextRuleDraft = {
|
|
249
|
-
contextRuleType,
|
|
250
|
-
name:
|
|
251
|
-
contextRuleType.kind === 'call_contract'
|
|
252
|
-
? `call_contract:${contextRuleType.contract}`
|
|
253
|
-
: 'default',
|
|
254
|
-
validUntilLedger,
|
|
255
|
-
signers: [],
|
|
256
|
-
policies: policyRefs,
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return { contextRule, policyRefs, uncovered, capExceeded }
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/** Convert a spend window in seconds to OZ `period_ledgers` (u32, >= 1).
|
|
263
|
-
* Stellar targets a ~5s ledger close time. */
|
|
264
|
-
function secondsToLedgers(windowSeconds: number): number {
|
|
265
|
-
return Math.max(1, Math.round(windowSeconds / SOROBAN_LIMITS.secondsPerLedger))
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
interface SpendingLimitMatch {
|
|
269
|
-
token: string
|
|
270
|
-
limit: string
|
|
271
|
-
windowSeconds: number
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/** Match the `window_spent(token, window) <= limit` compare that lowers to the
|
|
275
|
-
* OZ `spending_limit` primitive. Only `lte` matches (the spend-cap semantic). */
|
|
276
|
-
function matchSpendingLimit(c: IRCondition): SpendingLimitMatch | null {
|
|
277
|
-
if (c.op !== 'compare') return null
|
|
278
|
-
const { selector, operator, value } = c.compare
|
|
279
|
-
if (selector.kind !== 'window_spent' || operator !== 'lte') return null
|
|
280
|
-
return { token: selector.token, limit: value, windowSeconds: selector.windowSeconds }
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/** Human-readable descriptor for a construct the OZ built-in-primitive backend
|
|
284
|
-
* cannot express, used to populate `uncovered`. */
|
|
285
|
-
function describeCondition(cond: IRCondition): string {
|
|
286
|
-
switch (cond.op) {
|
|
287
|
-
case 'slippage_floor':
|
|
288
|
-
// OZ primitives bound a value against a constant; this bounds one call
|
|
289
|
-
// argument against another, which none of them can express.
|
|
290
|
-
return `slippage floor on arg[${cond.outArgIndex}] (OZ built-ins cannot bound one argument against another)`
|
|
291
|
-
case 'in':
|
|
292
|
-
return `value allowlist on ${describeSelector(cond.selector)} (arg allowlist)`
|
|
293
|
-
case 'eq_seq':
|
|
294
|
-
return `exact ordered sequence on ${describeSelector(cond.selector)} (OZ built-ins cannot express an exact vector)`
|
|
295
|
-
case 'not':
|
|
296
|
-
return 'negated condition (predicate DSL)'
|
|
297
|
-
case 'and':
|
|
298
|
-
case 'or':
|
|
299
|
-
return `nested ${cond.op} condition (predicate DSL)`
|
|
300
|
-
case 'compare': {
|
|
301
|
-
const s = cond.compare.selector
|
|
302
|
-
switch (s.kind) {
|
|
303
|
-
case 'oracle_price':
|
|
304
|
-
return `oracle price condition on ${s.asset} (oracle price not supported in week-1)`
|
|
305
|
-
case 'invocation_count':
|
|
306
|
-
return `invocation-count window (${s.windowSeconds}s) condition (not supported in week-1)`
|
|
307
|
-
case 'window_spent':
|
|
308
|
-
return `spend-window comparison with operator '${cond.compare.operator}' (only 'lte' lowers to spending_limit)`
|
|
309
|
-
case 'amount':
|
|
310
|
-
return `per-call amount comparison on ${s.token} (predicate DSL)`
|
|
311
|
-
case 'arg':
|
|
312
|
-
return `argument comparison on arg ${s.argIndex} (predicate DSL)`
|
|
313
|
-
case 'arg_len':
|
|
314
|
-
return `vec length comparison on arg ${s.argIndex} (predicate DSL)`
|
|
315
|
-
case 'arg_field':
|
|
316
|
-
return `map field comparison on arg ${s.argIndex}.${s.field} (predicate DSL)`
|
|
317
|
-
case 'calldata':
|
|
318
|
-
return 'EVM calldata comparison (predicate DSL)'
|
|
319
|
-
case 'value':
|
|
320
|
-
return 'tx.value comparison (predicate DSL)'
|
|
321
|
-
case 'now':
|
|
322
|
-
case 'valid_until':
|
|
323
|
-
return 'time comparison (predicate DSL)'
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
function describeSelector(s: IRSelector): string {
|
|
330
|
-
switch (s.kind) {
|
|
331
|
-
case 'arg':
|
|
332
|
-
return `arg ${s.argIndex}`
|
|
333
|
-
case 'arg_len':
|
|
334
|
-
return `arg_len(${s.argIndex})`
|
|
335
|
-
case 'arg_field':
|
|
336
|
-
return `arg_field(${s.argIndex}, ${s.element}, ${s.field})`
|
|
337
|
-
case 'amount':
|
|
338
|
-
return `amount(${s.token})`
|
|
339
|
-
case 'window_spent':
|
|
340
|
-
return `window_spent(${s.token})`
|
|
341
|
-
case 'oracle_price':
|
|
342
|
-
return `oracle_price(${s.asset})`
|
|
343
|
-
case 'invocation_count':
|
|
344
|
-
return `invocation_count(${s.windowSeconds}s)`
|
|
345
|
-
case 'calldata':
|
|
346
|
-
return `calldata[${s.offset}:${s.offset + s.length}]`
|
|
347
|
-
default:
|
|
348
|
-
return s.kind
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
function simulateStub(): SimulationResult {
|
|
353
|
-
return {
|
|
354
|
-
backend: 'ts-model',
|
|
355
|
-
permitted: null,
|
|
356
|
-
evaluations: [],
|
|
357
|
-
notes: ['stub: real permit/deny semantics wiring is a later phase'],
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/** Canonical JSON with recursively sorted object keys (stable across runs). */
|
|
362
|
-
function canonicalStringify(value: unknown): string {
|
|
363
|
-
return JSON.stringify(sortKeys(value))
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function sortKeys(value: unknown): unknown {
|
|
367
|
-
if (Array.isArray(value)) return value.map(sortKeys)
|
|
368
|
-
if (value && typeof value === 'object') {
|
|
369
|
-
const out: Record<string, unknown> = {}
|
|
370
|
-
for (const key of Object.keys(value as Record<string, unknown>).sort()) {
|
|
371
|
-
out[key] = sortKeys((value as Record<string, unknown>)[key])
|
|
372
|
-
}
|
|
373
|
-
return out
|
|
374
|
-
}
|
|
375
|
-
return value
|
|
376
|
-
}
|
package/src/adapters/oz/index.ts
DELETED
|
@@ -1,167 +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
|
-
|
|
11
|
-
import { spawn } from 'node:child_process'
|
|
12
|
-
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
13
|
-
import { tmpdir } from 'node:os'
|
|
14
|
-
import { join } from 'node:path'
|
|
15
|
-
|
|
16
|
-
export type CompileGateResult =
|
|
17
|
-
| { code: 'COMPILE_OK' }
|
|
18
|
-
| { code: 'COMPILE_GATE_FAILED'; stderr: string }
|
|
19
|
-
| { code: 'SKIPPED'; reason: string }
|
|
20
|
-
|
|
21
|
-
export interface CompileGateOpts {
|
|
22
|
-
/** Caller-controlled abort signal; aborting before toolchain probe resolves
|
|
23
|
-
* the gate to `SKIPPED` rather than starting a cargo invocation. */
|
|
24
|
-
signal?: AbortSignal
|
|
25
|
-
/** Override the `cargo` binary (mostly for tests). */
|
|
26
|
-
cargoBin?: string
|
|
27
|
-
/** Override the target directory (mostly for tests). */
|
|
28
|
-
tempDir?: string
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface ToolchainProbe {
|
|
32
|
-
present: boolean
|
|
33
|
-
reason: string
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Probe the host for the Rust toolchain the gate requires. We require BOTH
|
|
37
|
-
* `cargo` AND `stellar` (the Soroban CLI) because a real gate run invokes
|
|
38
|
-
* `stellar contract build`; absent either, the gate returns `SKIPPED`. */
|
|
39
|
-
export async function hasRustToolchain(): Promise<boolean> {
|
|
40
|
-
const probe = await probeToolchain()
|
|
41
|
-
return probe.present
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async function probeToolchain(): Promise<ToolchainProbe> {
|
|
45
|
-
const cargo = await which('cargo')
|
|
46
|
-
if (!cargo) return { present: false, reason: 'cargo not found on PATH' }
|
|
47
|
-
const stellar = await which('stellar')
|
|
48
|
-
if (!stellar) return { present: false, reason: 'stellar not found on PATH' }
|
|
49
|
-
return { present: true, reason: `${cargo} + ${stellar}` }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async function which(bin: string): Promise<string | null> {
|
|
53
|
-
try {
|
|
54
|
-
const out = await runProcess('sh', ['-c', `command -v ${bin}`], { capture: true })
|
|
55
|
-
const path = out.stdout.trim()
|
|
56
|
-
return path.length > 0 ? path : null
|
|
57
|
-
} catch {
|
|
58
|
-
return null
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
interface RunOpts {
|
|
63
|
-
capture?: boolean
|
|
64
|
-
signal?: AbortSignal
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function runProcess(
|
|
68
|
-
cmd: string,
|
|
69
|
-
args: string[],
|
|
70
|
-
opts: RunOpts
|
|
71
|
-
): Promise<{ stdout: string; stderr: string; code: number }> {
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
if (opts.signal?.aborted) {
|
|
74
|
-
reject(new Error('aborted'))
|
|
75
|
-
return
|
|
76
|
-
}
|
|
77
|
-
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'pipe'] })
|
|
78
|
-
let stdout = ''
|
|
79
|
-
let stderr = ''
|
|
80
|
-
child.stdout.on('data', (b: Buffer) => {
|
|
81
|
-
stdout += b.toString('utf8')
|
|
82
|
-
})
|
|
83
|
-
child.stderr.on('data', (b: Buffer) => {
|
|
84
|
-
stderr += b.toString('utf8')
|
|
85
|
-
})
|
|
86
|
-
const onAbort = () => {
|
|
87
|
-
child.kill('SIGTERM')
|
|
88
|
-
reject(new Error('aborted'))
|
|
89
|
-
}
|
|
90
|
-
opts.signal?.addEventListener('abort', onAbort, { once: true })
|
|
91
|
-
child.on('error', (e) => {
|
|
92
|
-
opts.signal?.removeEventListener('abort', onAbort)
|
|
93
|
-
reject(e)
|
|
94
|
-
})
|
|
95
|
-
child.on('close', (code) => {
|
|
96
|
-
opts.signal?.removeEventListener('abort', onAbort)
|
|
97
|
-
resolve({ stdout, stderr, code: code ?? -1 })
|
|
98
|
-
})
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const CARGO_MANIFEST = `[package]
|
|
103
|
-
name = "escape-hatch-gate"
|
|
104
|
-
version = "0.0.0"
|
|
105
|
-
edition = "2021"
|
|
106
|
-
publish = false
|
|
107
|
-
|
|
108
|
-
[lib]
|
|
109
|
-
crate-type = ["cdylib"]
|
|
110
|
-
# The gate writes the source beside the manifest rather than under src/, so
|
|
111
|
-
# the path is stated. Without it cargo looks for src/lib.rs and fails to
|
|
112
|
-
# parse the manifest before it ever compiles anything, which reads as a
|
|
113
|
-
# broken generator rather than a broken scaffold.
|
|
114
|
-
path = "lib.rs"
|
|
115
|
-
|
|
116
|
-
[dependencies]
|
|
117
|
-
soroban-sdk = "22"
|
|
118
|
-
|
|
119
|
-
[workspace]
|
|
120
|
-
`
|
|
121
|
-
|
|
122
|
-
/** Compile-check a Rust source string against the pinned Soroban SDK.
|
|
123
|
-
*
|
|
124
|
-
* - Toolchain absent -> `{ code: 'SKIPPED', reason }` (NOT a failure).
|
|
125
|
-
* - Toolchain present, source compiles -> `{ code: 'COMPILE_OK' }`.
|
|
126
|
-
* - Toolchain present, source broken -> `{ code: 'COMPILE_GATE_FAILED', stderr }`.
|
|
127
|
-
*
|
|
128
|
-
* The gate writes the source to a temporary crate and runs `cargo check`
|
|
129
|
-
* against it; the crate is cleaned up whether or not compilation succeeds.
|
|
130
|
-
* The caller is expected to surface `COMPILE_GATE_FAILED.stderr` to the
|
|
131
|
-
* user (the LLM agent) so the user can iterate. */
|
|
132
|
-
export async function compileCheck(
|
|
133
|
-
rustSource: string,
|
|
134
|
-
opts: CompileGateOpts = {}
|
|
135
|
-
): Promise<CompileGateResult> {
|
|
136
|
-
if (opts.signal?.aborted) {
|
|
137
|
-
return { code: 'SKIPPED', reason: 'aborted before probe' }
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const probe = await probeToolchain()
|
|
141
|
-
if (!probe.present) {
|
|
142
|
-
return { code: 'SKIPPED', reason: probe.reason }
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
let dir: string | null = null
|
|
146
|
-
try {
|
|
147
|
-
dir = await mkdtemp(join(opts.tempDir ?? tmpdir(), 'codegen-gate-'))
|
|
148
|
-
await writeFile(join(dir, 'Cargo.toml'), CARGO_MANIFEST)
|
|
149
|
-
await writeFile(join(dir, 'lib.rs'), rustSource)
|
|
150
|
-
|
|
151
|
-
const runOpts: RunOpts = opts.signal ? { signal: opts.signal } : {}
|
|
152
|
-
const result = await runProcess(
|
|
153
|
-
opts.cargoBin ?? 'cargo',
|
|
154
|
-
['check', '--quiet', '--offline', '--manifest-path', join(dir, 'Cargo.toml')],
|
|
155
|
-
runOpts
|
|
156
|
-
).catch((e: Error) => ({ stdout: '', stderr: e.message, code: -1 }))
|
|
157
|
-
|
|
158
|
-
if (result.code === 0) {
|
|
159
|
-
return { code: 'COMPILE_OK' }
|
|
160
|
-
}
|
|
161
|
-
return { code: 'COMPILE_GATE_FAILED', stderr: result.stderr || result.stdout }
|
|
162
|
-
} finally {
|
|
163
|
-
if (dir) {
|
|
164
|
-
await rm(dir, { recursive: true, force: true }).catch(() => {})
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
package/src/codegen/index.ts
DELETED
|
@@ -1,17 +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
|
-
|
|
8
|
-
export {
|
|
9
|
-
type CompileGateOpts,
|
|
10
|
-
type CompileGateResult,
|
|
11
|
-
compileCheck,
|
|
12
|
-
hasRustToolchain,
|
|
13
|
-
} from './compile-gate.ts'
|
|
14
|
-
export {
|
|
15
|
-
type EscapeHatchSpec,
|
|
16
|
-
generateRust,
|
|
17
|
-
} from './template.ts'
|