@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
package/src/errors.ts
CHANGED
|
@@ -28,7 +28,6 @@ export type ErrorCode =
|
|
|
28
28
|
| 'PREDICATE_TOO_DEEP'
|
|
29
29
|
| 'TOO_MANY_LEAVES'
|
|
30
30
|
| 'IN_OPERAND_LIMIT'
|
|
31
|
-
| 'PREDICATE_ORACLE_OVER_LIMIT'
|
|
32
31
|
| 'POLICY_CAP_EXCEEDED'
|
|
33
32
|
| 'WASM_TOO_LARGE'
|
|
34
33
|
// --- interpreter-side denies (canonical names; matches Rust DenyCode) ---
|
|
@@ -44,24 +43,6 @@ export type ErrorCode =
|
|
|
44
43
|
| 'UNSUPPORTED_NODE'
|
|
45
44
|
| 'STATEFUL_BOUND'
|
|
46
45
|
| 'NOT_IN_ALLOWLIST'
|
|
47
|
-
| 'FREQUENCY'
|
|
48
|
-
// A `call_arg >= call_arg_scaled(..)` slippage floor was not met. Distinct
|
|
49
|
-
// from the generic ARG_MISMATCH/STATEFUL_BOUND so a violated swap floor
|
|
50
|
-
// reads as itself on the review card.
|
|
51
|
-
| 'SLIPPAGE_FLOOR'
|
|
52
|
-
// --- oracle denies (fatal evaluator errors) ---
|
|
53
|
-
| 'ORACLE_STALE'
|
|
54
|
-
| 'ORACLE_MISSING'
|
|
55
|
-
| 'ORACLE_NO_CONFIRMATION'
|
|
56
|
-
| 'ORACLE_DEVIATION_EXCEEDED'
|
|
57
|
-
| 'ORACLE_MALFORMED_HISTORY'
|
|
58
|
-
| 'ORACLE_FINGERPRINT_DRIFT'
|
|
59
|
-
| 'ORACLE_PAUSED'
|
|
60
|
-
| 'ORACLE_LEAF_INVALID_POSITION'
|
|
61
|
-
| 'ORACLE_PARAMS_OUT_OF_RANGE'
|
|
62
|
-
| 'ORACLE_DECIMALS_MISMATCH'
|
|
63
|
-
| 'ORACLE_THRESHOLD_DECIMALS_OUT_OF_RANGE'
|
|
64
|
-
| 'ORACLE_THRESHOLD_BASIS_REQUIRED'
|
|
65
46
|
// --- escape-hatch compile gate ---
|
|
66
47
|
| 'COMPILE_OK'
|
|
67
48
|
| 'COMPILE_GATE_FAILED'
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
export * from './adapters/oz/index.ts'
|
|
2
1
|
export * from './errors.ts'
|
|
3
|
-
export * from './ir/index.ts'
|
|
4
|
-
export * from './mandate/index.ts'
|
|
5
2
|
export * from './predicate/index.ts'
|
|
6
3
|
export * from './record/index.ts'
|
|
7
4
|
export * from './registry/index.ts'
|
|
8
5
|
export * from './review-card/index.ts'
|
|
9
|
-
export * from './
|
|
6
|
+
export * from './simulate/index.ts'
|
|
10
7
|
export * from './synth/index.ts'
|
|
11
8
|
export * from './types.ts'
|
|
@@ -52,8 +52,8 @@ import { Address, xdr } from '@stellar/stellar-sdk'
|
|
|
52
52
|
import type { ToolError } from '../errors.ts'
|
|
53
53
|
import {
|
|
54
54
|
type ContextRuleDraft,
|
|
55
|
+
GRAMMAR_VERSION,
|
|
55
56
|
OZ_LIMITS,
|
|
56
|
-
type OZPrimitiveConfig,
|
|
57
57
|
type PolicyRef,
|
|
58
58
|
type SignerDraft,
|
|
59
59
|
} from '../types.ts'
|
|
@@ -66,8 +66,8 @@ export interface BuildAddContextRuleArgs {
|
|
|
66
66
|
* admin session. The deployer signs the install call via the admin rule
|
|
67
67
|
* already on the account, which is separate from this list. */
|
|
68
68
|
signers: SignerDraft[]
|
|
69
|
-
/** The policy(ies) to attach to the new rule.
|
|
70
|
-
*
|
|
69
|
+
/** The policy(ies) to attach to the new rule. `interpreter` is the only
|
|
70
|
+
* kind: the OpenZeppelin built-in backend was removed. */
|
|
71
71
|
policies: PolicyRef[]
|
|
72
72
|
/** Per-policy install nonce; 1 for a fresh install. */
|
|
73
73
|
installNonce: number
|
|
@@ -77,20 +77,12 @@ export interface BuildAddContextRuleArgs {
|
|
|
77
77
|
encodedPredicate: string
|
|
78
78
|
/** Hex sha256 of the canonical predicate XDR bytes. */
|
|
79
79
|
predicateHash: string
|
|
80
|
-
/** Per-policy oracle overrides. Absent on any key uses the wasm default. */
|
|
81
|
-
oracleParams?: {
|
|
82
|
-
maxStalenessSeconds?: number
|
|
83
|
-
maxDeviationBps?: number
|
|
84
|
-
/** Bound between the primary and secondary feeds. Tighten-only against
|
|
85
|
-
* the wasm default, like the other two. */
|
|
86
|
-
maxCrossFeedDeviationBps?: number
|
|
87
|
-
}
|
|
88
80
|
/** Hard pin to the interpreter's wasm grammar. Refusing here fails closed
|
|
89
81
|
* before the chain does; the contract will refuse a mismatch again. */
|
|
90
82
|
grammarVersion?: number
|
|
91
83
|
}
|
|
92
84
|
|
|
93
|
-
export const DEFAULT_GRAMMAR_VERSION =
|
|
85
|
+
export const DEFAULT_GRAMMAR_VERSION = GRAMMAR_VERSION
|
|
94
86
|
|
|
95
87
|
/** The verb `add_context_rule` takes on the wire. */
|
|
96
88
|
export const ADD_CONTEXT_RULE_SYMBOL = 'add_context_rule' as const
|
|
@@ -130,20 +122,6 @@ export function buildAddContextRuleArgs(
|
|
|
130
122
|
'a rule with no signers and no policies is refused at install (NoSignersAndPolicies)'
|
|
131
123
|
)
|
|
132
124
|
}
|
|
133
|
-
// OZ's spending_limit caps spend on the CONTEXT CONTRACT - it takes no
|
|
134
|
-
// token argument - so it refuses a Default rule on chain with a bare
|
|
135
|
-
// #3227. Say which rule shape it needs here, before the encoding, rather
|
|
136
|
-
// than letting the user read a numeric trap.
|
|
137
|
-
if (
|
|
138
|
-
args.policies.some((p) => p.kind === 'oz_builtin' && p.primitive.primitive === 'spending_limit')
|
|
139
|
-
) {
|
|
140
|
-
if (draft.contextRuleType.kind !== 'call_contract') {
|
|
141
|
-
throw limitError(
|
|
142
|
-
'INSTALL_BUILD_FAILED',
|
|
143
|
-
`spending_limit caps spend on the rule's context contract, so the rule must be call_contract-scoped to that token - a "${draft.contextRuleType.kind}" rule is refused on chain (#3227)`
|
|
144
|
-
)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
125
|
|
|
148
126
|
// ---- 2. context_type encoding ----
|
|
149
127
|
const contextType = encodeContextRuleType(draft.contextRuleType)
|
|
@@ -173,15 +151,6 @@ const POLICY_INSTALL_PARAM_FIELDS = [
|
|
|
173
151
|
'install_nonce',
|
|
174
152
|
'predicate',
|
|
175
153
|
'predicate_hash',
|
|
176
|
-
'oracle_max_staleness_seconds',
|
|
177
|
-
'oracle_max_deviation_bps',
|
|
178
|
-
// Cross-feed divergence bound. A field added to the contract's
|
|
179
|
-
// `PolicyInstallParams` changes the ABI: the host unpacks the map by field
|
|
180
|
-
// count, so omitting one fails the entire install with
|
|
181
|
-
// `Error(Object, UnexpectedSize)` and no indication of which field is
|
|
182
|
-
// missing. The Rust tests build that struct in Rust and never cross this
|
|
183
|
-
// boundary, so only a real install against a deployed contract catches it.
|
|
184
|
-
'oracle_max_xfeed_dev_bps',
|
|
185
154
|
] as const
|
|
186
155
|
|
|
187
156
|
function encodeContextRuleType(
|
|
@@ -232,104 +201,13 @@ function encodePoliciesMap(args: BuildAddContextRuleArgs): xdr.ScVal {
|
|
|
232
201
|
val: encodePolicyInstallParams(args),
|
|
233
202
|
})
|
|
234
203
|
)
|
|
235
|
-
} else if (ref.kind === 'oz_builtin') {
|
|
236
|
-
entries.push(
|
|
237
|
-
new xdr.ScMapEntry({
|
|
238
|
-
key: Address.fromString(ref.instanceAddress).toScVal(),
|
|
239
|
-
val: encodeOzPrimitiveParams(ref.primitive),
|
|
240
|
-
})
|
|
241
|
-
)
|
|
242
204
|
}
|
|
243
205
|
}
|
|
244
206
|
entries.sort(sortByScValSymbolString)
|
|
245
207
|
return xdr.ScVal.scvMap(entries)
|
|
246
208
|
}
|
|
247
209
|
|
|
248
|
-
|
|
249
|
-
*
|
|
250
|
-
* Field names and types are read from the deployed contracts' own spec:
|
|
251
|
-
* SpendingLimitAccountParams { period_ledgers: u32, spending_limit: i128 }
|
|
252
|
-
* SimpleThresholdAccountParams { threshold: u32 }
|
|
253
|
-
* WeightedThresholdAccountParams { signer_weights: map, threshold: u32 }
|
|
254
|
-
* Entries are symbol-string ordered for the same reason the interpreter's
|
|
255
|
-
* are: the host orders by symbol, not by XDR bytes.
|
|
256
|
-
*/
|
|
257
|
-
function encodeOzPrimitiveParams(primitive: OZPrimitiveConfig): xdr.ScVal {
|
|
258
|
-
const entries: xdr.ScMapEntry[] = []
|
|
259
|
-
const push = (name: string, val: xdr.ScVal) =>
|
|
260
|
-
entries.push(new xdr.ScMapEntry({ key: xdr.ScVal.scvSymbol(name), val }))
|
|
261
|
-
const p = primitive.params as Record<string, unknown>
|
|
262
|
-
switch (primitive.primitive) {
|
|
263
|
-
case 'spending_limit': {
|
|
264
|
-
const limit = p.spending_limit
|
|
265
|
-
const period = p.period_ledgers
|
|
266
|
-
if (typeof limit !== 'string' || typeof period !== 'number') {
|
|
267
|
-
throw limitError(
|
|
268
|
-
'INSTALL_BUILD_FAILED',
|
|
269
|
-
'spending_limit needs { spending_limit: string; period_ledgers: number }'
|
|
270
|
-
)
|
|
271
|
-
}
|
|
272
|
-
push('period_ledgers', xdr.ScVal.scvU32(period))
|
|
273
|
-
push('spending_limit', encodeI128(limit))
|
|
274
|
-
break
|
|
275
|
-
}
|
|
276
|
-
case 'simple_threshold': {
|
|
277
|
-
const threshold = p.threshold
|
|
278
|
-
if (typeof threshold !== 'number') {
|
|
279
|
-
throw limitError('INSTALL_BUILD_FAILED', 'simple_threshold needs { threshold: number }')
|
|
280
|
-
}
|
|
281
|
-
push('threshold', xdr.ScVal.scvU32(threshold))
|
|
282
|
-
break
|
|
283
|
-
}
|
|
284
|
-
case 'weighted_threshold': {
|
|
285
|
-
const threshold = p.threshold
|
|
286
|
-
const weights = p.weights as Record<string, number> | undefined
|
|
287
|
-
if (typeof threshold !== 'number' || !weights) {
|
|
288
|
-
throw limitError(
|
|
289
|
-
'INSTALL_BUILD_FAILED',
|
|
290
|
-
'weighted_threshold needs { threshold: number; weights: Record<address, number> }'
|
|
291
|
-
)
|
|
292
|
-
}
|
|
293
|
-
const weightEntries = Object.entries(weights)
|
|
294
|
-
.map(
|
|
295
|
-
([addr, w]) =>
|
|
296
|
-
new xdr.ScMapEntry({
|
|
297
|
-
key: Address.fromString(addr).toScVal(),
|
|
298
|
-
val: xdr.ScVal.scvU32(w),
|
|
299
|
-
})
|
|
300
|
-
)
|
|
301
|
-
.sort((a, b) => (a.key().toXDR('base64') < b.key().toXDR('base64') ? -1 : 1))
|
|
302
|
-
push('signer_weights', xdr.ScVal.scvMap(weightEntries))
|
|
303
|
-
push('threshold', xdr.ScVal.scvU32(threshold))
|
|
304
|
-
break
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
entries.sort((a, b) => sortBySymbolString(a.key(), b.key()))
|
|
308
|
-
return xdr.ScVal.scvMap(entries)
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
function encodeI128(value: string): xdr.ScVal {
|
|
312
|
-
const v = BigInt(value)
|
|
313
|
-
return xdr.ScVal.scvI128(
|
|
314
|
-
new xdr.Int128Parts({
|
|
315
|
-
hi: new xdr.Int64(BigInt.asIntN(64, v >> 64n)),
|
|
316
|
-
lo: new xdr.Uint64(BigInt.asUintN(64, v)),
|
|
317
|
-
})
|
|
318
|
-
)
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/** The `PolicyInstallParams` ScVal an interpreter policy receives.
|
|
322
|
-
*
|
|
323
|
-
* Exported so the merge remedy can re-install a policy through `add_policy`
|
|
324
|
-
* using the SAME encoder as a fresh install. Re-implementing it would risk
|
|
325
|
-
* exactly the drift this file warns about: the field order is ABI-significant
|
|
326
|
-
* and a differing encoding yields a rule that denies every call. */
|
|
327
|
-
export type PolicyInstallParamArgs = Pick<
|
|
328
|
-
BuildAddContextRuleArgs,
|
|
329
|
-
'encodedPredicate' | 'predicateHash' | 'installNonce' | 'oracleParams' | 'grammarVersion'
|
|
330
|
-
>
|
|
331
|
-
|
|
332
|
-
export function encodePolicyInstallParams(args: PolicyInstallParamArgs): xdr.ScVal {
|
|
210
|
+
function encodePolicyInstallParams(args: BuildAddContextRuleArgs): xdr.ScVal {
|
|
333
211
|
const predicate = Buffer.from(args.encodedPredicate, 'base64')
|
|
334
212
|
const computedHash = createHash('sha256').update(predicate).digest('hex')
|
|
335
213
|
if (computedHash !== args.predicateHash) {
|
|
@@ -341,15 +219,11 @@ export function encodePolicyInstallParams(args: PolicyInstallParamArgs): xdr.ScV
|
|
|
341
219
|
// Per-field value encoders. Each entry returns the ScVal to drop into the
|
|
342
220
|
// map; the key symbol comes from the surrounding loop. Adding a field is
|
|
343
221
|
// one row here plus the symbol in POLICY_INSTALL_PARAM_FIELDS.
|
|
344
|
-
const oracleParams = args.oracleParams
|
|
345
222
|
const valueFor: Record<(typeof POLICY_INSTALL_PARAM_FIELDS)[number], () => xdr.ScVal> = {
|
|
346
223
|
grammar_version: () => xdr.ScVal.scvU32(args.grammarVersion ?? DEFAULT_GRAMMAR_VERSION),
|
|
347
224
|
install_nonce: () => xdr.ScVal.scvU32(args.installNonce),
|
|
348
225
|
predicate: () => xdr.ScVal.scvBytes(predicate),
|
|
349
226
|
predicate_hash: () => xdr.ScVal.scvBytes(Buffer.from(args.predicateHash, 'hex')),
|
|
350
|
-
oracle_max_staleness_seconds: () => encodeOptionU32(oracleParams?.maxStalenessSeconds),
|
|
351
|
-
oracle_max_deviation_bps: () => encodeOptionU32(oracleParams?.maxDeviationBps),
|
|
352
|
-
oracle_max_xfeed_dev_bps: () => encodeOptionU32(oracleParams?.maxCrossFeedDeviationBps),
|
|
353
227
|
}
|
|
354
228
|
const entries = POLICY_INSTALL_PARAM_FIELDS.map(
|
|
355
229
|
(k) => new xdr.ScMapEntry({ key: xdr.ScVal.scvSymbol(k), val: valueFor[k]() })
|
|
@@ -362,14 +236,6 @@ export function encodePolicyInstallParams(args: PolicyInstallParamArgs): xdr.ScV
|
|
|
362
236
|
return xdr.ScVal.scvMap(entries)
|
|
363
237
|
}
|
|
364
238
|
|
|
365
|
-
function encodeOptionU32(v: number | undefined): xdr.ScVal {
|
|
366
|
-
if (v === undefined) return xdr.ScVal.scvVoid()
|
|
367
|
-
if (!Number.isFinite(v) || v < 0 || v > 0xffffffff) {
|
|
368
|
-
throw limitError('INSTALL_BUILD_FAILED', `oracle params value ${v} is outside u32 range`)
|
|
369
|
-
}
|
|
370
|
-
return xdr.ScVal.scvU32(v)
|
|
371
|
-
}
|
|
372
|
-
|
|
373
239
|
/** Sort host-map keys by their symbol-string form. The host orders map
|
|
374
240
|
* entries by the SYMBOL STRING, not by the key's XDR bytes (a length
|
|
375
241
|
* prefix in the encoding would otherwise put `amount` before `address`).
|