@tollxyz/guard 0.3.0 → 0.4.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 +167 -0
- package/dist/check.d.ts +1 -1
- package/dist/check.js +6 -2
- package/dist/index.d.ts +7 -3
- package/dist/index.js +4 -2
- package/dist/policy.d.ts +129 -3
- package/dist/policy.js +302 -4
- package/dist/spend.d.ts +48 -0
- package/dist/spend.js +239 -0
- package/dist/store.d.ts +46 -0
- package/dist/store.js +78 -0
- package/dist/wallets.d.ts +1 -1
- package/dist/wallets.js +76 -53
- package/dist/x402.d.ts +12 -0
- package/dist/x402.js +148 -7
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# @tollxyz/guard
|
|
2
2
|
|
|
3
|
+
> **New in 0.4.0: spend caps, allowlists, human approval and preview** (`spend` option). Unset, nothing changes.
|
|
4
|
+
> See [0.4.0: spend caps, allowlists, approval, preview](#040-spend-caps-allowlists-approval-preview).
|
|
5
|
+
|
|
6
|
+
> **0.3.1:** `signTypedData` (viem and ethers) now sends the signing account as `from`, so Toll can say what a Permit2 signature lets someone take.
|
|
7
|
+
>
|
|
3
8
|
> **What changed in 0.3.0: every signing method is checked.**
|
|
4
9
|
> - Messages: viem and ethers `signMessage`, and `request()` for `personal_sign` and `eth_sign`.
|
|
5
10
|
> - Typed data: `request()` for `eth_signTypedData_v4` too, any EIP-712 (orders, permits, sign-ins).
|
|
@@ -162,10 +167,172 @@ is at most `maxRetryWaitMs` (default 10 s); a coin still not read stays WARN, ne
|
|
|
162
167
|
| `timeoutMs` | `15000` | one request to Toll |
|
|
163
168
|
| `retry`, `maxRetryWaitMs` | `true`, `10000` | the one retry after Retry-After |
|
|
164
169
|
| `baseUrl`, `fetch` | `https://t0ll.xyz`, global fetch | |
|
|
170
|
+
| `spend` | none | 0.4.0: caps, allowlists, approval ([below](#040-spend-caps-allowlists-approval-preview)); unset = 0.3.x behaviour |
|
|
171
|
+
| `store` | in-process `MemorySpendStore` | where spend counters and paid x402 ids live |
|
|
172
|
+
| `failClosed` | `false` | with `spend`: a request whose spend cannot be read is blocked |
|
|
165
173
|
|
|
166
174
|
Errors: `TollBlockedError` (`.assessment`, `.codes`, message lists each code and message) and
|
|
167
175
|
`TollUnavailableError` (`.reason`, `.status`, `.paidUrl`).
|
|
168
176
|
|
|
177
|
+
## 0.4.0: spend caps, allowlists, approval, preview
|
|
178
|
+
|
|
179
|
+
Toll's verdict says whether a request looks safe. `spend` adds your own limits on top: how much the agent may spend
|
|
180
|
+
per call and per UTC day, who it may pay, and when a human has to say yes. Without `spend` nothing changes: 0.4.0
|
|
181
|
+
behaves exactly like 0.3.x.
|
|
182
|
+
|
|
183
|
+
The order for every request:
|
|
184
|
+
|
|
185
|
+
1. **Allowlists** (`allow.to`, `allow.spender`, `allow.payTo`): read from the request itself, before Toll is asked.
|
|
186
|
+
Off the list = blocked, and Toll is not called.
|
|
187
|
+
2. **Toll's verdict**: ALLOW / WARN / BLOCK with your `onWarn`, `onBlock`, `onUnavailable` as before. A WARN that
|
|
188
|
+
`block-high` blocks is still blocked, however small the amount.
|
|
189
|
+
3. **Caps** (`assets`, `perEndpoint`): per call, then per day. The day amount is reserved.
|
|
190
|
+
4. **Approval** (`approveAbove` + `approve`): above the threshold your `approve` function decides.
|
|
191
|
+
5. **After the send**: the reservation is committed. If the send throws (rejected, nonce too low, RPC down), it is
|
|
192
|
+
released, so failures never use up the day cap.
|
|
193
|
+
|
|
194
|
+
Caps never relax Toll's verdict, and Toll's ALLOW never lifts a cap: a request must pass both.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
import { guard, NATIVE, USDG, TollPolicyError } from "@tollxyz/guard";
|
|
198
|
+
|
|
199
|
+
const wallet = guard(walletClient, {
|
|
200
|
+
spend: {
|
|
201
|
+
// base units: ETH in wei, USDG with 6 decimals
|
|
202
|
+
assets: {
|
|
203
|
+
[NATIVE]: { perCall: 10n ** 16n, perDay: 5n * 10n ** 16n }, // 0.01 ETH a call, 0.05 ETH a day
|
|
204
|
+
[USDG]: { perCall: 20_000_000n, perDay: 100_000_000n }, // 20 USDG a call, 100 USDG a day
|
|
205
|
+
},
|
|
206
|
+
allow: { spender: ["0xYourRouter..."] }, // approvals only to your router
|
|
207
|
+
approveAbove: { [USDG]: 10_000_000n }, // above 10 USDG a human decides
|
|
208
|
+
approve: async (req) => {
|
|
209
|
+
const ok = await askOnTelegram(`Spend ${req.over[0].amount} of ${req.over[0].asset}?`);
|
|
210
|
+
return { approved: ok, approvalRef: ok ? "tg-msg-1234" : undefined };
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
failClosed: true, // a contract call whose spend cannot be read is blocked
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
await wallet.sendTransaction(tx);
|
|
218
|
+
} catch (e) {
|
|
219
|
+
if (e instanceof TollPolicyError) console.log(e.code, e.amount, e.limit); // "cap-per-day", ...
|
|
220
|
+
else throw e;
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**What counts as spend.** Only what leaves the wallet in a spend asset. The defaults are native ETH, WETH and USDG on
|
|
225
|
+
Robinhood Chain, plus every asset you name in `assets` or `approveAbove`. A sell of any other coin is never capped.
|
|
226
|
+
|
|
227
|
+
- Read locally, with no Toll answer needed: the transaction's value, ERC-20 `transfer` / `transferFrom` / `approve` /
|
|
228
|
+
`increaseAllowance` on a spend asset, EIP-2612 permits, Permit2 permits, EIP-3009 transfer authorizations, and an
|
|
229
|
+
x402 402's amount. So caps hold even when Toll is unreachable.
|
|
230
|
+
- Any other contract call (a swap, a router): the amount comes from Toll's simulation (`x-toll.simulation.deltas`).
|
|
231
|
+
When the simulation shows more leaving than the call declares, the larger amount counts.
|
|
232
|
+
- An approval counts against the per-call cap at its full size (an unlimited approval is over any cap) and must fit
|
|
233
|
+
in what is left of the day cap; it is not added to the day total (the later swap is).
|
|
234
|
+
- Unknown spend: a contract call with no simulation (Toll unreachable), a raw user operation, a bare EIP-7702
|
|
235
|
+
authorization. `onUnknownSpend` decides: `"allow"` by default, `"block"` under `failClosed: true`.
|
|
236
|
+
|
|
237
|
+
| `spend` field | |
|
|
238
|
+
| --- | --- |
|
|
239
|
+
| `assets` | `{ [asset]: { perCall?, perDay? } }`, asset = `"native"` or a token address, amounts in base units |
|
|
240
|
+
| `perEndpoint` | x402 caps per URL prefix (origin + path), e.g. `{ "https://api.example.com/v1/": { perDay: 1_000_000n } }` |
|
|
241
|
+
| `allow` | `{ to?, spender?, payTo? }`: every recipient (or called contract), approval spender, x402 payee must be listed |
|
|
242
|
+
| `approveAbove` | `{ [asset]: amount }`: above it, `approve` must say yes |
|
|
243
|
+
| `approve` | `(req) => Promise<{ approved, approvalRef? }>`; no function = denied. `approvalRef` lands in `report.spend.approvalRef` |
|
|
244
|
+
| `onUnknownSpend` | `"allow"` (default) or `"block"` (default under `failClosed`) |
|
|
245
|
+
|
|
246
|
+
New options next to `spend`: `store` (where counters live, below) and `failClosed`.
|
|
247
|
+
|
|
248
|
+
`TollPolicyError` extends `TollBlockedError` (existing `catch` blocks still see a block). `.code` is one of
|
|
249
|
+
`not-allowlisted`, `cap-per-call`, `cap-per-day`, `cap-endpoint`, `approval-denied`, `duplicate-payment`,
|
|
250
|
+
`unknown-spend`; `.asset`, `.amount`, `.limit` when they apply. The report has `basis: "policy"` and `policy: code`.
|
|
251
|
+
|
|
252
|
+
### Preview, then confirm
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
import { preview } from "@tollxyz/guard";
|
|
256
|
+
|
|
257
|
+
const p = await preview({ chainId: 4663, from, to, data, value }, opts); // same opts as the guard
|
|
258
|
+
// p.decision "allowed" | "blocked", p.basis, p.policy, p.spend, p.needsApproval, p.reason
|
|
259
|
+
if (p.decision === "allowed") await wallet.sendTransaction({ to, data, value, tollConfirm: p.confirmId });
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
`preview` asks Toll and checks every rule, but reserves nothing, calls no `approve` and no `onReport`. Passing its
|
|
263
|
+
`confirmId` as `tollConfirm` within 30 seconds reuses that Toll answer (one Toll call instead of two). It is used
|
|
264
|
+
once, and only for the identical request; anything else is checked again. The guard strips `tollConfirm` before the
|
|
265
|
+
wallet sees the request (viem `sendTransaction` / `signTransaction` / `writeContract`, ethers `sendTransaction` /
|
|
266
|
+
`signTransaction`).
|
|
267
|
+
|
|
268
|
+
### x402: payment ids and per-endpoint caps
|
|
269
|
+
|
|
270
|
+
With `spend` set, `guardX402Fetch` also:
|
|
271
|
+
|
|
272
|
+
- counts a 402's amount against `assets` and `perEndpoint` caps when it is checked. The reservation waits for the
|
|
273
|
+
paid retry: committed when the seller answers, released on another 402 or an error, released after `holdMs`
|
|
274
|
+
(default 2 minutes) when no paid retry comes.
|
|
275
|
+
- never pays the same x402 `payment-identifier` twice. An id is marked paid only when the seller accepts the paid
|
|
276
|
+
request (any answer but 402); sending that id again throws `TollPolicyError` `duplicate-payment` (ids are kept
|
|
277
|
+
`paymentIdTtlS`, default 1 day). A second send of an id that is still in flight is refused too. After a network
|
|
278
|
+
error or timeout the same id may be retried (the seller dedupes it): the retry reuses the reservation of its 402,
|
|
279
|
+
so nothing is counted twice. A different id is a new payment.
|
|
280
|
+
|
|
281
|
+
### Where the counters live
|
|
282
|
+
|
|
283
|
+
The default store is a `MemorySpendStore` in this process, one per `spend` object. **It resets when the process
|
|
284
|
+
restarts and is not shared between processes**: two workers each get the full day cap. For a cap that survives
|
|
285
|
+
restarts or spans workers, pass your own `store` implementing `SpendStore`:
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
interface SpendStore {
|
|
289
|
+
reserve(k: string, amt: bigint, limit: bigint, windowStart: number): Promise<{ ok: boolean; id: string; used: bigint }>;
|
|
290
|
+
commit(id: string): Promise<void>;
|
|
291
|
+
release(id: string): Promise<void>;
|
|
292
|
+
paymentSeen(id: string): Promise<boolean>;
|
|
293
|
+
markPaid(id: string, ttlS: number): Promise<void>;
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
A Redis store (example only, not shipped; `reserve` must be atomic, hence the Lua script):
|
|
298
|
+
|
|
299
|
+
```ts
|
|
300
|
+
import Redis from "ioredis";
|
|
301
|
+
import type { SpendStore } from "@tollxyz/guard";
|
|
302
|
+
|
|
303
|
+
const redis = new Redis(process.env.REDIS_URL!);
|
|
304
|
+
const RESERVE = `
|
|
305
|
+
local used = tonumber(redis.call('GET', KEYS[1]) or '0')
|
|
306
|
+
local amt, limit = tonumber(ARGV[1]), tonumber(ARGV[2])
|
|
307
|
+
if used + amt > limit then return {0, tostring(used)} end
|
|
308
|
+
redis.call('INCRBY', KEYS[1], ARGV[1])
|
|
309
|
+
redis.call('EXPIRE', KEYS[1], 172800)
|
|
310
|
+
redis.call('SET', KEYS[2], KEYS[1] .. '|' .. ARGV[1], 'EX', 3600)
|
|
311
|
+
return {1, tostring(used + amt)}`;
|
|
312
|
+
const RELEASE = `
|
|
313
|
+
local v = redis.call('GET', KEYS[1])
|
|
314
|
+
if not v then return 0 end
|
|
315
|
+
local sep = string.find(v, '|', 1, true)
|
|
316
|
+
redis.call('DECRBY', string.sub(v, 1, sep - 1), string.sub(v, sep + 1))
|
|
317
|
+
redis.call('DEL', KEYS[1])
|
|
318
|
+
return 1`;
|
|
319
|
+
|
|
320
|
+
export const redisStore: SpendStore = {
|
|
321
|
+
async reserve(k, amt, limit, windowStart) {
|
|
322
|
+
const id = `toll:res:${crypto.randomUUID()}`;
|
|
323
|
+
// amounts as numbers in Lua: fine below 2^53 base units; use string math for larger caps
|
|
324
|
+
const [ok, used] = (await redis.eval(RESERVE, 2, `toll:cap:${k}:${windowStart}`, id, amt.toString(), limit.toString())) as [number, string];
|
|
325
|
+
return { ok: ok === 1, id: ok === 1 ? id : "", used: BigInt(used) };
|
|
326
|
+
},
|
|
327
|
+
async commit(id) { await redis.del(id); }, // the amount stays in the day counter
|
|
328
|
+
async release(id) { await redis.eval(RELEASE, 1, id); },
|
|
329
|
+
async paymentSeen(id) { return (await redis.exists(`toll:paid:${id}`)) === 1; },
|
|
330
|
+
async markPaid(id, ttlS) { await redis.set(`toll:paid:${id}`, "1", "EX", ttlS); },
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const wallet = guard(walletClient, { spend: { /* ... */ }, store: redisStore });
|
|
334
|
+
```
|
|
335
|
+
|
|
169
336
|
## Fail-open or fail-closed
|
|
170
337
|
|
|
171
338
|
The default is fail-open: if Toll cannot answer, the signature goes ahead, with a `[toll-guard] ... NOT checked`
|
package/dist/check.d.ts
CHANGED
|
@@ -121,7 +121,7 @@ export interface CheckOptions {
|
|
|
121
121
|
export declare class TollBlockedError extends Error {
|
|
122
122
|
readonly assessment: RiskAssessment;
|
|
123
123
|
readonly codes: string[];
|
|
124
|
-
constructor(assessment: RiskAssessment, why?: Action);
|
|
124
|
+
constructor(assessment: RiskAssessment, why?: Action | "policy");
|
|
125
125
|
}
|
|
126
126
|
export type UnavailableReason = "network" | "timeout" | "rate-limited" | "server-error" | "bad-request" | "bad-answer" | "unsupported" | "encode";
|
|
127
127
|
export declare class TollUnavailableError extends Error {
|
package/dist/check.js
CHANGED
|
@@ -19,7 +19,7 @@ export class TollBlockedError extends Error {
|
|
|
19
19
|
const top = ws.filter((w) => w.severity !== "low").slice(0, 5);
|
|
20
20
|
const lines = (top.length ? top : ws.slice(0, 5)).map((w) => `${w.code}: ${w.message}`);
|
|
21
21
|
const link = assessment["x-toll"]?.link;
|
|
22
|
-
super(`Toll ${why}, not signed. ${lines.join(" | ") || "No reason given."}${link ? ` More: ${link}` : ""}`);
|
|
22
|
+
super(`${why === "policy" ? "Spend policy" : `Toll ${why}`}, not signed. ${lines.join(" | ") || "No reason given."}${link ? ` More: ${link}` : ""}`);
|
|
23
23
|
this.name = "TollBlockedError";
|
|
24
24
|
this.assessment = assessment;
|
|
25
25
|
this.codes = ws.map((w) => w.code);
|
|
@@ -81,8 +81,12 @@ export function bodyOf(input) {
|
|
|
81
81
|
b.from = input.from;
|
|
82
82
|
b.authorizationList = input.authorizationList;
|
|
83
83
|
}
|
|
84
|
-
if (input.typedData)
|
|
84
|
+
if (input.typedData) {
|
|
85
85
|
b.typedData = input.typedData;
|
|
86
|
+
// the signer of typed data alone (0.3.1): Toll reads what the signature lets someone take from it
|
|
87
|
+
if (b.from === undefined && input.from !== undefined)
|
|
88
|
+
b.from = input.from;
|
|
89
|
+
}
|
|
86
90
|
if (input.message)
|
|
87
91
|
b.message = input.message;
|
|
88
92
|
if (input.rpc)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export { checkTx, bodyOf, chainIdOf, paidUrlOf, isAssessment, TollBlockedError, TollUnavailableError, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRY_WAIT_MS, PAID_TX_PATH, PRESIGN_DOCS, TX_CHAIN_ID, } from "./check.js";
|
|
2
2
|
export type { Action, Severity, RiskWarning, ScannerVerdict, TollExtra, RiskAssessment, SignMessageLike, RpcLike, CheckInput, CheckOptions, TypedDataLike, FetchLike, Hex, Numberish, UnavailableReason, } from "./check.js";
|
|
3
|
-
export { enforce, enforceCalls, unsupportedWhy, refuse, gateAuthorization, resolvePolicy, hasHighWarning, isSignature, isEthSign } from "./policy.js";
|
|
4
|
-
export type { GuardOptions, GuardReport, GuardKind, WarnPolicy, BlockPolicy } from "./policy.js";
|
|
3
|
+
export { enforce, enforceCalls, unsupportedWhy, refuse, gateAuthorization, resolvePolicy, hasHighWarning, isSignature, isEthSign, TollPolicyError, commitSpend, releaseSpend, preview } from "./policy.js";
|
|
4
|
+
export type { GuardOptions, GuardReport, GuardKind, WarnPolicy, BlockPolicy, SpendPolicy, Caps, ApprovalRequest, PolicyCode, GuardDecision } from "./policy.js";
|
|
5
5
|
export { guard, guardViem, guardEthers, guardSmartAccount, isViemClient, isBundlerClient, primaryTypeOf } from "./wallets.js";
|
|
6
6
|
export type { ViemGuardOptions } from "./wallets.js";
|
|
7
|
-
export { guardX402Fetch, readPaymentRequired, readPaymentForms, paymentFormsDiffer } from "./x402.js";
|
|
7
|
+
export { guardX402Fetch, readPaymentRequired, readPaymentForms, paymentFormsDiffer, paymentIdOf } from "./x402.js";
|
|
8
8
|
export type { X402GuardOptions } from "./x402.js";
|
|
9
9
|
export { read8470, latestFinding8470, decodeGetObservation, decodeLatestFinding, encodeGetObservation, encodeLatestByToken, encodeLatestFinding, FINDING_KINDS, NOT_READ, OBS_REGISTRY, OBS_DETECTOR, OBS_CHAIN_ID, RH_RPC, } from "./erc8470.js";
|
|
10
10
|
export type { Observation8470, Finding8470, LatestFinding8470, Read8470Options, Lean, KeyKind, Unit } from "./erc8470.js";
|
|
11
|
+
export { spendOf, NATIVE, USDG, WETH, DEFAULT_SPEND_ASSETS, UNLIMITED } from "./spend.js";
|
|
12
|
+
export type { Spend, SpendItem } from "./spend.js";
|
|
13
|
+
export { MemorySpendStore, dayStart } from "./store.js";
|
|
14
|
+
export type { SpendStore } from "./store.js";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { checkTx, bodyOf, chainIdOf, paidUrlOf, isAssessment, TollBlockedError, TollUnavailableError, DEFAULT_BASE_URL, DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRY_WAIT_MS, PAID_TX_PATH, PRESIGN_DOCS, TX_CHAIN_ID, } from "./check.js";
|
|
2
|
-
export { enforce, enforceCalls, unsupportedWhy, refuse, gateAuthorization, resolvePolicy, hasHighWarning, isSignature, isEthSign } from "./policy.js";
|
|
2
|
+
export { enforce, enforceCalls, unsupportedWhy, refuse, gateAuthorization, resolvePolicy, hasHighWarning, isSignature, isEthSign, TollPolicyError, commitSpend, releaseSpend, preview } from "./policy.js";
|
|
3
3
|
export { guard, guardViem, guardEthers, guardSmartAccount, isViemClient, isBundlerClient, primaryTypeOf } from "./wallets.js";
|
|
4
|
-
export { guardX402Fetch, readPaymentRequired, readPaymentForms, paymentFormsDiffer } from "./x402.js";
|
|
4
|
+
export { guardX402Fetch, readPaymentRequired, readPaymentForms, paymentFormsDiffer, paymentIdOf } from "./x402.js";
|
|
5
5
|
export { read8470, latestFinding8470, decodeGetObservation, decodeLatestFinding, encodeGetObservation, encodeLatestByToken, encodeLatestFinding, FINDING_KINDS, NOT_READ, OBS_REGISTRY, OBS_DETECTOR, OBS_CHAIN_ID, RH_RPC, } from "./erc8470.js";
|
|
6
|
+
export { spendOf, NATIVE, USDG, WETH, DEFAULT_SPEND_ASSETS, UNLIMITED } from "./spend.js";
|
|
7
|
+
export { MemorySpendStore, dayStart } from "./store.js";
|
package/dist/policy.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { TollUnavailableError, type CheckInput, type CheckOptions, type RiskAssessment } from "./check.js";
|
|
1
|
+
import { TollBlockedError, TollUnavailableError, type CheckInput, type CheckOptions, type RiskAssessment } from "./check.js";
|
|
2
|
+
import { type Spend } from "./spend.js";
|
|
3
|
+
import { type SpendStore } from "./store.js";
|
|
2
4
|
export type WarnPolicy = "allow" | "block" | "block-high" | ((assessment: RiskAssessment) => boolean | Promise<boolean>);
|
|
3
5
|
export type BlockPolicy = "throw" | ((assessment: RiskAssessment) => boolean | Promise<boolean>);
|
|
4
6
|
export type GuardKind = "transaction" | "typedData" | "x402" | "message" | "userOp" | "calls";
|
|
@@ -10,11 +12,85 @@ export interface GuardReport {
|
|
|
10
12
|
assessment: RiskAssessment | null;
|
|
11
13
|
/** what the guard did */
|
|
12
14
|
decision: "allowed" | "blocked";
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
+
/**
|
|
16
|
+
* why: Toll's action, "unavailable" (Toll could not answer), "unsupported" (not a request Toll reads) or "policy"
|
|
17
|
+
* (blocked by your own spend rules: allowlist, cap, approval; `policy` says which)
|
|
18
|
+
*/
|
|
19
|
+
basis: "ALLOW" | "WARN" | "BLOCK" | "unavailable" | "unsupported" | "policy";
|
|
15
20
|
/** the paid route (no daily limit) when this caller's free checks are used */
|
|
16
21
|
paidUrl: string | null;
|
|
17
22
|
error?: TollUnavailableError;
|
|
23
|
+
/** the spend rule that blocked it (basis "policy") */
|
|
24
|
+
policy?: PolicyCode;
|
|
25
|
+
/** with `spend` set: what the request spends, the day-cap reservations it holds, and the approval reference */
|
|
26
|
+
spend?: {
|
|
27
|
+
spend: Spend;
|
|
28
|
+
reservations: string[];
|
|
29
|
+
approvalRef?: string;
|
|
30
|
+
needsApproval?: boolean;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export type PolicyCode = "not-allowlisted" | "cap-per-call" | "cap-per-day" | "cap-endpoint" | "approval-denied" | "duplicate-payment" | "unknown-spend";
|
|
34
|
+
export interface Caps {
|
|
35
|
+
/** the most one request may spend, base units */
|
|
36
|
+
perCall?: bigint;
|
|
37
|
+
/** the most all requests together may spend in one UTC day, base units */
|
|
38
|
+
perDay?: bigint;
|
|
39
|
+
}
|
|
40
|
+
export interface ApprovalRequest {
|
|
41
|
+
kind: GuardKind;
|
|
42
|
+
input: CheckInput | null;
|
|
43
|
+
/** Toll's answer (null when Toll could not answer) */
|
|
44
|
+
assessment: RiskAssessment | null;
|
|
45
|
+
spend: Spend;
|
|
46
|
+
/** each asset over its approveAbove threshold */
|
|
47
|
+
over: Array<{
|
|
48
|
+
asset: string;
|
|
49
|
+
amount: bigint;
|
|
50
|
+
threshold: bigint;
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Your own rules around Toll's verdict. Order: allowlists (before Toll is asked) -> Toll's ALLOW / WARN / BLOCK
|
|
55
|
+
* (unchanged) -> per-call and per-day caps -> human approval -> the day reservation is committed after the send
|
|
56
|
+
* (released when the send throws). Keys are "native" or a token address (any case); amounts are base units.
|
|
57
|
+
*/
|
|
58
|
+
export interface SpendPolicy {
|
|
59
|
+
/** caps per spend asset; the spend assets are native, WETH and USDG plus every key here and in approveAbove */
|
|
60
|
+
assets?: Record<string, Caps>;
|
|
61
|
+
/** x402 caps per endpoint: a URL prefix (origin + path), in the payment asset's base units */
|
|
62
|
+
perEndpoint?: Record<string, Caps>;
|
|
63
|
+
/** when a list is given, every recipient / called contract, approval spender or x402 payee must be on it */
|
|
64
|
+
allow?: {
|
|
65
|
+
to?: string[];
|
|
66
|
+
spender?: string[];
|
|
67
|
+
payTo?: string[];
|
|
68
|
+
};
|
|
69
|
+
/** a spend (or allowance) above this amount needs `approve` to say yes */
|
|
70
|
+
approveAbove?: Record<string, bigint>;
|
|
71
|
+
/** asks a human (or another system); no function = denied */
|
|
72
|
+
approve?: (r: ApprovalRequest) => Promise<{
|
|
73
|
+
approved: boolean;
|
|
74
|
+
approvalRef?: string;
|
|
75
|
+
}>;
|
|
76
|
+
/** a request whose spend could not be read (a contract call Toll did not simulate): default "allow", "block" under failClosed */
|
|
77
|
+
onUnknownSpend?: "allow" | "block";
|
|
78
|
+
}
|
|
79
|
+
/** blocked by your own spend rules; a TollBlockedError, so existing catch blocks still see a block */
|
|
80
|
+
export declare class TollPolicyError extends TollBlockedError {
|
|
81
|
+
readonly code: PolicyCode;
|
|
82
|
+
readonly asset?: string;
|
|
83
|
+
readonly amount?: bigint;
|
|
84
|
+
readonly limit?: bigint;
|
|
85
|
+
/** the guard's report of this request */
|
|
86
|
+
readonly report?: GuardReport;
|
|
87
|
+
constructor(code: PolicyCode, message: string, x?: {
|
|
88
|
+
asset?: string;
|
|
89
|
+
amount?: bigint;
|
|
90
|
+
limit?: bigint;
|
|
91
|
+
assessment?: RiskAssessment | null;
|
|
92
|
+
report?: GuardReport;
|
|
93
|
+
});
|
|
18
94
|
}
|
|
19
95
|
export interface GuardOptions extends CheckOptions {
|
|
20
96
|
/**
|
|
@@ -53,6 +129,23 @@ export interface GuardOptions extends CheckOptions {
|
|
|
53
129
|
onAuthorization?: "check" | "block" | "allow";
|
|
54
130
|
/** the site asking for a signature (a URL or host name), sent with messages so Toll can match a sign-in's domain */
|
|
55
131
|
origin?: string;
|
|
132
|
+
/** 0.4.0: spend caps, allowlists and human approval around Toll's verdict; unset = 0.3.x behaviour */
|
|
133
|
+
spend?: SpendPolicy;
|
|
134
|
+
/** where caps and paid x402 ids are counted; default an in-process MemorySpendStore per SpendPolicy object */
|
|
135
|
+
store?: SpendStore;
|
|
136
|
+
/** with `spend`: a request whose spend cannot be read is blocked (onUnknownSpend default "block") */
|
|
137
|
+
failClosed?: boolean;
|
|
138
|
+
}
|
|
139
|
+
/** fields the wrappers pass to enforce; not part of the public options */
|
|
140
|
+
export interface InternalOptions extends GuardOptions {
|
|
141
|
+
/** spend-mode reports of one wrapped call, committed after the send or released when it throws */
|
|
142
|
+
_collect?: GuardReport[];
|
|
143
|
+
/** a preview's confirm id: Toll's answer from that preview is reused once when the request is identical */
|
|
144
|
+
_confirm?: string;
|
|
145
|
+
/** preview: check everything, reserve nothing, never ask for approval */
|
|
146
|
+
_preview?: boolean;
|
|
147
|
+
/** the x402 endpoint URL (per-endpoint caps) */
|
|
148
|
+
_endpoint?: string;
|
|
56
149
|
}
|
|
57
150
|
/** the effective policy: explicit options win, then the strict preset, then the defaults */
|
|
58
151
|
export declare function resolvePolicy(o?: GuardOptions): {
|
|
@@ -91,3 +184,36 @@ export declare function gateAuthorization(kind: GuardKind, where: string, input:
|
|
|
91
184
|
* from the batch's sender, in order; the first one blocked stops the batch. Reports carry kind "calls".
|
|
92
185
|
*/
|
|
93
186
|
export declare function enforceCalls(build: () => CheckInput[] | Promise<CheckInput[]>, o?: GuardOptions): Promise<GuardReport[]>;
|
|
187
|
+
/** the store for these options: the one given, else one in-process store per SpendPolicy object */
|
|
188
|
+
export declare function storeOf(o: GuardOptions): SpendStore;
|
|
189
|
+
/** the reservations of these reports, committed: the request was sent */
|
|
190
|
+
export declare function commitSpend(reports: GuardReport | GuardReport[], o: GuardOptions): Promise<void>;
|
|
191
|
+
/** the reservations of these reports, released: the request was refused or the send threw */
|
|
192
|
+
export declare function releaseSpend(reports: GuardReport | GuardReport[], o: GuardOptions): Promise<void>;
|
|
193
|
+
/** a request the spend policy refuses outside enforce (a repeated x402 payment id): reported, then TollPolicyError */
|
|
194
|
+
export declare function policyRefuse(kind: GuardKind, input: CheckInput | null, code: PolicyCode, message: string, o: GuardOptions): Promise<never>;
|
|
195
|
+
export interface GuardDecision {
|
|
196
|
+
decision: "allowed" | "blocked";
|
|
197
|
+
basis: GuardReport["basis"];
|
|
198
|
+
/** Toll's answer (null when Toll was not asked or could not answer) */
|
|
199
|
+
assessment: RiskAssessment | null;
|
|
200
|
+
/** the spend rule that would block it */
|
|
201
|
+
policy?: PolicyCode;
|
|
202
|
+
/** what it would spend (with `spend` set) */
|
|
203
|
+
spend?: Spend;
|
|
204
|
+
/** over an approveAbove threshold: the send will ask `approve` */
|
|
205
|
+
needsApproval?: boolean;
|
|
206
|
+
/** why it would be blocked */
|
|
207
|
+
reason?: string;
|
|
208
|
+
paidUrl: string | null;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* What the guard would do with this request, without doing it: Toll is asked and every spend rule is checked, but
|
|
212
|
+
* nothing is reserved, `approve` is not called and onReport is not called. When allowed, `confirmId` passed as
|
|
213
|
+
* `tollConfirm` on the send within 30 s skips the second Toll call (once, and only for the identical request).
|
|
214
|
+
*/
|
|
215
|
+
export declare function preview(input: CheckInput, opts?: GuardOptions & {
|
|
216
|
+
kind?: GuardKind;
|
|
217
|
+
}): Promise<GuardDecision & {
|
|
218
|
+
confirmId: string;
|
|
219
|
+
}>;
|