@crediolabs/policy-synth 0.1.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/dist/adapters/oz/adapter.d.ts +20 -0
- package/dist/adapters/oz/adapter.js +282 -0
- package/dist/adapters/oz/index.d.ts +1 -0
- package/dist/adapters/oz/index.js +2 -0
- package/dist/errors.d.ts +37 -0
- package/dist/errors.js +2 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/ir/index.d.ts +1 -0
- package/dist/ir/index.js +2 -0
- package/dist/ir/types.d.ts +97 -0
- package/dist/ir/types.js +11 -0
- package/dist/mandate/index.d.ts +2 -0
- package/dist/mandate/index.js +2 -0
- package/dist/mandate/to-ir.d.ts +3 -0
- package/dist/mandate/to-ir.js +60 -0
- package/dist/mandate/types.d.ts +20 -0
- package/dist/mandate/types.js +8 -0
- package/dist/record/decode.d.ts +76 -0
- package/dist/record/decode.js +372 -0
- package/dist/record/freshness.d.ts +17 -0
- package/dist/record/freshness.js +50 -0
- package/dist/record/index.d.ts +21 -0
- package/dist/record/index.js +163 -0
- package/dist/record/movements.d.ts +20 -0
- package/dist/record/movements.js +187 -0
- package/dist/record/rpc.d.ts +22 -0
- package/dist/record/rpc.js +70 -0
- package/dist/record/validate.d.ts +22 -0
- package/dist/record/validate.js +60 -0
- package/dist/registry/identify.d.ts +11 -0
- package/dist/registry/identify.js +84 -0
- package/dist/registry/index.d.ts +3 -0
- package/dist/registry/index.js +4 -0
- package/dist/registry/known-addresses.d.ts +16 -0
- package/dist/registry/known-addresses.js +49 -0
- package/dist/registry/protocols.d.ts +38 -0
- package/dist/registry/protocols.js +149 -0
- package/dist/seams/index.d.ts +1 -0
- package/dist/seams/index.js +2 -0
- package/dist/seams/types.d.ts +66 -0
- package/dist/seams/types.js +11 -0
- package/dist/synth/compose-from-recording.d.ts +36 -0
- package/dist/synth/compose-from-recording.js +162 -0
- package/dist/synth/index.d.ts +5 -0
- package/dist/synth/index.js +6 -0
- package/dist/synth/lower.d.ts +23 -0
- package/dist/synth/lower.js +116 -0
- package/dist/synth/scope.d.ts +26 -0
- package/dist/synth/scope.js +77 -0
- package/dist/synth/synthesize-from-mandate.d.ts +5 -0
- package/dist/synth/synthesize-from-mandate.js +34 -0
- package/dist/synth/synthesize-from-recording.d.ts +17 -0
- package/dist/synth/synthesize-from-recording.js +178 -0
- package/dist/types.d.ts +249 -0
- package/dist/types.js +35 -0
- package/package.json +37 -0
- package/src/adapters/oz/adapter.ts +363 -0
- package/src/adapters/oz/index.ts +9 -0
- package/src/errors.ts +79 -0
- package/src/index.ts +9 -0
- package/src/ir/index.ts +13 -0
- package/src/ir/types.ts +94 -0
- package/src/mandate/index.ts +4 -0
- package/src/mandate/to-ir.ts +71 -0
- package/src/mandate/types.ts +21 -0
- package/src/record/decode.ts +500 -0
- package/src/record/freshness.ts +63 -0
- package/src/record/index.ts +224 -0
- package/src/record/movements.ts +188 -0
- package/src/record/rpc.ts +88 -0
- package/src/record/validate.ts +75 -0
- package/src/registry/identify.ts +99 -0
- package/src/registry/index.ts +24 -0
- package/src/registry/known-addresses.ts +71 -0
- package/src/registry/protocols.ts +176 -0
- package/src/seams/index.ts +11 -0
- package/src/seams/types.ts +81 -0
- package/src/synth/compose-from-recording.ts +226 -0
- package/src/synth/index.ts +19 -0
- package/src/synth/lower.ts +144 -0
- package/src/synth/scope.ts +108 -0
- package/src/synth/synthesize-from-mandate.ts +46 -0
- package/src/synth/synthesize-from-recording.ts +226 -0
- package/src/types.ts +218 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CustodyAdapter } from '../../seams/types.ts';
|
|
2
|
+
import type { Network } from '../../types.ts';
|
|
3
|
+
/** OZ built-in policy contract instance addresses, per primitive. */
|
|
4
|
+
export interface OzPrimitiveInstances {
|
|
5
|
+
spending_limit: string;
|
|
6
|
+
simple_threshold: string;
|
|
7
|
+
weighted_threshold: string;
|
|
8
|
+
}
|
|
9
|
+
/** Per-network config for the OZ adapter. */
|
|
10
|
+
export interface OzAdapterConfig {
|
|
11
|
+
network: Network;
|
|
12
|
+
instances: OzPrimitiveInstances;
|
|
13
|
+
}
|
|
14
|
+
/** [VERIFY] NOT real deployed addresses. The OZ built-in policy instances are
|
|
15
|
+
* per-network deploy artifacts we do not have yet; install is a later phase.
|
|
16
|
+
* Injected so the adapter never invents a Stellar contract address. */
|
|
17
|
+
export declare const PLACEHOLDER_OZ_INSTANCES: OzPrimitiveInstances;
|
|
18
|
+
/** Week-1 OZ adapter config with placeholder instance addresses. */
|
|
19
|
+
export declare function placeholderOzConfig(network: Network): OzAdapterConfig;
|
|
20
|
+
export declare function createOzAdapter(config: OzAdapterConfig): CustodyAdapter;
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// src/adapters/oz/adapter.ts - the OZ Accounts CustodyAdapter (Path-A lowering).
|
|
2
|
+
//
|
|
3
|
+
// Compiles a PolicyIR to an OZ `ProposedPolicy` using OZ built-in policy
|
|
4
|
+
// primitives (Path A). 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 - the uncovered constructs are the Path-B markers.
|
|
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
|
+
import { OZ_LIMITS, SOROBAN_LIMITS } from "../../types.js";
|
|
18
|
+
/** [VERIFY] NOT real deployed addresses. The OZ built-in policy instances are
|
|
19
|
+
* per-network deploy artifacts we do not have yet; install is a later phase.
|
|
20
|
+
* Injected so the adapter never invents a Stellar contract address. */
|
|
21
|
+
export const PLACEHOLDER_OZ_INSTANCES = {
|
|
22
|
+
spending_limit: 'VERIFY-oz-spending-limit-instance-address',
|
|
23
|
+
simple_threshold: 'VERIFY-oz-simple-threshold-instance-address',
|
|
24
|
+
weighted_threshold: 'VERIFY-oz-weighted-threshold-instance-address',
|
|
25
|
+
};
|
|
26
|
+
/** Week-1 OZ adapter config with placeholder instance addresses. */
|
|
27
|
+
export function placeholderOzConfig(network) {
|
|
28
|
+
return { network, instances: PLACEHOLDER_OZ_INSTANCES };
|
|
29
|
+
}
|
|
30
|
+
/** Parse confidence for a deterministic (non-decoded) input: full, with an
|
|
31
|
+
* empty unknown/opaque breakdown. A mandate needs no decoding, so the gate is
|
|
32
|
+
* not applicable and confidence is 1. */
|
|
33
|
+
const FULL_PARSE_CONFIDENCE = {
|
|
34
|
+
overall: 1,
|
|
35
|
+
knownContracts: [],
|
|
36
|
+
unknownContracts: [],
|
|
37
|
+
opaqueScVals: [],
|
|
38
|
+
thresholdUsed: 1,
|
|
39
|
+
};
|
|
40
|
+
const CAPABILITIES = {
|
|
41
|
+
supportsSpendWindow: true,
|
|
42
|
+
supportsThreshold: true,
|
|
43
|
+
supportsTimeExpiry: true,
|
|
44
|
+
supportsOraclePrice: false,
|
|
45
|
+
supportsInvocationCount: false,
|
|
46
|
+
supportsGeneralPredicate: false,
|
|
47
|
+
};
|
|
48
|
+
export function createOzAdapter(config) {
|
|
49
|
+
return {
|
|
50
|
+
name: 'oz-accounts',
|
|
51
|
+
mode: 'enforce',
|
|
52
|
+
capabilities: () => ({ ...CAPABILITIES }),
|
|
53
|
+
compile: (ir) => compile(ir, config),
|
|
54
|
+
simulate: () => simulateStub(),
|
|
55
|
+
export: (ir) => canonicalStringify(ir),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function compile(ir, config) {
|
|
59
|
+
const uncovered = [];
|
|
60
|
+
const firstRule = ir.rules[0];
|
|
61
|
+
if (!firstRule) {
|
|
62
|
+
return { covered: false, uncovered: ['empty PolicyIR (no rules to compile)'] };
|
|
63
|
+
}
|
|
64
|
+
if (ir.rules.length > 1) {
|
|
65
|
+
uncovered.push(`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)`);
|
|
66
|
+
}
|
|
67
|
+
const lowered = lowerRule(firstRule, config);
|
|
68
|
+
uncovered.push(...lowered.uncovered);
|
|
69
|
+
const result = { covered: uncovered.length === 0, uncovered };
|
|
70
|
+
if (!lowered.capExceeded) {
|
|
71
|
+
const proposed = {
|
|
72
|
+
contextRule: lowered.contextRule,
|
|
73
|
+
policyDocuments: [],
|
|
74
|
+
policyRefs: lowered.policyRefs,
|
|
75
|
+
parseConfidence: { ...FULL_PARSE_CONFIDENCE },
|
|
76
|
+
warnings: [],
|
|
77
|
+
ambiguities: [],
|
|
78
|
+
};
|
|
79
|
+
result.proposed = proposed;
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
function lowerRule(rule, config) {
|
|
84
|
+
const uncovered = [];
|
|
85
|
+
const policyRefs = [];
|
|
86
|
+
// scope -> context rule type. OZ scopes by contract (CallContract); a finer
|
|
87
|
+
// method-level restriction is a predicate concern and must be flagged Path-B
|
|
88
|
+
// because CallContract alone permits other methods on the same contract
|
|
89
|
+
// (e.g. an unbounded approve alongside a capped transfer).
|
|
90
|
+
const contextRuleType = rule.scope.contract !== undefined
|
|
91
|
+
? { kind: 'call_contract', contract: rule.scope.contract }
|
|
92
|
+
: { kind: 'default' };
|
|
93
|
+
if (rule.scope.method !== undefined && rule.scope.contract !== undefined) {
|
|
94
|
+
uncovered.push(`per-method scoping to \`${rule.scope.method}\` (OZ CallContract scopes by contract only; requires the interpreter predicate)`);
|
|
95
|
+
}
|
|
96
|
+
if (rule.scope.chainId !== undefined) {
|
|
97
|
+
uncovered.push(`chainId \`${rule.scope.chainId}\` not bindable by the Stellar OZ adapter (network is per-context, not per-rule)`);
|
|
98
|
+
}
|
|
99
|
+
if (rule.roles.length > 0) {
|
|
100
|
+
uncovered.push(`roles [${rule.roles.join(', ')}] dropped (role-to-signer mapping is a later phase; OZ signers carry addresses, not role names)`);
|
|
101
|
+
}
|
|
102
|
+
// expiry -> validUntilLedger. Unix-timestamp expiry cannot be expressed by an
|
|
103
|
+
// OZ context rule (it expires by ledger sequence), so flag it.
|
|
104
|
+
let validUntilLedger = null;
|
|
105
|
+
if (rule.expiry) {
|
|
106
|
+
if (rule.expiry.validUntilLedger !== undefined) {
|
|
107
|
+
validUntilLedger = rule.expiry.validUntilLedger;
|
|
108
|
+
}
|
|
109
|
+
else if (rule.expiry.validUntilUnixSeconds !== undefined) {
|
|
110
|
+
uncovered.push('time expiry given as a unix timestamp (OZ context rules expire by ledger sequence; supply expiry.validUntilLedger)');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// guard -> Path B (applicability predicates are not an OZ built-in).
|
|
114
|
+
if (rule.guard) {
|
|
115
|
+
uncovered.push(`guard: ${describeCondition(rule.guard)}`);
|
|
116
|
+
}
|
|
117
|
+
// constraints -> spending_limit where they match; else Path B. The OZ
|
|
118
|
+
// spending_limit policy takes `{ spending_limit: i128, period_ledgers: u32 }`
|
|
119
|
+
// and has NO token param: it only accepts a CallContract context rule
|
|
120
|
+
// (OnlyCallContractAllowed) and limits transfers of that context's contract,
|
|
121
|
+
// so the spent token must equal the scope contract, and the window is a
|
|
122
|
+
// ledger count (~5s/ledger), not seconds.
|
|
123
|
+
for (const c of rule.constraints) {
|
|
124
|
+
const spend = matchSpendingLimit(c);
|
|
125
|
+
if (!spend) {
|
|
126
|
+
uncovered.push(describeCondition(c));
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (contextRuleType.kind !== 'call_contract' || spend.token !== contextRuleType.contract) {
|
|
130
|
+
uncovered.push(`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)`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
policyRefs.push({
|
|
134
|
+
kind: 'oz_builtin',
|
|
135
|
+
primitive: {
|
|
136
|
+
primitive: 'spending_limit',
|
|
137
|
+
params: {
|
|
138
|
+
spending_limit: spend.limit,
|
|
139
|
+
period_ledgers: secondsToLedgers(spend.windowSeconds),
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
instanceAddress: config.instances.spending_limit,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
// approval.threshold -> simple/weighted threshold primitive. A threshold < 1
|
|
146
|
+
// is not a real M-of-N gate (0 approvals authorises everything), so refuse to
|
|
147
|
+
// emit a no-op primitive and flag it Path-B instead.
|
|
148
|
+
if (rule.approval) {
|
|
149
|
+
if (!Number.isInteger(rule.approval.threshold) || rule.approval.threshold < 1) {
|
|
150
|
+
uncovered.push(`approval threshold ${rule.approval.threshold} is not a positive integer (a 0 or negative threshold is not an M-of-N gate)`);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const weights = rule.approval.weights;
|
|
154
|
+
if (weights && Object.keys(weights).length > 0) {
|
|
155
|
+
policyRefs.push({
|
|
156
|
+
kind: 'oz_builtin',
|
|
157
|
+
primitive: {
|
|
158
|
+
primitive: 'weighted_threshold',
|
|
159
|
+
params: { threshold: rule.approval.threshold, weights },
|
|
160
|
+
},
|
|
161
|
+
instanceAddress: config.instances.weighted_threshold,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
policyRefs.push({
|
|
166
|
+
kind: 'oz_builtin',
|
|
167
|
+
primitive: {
|
|
168
|
+
primitive: 'simple_threshold',
|
|
169
|
+
params: { threshold: rule.approval.threshold },
|
|
170
|
+
},
|
|
171
|
+
instanceAddress: config.instances.simple_threshold,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const capExceeded = policyRefs.length > OZ_LIMITS.maxPoliciesPerRule;
|
|
177
|
+
if (capExceeded) {
|
|
178
|
+
uncovered.push(`policy count ${policyRefs.length} exceeds OZ maxPoliciesPerRule (${OZ_LIMITS.maxPoliciesPerRule})`);
|
|
179
|
+
}
|
|
180
|
+
const contextRule = {
|
|
181
|
+
contextRuleType,
|
|
182
|
+
name: contextRuleType.kind === 'call_contract'
|
|
183
|
+
? `call_contract:${contextRuleType.contract}`
|
|
184
|
+
: 'default',
|
|
185
|
+
validUntilLedger,
|
|
186
|
+
signers: [],
|
|
187
|
+
policies: policyRefs,
|
|
188
|
+
};
|
|
189
|
+
return { contextRule, policyRefs, uncovered, capExceeded };
|
|
190
|
+
}
|
|
191
|
+
/** Convert a spend window in seconds to OZ `period_ledgers` (u32, >= 1).
|
|
192
|
+
* Stellar targets a ~5s ledger close time. */
|
|
193
|
+
function secondsToLedgers(windowSeconds) {
|
|
194
|
+
return Math.max(1, Math.round(windowSeconds / SOROBAN_LIMITS.secondsPerLedger));
|
|
195
|
+
}
|
|
196
|
+
/** Match the `window_spent(token, window) <= limit` compare that lowers to the
|
|
197
|
+
* OZ `spending_limit` primitive. Only `lte` matches (the spend-cap semantic). */
|
|
198
|
+
function matchSpendingLimit(c) {
|
|
199
|
+
if (c.op !== 'compare')
|
|
200
|
+
return null;
|
|
201
|
+
const { selector, operator, value } = c.compare;
|
|
202
|
+
if (selector.kind !== 'window_spent' || operator !== 'lte')
|
|
203
|
+
return null;
|
|
204
|
+
return { token: selector.token, limit: value, windowSeconds: selector.windowSeconds };
|
|
205
|
+
}
|
|
206
|
+
/** Human-readable descriptor for a construct the OZ Path-A backend cannot
|
|
207
|
+
* express, used to populate `uncovered` (the Path-B markers). */
|
|
208
|
+
function describeCondition(cond) {
|
|
209
|
+
switch (cond.op) {
|
|
210
|
+
case 'in':
|
|
211
|
+
return `value allowlist on ${describeSelector(cond.selector)} (arg allowlist, Path B)`;
|
|
212
|
+
case 'not':
|
|
213
|
+
return 'negated condition (predicate DSL, Path B)';
|
|
214
|
+
case 'and':
|
|
215
|
+
case 'or':
|
|
216
|
+
return `nested ${cond.op} condition (predicate DSL, Path B)`;
|
|
217
|
+
case 'compare': {
|
|
218
|
+
const s = cond.compare.selector;
|
|
219
|
+
switch (s.kind) {
|
|
220
|
+
case 'oracle_price':
|
|
221
|
+
return `oracle price condition on ${s.asset} (oracle price not supported in week-1)`;
|
|
222
|
+
case 'invocation_count':
|
|
223
|
+
return `invocation-count window (${s.windowSeconds}s) condition (not supported in week-1)`;
|
|
224
|
+
case 'window_spent':
|
|
225
|
+
return `spend-window comparison with operator '${cond.compare.operator}' (only 'lte' lowers to spending_limit)`;
|
|
226
|
+
case 'amount':
|
|
227
|
+
return `per-call amount comparison on ${s.token} (predicate DSL, Path B)`;
|
|
228
|
+
case 'arg':
|
|
229
|
+
return `argument comparison on arg ${s.argIndex} (predicate DSL, Path B)`;
|
|
230
|
+
case 'calldata':
|
|
231
|
+
return 'EVM calldata comparison (predicate DSL, Path B)';
|
|
232
|
+
case 'value':
|
|
233
|
+
return 'tx.value comparison (predicate DSL, Path B)';
|
|
234
|
+
case 'now':
|
|
235
|
+
case 'valid_until':
|
|
236
|
+
return 'time comparison (predicate DSL, Path B)';
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function describeSelector(s) {
|
|
242
|
+
switch (s.kind) {
|
|
243
|
+
case 'arg':
|
|
244
|
+
return `arg ${s.argIndex}`;
|
|
245
|
+
case 'amount':
|
|
246
|
+
return `amount(${s.token})`;
|
|
247
|
+
case 'window_spent':
|
|
248
|
+
return `window_spent(${s.token})`;
|
|
249
|
+
case 'oracle_price':
|
|
250
|
+
return `oracle_price(${s.asset})`;
|
|
251
|
+
case 'invocation_count':
|
|
252
|
+
return `invocation_count(${s.windowSeconds}s)`;
|
|
253
|
+
case 'calldata':
|
|
254
|
+
return `calldata[${s.offset}:${s.offset + s.length}]`;
|
|
255
|
+
default:
|
|
256
|
+
return s.kind;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function simulateStub() {
|
|
260
|
+
return {
|
|
261
|
+
backend: 'ts-model',
|
|
262
|
+
permitted: null,
|
|
263
|
+
evaluations: [],
|
|
264
|
+
notes: ['stub: real permit/deny semantics wiring is a later phase'],
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/** Canonical JSON with recursively sorted object keys (stable across runs). */
|
|
268
|
+
function canonicalStringify(value) {
|
|
269
|
+
return JSON.stringify(sortKeys(value));
|
|
270
|
+
}
|
|
271
|
+
function sortKeys(value) {
|
|
272
|
+
if (Array.isArray(value))
|
|
273
|
+
return value.map(sortKeys);
|
|
274
|
+
if (value && typeof value === 'object') {
|
|
275
|
+
const out = {};
|
|
276
|
+
for (const key of Object.keys(value).sort()) {
|
|
277
|
+
out[key] = sortKeys(value[key]);
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createOzAdapter, type OzAdapterConfig, type OzPrimitiveInstances, PLACEHOLDER_OZ_INSTANCES, placeholderOzConfig, } from './adapter.ts';
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type ErrorCode = 'RECORDING_FAILED' | 'RECORDING_VALIDATION_FAILED' | 'SCOPE_UNRESOLVED' | 'SYNTHESIS_ERROR' | 'MALFORMED_PREDICATE' | 'SIMULATION_ERROR' | 'VERIFICATION_FAILED' | 'DENY_CASE_FAILURE' | 'PERMIT_CASE_FAILED' | 'SUMMARY_DRIFT' | 'INSTALL_BUILD_FAILED' | 'INSTALL_CONFIRM_MISSING' | 'INSTALL_CONFIRM_EXPIRED' | 'REVOKE_BUILD_FAILED' | 'REVOKE_CONFIRM_MISSING' | 'USER_REJECTED_SIGN' | 'WALLET_TIMEOUT' | 'WALLET_UNAVAILABLE' | 'PREDICATE_TOO_LARGE' | 'PREDICATE_TOO_DEEP' | 'PREDICATE_ORACLE_OVER_LIMIT' | 'POLICY_CAP_EXCEEDED' | 'WASM_TOO_LARGE' | 'MASTER_AUTH_REQUIRED' | 'NONCE_REPLAY' | 'VERSION_MISMATCH' | 'ARITHMETIC_OVERFLOW' | 'AMOUNT_OVERFLOW' | 'RULE_SIGNERS_CHANGED' | 'SCOPE_SELF_CALL' | 'ORACLE_STALE' | 'ORACLE_MISSING' | 'ORACLE_DEVIATION_EXCEEDED' | 'ORACLE_MALFORMED_HISTORY' | 'ORACLE_FINGERPRINT_DRIFT' | 'ORACLE_PAUSED' | 'ORACLE_LEAF_INVALID_POSITION' | 'ORACLE_PARAMS_OUT_OF_RANGE' | 'ORACLE_DECIMALS_MISMATCH' | 'COMPILE_OK' | 'COMPILE_GATE_FAILED';
|
|
2
|
+
export interface ToolError {
|
|
3
|
+
code: ErrorCode;
|
|
4
|
+
message: string;
|
|
5
|
+
/** LLM-actionable hint for the agent skill. Per-code severity is pinned
|
|
6
|
+
* in CODE_SEVERITY below; this field is the resolved value at runtime. */
|
|
7
|
+
severity: 'info' | 'warning' | 'error' | 'fatal';
|
|
8
|
+
retryable: boolean;
|
|
9
|
+
remediation?: {
|
|
10
|
+
toolCall?: {
|
|
11
|
+
name: string;
|
|
12
|
+
args: Record<string, unknown>;
|
|
13
|
+
};
|
|
14
|
+
userQuestion?: {
|
|
15
|
+
code: string;
|
|
16
|
+
question: string;
|
|
17
|
+
};
|
|
18
|
+
docsUrl?: string;
|
|
19
|
+
};
|
|
20
|
+
/** Precedence rule for an LLM agent that sees BOTH toolCall AND userQuestion:
|
|
21
|
+
* - toolCall + userQuestion together = userQuestion wins (the human decides).
|
|
22
|
+
* - toolCall alone = agent proceeds.
|
|
23
|
+
* - userQuestion alone = agent stops and asks.
|
|
24
|
+
* No silent ambiguity. */
|
|
25
|
+
details?: unknown;
|
|
26
|
+
}
|
|
27
|
+
export type ToolResponse<T> = {
|
|
28
|
+
ok: true;
|
|
29
|
+
data: T;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
error: ToolError;
|
|
33
|
+
};
|
|
34
|
+
/** Unsigned Soroban transaction envelope, base64-encoded XDR (Soroban SDK convention).
|
|
35
|
+
* Pinned for CLI + MCP + WalletAdapter compatibility. Hex / raw-bytes / TS objects
|
|
36
|
+
* are NOT accepted - the contract is base64 string, full stop. */
|
|
37
|
+
export type UnsignedXdrB64 = string;
|
package/dist/errors.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './adapters/oz/index.ts';
|
|
2
|
+
export * from './errors.ts';
|
|
3
|
+
export * from './ir/index.ts';
|
|
4
|
+
export * from './mandate/index.ts';
|
|
5
|
+
export * from './record/index.ts';
|
|
6
|
+
export * from './registry/index.ts';
|
|
7
|
+
export * from './seams/index.ts';
|
|
8
|
+
export * from './synth/index.ts';
|
|
9
|
+
export * from './types.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./adapters/oz/index.js";
|
|
2
|
+
export * from "./errors.js";
|
|
3
|
+
export * from "./ir/index.js";
|
|
4
|
+
export * from "./mandate/index.js";
|
|
5
|
+
export * from "./record/index.js";
|
|
6
|
+
export * from "./registry/index.js";
|
|
7
|
+
export * from "./seams/index.js";
|
|
8
|
+
export * from "./synth/index.js";
|
|
9
|
+
export * from "./types.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { IRCompare, IRCompOp, IRCondition, IRLogic, IRPolicyRule, IRScalarType, IRSelector, IRVecMode, PolicyIR, } from './types.ts';
|
package/dist/ir/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** Comparison operator. Mirrors NEAR-V2 `CompOp`. */
|
|
2
|
+
export type IRCompOp = 'eq' | 'lt' | 'lte' | 'gt' | 'gte';
|
|
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';
|
|
9
|
+
/** WHERE in the authorized call a value is read. NEAR-V2 selectors first, OZ
|
|
10
|
+
* extensions after (an adapter that cannot express an extension flags it). */
|
|
11
|
+
export type IRSelector = {
|
|
12
|
+
kind: 'arg';
|
|
13
|
+
argIndex: number;
|
|
14
|
+
fieldIndex?: number;
|
|
15
|
+
vecMode?: IRVecMode;
|
|
16
|
+
scalarType: IRScalarType;
|
|
17
|
+
} | {
|
|
18
|
+
kind: 'calldata';
|
|
19
|
+
offset: number;
|
|
20
|
+
length: number;
|
|
21
|
+
} | {
|
|
22
|
+
kind: 'value';
|
|
23
|
+
} | {
|
|
24
|
+
kind: 'amount';
|
|
25
|
+
token: string;
|
|
26
|
+
} | {
|
|
27
|
+
kind: 'window_spent';
|
|
28
|
+
token: string;
|
|
29
|
+
windowSeconds: number;
|
|
30
|
+
} | {
|
|
31
|
+
kind: 'invocation_count';
|
|
32
|
+
windowSeconds: number;
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'now';
|
|
35
|
+
} | {
|
|
36
|
+
kind: 'valid_until';
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'oracle_price';
|
|
39
|
+
asset: string;
|
|
40
|
+
};
|
|
41
|
+
/** A single comparison leaf. `value` is a decimal/hex string (i128-safe;
|
|
42
|
+
* never a JS number). */
|
|
43
|
+
export interface IRCompare {
|
|
44
|
+
selector: IRSelector;
|
|
45
|
+
operator: IRCompOp;
|
|
46
|
+
value: string;
|
|
47
|
+
}
|
|
48
|
+
/** Condition tree. NEAR-V2 guard/constraint are flat And/Or; the IR allows
|
|
49
|
+
* nesting + `not` + `in` so the same IR can later lower to the OZ predicate
|
|
50
|
+
* DSL. Week-1 adapters only lower the flat supported subset and flag the rest. */
|
|
51
|
+
export type IRCondition = {
|
|
52
|
+
op: 'and' | 'or';
|
|
53
|
+
children: IRCondition[];
|
|
54
|
+
} | {
|
|
55
|
+
op: 'not';
|
|
56
|
+
child: IRCondition;
|
|
57
|
+
} | {
|
|
58
|
+
op: 'compare';
|
|
59
|
+
compare: IRCompare;
|
|
60
|
+
} | {
|
|
61
|
+
op: 'in';
|
|
62
|
+
selector: IRSelector;
|
|
63
|
+
values: string[];
|
|
64
|
+
};
|
|
65
|
+
export interface IRPolicyRule {
|
|
66
|
+
/** NEAR-V2 roles whitelist (empty = any; owner exempt). */
|
|
67
|
+
roles: string[];
|
|
68
|
+
/** NEAR-V2 scope filter; each field optional (absent = wildcard). */
|
|
69
|
+
scope: {
|
|
70
|
+
chainId?: number;
|
|
71
|
+
contract?: string;
|
|
72
|
+
method?: string;
|
|
73
|
+
};
|
|
74
|
+
/** NEAR-V2 guard (applicability; skip the rule when it fails). */
|
|
75
|
+
guard?: IRCondition;
|
|
76
|
+
/** NEAR-V2 constraint (AND; reject the transaction when any fails). */
|
|
77
|
+
constraints: IRCondition[];
|
|
78
|
+
/** M-of-N approval (OZ threshold primitives). */
|
|
79
|
+
approval?: {
|
|
80
|
+
kind: 'threshold';
|
|
81
|
+
threshold: number;
|
|
82
|
+
weights?: Record<string, number>;
|
|
83
|
+
};
|
|
84
|
+
/** OZ context-rule expiry. */
|
|
85
|
+
expiry?: {
|
|
86
|
+
validUntilLedger?: number;
|
|
87
|
+
validUntilUnixSeconds?: number;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export interface PolicyIR {
|
|
91
|
+
/** NEAR-V2 PolicyChain. */
|
|
92
|
+
chain: 'stellar' | 'evm';
|
|
93
|
+
/** NEAR-V2 DefaultBehavior. OZ context rules are deny-by-default, so
|
|
94
|
+
* `deny_all` is the OZ default fallback when no rule matches. */
|
|
95
|
+
defaultBehavior: 'allow_all' | 'deny_all';
|
|
96
|
+
rules: IRPolicyRule[];
|
|
97
|
+
}
|
package/dist/ir/types.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/ir/types.ts - the PolicyIR ("Policy Tree").
|
|
2
|
+
//
|
|
3
|
+
// `PolicyIR` is the custody-agnostic "Policy Tree" hub (the diagram's Policy
|
|
4
|
+
// Tree) that every CustodyAdapter compiles FROM. It generalizes the NEAR-V2
|
|
5
|
+
// policy schema (roles / scope filter / guard / constraint / comparison leaves /
|
|
6
|
+
// default behaviour) into one chain-neutral shape and extends it with the
|
|
7
|
+
// OZ-only selectors (spend window, oracle price, invocation count, time) so a
|
|
8
|
+
// single IR can serve every backend. NEAR-V2 has no stateful spend window,
|
|
9
|
+
// oracle, time/expiry, or invocation-count; those are marked as OZ extensions
|
|
10
|
+
// below and are only lowered by adapters that declare support for them.
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/mandate/to-ir.ts - deterministic lowering of a MandateSpec to a PolicyIR.
|
|
2
|
+
//
|
|
3
|
+
// `mandateToPolicyIR` is pure and total: the same spec always lowers to a
|
|
4
|
+
// byte-identical PolicyIR. No decoding, no inference, no clock. Each mandate
|
|
5
|
+
// field maps to exactly one IR construct:
|
|
6
|
+
// contract/method -> rule.scope
|
|
7
|
+
// spendingLimit -> a `window_spent(token,window) <= limit` compare
|
|
8
|
+
// approvalThreshold-> rule.approval.threshold
|
|
9
|
+
// recipients -> an `in` condition on the recipient arg (Path-B flagged
|
|
10
|
+
// by the OZ adapter; that is expected)
|
|
11
|
+
// expiry -> rule.expiry
|
|
12
|
+
// The top-level default is `deny_all` (OZ context rules are deny-by-default).
|
|
13
|
+
/** Arg index the recipient allowlist constrains. Pinned to the SEP-41
|
|
14
|
+
* `transfer(from, to, amount)` convention where `to` is arg 1. This condition
|
|
15
|
+
* is flagged Path-B by the OZ adapter (no built-in primitive expresses an arg
|
|
16
|
+
* allowlist), so the index only needs to be deterministic in week-1. */
|
|
17
|
+
const RECIPIENT_ARG_INDEX = 1;
|
|
18
|
+
export function mandateToPolicyIR(spec) {
|
|
19
|
+
const scope = { contract: spec.contract };
|
|
20
|
+
if (spec.method !== undefined)
|
|
21
|
+
scope.method = spec.method;
|
|
22
|
+
const constraints = [];
|
|
23
|
+
if (spec.spendingLimit) {
|
|
24
|
+
constraints.push({
|
|
25
|
+
op: 'compare',
|
|
26
|
+
compare: {
|
|
27
|
+
selector: {
|
|
28
|
+
kind: 'window_spent',
|
|
29
|
+
token: spec.spendingLimit.token,
|
|
30
|
+
windowSeconds: spec.spendingLimit.windowSeconds,
|
|
31
|
+
},
|
|
32
|
+
operator: 'lte',
|
|
33
|
+
value: spec.spendingLimit.limit,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (spec.recipients && spec.recipients.length > 0) {
|
|
38
|
+
const selector = {
|
|
39
|
+
kind: 'arg',
|
|
40
|
+
argIndex: RECIPIENT_ARG_INDEX,
|
|
41
|
+
scalarType: 'address',
|
|
42
|
+
};
|
|
43
|
+
constraints.push({ op: 'in', selector, values: [...spec.recipients] });
|
|
44
|
+
}
|
|
45
|
+
const rule = { roles: [], scope, constraints };
|
|
46
|
+
if (spec.approvalThreshold !== undefined) {
|
|
47
|
+
rule.approval = { kind: 'threshold', threshold: spec.approvalThreshold };
|
|
48
|
+
}
|
|
49
|
+
if (spec.expiry) {
|
|
50
|
+
const expiry = {};
|
|
51
|
+
if (spec.expiry.validUntilLedger !== undefined) {
|
|
52
|
+
expiry.validUntilLedger = spec.expiry.validUntilLedger;
|
|
53
|
+
}
|
|
54
|
+
if (spec.expiry.validUntilUnixSeconds !== undefined) {
|
|
55
|
+
expiry.validUntilUnixSeconds = spec.expiry.validUntilUnixSeconds;
|
|
56
|
+
}
|
|
57
|
+
rule.expiry = expiry;
|
|
58
|
+
}
|
|
59
|
+
return { chain: spec.chain, defaultBehavior: 'deny_all', rules: [rule] };
|
|
60
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface MandateSpec {
|
|
2
|
+
chain: 'stellar';
|
|
3
|
+
contract: string;
|
|
4
|
+
method?: string;
|
|
5
|
+
/** -> OZ `spending_limit` primitive. `limit` is an i128 decimal string. */
|
|
6
|
+
spendingLimit?: {
|
|
7
|
+
token: string;
|
|
8
|
+
limit: string;
|
|
9
|
+
windowSeconds: number;
|
|
10
|
+
};
|
|
11
|
+
/** -> OZ `simple_threshold` / `weighted_threshold` primitive. */
|
|
12
|
+
approvalThreshold?: number;
|
|
13
|
+
/** Recipient allowlist -> an `in` arg condition (flagged Path-B in week-1). */
|
|
14
|
+
recipients?: string[];
|
|
15
|
+
/** -> OZ context-rule expiry. */
|
|
16
|
+
expiry?: {
|
|
17
|
+
validUntilLedger?: number;
|
|
18
|
+
validUntilUnixSeconds?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// src/mandate/types.ts - the deterministic Mandate source.
|
|
2
|
+
//
|
|
3
|
+
// A MandateSpec is a declarative policy statement: "this account may call this
|
|
4
|
+
// contract, spending at most L per window, requiring M approvals, to these
|
|
5
|
+
// recipients, until this expiry". It carries NO transaction to decode, NO
|
|
6
|
+
// parseConfidence, and NO inference - it lowers deterministically to a
|
|
7
|
+
// PolicyIR. It is the clean end-to-end demo path, co-equal with the recorder.
|
|
8
|
+
export {};
|