@crediolabs/policy-synth 0.2.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.
Files changed (249) hide show
  1. package/README.md +4 -4
  2. package/dist/adapters/interpreter/adapter.d.ts +19 -25
  3. package/dist/adapters/interpreter/adapter.js +54 -487
  4. package/dist/errors.d.ts +1 -1
  5. package/dist/index.d.ts +1 -4
  6. package/dist/index.js +1 -4
  7. package/dist/install/build-add-context-rule.d.ts +3 -19
  8. package/dist/install/build-add-context-rule.js +3 -98
  9. package/dist/install/build-install-policy.d.ts +4 -53
  10. package/dist/install/build-install-policy.js +44 -123
  11. package/dist/install/index.d.ts +0 -2
  12. package/dist/install/index.js +0 -7
  13. package/dist/predicate/decode.d.ts +1 -1
  14. package/dist/predicate/decode.js +2 -70
  15. package/dist/predicate/encode.js +45 -229
  16. package/dist/predicate/from-json.js +1 -42
  17. package/dist/record/decode.js +3 -8
  18. package/dist/record/freshness.d.ts +17 -11
  19. package/dist/record/freshness.js +28 -18
  20. package/dist/review-card/builder.d.ts +4 -3
  21. package/dist/review-card/builder.js +18 -162
  22. package/dist/review-card/cross-check.js +2 -105
  23. package/dist/review-card/render-leaf.d.ts +4 -0
  24. package/dist/review-card/render-leaf.js +47 -0
  25. package/dist/run/index.d.ts +36 -139
  26. package/dist/run/index.js +125 -437
  27. package/dist/run/schemas.d.ts +168 -1929
  28. package/dist/run/schemas.js +40 -269
  29. package/dist/{synth → simulate}/deny-cases.d.ts +4 -8
  30. package/dist/simulate/deny-cases.js +282 -0
  31. package/dist/simulate/evaluate.d.ts +18 -0
  32. package/dist/simulate/evaluate.js +271 -0
  33. package/dist/simulate/index.d.ts +4 -0
  34. package/dist/simulate/index.js +8 -0
  35. package/dist/synth/compose-from-recording.d.ts +24 -51
  36. package/dist/synth/compose-from-recording.js +120 -282
  37. package/dist/synth/index.d.ts +0 -5
  38. package/dist/synth/index.js +0 -5
  39. package/dist/synth/synthesize-from-recording.d.ts +1 -29
  40. package/dist/synth/synthesize-from-recording.js +41 -356
  41. package/dist/types.d.ts +8 -60
  42. package/dist/types.js +5 -2
  43. package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
  44. package/dist-cjs/adapters/interpreter/adapter.js +56 -489
  45. package/dist-cjs/errors.d.ts +1 -1
  46. package/dist-cjs/index.d.ts +1 -4
  47. package/dist-cjs/index.js +1 -4
  48. package/dist-cjs/install/build-add-context-rule.d.ts +3 -19
  49. package/dist-cjs/install/build-add-context-rule.js +1 -97
  50. package/dist-cjs/install/build-install-policy.d.ts +4 -53
  51. package/dist-cjs/install/build-install-policy.js +44 -122
  52. package/dist-cjs/install/index.d.ts +0 -2
  53. package/dist-cjs/install/index.js +2 -23
  54. package/dist-cjs/predicate/decode.d.ts +1 -1
  55. package/dist-cjs/predicate/decode.js +2 -70
  56. package/dist-cjs/predicate/encode.js +45 -229
  57. package/dist-cjs/predicate/from-json.js +1 -42
  58. package/dist-cjs/record/decode.js +3 -8
  59. package/dist-cjs/record/freshness.d.ts +17 -11
  60. package/dist-cjs/record/freshness.js +28 -18
  61. package/dist-cjs/review-card/builder.d.ts +4 -3
  62. package/dist-cjs/review-card/builder.js +23 -167
  63. package/dist-cjs/review-card/cross-check.js +7 -110
  64. package/dist-cjs/review-card/render-leaf.d.ts +4 -0
  65. package/dist-cjs/review-card/render-leaf.js +52 -0
  66. package/dist-cjs/run/index.d.ts +36 -139
  67. package/dist-cjs/run/index.js +125 -444
  68. package/dist-cjs/run/schemas.d.ts +168 -1929
  69. package/dist-cjs/run/schemas.js +41 -270
  70. package/dist-cjs/{synth → simulate}/deny-cases.d.ts +4 -8
  71. package/dist-cjs/simulate/deny-cases.js +286 -0
  72. package/dist-cjs/simulate/evaluate.d.ts +18 -0
  73. package/dist-cjs/simulate/evaluate.js +274 -0
  74. package/dist-cjs/simulate/index.d.ts +4 -0
  75. package/dist-cjs/simulate/index.js +13 -0
  76. package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
  77. package/dist-cjs/synth/compose-from-recording.js +120 -282
  78. package/dist-cjs/synth/index.d.ts +0 -5
  79. package/dist-cjs/synth/index.js +1 -11
  80. package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
  81. package/dist-cjs/synth/synthesize-from-recording.js +39 -354
  82. package/dist-cjs/types.d.ts +8 -60
  83. package/dist-cjs/types.js +6 -3
  84. package/package.json +5 -5
  85. package/src/adapters/interpreter/adapter.ts +76 -572
  86. package/src/errors.ts +0 -19
  87. package/src/index.ts +1 -4
  88. package/src/install/build-add-context-rule.ts +5 -139
  89. package/src/install/build-install-policy.ts +93 -214
  90. package/src/install/index.ts +0 -34
  91. package/src/predicate/decode.ts +2 -70
  92. package/src/predicate/encode.ts +49 -261
  93. package/src/predicate/from-json.ts +1 -43
  94. package/src/record/decode.ts +3 -8
  95. package/src/record/freshness.ts +29 -18
  96. package/src/review-card/builder.ts +19 -168
  97. package/src/review-card/cross-check.ts +3 -105
  98. package/src/review-card/render-leaf.ts +48 -0
  99. package/src/run/index.ts +144 -572
  100. package/src/run/schemas.ts +42 -291
  101. package/src/simulate/deny-cases.ts +334 -0
  102. package/src/simulate/evaluate.ts +284 -0
  103. package/src/simulate/index.ts +11 -0
  104. package/src/synth/compose-from-recording.ts +148 -347
  105. package/src/synth/index.ts +0 -13
  106. package/src/synth/synthesize-from-recording.ts +43 -456
  107. package/src/types.ts +13 -49
  108. package/dist/adapters/interpreter/index.d.ts +0 -1
  109. package/dist/adapters/interpreter/index.js +0 -2
  110. package/dist/adapters/oz/adapter.d.ts +0 -20
  111. package/dist/adapters/oz/adapter.js +0 -295
  112. package/dist/adapters/oz/index.d.ts +0 -1
  113. package/dist/adapters/oz/index.js +0 -2
  114. package/dist/codegen/compile-gate.d.ts +0 -33
  115. package/dist/codegen/compile-gate.js +0 -124
  116. package/dist/codegen/index.d.ts +0 -2
  117. package/dist/codegen/index.js +0 -8
  118. package/dist/codegen/template.d.ts +0 -18
  119. package/dist/codegen/template.js +0 -148
  120. package/dist/install/authority-overlap.d.ts +0 -134
  121. package/dist/install/authority-overlap.js +0 -0
  122. package/dist/install/build-install-predicate.d.ts +0 -96
  123. package/dist/install/build-install-predicate.js +0 -444
  124. package/dist/install/build-merge-policy.d.ts +0 -70
  125. package/dist/install/build-merge-policy.js +0 -130
  126. package/dist/install/plan-merge-policy.d.ts +0 -49
  127. package/dist/install/plan-merge-policy.js +0 -86
  128. package/dist/install/read-account-rules.d.ts +0 -100
  129. package/dist/install/read-account-rules.js +0 -283
  130. package/dist/ir/index.d.ts +0 -1
  131. package/dist/ir/index.js +0 -2
  132. package/dist/ir/types.d.ts +0 -140
  133. package/dist/ir/types.js +0 -11
  134. package/dist/mandate/index.d.ts +0 -2
  135. package/dist/mandate/index.js +0 -2
  136. package/dist/mandate/to-ir.d.ts +0 -3
  137. package/dist/mandate/to-ir.js +0 -60
  138. package/dist/mandate/types.d.ts +0 -20
  139. package/dist/mandate/types.js +0 -8
  140. package/dist/seams/index.d.ts +0 -1
  141. package/dist/seams/index.js +0 -2
  142. package/dist/seams/types.d.ts +0 -66
  143. package/dist/seams/types.js +0 -11
  144. package/dist/synth/deny-cases.js +0 -562
  145. package/dist/synth/evaluate.d.ts +0 -39
  146. package/dist/synth/evaluate.js +0 -551
  147. package/dist/synth/harness.d.ts +0 -28
  148. package/dist/synth/harness.js +0 -47
  149. package/dist/synth/minimize.d.ts +0 -4
  150. package/dist/synth/minimize.js +0 -38
  151. package/dist/synth/permit-context.d.ts +0 -15
  152. package/dist/synth/permit-context.js +0 -116
  153. package/dist/synth/predicate-literals.d.ts +0 -5
  154. package/dist/synth/predicate-literals.js +0 -25
  155. package/dist/synth/synthesize-from-mandate.d.ts +0 -20
  156. package/dist/synth/synthesize-from-mandate.js +0 -59
  157. package/dist/verify/envelope.d.ts +0 -15
  158. package/dist/verify/envelope.js +0 -22
  159. package/dist/verify/index.d.ts +0 -3
  160. package/dist/verify/index.js +0 -3
  161. package/dist/verify/simulate.d.ts +0 -31
  162. package/dist/verify/simulate.js +0 -258
  163. package/dist/verify/verify.d.ts +0 -21
  164. package/dist/verify/verify.js +0 -189
  165. package/dist-cjs/adapters/interpreter/index.d.ts +0 -1
  166. package/dist-cjs/adapters/interpreter/index.js +0 -8
  167. package/dist-cjs/adapters/oz/adapter.d.ts +0 -20
  168. package/dist-cjs/adapters/oz/adapter.js +0 -300
  169. package/dist-cjs/adapters/oz/index.d.ts +0 -1
  170. package/dist-cjs/adapters/oz/index.js +0 -8
  171. package/dist-cjs/codegen/compile-gate.d.ts +0 -33
  172. package/dist-cjs/codegen/compile-gate.js +0 -128
  173. package/dist-cjs/codegen/index.d.ts +0 -2
  174. package/dist-cjs/codegen/index.js +0 -14
  175. package/dist-cjs/codegen/template.d.ts +0 -18
  176. package/dist-cjs/codegen/template.js +0 -151
  177. package/dist-cjs/install/authority-overlap.d.ts +0 -134
  178. package/dist-cjs/install/authority-overlap.js +0 -0
  179. package/dist-cjs/install/build-install-predicate.d.ts +0 -96
  180. package/dist-cjs/install/build-install-predicate.js +0 -479
  181. package/dist-cjs/install/build-merge-policy.d.ts +0 -70
  182. package/dist-cjs/install/build-merge-policy.js +0 -134
  183. package/dist-cjs/install/plan-merge-policy.d.ts +0 -49
  184. package/dist-cjs/install/plan-merge-policy.js +0 -90
  185. package/dist-cjs/install/read-account-rules.d.ts +0 -100
  186. package/dist-cjs/install/read-account-rules.js +0 -296
  187. package/dist-cjs/ir/index.d.ts +0 -1
  188. package/dist-cjs/ir/index.js +0 -3
  189. package/dist-cjs/ir/types.d.ts +0 -140
  190. package/dist-cjs/ir/types.js +0 -12
  191. package/dist-cjs/mandate/index.d.ts +0 -2
  192. package/dist-cjs/mandate/index.js +0 -6
  193. package/dist-cjs/mandate/to-ir.d.ts +0 -3
  194. package/dist-cjs/mandate/to-ir.js +0 -63
  195. package/dist-cjs/mandate/types.d.ts +0 -20
  196. package/dist-cjs/mandate/types.js +0 -9
  197. package/dist-cjs/seams/index.d.ts +0 -1
  198. package/dist-cjs/seams/index.js +0 -3
  199. package/dist-cjs/seams/types.d.ts +0 -66
  200. package/dist-cjs/seams/types.js +0 -12
  201. package/dist-cjs/synth/deny-cases.js +0 -568
  202. package/dist-cjs/synth/evaluate.d.ts +0 -39
  203. package/dist-cjs/synth/evaluate.js +0 -554
  204. package/dist-cjs/synth/harness.d.ts +0 -28
  205. package/dist-cjs/synth/harness.js +0 -50
  206. package/dist-cjs/synth/minimize.d.ts +0 -4
  207. package/dist-cjs/synth/minimize.js +0 -41
  208. package/dist-cjs/synth/permit-context.d.ts +0 -15
  209. package/dist-cjs/synth/permit-context.js +0 -119
  210. package/dist-cjs/synth/predicate-literals.d.ts +0 -5
  211. package/dist-cjs/synth/predicate-literals.js +0 -28
  212. package/dist-cjs/synth/synthesize-from-mandate.d.ts +0 -20
  213. package/dist-cjs/synth/synthesize-from-mandate.js +0 -62
  214. package/dist-cjs/verify/envelope.d.ts +0 -15
  215. package/dist-cjs/verify/envelope.js +0 -23
  216. package/dist-cjs/verify/index.d.ts +0 -3
  217. package/dist-cjs/verify/index.js +0 -8
  218. package/dist-cjs/verify/simulate.d.ts +0 -31
  219. package/dist-cjs/verify/simulate.js +0 -261
  220. package/dist-cjs/verify/verify.d.ts +0 -21
  221. package/dist-cjs/verify/verify.js +0 -192
  222. package/src/adapters/interpreter/index.ts +0 -8
  223. package/src/adapters/oz/adapter.ts +0 -376
  224. package/src/adapters/oz/index.ts +0 -9
  225. package/src/codegen/compile-gate.ts +0 -167
  226. package/src/codegen/index.ts +0 -17
  227. package/src/codegen/template.ts +0 -165
  228. package/src/install/authority-overlap.ts +0 -0
  229. package/src/install/build-merge-policy.ts +0 -219
  230. package/src/install/plan-merge-policy.ts +0 -133
  231. package/src/install/read-account-rules.ts +0 -376
  232. package/src/ir/index.ts +0 -13
  233. package/src/ir/types.ts +0 -132
  234. package/src/mandate/index.ts +0 -4
  235. package/src/mandate/to-ir.ts +0 -71
  236. package/src/mandate/types.ts +0 -21
  237. package/src/seams/index.ts +0 -11
  238. package/src/seams/types.ts +0 -81
  239. package/src/synth/deny-cases.ts +0 -663
  240. package/src/synth/evaluate.ts +0 -613
  241. package/src/synth/harness.ts +0 -68
  242. package/src/synth/minimize.ts +0 -48
  243. package/src/synth/permit-context.ts +0 -136
  244. package/src/synth/predicate-literals.ts +0 -27
  245. package/src/synth/synthesize-from-mandate.ts +0 -82
  246. package/src/verify/envelope.ts +0 -28
  247. package/src/verify/index.ts +0 -5
  248. package/src/verify/simulate.ts +0 -311
  249. 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 './seams/index.ts'
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. Only `interpreter` refs
70
- * reach the wire today; `oz_builtin` is reserved for Phase 06 follow-ups. */
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 = 1 as const
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
- /** Encode the install params for one OZ built-in policy.
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`).