@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,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_GRAMMAR_VERSION = exports.buildAddContextRuleArgs = exports.ADD_CONTEXT_RULE_SYMBOL = void 0;
|
|
2
4
|
// Public entry for the install-argument builders.
|
|
3
5
|
//
|
|
4
6
|
// `buildAddContextRuleArgs` is the encoder `runInstallPolicy` uses to turn a
|
|
@@ -13,30 +15,7 @@
|
|
|
13
15
|
// Exported here rather than from the package root to keep the root surface
|
|
14
16
|
// about synthesis, and because these are transaction-building primitives whose
|
|
15
17
|
// callers should know they are reaching for them.
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.MAX_RULE_ID_SCAN = exports.docLedgerKey = exports.decodeSigner = exports.decodeContextType = exports.decodeContextRule = exports.collectObservedRules = exports.accountRuleReaderFromServer = exports.DEFAULT_GRAMMAR_VERSION = exports.buildAddContextRuleArgs = exports.ADD_CONTEXT_RULE_SYMBOL = exports.signerKey = exports.permittedSelectors = exports.mergeIntoAnd = exports.intersectSelectors = exports.findAuthorityOverlaps = exports.effectiveSelectors = exports.ANY = void 0;
|
|
18
|
-
// Cross-rule authority analysis. A caller installing a policy needs to know
|
|
19
|
-
// whether its signers can already reach the same calls through another context
|
|
20
|
-
// rule, because OZ lets the signer name the rule and enforces only that one's
|
|
21
|
-
// policies. Without this, a second, tighter rule reads as a restriction while
|
|
22
|
-
// restricting nothing.
|
|
23
|
-
var authority_overlap_ts_1 = require("./authority-overlap.js");
|
|
24
|
-
Object.defineProperty(exports, "ANY", { enumerable: true, get: function () { return authority_overlap_ts_1.ANY; } });
|
|
25
|
-
Object.defineProperty(exports, "effectiveSelectors", { enumerable: true, get: function () { return authority_overlap_ts_1.effectiveSelectors; } });
|
|
26
|
-
Object.defineProperty(exports, "findAuthorityOverlaps", { enumerable: true, get: function () { return authority_overlap_ts_1.findAuthorityOverlaps; } });
|
|
27
|
-
Object.defineProperty(exports, "intersectSelectors", { enumerable: true, get: function () { return authority_overlap_ts_1.intersectSelectors; } });
|
|
28
|
-
Object.defineProperty(exports, "mergeIntoAnd", { enumerable: true, get: function () { return authority_overlap_ts_1.mergeIntoAnd; } });
|
|
29
|
-
Object.defineProperty(exports, "permittedSelectors", { enumerable: true, get: function () { return authority_overlap_ts_1.permittedSelectors; } });
|
|
30
|
-
Object.defineProperty(exports, "signerKey", { enumerable: true, get: function () { return authority_overlap_ts_1.signerKey; } });
|
|
31
18
|
var build_add_context_rule_ts_1 = require("./build-add-context-rule.js");
|
|
32
19
|
Object.defineProperty(exports, "ADD_CONTEXT_RULE_SYMBOL", { enumerable: true, get: function () { return build_add_context_rule_ts_1.ADD_CONTEXT_RULE_SYMBOL; } });
|
|
33
20
|
Object.defineProperty(exports, "buildAddContextRuleArgs", { enumerable: true, get: function () { return build_add_context_rule_ts_1.buildAddContextRuleArgs; } });
|
|
34
21
|
Object.defineProperty(exports, "DEFAULT_GRAMMAR_VERSION", { enumerable: true, get: function () { return build_add_context_rule_ts_1.DEFAULT_GRAMMAR_VERSION; } });
|
|
35
|
-
var read_account_rules_ts_1 = require("./read-account-rules.js");
|
|
36
|
-
Object.defineProperty(exports, "accountRuleReaderFromServer", { enumerable: true, get: function () { return read_account_rules_ts_1.accountRuleReaderFromServer; } });
|
|
37
|
-
Object.defineProperty(exports, "collectObservedRules", { enumerable: true, get: function () { return read_account_rules_ts_1.collectObservedRules; } });
|
|
38
|
-
Object.defineProperty(exports, "decodeContextRule", { enumerable: true, get: function () { return read_account_rules_ts_1.decodeContextRule; } });
|
|
39
|
-
Object.defineProperty(exports, "decodeContextType", { enumerable: true, get: function () { return read_account_rules_ts_1.decodeContextType; } });
|
|
40
|
-
Object.defineProperty(exports, "decodeSigner", { enumerable: true, get: function () { return read_account_rules_ts_1.decodeSigner; } });
|
|
41
|
-
Object.defineProperty(exports, "docLedgerKey", { enumerable: true, get: function () { return read_account_rules_ts_1.docLedgerKey; } });
|
|
42
|
-
Object.defineProperty(exports, "MAX_RULE_ID_SCAN", { enumerable: true, get: function () { return read_account_rules_ts_1.MAX_RULE_ID_SCAN; } });
|
package/dist-cjs/ir/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export type { IRCompare, IRCompOp, IRCondition, IRLogic, IRPolicyRule, IRScalarType, IRSelector,
|
|
1
|
+
export type { IRCompare, IRCompOp, IRCondition, IRLogic, IRPolicyRule, IRScalarType, IRSelector, PolicyIR, } from './types.ts';
|
package/dist-cjs/ir/types.d.ts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
/** Comparison operator. Mirrors NEAR-V2 `CompOp`. */
|
|
2
|
-
export type IRCompOp = 'eq' | '
|
|
2
|
+
export type IRCompOp = 'eq' | 'lte';
|
|
3
3
|
/** Boolean combinator. Mirrors NEAR-V2 `LogicOp`. */
|
|
4
|
-
export type IRLogic = 'and'
|
|
5
|
-
/**
|
|
6
|
-
export type
|
|
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'
|
|
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
|
-
/**
|
|
122
|
-
|
|
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
|
}
|
package/dist-cjs/ir/types.js
CHANGED
|
@@ -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.
|
|
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.
|
|
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,
|