presign-guard-wallet 0.21.0 → 0.22.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 +12 -1
- package/index.d.ts +5 -1
- package/index.js +71 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,7 +52,18 @@ try {
|
|
|
52
52
|
}
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
`wallet` is the same client. `signMessage` is checked too, as a plain message: a sign-in message (Sign-In with Ethereum) for another site than `origin` is red, unreadable bytes are orange; it counts toward no limit (`checkMessages: false` turns this off).
|
|
55
|
+
`wallet` is the same client. `signMessage` is checked too, as a plain message: a sign-in message (Sign-In with Ethereum) for another site than `origin` is red, unreadable bytes are orange; it counts toward no limit (`checkMessages: false` turns this off).
|
|
56
|
+
|
|
57
|
+
viem's other ways to sign or send are checked the same way, so a library that uses them can't go around the wallet:
|
|
58
|
+
|
|
59
|
+
| Method | Checked as |
|
|
60
|
+
|---|---|
|
|
61
|
+
| `sendTransactionSync`, `signTransaction` | `sendTransaction` (a signed transaction is as good as sent: anyone can broadcast it) |
|
|
62
|
+
| `writeContractSync` | `writeContract` |
|
|
63
|
+
| `sendCalls`, `sendCallsSync` (EIP-5792 batches) | every call, as a transaction from the wallet; one red or stopped call stops the whole batch. Each call counts toward `perTx` on its own and all of them toward the day's limit, booked before anything is signed |
|
|
64
|
+
| `deployContract` | a deployment: not checked by presign-guard, but the value it sends counts toward the limits |
|
|
65
|
+
|
|
66
|
+
Every other method (`getAddresses`, `extend`, `sendRawTransaction` for a transaction that is already signed, …) works as before, unchecked.
|
|
56
67
|
|
|
57
68
|
## Options
|
|
58
69
|
|
package/index.d.ts
CHANGED
|
@@ -203,7 +203,11 @@ export interface GuardOptions {
|
|
|
203
203
|
server?: { url: string; key: string; fetch?: typeof globalThis.fetch; requestTimeoutMs?: number };
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* The same wallet client. Checked first: sendTransaction, writeContract, signTypedData, signAuthorization (EIP-7702),
|
|
208
|
+
* signMessage, and viem's other ways to sign or send: signTransaction, sendTransactionSync, writeContractSync,
|
|
209
|
+
* sendCalls and sendCallsSync (every call), deployContract (its value counts toward the limits).
|
|
210
|
+
*/
|
|
207
211
|
export declare function guardWallet<W extends WalletClient>(wallet: W, options: GuardOptions): W & GuardControls;
|
|
208
212
|
export declare function checkRequestFor(method: string, args: unknown, options?: { chainId?: number; origin?: string; x402?: Record<string, unknown>; from?: string }): CheckRequest | null;
|
|
209
213
|
/**
|
package/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// presign-guard-wallet: a viem wallet for AI agents that asks presign-guard
|
|
2
2
|
// before it signs.
|
|
3
3
|
//
|
|
4
|
-
// Before every sendTransaction, writeContract and signTypedData
|
|
4
|
+
// Before every sendTransaction, writeContract and signTypedData (and viem's
|
|
5
|
+
// other ways to sign or send: signTransaction, sendTransactionSync,
|
|
6
|
+
// writeContractSync, sendCalls, sendCallsSync, deployContract), the wallet
|
|
5
7
|
// pays $0.01 (x402, USDC) for a presign-guard verdict on exactly what is about
|
|
6
8
|
// to be signed, and acts on it:
|
|
7
9
|
// green → signs
|
|
@@ -86,7 +88,7 @@ const ESCROW_ABI = parseAbi([
|
|
|
86
88
|
const TRANSFER_ABI = parseAbi(["function transfer(address to, uint256 amount)"]);
|
|
87
89
|
const VOUCHER_DOMAIN = "TIP20 Channel Reserve";
|
|
88
90
|
export const XRPL_NETWORKS = ["xrpl:0", "xrpl:1"];
|
|
89
|
-
export const VERSION = "0.
|
|
91
|
+
export const VERSION = "0.22.0";
|
|
90
92
|
export const CREDIT_HEADER = "x-credit-key";
|
|
91
93
|
const ROUTE = "POST /v1/check";
|
|
92
94
|
|
|
@@ -139,6 +141,7 @@ function senderOf(args, client) {
|
|
|
139
141
|
// What presign-guard should check for a wallet call, or null for calls it does not cover. With `from` (the
|
|
140
142
|
// sending wallet) presign-guard simulates a transaction and reports what really leaves the wallet.
|
|
141
143
|
export function checkRequestFor(method, args, { chainId, origin, x402, from } = {}) {
|
|
144
|
+
method = SAME_AS[method] ?? method;
|
|
142
145
|
const base = { chainId, ...(origin ? { origin } : {}) };
|
|
143
146
|
const sender = from ? { from } : {};
|
|
144
147
|
if (method === "sendTransaction") {
|
|
@@ -207,7 +210,13 @@ export function sessionDeposit(tx, chainId, address) {
|
|
|
207
210
|
return { token: descriptor.token.toLowerCase(), payee: descriptor.payee, deposit };
|
|
208
211
|
}
|
|
209
212
|
|
|
210
|
-
|
|
213
|
+
// viem's other ways to sign or send, each checked like the call it stands for. A signed transaction is as good as
|
|
214
|
+
// sent (anyone can broadcast it), and a deployment is checked like a transaction without a recipient: not by
|
|
215
|
+
// presign-guard, but the value it sends counts.
|
|
216
|
+
const SAME_AS = { sendTransactionSync: "sendTransaction", signTransaction: "sendTransaction", deployContract: "sendTransaction", writeContractSync: "writeContract" };
|
|
217
|
+
// EIP-5792 batches: every call in them is checked.
|
|
218
|
+
const BATCHES = new Set(["sendCalls", "sendCallsSync"]);
|
|
219
|
+
const GUARDED = new Set(["sendTransaction", "writeContract", "signTypedData", "signAuthorization", ...Object.keys(SAME_AS), ...BATCHES]);
|
|
211
220
|
|
|
212
221
|
// A signed EIP-7702 authorization, by its signature.
|
|
213
222
|
const authorizationKey = (a) => (typeof a?.r === "string" && typeof a?.s === "string" ? `${a.r}:${a.s}`.toLowerCase() : null);
|
|
@@ -373,10 +382,45 @@ export function guardWallet(wallet, {
|
|
|
373
382
|
return result;
|
|
374
383
|
}
|
|
375
384
|
|
|
385
|
+
// Several spends signed at once (a batch): every one booked before anything is signed, so each counts toward the
|
|
386
|
+
// window (perTx applies to each call on its own); all given back when one doesn't fit or signing fails.
|
|
387
|
+
async function withinLimitsAll(method, items, run) {
|
|
388
|
+
const ctx = purchases.getStore();
|
|
389
|
+
const purchase = ctx?.info ?? null;
|
|
390
|
+
const booked = [];
|
|
391
|
+
const giveBack = (error) => Promise.all(booked.map((b) => limiter.release(b.entries, { purchaseId: b.purchaseId, error }).catch((e) => console.warn(`[presign-guard-wallet] could not give back spending: ${e.message}`))));
|
|
392
|
+
for (const { request, verdict } of items) {
|
|
393
|
+
let b;
|
|
394
|
+
try {
|
|
395
|
+
b = await limiter.reserve({ method, request, verdict, purchase });
|
|
396
|
+
} catch (err) {
|
|
397
|
+
await giveBack(err.message);
|
|
398
|
+
throw new PresignBlockedError(`spending limits could not be checked (${err.message}); nothing was signed`, { code: "limit_unavailable", verdict, request });
|
|
399
|
+
}
|
|
400
|
+
if (!b.ok) {
|
|
401
|
+
await giveBack(b.summary);
|
|
402
|
+
const err = new PresignBlockedError(`not signed: ${b.summary}`, { code: b.paused ? "paused" : "over_limit", verdict, request });
|
|
403
|
+
err.reasons = b.reasons;
|
|
404
|
+
throw err;
|
|
405
|
+
}
|
|
406
|
+
booked.push({ ...b, verdict });
|
|
407
|
+
if (b.purchaseId) ctx?.purchaseIds.push(b.purchaseId);
|
|
408
|
+
}
|
|
409
|
+
let result;
|
|
410
|
+
try {
|
|
411
|
+
result = await run();
|
|
412
|
+
} catch (err) {
|
|
413
|
+
await giveBack(err.message);
|
|
414
|
+
throw err;
|
|
415
|
+
}
|
|
416
|
+
for (const b of booked) limiter.spent(b.entries, result, { verdict: b.verdict, purchase, purchaseId: b.purchaseId, approval: b.approval ?? null });
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
419
|
+
|
|
376
420
|
// Tempo: only a plain stablecoin transfer, checked here instead of by presign-guard.
|
|
377
421
|
function tempoRequest(method, args, chainId) {
|
|
378
422
|
const tokens = TEMPO_TOKENS[chainId];
|
|
379
|
-
const ok = method === "writeContract" && tokens.includes(String(args?.address ?? "").toLowerCase())
|
|
423
|
+
const ok = (SAME_AS[method] ?? method) === "writeContract" && tokens.includes(String(args?.address ?? "").toLowerCase())
|
|
380
424
|
&& TEMPO_TRANSFERS.has(args?.functionName) && !(BigInt(args?.value ?? 0n) > 0n);
|
|
381
425
|
let request = null;
|
|
382
426
|
try { request = ok ? checkRequestFor(method, args, { chainId, origin }) : null; } catch { request = null; }
|
|
@@ -422,10 +466,32 @@ export function guardWallet(wallet, {
|
|
|
422
466
|
}
|
|
423
467
|
}
|
|
424
468
|
|
|
469
|
+
// EIP-5792 batches (sendCalls, sendCallsSync): every call is checked like a transaction from the same wallet and
|
|
470
|
+
// counts toward the limits; one red or stopped call stops the whole batch, before anything is signed.
|
|
471
|
+
async function sendCallsGuarded(target, method, original, args, rest, chainId) {
|
|
472
|
+
if (TEMPO_TOKENS[chainId]) throw new PresignBlockedError(`on Tempo (chain ${chainId}) this wallet sends no batches; nothing was signed`, { code: "unsupported_chain" });
|
|
473
|
+
const calls = Array.isArray(args?.calls) ? args.calls : [];
|
|
474
|
+
if (!calls.length) throw new PresignBlockedError("a batch without calls; nothing was signed", { code: "check_failed" });
|
|
475
|
+
const from = senderOf(args, target);
|
|
476
|
+
const checked = [];
|
|
477
|
+
for (const call of calls) {
|
|
478
|
+
// A call is { to, data, value } or { to, abi, functionName, args, value }, as viem takes them.
|
|
479
|
+
const request = call?.abi
|
|
480
|
+
? checkRequestFor("writeContract", { address: call.to, abi: call.abi, functionName: call.functionName, args: call.args, value: call.value }, { chainId, origin, from })
|
|
481
|
+
: checkRequestFor("sendTransaction", call, { chainId, origin, from });
|
|
482
|
+
// A deployment in the batch: not checked, but the value it sends counts.
|
|
483
|
+
if (!request) checked.push({ request: { type: "transaction", chainId, to: null, data: call?.data ?? "0x", value: String(call?.value ?? 0n) }, verdict: null });
|
|
484
|
+
else checked.push({ request, verdict: await verdictFor(method, request, chainId) });
|
|
485
|
+
}
|
|
486
|
+
const run = () => original(args, ...rest);
|
|
487
|
+
return limiter ? withinLimitsAll(method, checked, run) : run();
|
|
488
|
+
}
|
|
489
|
+
|
|
425
490
|
async function guarded(target, method, original, args, rest) {
|
|
426
491
|
if (paused) throw new PresignBlockedError(`wallet is paused; nothing was signed`, { code: "paused" });
|
|
427
492
|
const chainId = args?.chain?.id ?? target.chain?.id;
|
|
428
493
|
const run = () => original(args, ...rest);
|
|
494
|
+
if (BATCHES.has(method)) return sendCallsGuarded(target, method, original, args, rest, chainId);
|
|
429
495
|
if (method === "signAuthorization") {
|
|
430
496
|
if (TEMPO_TOKENS[chainId]) throw new PresignBlockedError(`on Tempo (chain ${chainId}) this wallet signs no EIP-7702 authorization; nothing was signed`, { code: "unsupported_chain" });
|
|
431
497
|
return signAuthorizationGuarded(target, original, args, rest, chainId);
|
|
@@ -446,7 +512,7 @@ export function guardWallet(wallet, {
|
|
|
446
512
|
const request = checkRequestFor(method, args, { chainId, origin, x402: x402Paying.getStore(), from: senderOf(args, target) });
|
|
447
513
|
if (!request) {
|
|
448
514
|
// A contract deployment is not checked, but the value it sends still counts.
|
|
449
|
-
if (limiter && method === "sendTransaction") {
|
|
515
|
+
if (limiter && (SAME_AS[method] ?? method) === "sendTransaction") {
|
|
450
516
|
return withinLimits(method, { type: "transaction", chainId, to: null, data: args?.data ?? "0x", value: String(args?.value ?? 0n) }, null, run);
|
|
451
517
|
}
|
|
452
518
|
return run();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "presign-guard-wallet",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "A wallet for AI agents (viem on EVM chains, Solana and Algorand) that asks presign-guard ($0.01 over x402) before every transaction, contract write and signature, refuses to sign red, and keeps to per-token spending limits, with approval from your phone over the limit (Telegram, or signed in your own wallet over AC2).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|