@crediolabs/policy-synth 0.1.18 → 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.
Files changed (159) hide show
  1. package/README.md +6 -5
  2. package/dist/adapters/interpreter/adapter.d.ts +19 -25
  3. package/dist/adapters/interpreter/adapter.js +54 -487
  4. package/dist/adapters/interpreter/index.d.ts +1 -1
  5. package/dist/adapters/interpreter/index.js +1 -1
  6. package/dist/adapters/oz/adapter.js +1 -15
  7. package/dist/errors.d.ts +1 -1
  8. package/dist/index.d.ts +1 -4
  9. package/dist/index.js +1 -4
  10. package/dist/install/build-add-context-rule.d.ts +3 -11
  11. package/dist/install/build-add-context-rule.js +2 -97
  12. package/dist/install/build-install-policy.d.ts +4 -53
  13. package/dist/install/build-install-policy.js +44 -123
  14. package/dist/ir/index.d.ts +1 -1
  15. package/dist/ir/types.d.ts +7 -76
  16. package/dist/ir/types.js +0 -2
  17. package/dist/predicate/decode.d.ts +1 -1
  18. package/dist/predicate/decode.js +2 -70
  19. package/dist/predicate/encode.js +45 -229
  20. package/dist/predicate/from-json.js +1 -42
  21. package/dist/record/decode.js +3 -8
  22. package/dist/review-card/builder.d.ts +4 -3
  23. package/dist/review-card/builder.js +18 -162
  24. package/dist/review-card/cross-check.js +2 -105
  25. package/dist/review-card/render-leaf.d.ts +4 -0
  26. package/dist/review-card/render-leaf.js +47 -0
  27. package/dist/run/index.d.ts +35 -53
  28. package/dist/run/index.js +115 -156
  29. package/dist/run/schemas.d.ts +168 -1650
  30. package/dist/run/schemas.js +36 -199
  31. package/dist/seams/types.d.ts +0 -2
  32. package/dist/simulate/deny-cases.d.ts +26 -0
  33. package/dist/simulate/deny-cases.js +282 -0
  34. package/dist/simulate/evaluate.d.ts +18 -0
  35. package/dist/simulate/evaluate.js +271 -0
  36. package/dist/simulate/index.d.ts +4 -0
  37. package/dist/simulate/index.js +8 -0
  38. package/dist/synth/compose-from-recording.d.ts +24 -51
  39. package/dist/synth/compose-from-recording.js +120 -282
  40. package/dist/synth/deny-cases.d.ts +5 -7
  41. package/dist/synth/deny-cases.js +23 -373
  42. package/dist/synth/evaluate.d.ts +0 -21
  43. package/dist/synth/evaluate.js +56 -336
  44. package/dist/synth/index.d.ts +0 -5
  45. package/dist/synth/index.js +0 -5
  46. package/dist/synth/permit-context.d.ts +2 -9
  47. package/dist/synth/permit-context.js +1 -82
  48. package/dist/synth/synthesize-from-recording.d.ts +1 -29
  49. package/dist/synth/synthesize-from-recording.js +41 -356
  50. package/dist/types.d.ts +8 -60
  51. package/dist/types.js +5 -2
  52. package/dist/verify/simulate.d.ts +1 -14
  53. package/dist/verify/simulate.js +2 -88
  54. package/dist/verify/verify.d.ts +1 -7
  55. package/dist/verify/verify.js +0 -8
  56. package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
  57. package/dist-cjs/adapters/interpreter/adapter.js +56 -489
  58. package/dist-cjs/adapters/interpreter/index.d.ts +1 -1
  59. package/dist-cjs/adapters/interpreter/index.js +1 -2
  60. package/dist-cjs/adapters/oz/adapter.js +1 -15
  61. package/dist-cjs/errors.d.ts +1 -1
  62. package/dist-cjs/index.d.ts +1 -4
  63. package/dist-cjs/index.js +1 -4
  64. package/dist-cjs/install/build-add-context-rule.d.ts +3 -11
  65. package/dist-cjs/install/build-add-context-rule.js +1 -96
  66. package/dist-cjs/install/build-install-policy.d.ts +4 -53
  67. package/dist-cjs/install/build-install-policy.js +44 -122
  68. package/dist-cjs/ir/index.d.ts +1 -1
  69. package/dist-cjs/ir/types.d.ts +7 -76
  70. package/dist-cjs/ir/types.js +0 -2
  71. package/dist-cjs/predicate/decode.d.ts +1 -1
  72. package/dist-cjs/predicate/decode.js +2 -70
  73. package/dist-cjs/predicate/encode.js +45 -229
  74. package/dist-cjs/predicate/from-json.js +1 -42
  75. package/dist-cjs/record/decode.js +3 -8
  76. package/dist-cjs/review-card/builder.d.ts +4 -3
  77. package/dist-cjs/review-card/builder.js +23 -167
  78. package/dist-cjs/review-card/cross-check.js +7 -110
  79. package/dist-cjs/review-card/render-leaf.d.ts +4 -0
  80. package/dist-cjs/review-card/render-leaf.js +52 -0
  81. package/dist-cjs/run/index.d.ts +35 -53
  82. package/dist-cjs/run/index.js +116 -162
  83. package/dist-cjs/run/schemas.d.ts +168 -1650
  84. package/dist-cjs/run/schemas.js +37 -200
  85. package/dist-cjs/seams/types.d.ts +0 -2
  86. package/dist-cjs/simulate/deny-cases.d.ts +26 -0
  87. package/dist-cjs/simulate/deny-cases.js +286 -0
  88. package/dist-cjs/simulate/evaluate.d.ts +18 -0
  89. package/dist-cjs/simulate/evaluate.js +274 -0
  90. package/dist-cjs/simulate/index.d.ts +4 -0
  91. package/dist-cjs/simulate/index.js +13 -0
  92. package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
  93. package/dist-cjs/synth/compose-from-recording.js +120 -282
  94. package/dist-cjs/synth/deny-cases.d.ts +5 -7
  95. package/dist-cjs/synth/deny-cases.js +23 -374
  96. package/dist-cjs/synth/evaluate.d.ts +0 -21
  97. package/dist-cjs/synth/evaluate.js +57 -337
  98. package/dist-cjs/synth/index.d.ts +0 -5
  99. package/dist-cjs/synth/index.js +1 -11
  100. package/dist-cjs/synth/permit-context.d.ts +2 -9
  101. package/dist-cjs/synth/permit-context.js +1 -82
  102. package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
  103. package/dist-cjs/synth/synthesize-from-recording.js +39 -354
  104. package/dist-cjs/types.d.ts +8 -60
  105. package/dist-cjs/types.js +6 -3
  106. package/dist-cjs/verify/simulate.d.ts +1 -14
  107. package/dist-cjs/verify/simulate.js +2 -88
  108. package/dist-cjs/verify/verify.d.ts +1 -7
  109. package/dist-cjs/verify/verify.js +0 -8
  110. package/package.json +4 -4
  111. package/src/adapters/interpreter/adapter.ts +76 -572
  112. package/src/errors.ts +0 -19
  113. package/src/index.ts +1 -4
  114. package/src/install/build-add-context-rule.ts +4 -127
  115. package/src/install/build-install-policy.ts +93 -214
  116. package/src/predicate/decode.ts +2 -70
  117. package/src/predicate/encode.ts +49 -261
  118. package/src/predicate/from-json.ts +1 -43
  119. package/src/record/decode.ts +3 -8
  120. package/src/review-card/builder.ts +19 -168
  121. package/src/review-card/cross-check.ts +3 -105
  122. package/src/review-card/render-leaf.ts +48 -0
  123. package/src/run/index.ts +130 -186
  124. package/src/run/schemas.ts +38 -214
  125. package/src/simulate/deny-cases.ts +334 -0
  126. package/src/simulate/evaluate.ts +284 -0
  127. package/src/simulate/index.ts +11 -0
  128. package/src/synth/compose-from-recording.ts +148 -347
  129. package/src/synth/index.ts +0 -13
  130. package/src/synth/synthesize-from-recording.ts +43 -456
  131. package/src/types.ts +13 -49
  132. package/dist/install/build-install-predicate.d.ts +0 -96
  133. package/dist/install/build-install-predicate.js +0 -444
  134. package/dist-cjs/install/build-install-predicate.d.ts +0 -96
  135. package/dist-cjs/install/build-install-predicate.js +0 -479
  136. package/src/adapters/interpreter/index.ts +0 -8
  137. package/src/adapters/oz/adapter.ts +0 -376
  138. package/src/adapters/oz/index.ts +0 -9
  139. package/src/codegen/compile-gate.ts +0 -167
  140. package/src/codegen/index.ts +0 -17
  141. package/src/codegen/template.ts +0 -165
  142. package/src/ir/index.ts +0 -13
  143. package/src/ir/types.ts +0 -132
  144. package/src/mandate/index.ts +0 -4
  145. package/src/mandate/to-ir.ts +0 -71
  146. package/src/mandate/types.ts +0 -21
  147. package/src/seams/index.ts +0 -11
  148. package/src/seams/types.ts +0 -81
  149. package/src/synth/deny-cases.ts +0 -663
  150. package/src/synth/evaluate.ts +0 -613
  151. package/src/synth/harness.ts +0 -68
  152. package/src/synth/minimize.ts +0 -48
  153. package/src/synth/permit-context.ts +0 -136
  154. package/src/synth/predicate-literals.ts +0 -27
  155. package/src/synth/synthesize-from-mandate.ts +0 -82
  156. package/src/verify/envelope.ts +0 -28
  157. package/src/verify/index.ts +0 -5
  158. package/src/verify/simulate.ts +0 -311
  159. package/src/verify/verify.ts +0 -243
