sigild 0.0.12 → 0.0.14
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 +63 -3
- package/THREAT_MODEL.md +4 -0
- package/dist/src/audit/index.d.ts +2 -1
- package/dist/src/audit/index.d.ts.map +1 -1
- package/dist/src/audit/index.js +1 -0
- package/dist/src/audit/index.js.map +1 -1
- package/dist/src/audit/log.d.ts +52 -3
- package/dist/src/audit/log.d.ts.map +1 -1
- package/dist/src/audit/log.js +226 -40
- package/dist/src/audit/log.js.map +1 -1
- package/dist/src/bin/sigil-mcp.js +79 -9
- package/dist/src/bin/sigil-mcp.js.map +1 -1
- package/dist/src/cli/main.d.ts.map +1 -1
- package/dist/src/cli/main.js +35 -1
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/cli/paths.d.ts +4 -0
- package/dist/src/cli/paths.d.ts.map +1 -1
- package/dist/src/cli/paths.js +2 -0
- package/dist/src/cli/paths.js.map +1 -1
- package/dist/src/cli/portal.d.ts.map +1 -1
- package/dist/src/cli/portal.js +11 -1
- package/dist/src/cli/portal.js.map +1 -1
- package/dist/src/confirm/config.d.ts +19 -0
- package/dist/src/confirm/config.d.ts.map +1 -1
- package/dist/src/confirm/config.js +48 -0
- package/dist/src/confirm/config.js.map +1 -1
- package/dist/src/confirm/index.d.ts +1 -1
- package/dist/src/confirm/index.d.ts.map +1 -1
- package/dist/src/confirm/index.js.map +1 -1
- package/dist/src/daemon/handles.d.ts +6 -0
- package/dist/src/daemon/handles.d.ts.map +1 -1
- package/dist/src/daemon/handles.js +11 -0
- package/dist/src/daemon/handles.js.map +1 -1
- package/dist/src/daemon/index.d.ts +1 -1
- package/dist/src/daemon/index.d.ts.map +1 -1
- package/dist/src/daemon/index.js.map +1 -1
- package/dist/src/daemon/methods.d.ts +29 -1
- package/dist/src/daemon/methods.d.ts.map +1 -1
- package/dist/src/daemon/methods.js +293 -27
- package/dist/src/daemon/methods.js.map +1 -1
- package/dist/src/eth/abi.d.ts +82 -0
- package/dist/src/eth/abi.d.ts.map +1 -0
- package/dist/src/eth/abi.js +328 -0
- package/dist/src/eth/abi.js.map +1 -0
- package/dist/src/eth/index.d.ts +1 -0
- package/dist/src/eth/index.d.ts.map +1 -1
- package/dist/src/eth/index.js +1 -0
- package/dist/src/eth/index.js.map +1 -1
- package/dist/src/fs/index.d.ts +2 -0
- package/dist/src/fs/index.d.ts.map +1 -0
- package/dist/src/fs/index.js +2 -0
- package/dist/src/fs/index.js.map +1 -0
- package/dist/src/fs/lock.d.ts +84 -0
- package/dist/src/fs/lock.d.ts.map +1 -0
- package/dist/src/fs/lock.js +329 -0
- package/dist/src/fs/lock.js.map +1 -0
- package/dist/src/mcp/parent-watchdog.d.ts +31 -0
- package/dist/src/mcp/parent-watchdog.d.ts.map +1 -0
- package/dist/src/mcp/parent-watchdog.js +35 -0
- package/dist/src/mcp/parent-watchdog.js.map +1 -0
- package/dist/src/mcp/server.d.ts +7 -0
- package/dist/src/mcp/server.d.ts.map +1 -1
- package/dist/src/mcp/server.js +13 -2
- package/dist/src/mcp/server.js.map +1 -1
- package/dist/src/mcp/tools.d.ts.map +1 -1
- package/dist/src/mcp/tools.js +21 -1
- package/dist/src/mcp/tools.js.map +1 -1
- package/dist/src/policy/evaluate.d.ts.map +1 -1
- package/dist/src/policy/evaluate.js +69 -4
- package/dist/src/policy/evaluate.js.map +1 -1
- package/dist/src/policy/index.d.ts +2 -0
- package/dist/src/policy/index.d.ts.map +1 -1
- package/dist/src/policy/index.js +2 -0
- package/dist/src/policy/index.js.map +1 -1
- package/dist/src/policy/ledger.d.ts +84 -0
- package/dist/src/policy/ledger.d.ts.map +1 -0
- package/dist/src/policy/ledger.js +276 -0
- package/dist/src/policy/ledger.js.map +1 -0
- package/dist/src/policy/loader.d.ts.map +1 -1
- package/dist/src/policy/loader.js +54 -0
- package/dist/src/policy/loader.js.map +1 -1
- package/dist/src/policy/template.d.ts +2 -2
- package/dist/src/policy/template.d.ts.map +1 -1
- package/dist/src/policy/template.js +52 -0
- package/dist/src/policy/template.js.map +1 -1
- package/dist/src/policy/types.d.ts +42 -0
- package/dist/src/policy/types.d.ts.map +1 -1
- package/dist/src/policy/types.js.map +1 -1
- package/dist/src/policy/window.d.ts +22 -0
- package/dist/src/policy/window.d.ts.map +1 -0
- package/dist/src/policy/window.js +66 -0
- package/dist/src/policy/window.js.map +1 -0
- package/dist/src/preview/abi-dir.d.ts +50 -0
- package/dist/src/preview/abi-dir.d.ts.map +1 -0
- package/dist/src/preview/abi-dir.js +175 -0
- package/dist/src/preview/abi-dir.js.map +1 -0
- package/dist/src/preview/decode.d.ts +26 -0
- package/dist/src/preview/decode.d.ts.map +1 -0
- package/dist/src/preview/decode.js +110 -0
- package/dist/src/preview/decode.js.map +1 -0
- package/dist/src/preview/format.d.ts +45 -0
- package/dist/src/preview/format.d.ts.map +1 -0
- package/dist/src/preview/format.js +222 -0
- package/dist/src/preview/format.js.map +1 -0
- package/dist/src/preview/index.d.ts +60 -0
- package/dist/src/preview/index.d.ts.map +1 -0
- package/dist/src/preview/index.js +163 -0
- package/dist/src/preview/index.js.map +1 -0
- package/dist/src/preview/known.d.ts +74 -0
- package/dist/src/preview/known.d.ts.map +1 -0
- package/dist/src/preview/known.js +425 -0
- package/dist/src/preview/known.js.map +1 -0
- package/dist/src/preview/simulate.d.ts +156 -0
- package/dist/src/preview/simulate.d.ts.map +1 -0
- package/dist/src/preview/simulate.js +768 -0
- package/dist/src/preview/simulate.js.map +1 -0
- package/dist/src/preview/simulators.d.ts +39 -0
- package/dist/src/preview/simulators.d.ts.map +1 -0
- package/dist/src/preview/simulators.js +71 -0
- package/dist/src/preview/simulators.js.map +1 -0
- package/dist/src/preview/trust.d.ts +49 -0
- package/dist/src/preview/trust.d.ts.map +1 -0
- package/dist/src/preview/trust.js +279 -0
- package/dist/src/preview/trust.js.map +1 -0
- package/dist/src/preview/types.d.ts +69 -0
- package/dist/src/preview/types.d.ts.map +1 -0
- package/dist/src/preview/types.js +11 -0
- package/dist/src/preview/types.js.map +1 -0
- package/dist/src/preview/units.d.ts +75 -0
- package/dist/src/preview/units.d.ts.map +1 -0
- package/dist/src/preview/units.js +228 -0
- package/dist/src/preview/units.js.map +1 -0
- package/package.json +5 -5
|
@@ -47,13 +47,15 @@ export function evaluate(request, policy) {
|
|
|
47
47
|
kind: 'deny',
|
|
48
48
|
reason: 'personal_sign denied — strict mode + allow_message_signing=false',
|
|
49
49
|
};
|
|
50
|
-
case 'typed_data':
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
: {
|
|
50
|
+
case 'typed_data': {
|
|
51
|
+
if (!policy.allowTypedData) {
|
|
52
|
+
return {
|
|
54
53
|
kind: 'deny',
|
|
55
54
|
reason: 'EIP-712 typed-data denied — strict mode + allow_typed_data=false',
|
|
56
55
|
};
|
|
56
|
+
}
|
|
57
|
+
return evaluateTypedDataStrict(request.typedData, policy) ?? { kind: 'allow' };
|
|
58
|
+
}
|
|
57
59
|
case 'svm_message':
|
|
58
60
|
return policy.allowSvmMessageSigning
|
|
59
61
|
? { kind: 'allow' }
|
|
@@ -238,4 +240,67 @@ function formatWei(v) {
|
|
|
238
240
|
const fracStr = frac.toString().padStart(18, '0').slice(0, 6).replace(/0+$/, '');
|
|
239
241
|
return fracStr === '' ? whole.toString() : `${whole}.${fracStr}`;
|
|
240
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Strict-mode EIP-712 checks, applied once allow_typed_data has passed.
|
|
245
|
+
* A typed-data signature can move funds (Permit, Permit2, exchange orders),
|
|
246
|
+
* so the domain gets the same treatment a transaction's `to` does:
|
|
247
|
+
* 1. domain.chainId must be present and in chain_ids — a signature for
|
|
248
|
+
* another chain is exactly the kind of thing an injected prompt asks
|
|
249
|
+
* for, and a chain-less domain can't be checked at all.
|
|
250
|
+
* 2. domain.verifyingContract must be allowlisted when the list is set.
|
|
251
|
+
* 3. primaryType must be allowlisted when the list is set.
|
|
252
|
+
* Domain fields are read defensively: the evaluator runs before sign-typed.ts
|
|
253
|
+
* validates the structure, and a malformed domain must deny, not throw.
|
|
254
|
+
*/
|
|
255
|
+
function evaluateTypedDataStrict(td, policy) {
|
|
256
|
+
if (typeof td.domain !== 'object' || td.domain === null || Array.isArray(td.domain)) {
|
|
257
|
+
return { kind: 'deny', reason: 'EIP-712 denied — domain must be an object' };
|
|
258
|
+
}
|
|
259
|
+
const domain = td.domain;
|
|
260
|
+
const chainId = domain['chainId'];
|
|
261
|
+
if (chainId === undefined) {
|
|
262
|
+
// A chain-less domain can't be checked against chain_ids, and a
|
|
263
|
+
// signature over one is valid wherever that domain is accepted. Strict
|
|
264
|
+
// mode therefore requires the domain to be chain-bound.
|
|
265
|
+
return {
|
|
266
|
+
kind: 'deny',
|
|
267
|
+
reason: 'EIP-712 denied — strict mode requires domain.chainId (chain-less domains cannot be checked against chain_ids)',
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const id = exactChainId(chainId);
|
|
271
|
+
if (id === null || !policy.chainIds.some((c) => BigInt(c) === id)) {
|
|
272
|
+
return {
|
|
273
|
+
kind: 'deny',
|
|
274
|
+
reason: `EIP-712 denied — domain.chainId ${String(chainId)} not in chain_ids ${JSON.stringify(policy.chainIds)}`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (policy.typedDataVerifyingContracts.length > 0) {
|
|
278
|
+
const vc = domain['verifyingContract'];
|
|
279
|
+
const norm = typeof vc === 'string' ? vc.toLowerCase() : undefined;
|
|
280
|
+
if (norm === undefined || !policy.typedDataVerifyingContracts.includes(norm)) {
|
|
281
|
+
return {
|
|
282
|
+
kind: 'deny',
|
|
283
|
+
reason: `EIP-712 denied — domain.verifyingContract ${norm ?? '(absent)'} not in typed_data_verifying_contracts`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (policy.typedDataPrimaryTypes.length > 0) {
|
|
288
|
+
const pt = td.primaryType;
|
|
289
|
+
if (typeof pt !== 'string' || !policy.typedDataPrimaryTypes.includes(pt)) {
|
|
290
|
+
return {
|
|
291
|
+
kind: 'deny',
|
|
292
|
+
reason: `EIP-712 denied — primaryType ${typeof pt === 'string' ? pt : '(absent)'} not in typed_data_primary_types`,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
/** Exact integer chain id as a bigint, or null if it isn't a safe integer / bigint. */
|
|
299
|
+
function exactChainId(v) {
|
|
300
|
+
if (typeof v === 'bigint')
|
|
301
|
+
return v >= 0n ? v : null;
|
|
302
|
+
if (typeof v === 'number')
|
|
303
|
+
return Number.isSafeInteger(v) && v >= 0 ? BigInt(v) : null;
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
241
306
|
//# sourceMappingURL=evaluate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../../src/policy/evaluate.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CAAC,OAAsB,EAAE,MAAc;IAC7D,IAAI,MAAM,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACjC,IAAI,OAAO,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACnC,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;QAC9B,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YACvC,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACjD,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;QAC9B,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC;IACD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,aAAa,CAAC,C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AAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvF,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -2,4 +2,6 @@ export { type Policy, type PolicyRequest, type PolicyDecision, type PolicyResolv
|
|
|
2
2
|
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver } from './loader.js';
|
|
3
3
|
export { evaluate } from './evaluate.js';
|
|
4
4
|
export { type PolicyMode, PERMISSIVE_TEMPLATE, STRICT_TEMPLATE, policyTemplate, } from './template.js';
|
|
5
|
+
export { type SpendAsset, type WindowCap, HOUR_MS, DAY_MS, windowCapsFor, checkWindowCaps, } from './window.js';
|
|
6
|
+
export { type SpendLedger, type SpendLedgerOpts, SpendLedgerError, MemorySpendLedger, FileSpendLedger, _ledgerTestHooks, } from './ledger.js';
|
|
5
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,KAAK,UAAU,EACf,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,KAAK,UAAU,EACf,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,KAAK,UAAU,EACf,KAAK,SAAS,EACd,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
|
package/dist/src/policy/index.js
CHANGED
|
@@ -2,4 +2,6 @@ export { PolicyLoadError, } from './types.js';
|
|
|
2
2
|
export { parsePolicy, FileSystemPolicyResolver, permissivePolicyResolver } from './loader.js';
|
|
3
3
|
export { evaluate } from './evaluate.js';
|
|
4
4
|
export { PERMISSIVE_TEMPLATE, STRICT_TEMPLATE, policyTemplate, } from './template.js';
|
|
5
|
+
export { HOUR_MS, DAY_MS, windowCapsFor, checkWindowCaps, } from './window.js';
|
|
6
|
+
export { SpendLedgerError, MemorySpendLedger, FileSpendLedger, _ledgerTestHooks, } from './ledger.js';
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAEL,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/policy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,eAAe,GAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAEL,mBAAmB,EACnB,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC;AACvB,OAAO,EAGL,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAGL,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type SpendAsset, type WindowCap } from './window.js';
|
|
2
|
+
/**
|
|
3
|
+
* What the sign path needs from the spend ledger. `reserve` is the only
|
|
4
|
+
* mutating call: it checks every cap against what is already recorded and,
|
|
5
|
+
* only if all hold, records the new amount — atomically with respect to
|
|
6
|
+
* other sigil-mcp processes, so two windows can't both squeeze under a cap
|
|
7
|
+
* in the same instant.
|
|
8
|
+
*
|
|
9
|
+
* The ledger owns the clock. Time is sampled *inside* the critical section
|
|
10
|
+
* so a process that read the clock, got descheduled, and then took the lock
|
|
11
|
+
* cannot judge a competitor's later spend as "in the future" and ignore it.
|
|
12
|
+
* Entries dated after now (another process's later sample, or a clock that
|
|
13
|
+
* stepped back) are always counted: a spend that has happened has happened.
|
|
14
|
+
*/
|
|
15
|
+
export interface SpendLedger {
|
|
16
|
+
/** Total recorded for (handle, asset) with ts > now - windowMs, including any ts > now. */
|
|
17
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
18
|
+
/**
|
|
19
|
+
* Check-then-record. Returns null on success (amount recorded), or the
|
|
20
|
+
* deny reason (nothing recorded). A zero amount is a no-op success.
|
|
21
|
+
*/
|
|
22
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare class SpendLedgerError extends Error {
|
|
25
|
+
cause?: unknown;
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
export interface SpendLedgerOpts {
|
|
29
|
+
/** Clock override for tests. Defaults to Date.now. */
|
|
30
|
+
now?: () => number;
|
|
31
|
+
}
|
|
32
|
+
/** In-memory ledger for tests and for callers that don't persist. */
|
|
33
|
+
export declare class MemorySpendLedger implements SpendLedger {
|
|
34
|
+
#private;
|
|
35
|
+
constructor(opts?: SpendLedgerOpts);
|
|
36
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
37
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* File-backed ledger: one append-only JSONL file per portal at
|
|
41
|
+
* `<stateDir>/<handle>.ledger`, lines of `{"ts":…,"asset":"wei","amount":"…"}`.
|
|
42
|
+
*
|
|
43
|
+
* Every read and write runs under the cross-process file lock, the clock is
|
|
44
|
+
* sampled inside it, and each `reserve` re-reads the file before deciding,
|
|
45
|
+
* so the decision is always made against the union of every window's
|
|
46
|
+
* spends.
|
|
47
|
+
*
|
|
48
|
+
* Integrity is fail-closed: a line that does not parse makes every reserve
|
|
49
|
+
* for that portal deny with a message naming the file, until a human fixes
|
|
50
|
+
* or removes it. Skipping bad lines would silently loosen the cap, which is
|
|
51
|
+
* the one thing a rate limit must not do. A crash can leave a torn final
|
|
52
|
+
* line (writes are complete and fsynced, so only power loss does this); the
|
|
53
|
+
* next append starts on a fresh line so the torn fragment stays isolated —
|
|
54
|
+
* and still trips the check, on purpose.
|
|
55
|
+
*
|
|
56
|
+
* Entries older than the longest window sigil supports (24h) can never
|
|
57
|
+
* influence a decision again; when more than COMPACT_AFTER of them have
|
|
58
|
+
* piled up the file is rewritten (tmp + fsync + rename + directory fsync,
|
|
59
|
+
* under the same lock) with only the live tail. Entries dated in the future
|
|
60
|
+
* are always kept.
|
|
61
|
+
*
|
|
62
|
+
* Threat model: an attacker who can delete or rewrite this file can also
|
|
63
|
+
* rewrite the policy file beside it and remove the cap outright, so the
|
|
64
|
+
* ledger earns no extra protection beyond its 0600 mode inside the 0700
|
|
65
|
+
* sigil home; a missing file is an empty ledger. What it does defend
|
|
66
|
+
* against is the prompt-injected *agent*, which never touches the
|
|
67
|
+
* filesystem directly. The hash-chained audit log is the record.
|
|
68
|
+
*/
|
|
69
|
+
export declare class FileSpendLedger implements SpendLedger {
|
|
70
|
+
#private;
|
|
71
|
+
static readonly COMPACT_AFTER = 256;
|
|
72
|
+
constructor(stateDir: string, opts?: SpendLedgerOpts);
|
|
73
|
+
pathFor(handle: string): string;
|
|
74
|
+
spent(handle: string, asset: SpendAsset, windowMs: number): bigint;
|
|
75
|
+
reserve(handle: string, asset: SpendAsset, amount: bigint, caps: readonly WindowCap[]): string | null;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Test-only seam: replaces the directory fsync so a test can make it fail.
|
|
79
|
+
* Never set by production code.
|
|
80
|
+
*/
|
|
81
|
+
export declare const _ledgerTestHooks: {
|
|
82
|
+
fsyncDir?: (dir: string) => void;
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=ledger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../../../src/policy/ledger.ts"],"names":[],"mappings":"AAGA,OAAO,EAA2B,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvF;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,WAAW;IAC1B,2FAA2F;IAC3F,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IACnE;;;OAGG;IAC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AAK,IAAI,CAAA;CAAO,CAAC"}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { closeSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { withFileLock, writeAllSync } from '../fs/index.js';
|
|
4
|
+
import { checkWindowCaps, DAY_MS } from './window.js';
|
|
5
|
+
export class SpendLedgerError extends Error {
|
|
6
|
+
cause;
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'SpendLedgerError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** A negative spend is a caller bug, never a credit: refuse without touching the file. */
|
|
13
|
+
const NEGATIVE_REASON = 'denied — negative amount is not a valid spend';
|
|
14
|
+
function sumWindow(entries, asset, windowMs, now) {
|
|
15
|
+
const floor = now - windowMs;
|
|
16
|
+
let total = 0n;
|
|
17
|
+
for (const e of entries) {
|
|
18
|
+
if (e.asset === asset && e.ts > floor)
|
|
19
|
+
total += e.amount;
|
|
20
|
+
}
|
|
21
|
+
return total;
|
|
22
|
+
}
|
|
23
|
+
/** In-memory ledger for tests and for callers that don't persist. */
|
|
24
|
+
export class MemorySpendLedger {
|
|
25
|
+
#entries = new Map();
|
|
26
|
+
#now;
|
|
27
|
+
constructor(opts = {}) {
|
|
28
|
+
this.#now = opts.now ?? (() => Date.now());
|
|
29
|
+
}
|
|
30
|
+
#for(handle) {
|
|
31
|
+
let list = this.#entries.get(handle);
|
|
32
|
+
if (!list) {
|
|
33
|
+
list = [];
|
|
34
|
+
this.#entries.set(handle, list);
|
|
35
|
+
}
|
|
36
|
+
return list;
|
|
37
|
+
}
|
|
38
|
+
spent(handle, asset, windowMs) {
|
|
39
|
+
return sumWindow(this.#for(handle), asset, windowMs, this.#now());
|
|
40
|
+
}
|
|
41
|
+
reserve(handle, asset, amount, caps) {
|
|
42
|
+
if (amount < 0n)
|
|
43
|
+
return NEGATIVE_REASON;
|
|
44
|
+
const list = this.#for(handle);
|
|
45
|
+
const now = this.#now();
|
|
46
|
+
const reason = checkWindowCaps(caps, amount, (w) => sumWindow(list, asset, w, now), asset);
|
|
47
|
+
if (reason !== null)
|
|
48
|
+
return reason;
|
|
49
|
+
if (amount > 0n)
|
|
50
|
+
list.push({ ts: now, asset, amount });
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* File-backed ledger: one append-only JSONL file per portal at
|
|
56
|
+
* `<stateDir>/<handle>.ledger`, lines of `{"ts":…,"asset":"wei","amount":"…"}`.
|
|
57
|
+
*
|
|
58
|
+
* Every read and write runs under the cross-process file lock, the clock is
|
|
59
|
+
* sampled inside it, and each `reserve` re-reads the file before deciding,
|
|
60
|
+
* so the decision is always made against the union of every window's
|
|
61
|
+
* spends.
|
|
62
|
+
*
|
|
63
|
+
* Integrity is fail-closed: a line that does not parse makes every reserve
|
|
64
|
+
* for that portal deny with a message naming the file, until a human fixes
|
|
65
|
+
* or removes it. Skipping bad lines would silently loosen the cap, which is
|
|
66
|
+
* the one thing a rate limit must not do. A crash can leave a torn final
|
|
67
|
+
* line (writes are complete and fsynced, so only power loss does this); the
|
|
68
|
+
* next append starts on a fresh line so the torn fragment stays isolated —
|
|
69
|
+
* and still trips the check, on purpose.
|
|
70
|
+
*
|
|
71
|
+
* Entries older than the longest window sigil supports (24h) can never
|
|
72
|
+
* influence a decision again; when more than COMPACT_AFTER of them have
|
|
73
|
+
* piled up the file is rewritten (tmp + fsync + rename + directory fsync,
|
|
74
|
+
* under the same lock) with only the live tail. Entries dated in the future
|
|
75
|
+
* are always kept.
|
|
76
|
+
*
|
|
77
|
+
* Threat model: an attacker who can delete or rewrite this file can also
|
|
78
|
+
* rewrite the policy file beside it and remove the cap outright, so the
|
|
79
|
+
* ledger earns no extra protection beyond its 0600 mode inside the 0700
|
|
80
|
+
* sigil home; a missing file is an empty ledger. What it does defend
|
|
81
|
+
* against is the prompt-injected *agent*, which never touches the
|
|
82
|
+
* filesystem directly. The hash-chained audit log is the record.
|
|
83
|
+
*/
|
|
84
|
+
export class FileSpendLedger {
|
|
85
|
+
static COMPACT_AFTER = 256;
|
|
86
|
+
#dir;
|
|
87
|
+
#now;
|
|
88
|
+
constructor(stateDir, opts = {}) {
|
|
89
|
+
this.#dir = stateDir;
|
|
90
|
+
this.#now = opts.now ?? (() => Date.now());
|
|
91
|
+
}
|
|
92
|
+
pathFor(handle) {
|
|
93
|
+
return join(this.#dir, `${handle}.ledger`);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create the state dir and make its directory entry durable. Not cached:
|
|
97
|
+
* a sync that failed must not be remembered as done, and a reservation
|
|
98
|
+
* from another daemon must not mistake the directory's existence for
|
|
99
|
+
* established durability. One mkdir + one fsync per reserve is cheap.
|
|
100
|
+
*/
|
|
101
|
+
#ensureDir() {
|
|
102
|
+
mkdirSync(this.#dir, { recursive: true, mode: 0o700 });
|
|
103
|
+
fsyncDirOrFail(dirname(this.#dir));
|
|
104
|
+
}
|
|
105
|
+
spent(handle, asset, windowMs) {
|
|
106
|
+
const path = this.pathFor(handle);
|
|
107
|
+
return ledgerIo(path, () => {
|
|
108
|
+
mkdirSync(this.#dir, { recursive: true, mode: 0o700 });
|
|
109
|
+
return withFileLock(path, () => sumWindow(readEntries(path).entries, asset, windowMs, this.#now()));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
reserve(handle, asset, amount, caps) {
|
|
113
|
+
if (amount < 0n)
|
|
114
|
+
return NEGATIVE_REASON;
|
|
115
|
+
const path = this.pathFor(handle);
|
|
116
|
+
return ledgerIo(path, () => {
|
|
117
|
+
this.#ensureDir();
|
|
118
|
+
return withFileLock(path, () => {
|
|
119
|
+
const { entries, endsWithNewline, size } = readEntries(path);
|
|
120
|
+
const now = this.#now();
|
|
121
|
+
const reason = checkWindowCaps(caps, amount, (w) => sumWindow(entries, asset, w, now), asset);
|
|
122
|
+
if (reason !== null)
|
|
123
|
+
return reason;
|
|
124
|
+
if (amount === 0n)
|
|
125
|
+
return null;
|
|
126
|
+
const entry = { ts: now, asset, amount };
|
|
127
|
+
const stale = entries.filter((e) => e.ts <= now - DAY_MS).length;
|
|
128
|
+
if (stale > FileSpendLedger.COMPACT_AFTER) {
|
|
129
|
+
const live = entries.filter((e) => e.ts > now - DAY_MS);
|
|
130
|
+
live.push(entry);
|
|
131
|
+
const tmp = `${path}.tmp`;
|
|
132
|
+
const fd = openSync(tmp, 'w', 0o600);
|
|
133
|
+
try {
|
|
134
|
+
writeAllSync(fd, live.map(serialize).join(''));
|
|
135
|
+
fsyncSync(fd);
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
closeSync(fd);
|
|
139
|
+
}
|
|
140
|
+
renameSync(tmp, path);
|
|
141
|
+
fsyncDirOrFail(dirname(path));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const fd = openSync(path, 'a', 0o600);
|
|
145
|
+
try {
|
|
146
|
+
// Never merge with a torn fragment: start on a fresh line.
|
|
147
|
+
writeAllSync(fd, (size > 0 && !endsWithNewline ? '\n' : '') + serialize(entry));
|
|
148
|
+
fsyncSync(fd);
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
closeSync(fd);
|
|
152
|
+
}
|
|
153
|
+
// The directory entry needs its own fsync, or a power cut right after
|
|
154
|
+
// a signature can lose a brand-new ledger and hand back a fresh
|
|
155
|
+
// allowance on reboot. Done on every append rather than only the
|
|
156
|
+
// first: if a previous attempt's sync failed, the file's mere
|
|
157
|
+
// existence proves nothing about durability.
|
|
158
|
+
fsyncDirOrFail(dirname(path));
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Run a ledger operation, turning any I/O failure — read, open, write,
|
|
167
|
+
* fsync, rename, lock — into a SpendLedgerError naming the file, so the
|
|
168
|
+
* sign path records an audited deny rather than surfacing a bare
|
|
169
|
+
* filesystem error (#91). Errors that are already SpendLedgerError (parse
|
|
170
|
+
* failures, durability failures) pass through unchanged. Signing stops
|
|
171
|
+
* either way; this only makes the audit log say why.
|
|
172
|
+
*/
|
|
173
|
+
function ledgerIo(path, fn) {
|
|
174
|
+
try {
|
|
175
|
+
return fn();
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
if (err instanceof SpendLedgerError)
|
|
179
|
+
throw err;
|
|
180
|
+
const e = err;
|
|
181
|
+
const wrapped = new SpendLedgerError(`spend ledger ${path}: I/O failure (${e.code ?? e.name}: ${e.message}) — refusing to sign`);
|
|
182
|
+
wrapped.cause = err; // keep the stack and errno fields for diagnostics
|
|
183
|
+
throw wrapped;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Test-only seam: replaces the directory fsync so a test can make it fail.
|
|
188
|
+
* Never set by production code.
|
|
189
|
+
*/
|
|
190
|
+
export const _ledgerTestHooks = {};
|
|
191
|
+
/**
|
|
192
|
+
* fsync a directory, converting any failure into a SpendLedgerError. The
|
|
193
|
+
* spend may already be on disk at this point; refusing to sign then errs
|
|
194
|
+
* on the side of over-counting, never under-counting. Nothing is swallowed:
|
|
195
|
+
* a filesystem that can't confirm durability can't back a rate limit.
|
|
196
|
+
*/
|
|
197
|
+
function fsyncDirOrFail(dir) {
|
|
198
|
+
try {
|
|
199
|
+
if (_ledgerTestHooks.fsyncDir) {
|
|
200
|
+
_ledgerTestHooks.fsyncDir(dir);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const fd = openSync(dir, 'r');
|
|
204
|
+
try {
|
|
205
|
+
fsyncSync(fd);
|
|
206
|
+
}
|
|
207
|
+
finally {
|
|
208
|
+
closeSync(fd);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
catch (err) {
|
|
212
|
+
throw new SpendLedgerError(`spend ledger: could not make ${dir} durable (${err.message}) — refusing to sign`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function serialize(e) {
|
|
216
|
+
return JSON.stringify({ ts: e.ts, asset: e.asset, amount: e.amount.toString() }) + '\n';
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Parse the ledger. Any line that does not parse — including a torn final
|
|
220
|
+
* line — throws SpendLedgerError naming the file: the cap cannot be
|
|
221
|
+
* evaluated against unknown history, so every reserve denies until a human
|
|
222
|
+
* repairs or removes the file. A missing file is an empty ledger.
|
|
223
|
+
*/
|
|
224
|
+
function readEntries(path) {
|
|
225
|
+
let text;
|
|
226
|
+
try {
|
|
227
|
+
text = readFileSync(path, 'utf8');
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
if (err.code === 'ENOENT') {
|
|
231
|
+
return { entries: [], endsWithNewline: true, size: 0 };
|
|
232
|
+
}
|
|
233
|
+
throw err;
|
|
234
|
+
}
|
|
235
|
+
const out = [];
|
|
236
|
+
const lines = text.split('\n');
|
|
237
|
+
const endsWithNewline = text === '' || text.endsWith('\n');
|
|
238
|
+
for (let i = 0; i < lines.length; i++) {
|
|
239
|
+
const line = lines[i];
|
|
240
|
+
if (line === '') {
|
|
241
|
+
// Blank lines (the split's trailing empty string, or a separator we
|
|
242
|
+
// wrote to isolate a torn fragment) carry no information.
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const e = parseEntry(line);
|
|
246
|
+
if (!e) {
|
|
247
|
+
throw new SpendLedgerError(`spend ledger ${path} has an unreadable line ${i + 1} — refusing to sign against ` +
|
|
248
|
+
`unknown history; inspect and repair or remove the file (this resets the allowance)`);
|
|
249
|
+
}
|
|
250
|
+
out.push(e);
|
|
251
|
+
}
|
|
252
|
+
return { entries: out, endsWithNewline, size: Buffer.byteLength(text, 'utf8') };
|
|
253
|
+
}
|
|
254
|
+
function parseEntry(line) {
|
|
255
|
+
let obj;
|
|
256
|
+
try {
|
|
257
|
+
obj = JSON.parse(line);
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
if (typeof obj !== 'object' || obj === null)
|
|
263
|
+
return null;
|
|
264
|
+
const r = obj;
|
|
265
|
+
const ts = r['ts'];
|
|
266
|
+
const asset = r['asset'];
|
|
267
|
+
const amount = r['amount'];
|
|
268
|
+
if (typeof ts !== 'number' || !Number.isFinite(ts))
|
|
269
|
+
return null;
|
|
270
|
+
if (asset !== 'wei' && asset !== 'lamports')
|
|
271
|
+
return null;
|
|
272
|
+
if (typeof amount !== 'string' || !/^[0-9]+$/.test(amount))
|
|
273
|
+
return null;
|
|
274
|
+
return { ts, asset, amount: BigInt(amount) };
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../../src/policy/ledger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC9F,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,EAAmC,MAAM,aAAa,CAAC;AA8BvF,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,KAAK,CAAW;IACzB,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AAED,0FAA0F;AAC1F,MAAM,eAAe,GAAG,+CAA+C,CAAC;AAaxE,SAAS,SAAS,CAChB,OAA+B,EAC/B,KAAiB,EACjB,QAAgB,EAChB,GAAW;IAEX,MAAM,KAAK,GAAG,GAAG,GAAG,QAAQ,CAAC;IAC7B,IAAI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3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../src/policy/loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAmB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAM/E;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../src/policy/loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAmB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAM/E;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAoLlD;AAmCD;;;;GAIG;AACH,wBAAgB,wBAAwB,IAAI,cAAc,CAmBzD;AAED;;;;;GAKG;AACH,qBAAa,wBAAyB,YAAW,cAAc;;gBAEjD,SAAS,EAAE,MAAM;IAG7B,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;CAehC"}
|
|
@@ -35,6 +35,13 @@ export function parsePolicy(source) {
|
|
|
35
35
|
// that applies whether or not the rest of the file is strict.
|
|
36
36
|
const requireConfirmAboveWei = parseOptionalWei(raw['require_confirm_above_wei'], 'require_confirm_above_wei');
|
|
37
37
|
const requireConfirmAboveLamports = parseOptionalDec(raw['require_confirm_above_lamports'], 'require_confirm_above_lamports');
|
|
38
|
+
// Rolling-window caps are mode-independent too.
|
|
39
|
+
const windows = parseWindowCaps(raw);
|
|
40
|
+
// As are the transaction-preview gates.
|
|
41
|
+
const previewGates = {
|
|
42
|
+
requireSimulation: asBool(raw['require_simulation'], 'require_simulation', false),
|
|
43
|
+
signOnRevert: asBool(raw['sign_on_revert'], 'sign_on_revert', false),
|
|
44
|
+
};
|
|
38
45
|
if (mode === 'permissive') {
|
|
39
46
|
return {
|
|
40
47
|
mode: 'permissive',
|
|
@@ -48,8 +55,12 @@ export function parsePolicy(source) {
|
|
|
48
55
|
allowSvmMessageSigning: true,
|
|
49
56
|
svmAllowTo: [],
|
|
50
57
|
svmMaxLamports: 0n,
|
|
58
|
+
typedDataVerifyingContracts: [],
|
|
59
|
+
typedDataPrimaryTypes: [],
|
|
60
|
+
...previewGates,
|
|
51
61
|
...(requireConfirmAboveWei !== undefined ? { requireConfirmAboveWei } : {}),
|
|
52
62
|
...(requireConfirmAboveLamports !== undefined ? { requireConfirmAboveLamports } : {}),
|
|
63
|
+
...windows,
|
|
53
64
|
};
|
|
54
65
|
}
|
|
55
66
|
// strict mode — every field is consulted; apply defaults for absent ones.
|
|
@@ -77,6 +88,18 @@ export function parsePolicy(source) {
|
|
|
77
88
|
const allowContractCreation = asBool(raw['allow_contract_creation'], 'allow_contract_creation', false);
|
|
78
89
|
const allowMessageSigning = asBool(raw['allow_message_signing'], 'allow_message_signing', false);
|
|
79
90
|
const allowTypedData = asBool(raw['allow_typed_data'], 'allow_typed_data', false);
|
|
91
|
+
const typedDataVerifyingContracts = asStringArray(raw['typed_data_verifying_contracts'], 'typed_data_verifying_contracts').map((s, i) => {
|
|
92
|
+
if (!ADDR_RE.test(s)) {
|
|
93
|
+
throw new PolicyLoadError(`policy.typed_data_verifying_contracts[${i}] must be 0x-prefixed 20-byte address`);
|
|
94
|
+
}
|
|
95
|
+
return s.toLowerCase();
|
|
96
|
+
});
|
|
97
|
+
const typedDataPrimaryTypes = asStringArray(raw['typed_data_primary_types'], 'typed_data_primary_types').map((s, i) => {
|
|
98
|
+
if (s.trim() === '') {
|
|
99
|
+
throw new PolicyLoadError(`policy.typed_data_primary_types[${i}] must be a non-empty string`);
|
|
100
|
+
}
|
|
101
|
+
return s;
|
|
102
|
+
});
|
|
80
103
|
const allowSvmMessageSigning = asBool(raw['allow_svm_message_signing'], 'allow_svm_message_signing', false);
|
|
81
104
|
const svmAllowToRaw = asStringArray(raw['svm_allow_to'], 'svm_allow_to');
|
|
82
105
|
const svmAllowTo = svmAllowToRaw.map((s, i) => {
|
|
@@ -122,8 +145,35 @@ export function parsePolicy(source) {
|
|
|
122
145
|
allowSvmMessageSigning,
|
|
123
146
|
svmAllowTo,
|
|
124
147
|
svmMaxLamports,
|
|
148
|
+
typedDataVerifyingContracts,
|
|
149
|
+
typedDataPrimaryTypes,
|
|
150
|
+
...previewGates,
|
|
125
151
|
...(requireConfirmAboveWei !== undefined ? { requireConfirmAboveWei } : {}),
|
|
126
152
|
...(requireConfirmAboveLamports !== undefined ? { requireConfirmAboveLamports } : {}),
|
|
153
|
+
...windows,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Parse the four optional rolling-window caps. An hourly cap above the daily
|
|
158
|
+
* cap for the same asset can never bind, so it is rejected as a
|
|
159
|
+
* misconfiguration rather than silently ignored.
|
|
160
|
+
*/
|
|
161
|
+
function parseWindowCaps(raw) {
|
|
162
|
+
const hourWei = parseOptionalDec(raw['max_value_per_hour_wei'], 'max_value_per_hour_wei');
|
|
163
|
+
const dayWei = parseOptionalDec(raw['max_value_per_day_wei'], 'max_value_per_day_wei');
|
|
164
|
+
const hourLam = parseOptionalDec(raw['svm_max_lamports_per_hour'], 'svm_max_lamports_per_hour');
|
|
165
|
+
const dayLam = parseOptionalDec(raw['svm_max_lamports_per_day'], 'svm_max_lamports_per_day');
|
|
166
|
+
if (hourWei !== undefined && dayWei !== undefined && hourWei > dayWei) {
|
|
167
|
+
throw new PolicyLoadError(`policy.max_value_per_hour_wei (${hourWei}) must not exceed max_value_per_day_wei (${dayWei})`);
|
|
168
|
+
}
|
|
169
|
+
if (hourLam !== undefined && dayLam !== undefined && hourLam > dayLam) {
|
|
170
|
+
throw new PolicyLoadError(`policy.svm_max_lamports_per_hour (${hourLam}) must not exceed svm_max_lamports_per_day (${dayLam})`);
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
...(hourWei !== undefined ? { maxValuePerHourWei: hourWei } : {}),
|
|
174
|
+
...(dayWei !== undefined ? { maxValuePerDayWei: dayWei } : {}),
|
|
175
|
+
...(hourLam !== undefined ? { svmMaxLamportsPerHour: hourLam } : {}),
|
|
176
|
+
...(dayLam !== undefined ? { svmMaxLamportsPerDay: dayLam } : {}),
|
|
127
177
|
};
|
|
128
178
|
}
|
|
129
179
|
/**
|
|
@@ -144,6 +194,10 @@ export function permissivePolicyResolver() {
|
|
|
144
194
|
allowSvmMessageSigning: true,
|
|
145
195
|
svmAllowTo: [],
|
|
146
196
|
svmMaxLamports: 0n,
|
|
197
|
+
typedDataVerifyingContracts: [],
|
|
198
|
+
typedDataPrimaryTypes: [],
|
|
199
|
+
requireSimulation: false,
|
|
200
|
+
signOnRevert: false,
|
|
147
201
|
};
|
|
148
202
|
return { resolve: () => policy };
|
|
149
203
|
}
|