@@ -1,18 +1,14 @@
1
1
  /** Comparison operator. Mirrors NEAR-V2 `CompOp`. */
2
- export type IRCompOp = 'eq' | 'lt' | 'lte' | 'gt' | 'gte';
2
+ export type IRCompOp = 'eq' | 'lte';
3
3
  /** Boolean combinator. Mirrors NEAR-V2 `LogicOp`. */
4
- export type IRLogic = 'and' | 'or';
5
- /** Vector match mode for a repeated arg. Mirrors NEAR-V2 `VecMode`. */
6
- export type IRVecMode = 'all' | 'any';
7
- /** Scalar value type. Mirrors NEAR-V2 `ScValType`, superset for OZ. */
8
- export type IRScalarType = 'address' | 'i128' | 'u128' | 'u32' | 'u64' | 'i64' | 'symbol' | 'bytes' | 'bool';
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';
9
7
  /** WHERE in the authorized call a value is read. NEAR-V2 selectors first, OZ
10
8
  * extensions after (an adapter that cannot express an extension flags it). */
11
9
  export type IRSelector = {
12
10
  kind: 'arg';
13
11
  argIndex: number;
14
- fieldIndex?: number;
15
- vecMode?: IRVecMode;
16
12
  scalarType: IRScalarType;
17
13
  } | {
18
14
  kind: 'arg_len';
@@ -23,29 +19,6 @@ export type IRSelector = {
23
19
  element: number;
24
20
  field: string;
25
21
  scalarType: IRScalarType;
26
- } | {
27
- kind: 'calldata';
28
- offset: number;
29
- length: number;
30
- } | {
31
- kind: 'value';
32
- } | {
33
- kind: 'amount';
34
- token: string;
35
- } | {
36
- kind: 'window_spent';
37
- token: string;
38
- windowSeconds: number;
39
- } | {
40
- kind: 'invocation_count';
41
- windowSeconds: number;
42
- } | {
43
- kind: 'now';
44
- } | {
45
- kind: 'valid_until';
46
- } | {
47
- kind: 'oracle_price';
48
- asset: string;
49
22
  };
50
23
  /** A single comparison leaf. `value` is a decimal/hex string (i128-safe;
51
24
  * never a JS number). */
@@ -53,21 +26,14 @@ export interface IRCompare {
53
26
  selector: IRSelector;
54
27
  operator: IRCompOp;
55
28
  value: string;
56
- /** Decimal basis of `value`, for selectors whose comparand is not on a basis
57
- * the contract can infer. Set for `oracle_price`, where prices normalise to
58
- * 9 dp and an undeclared basis fails OPEN. */
59
- valueDecimals?: number;
60
29
  }
61
30
  /** Condition tree. NEAR-V2 guard/constraint are flat And/Or; the IR allows
62
31
  * nesting + `not` + `in` + `eq_seq` so the same IR can later lower to the OZ
63
32
  * predicate DSL. Week-1 adapters only lower the flat supported subset and flag
64
33
  * the rest. */
65
34
  export type IRCondition = {
66
- op: 'and' | 'or';
35
+ op: 'and';
67
36
  children: IRCondition[];
68
- } | {
69
- op: 'not';
70
- child: IRCondition;
71
37
  } | {
72
38
  op: 'compare';
73
39
  compare: IRCompare;
@@ -85,56 +51,21 @@ export type IRCondition = {
85
51
  op: 'eq_seq';
86
52
  selector: IRSelector;
87
53
  values: string[];
88
- }
89
- /** A swap's output floor, expressed against its own input: the output arg
90
- * must be at least `inArgIndex * num / den`.
91
- *
92
- * It is its own construct because `compare` bounds a selector against a
93
- * CONSTANT, and a slippage floor has no constant to bound against - the
94
- * acceptable output depends on the input of the same call. Encoding the
95
- * recorded output as a constant would pin a policy to one trade size.
96
- *
97
- * `num/den` is the caller's minimum acceptable output per unit of input.
98
- * It is never derived from the recording: the recorded rate is a price at
99
- * one moment, and freezing it as policy would deny normal trades later.
100
- * Adapters that cannot express it (the OZ built-ins) flag it `uncovered`. */
101
- | {
102
- op: 'slippage_floor';
103
- outArgIndex: number;
104
- inArgIndex: number;
105
- num: string;
106
- den: string;
107
54
  };
108
55
  export interface IRPolicyRule {
109
- /** NEAR-V2 roles whitelist (empty = any; owner exempt). */
110
- roles: string[];
111
56
  /** NEAR-V2 scope filter; each field optional (absent = wildcard). */
112
57
  scope: {
113
- chainId?: number;
114
58
  contract?: string;
115
59
  method?: string;
116
60
  };
117
- /** NEAR-V2 guard (applicability; skip the rule when it fails). */
118
- guard?: IRCondition;
119
61
  /** NEAR-V2 constraint (AND; reject the transaction when any fails). */
120
62
  constraints: IRCondition[];
121
- /** M-of-N approval (OZ threshold primitives). */
122
- approval?: {
123
- kind: 'threshold';
124
- threshold: number;
125
- weights?: Record<string, number>;
126
- };
127
- /** OZ context-rule expiry. */
63
+ /** OZ context-rule expiry, by ledger sequence - the only clock the OZ
64
+ * account model exposes to a rule. */
128
65
  expiry?: {
129
66
  validUntilLedger?: number;
130
- validUntilUnixSeconds?: number;
131
67
  };
132
68
  }
133
69
  export interface PolicyIR {
134
- /** NEAR-V2 PolicyChain. */
135
- chain: 'stellar' | 'evm';
136
- /** NEAR-V2 DefaultBehavior. OZ context rules are deny-by-default, so
137
- * `deny_all` is the OZ default fallback when no rule matches. */
138
- defaultBehavior: 'allow_all' | 'deny_all';
139
70
  rules: IRPolicyRule[];
140
71
  }
@@ -5,8 +5,6 @@
5
5
  // Tree) that every CustodyAdapter compiles FROM. It generalizes the NEAR-V2
6
6
  // policy schema (roles / scope filter / guard / constraint / comparison leaves /
7
7
  // default behaviour) into one chain-neutral shape and extends it with the
8
- // OZ-only selectors (spend window, oracle price, invocation count, time) so a
9
8
  // single IR can serve every backend. NEAR-V2 has no stateful spend window,
10
- // oracle, time/expiry, or invocation-count; those are marked as OZ extensions
11
9
  // below and are only lowered by adapters that declare support for them.
12
10
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -2,7 +2,7 @@ import { xdr } from '@stellar/stellar-sdk';
2
2
  import type { PredicateLeaf, PredicateNode } from '../types.ts';
3
3
  /** One leaf. Order of the bare-literal checks mirrors `decode_leaf` in dsl.rs. */
4
4
  export declare function decodeLeaf(v: xdr.ScVal): PredicateLeaf;
5
- /** One node. `not` wraps a NODE; the comparison ops wrap two LEAVES. */
5
+ /** One node. The comparison ops wrap two LEAVES. */
6
6
  export declare function decodeNode(v: xdr.ScVal): PredicateNode;
7
7
  /** Decode a predicate from the wire bytes the interpreter stores.
8
8
  *
@@ -60,21 +60,6 @@ function expectSymbol(v, what) {
60
60
  throw malformed(`${what} is not a symbol`);
61
61
  return v.sym().toString();
62
62
  }
63
- function expectAddress(v, what) {
64
- if (!v || v.switch() !== stellar_sdk_1.xdr.ScValType.scvAddress())
65
- throw malformed(`${what} is not an address`);
66
- return stellar_sdk_1.Address.fromScAddress(v.address()).toString();
67
- }
68
- function expectU64(v, what) {
69
- if (!v || v.switch() !== stellar_sdk_1.xdr.ScValType.scvU64())
70
- throw malformed(`${what} is not a u64`);
71
- return v.u64().toString();
72
- }
73
- function expectI128(v, what) {
74
- if (!v || v.switch() !== stellar_sdk_1.xdr.ScValType.scvI128())
75
- throw malformed(`${what} is not an i128`);
76
- return (0, stellar_sdk_1.scValToBigInt)(v).toString();
77
- }
78
63
  /** Arity check with the same intent as the Rust `check_arity`: a selector with
79
64
  * the wrong element count is malformed, not silently truncated. */
80
65
  function arity(items, n, selector) {
@@ -104,48 +89,6 @@ function decodeSelectorLeaf(items, sym) {
104
89
  element: expectU32(items[2], 'call_arg_field element'),
105
90
  field: expectSymbol(items[3], 'call_arg_field field'),
106
91
  };
107
- case 'call_arg_scaled':
108
- arity(items, 4, sym);
109
- return {
110
- kind: 'call_arg_scaled',
111
- index: expectU32(items[1], 'call_arg_scaled index'),
112
- num: expectI128(items[2], 'call_arg_scaled num'),
113
- den: expectI128(items[3], 'call_arg_scaled den'),
114
- };
115
- case 'amount':
116
- arity(items, 2, sym);
117
- return { kind: 'amount', token: expectAddress(items[1], 'amount token') };
118
- case 'window_spent':
119
- arity(items, 3, sym);
120
- return {
121
- kind: 'window_spent',
122
- token: expectAddress(items[1], 'window_spent token'),
123
- windowSeconds: Number(expectU64(items[2], 'window_spent windowSeconds')),
124
- };
125
- case 'now':
126
- arity(items, 1, sym);
127
- return { kind: 'now' };
128
- case 'valid_until':
129
- // The encoder refuses to emit this and the interpreter refuses it at
130
- // install, so it cannot appear in a document that installed. Decoding it
131
- // anyway would present a rule the chain would never have accepted.
132
- throw malformed('valid_until is not a usable predicate leaf');
133
- case 'invocation_count':
134
- arity(items, 2, sym);
135
- return {
136
- kind: 'invocation_count_in_window',
137
- windowSecs: Number(expectU64(items[1], 'invocation_count windowSecs')),
138
- };
139
- case 'oracle_price':
140
- arity(items, 2, sym);
141
- return { kind: 'oracle_price', asset: expectAddress(items[1], 'oracle_price asset') };
142
- case 'oracle_threshold':
143
- arity(items, 3, sym);
144
- return {
145
- kind: 'oracle_threshold',
146
- value: expectI128(items[1], 'oracle_threshold value'),
147
- decimals: expectU32(items[2], 'oracle_threshold decimals'),
148
- };
149
92
  default:
150
93
  // Deliberately NOT a literal_vec fallback - see the header note.
151
94
  throw malformed(`unknown selector symbol '${sym}'`);
@@ -169,39 +112,28 @@ function decodeLeaf(v) {
169
112
  return { kind: 'literal_symbol', value: v.sym().toString() };
170
113
  case stellar_sdk_1.xdr.ScValType.scvU32():
171
114
  return { kind: 'literal_u32', value: v.u32() };
172
- case stellar_sdk_1.xdr.ScValType.scvU64():
173
- return { kind: 'literal_u64', value: v.u64().toString() };
174
115
  case stellar_sdk_1.xdr.ScValType.scvI128():
175
116
  return { kind: 'literal_i128', value: (0, stellar_sdk_1.scValToBigInt)(v).toString() };
176
- case stellar_sdk_1.xdr.ScValType.scvBytes():
177
- return { kind: 'literal_bytes', value: Buffer.from(v.bytes()).toString('hex') };
178
117
  default:
179
118
  throw malformed(`unsupported leaf value type ${v.switch().name}`);
180
119
  }
181
120
  }
182
- /** One node. `not` wraps a NODE; the comparison ops wrap two LEAVES. */
121
+ /** One node. The comparison ops wrap two LEAVES. */
183
122
  function decodeNode(v) {
184
123
  const items = expectVec(v, 'node');
185
124
  const op = selectorSymbol(items);
186
125
  if (op === null)
187
126
  throw malformed('node does not start with an operator symbol');
188
127
  switch (op) {
189
- case 'and':
190
- case 'or': {
128
+ case 'and': {
191
129
  arity(items, 2, op);
192
130
  const children = expectVec(items[1], `${op} children`).map(decodeNode);
193
131
  if (children.length === 0)
194
132
  throw malformed(`${op} has no children`);
195
133
  return { op, children };
196
134
  }
197
- case 'not':
198
- arity(items, 2, op);
199
- return { op: 'not', child: decodeNode(items[1]) };
200
135
  case 'eq':
201
- case 'lt':
202
136
  case 'lte':
203
- case 'gt':
204
- case 'gte':
205
137
  arity(items, 3, op);
206
138
  return {
207
139
  op,