r/ethdev • • 10d ago

Question I built on-chain spending limits + passkey approvals for AI agents (ERC-7710 delegations, open source,feedback wanted)

I've been working on a problem I think more of us will hit soon: AI agents that can spend money.

The usual options are bad: give the agent a key (unbounded loss on the first prompt injection), or use a custodial agent wallet whose policy engine runs on someone else's servers.

cyphos-guard takes a different route, using the MetaMask Delegation Framework:

  • The owner has a Hybrid DeleGator smart account (passkey owner via RIP-7212, optional EOA recovery).
  • The agent gets one delegation per permission, with caveats: recipient allowlist, per-period caps (NativeTokenPeriodTransfer / ERC20PeriodTransfer), allowed methods, expiry.
  • ERC-20 per-tx caps can't be expressed with stock enforcers, so I wrote a small CosignerEnforcer: a policy service co-signs each redemption (EIP-712, single-use, 5-min deadline). It can only block, never move funds.
  • Over the limit → approval request → the owner signs a one-shot delegation (ExactExecution + LimitedCalls(1) + Timestamp) with a passkey on their phone.
  • For x402 APIs: an ERC-1271 "vault" is the payer and only approves EIP-3009 TransferWithAuthorization to listed sellers. Verified against the official x402 facilitator on a fork, then live on Base Sepolia.
  • A static recovery page lets the recovery EOA sign a UserOp (EIP-712) and self-submit via EntryPoint.handleOps: no bundler, no backend.

Status: testnet only, not audited; I published an internal security review (3 findings fixed). Live runs with Basescan links are in the README.

Questions I'd love input on:

  1. Anyone found a clean way to express ERC-20 per-tx caps without a co-signer?
  2. Would you trust an ERC-1271 vault as an x402 payer, or wait for ERC-7710 support in facilitators?

Links: https://github.com/Phosse136/cyphos-guard · https://cyphosguard.com · npm: @cyphos-guard/sdk

5 Upvotes

11 comments sorted by

1

u/Bloqarl 9d ago

for the per-tx cap, you can drop the co-signer with a small stateless enforcer: decode the amount from the execution calldata in beforeHook and check it against the terms, basically ERC20TransferAmountEnforcer without the running total. pin the selector to transfer and single-call mode in the same enforcer too, or an approve or a batch walks around the cap.

1

u/Mysterious-Day-9896 9d ago

Oof, good catch! That was a real gap, and it's built now. Your feedback is really valuable to me, thanks a lot.

Before: erc20 perTx was only an SDK pre-check unless the owner turned on the co-signer. An agent redeeming its delegation directly could spend the whole period budget in one transfer.

Now: ERC20PerTransferCapEnforcer, stateless, terms = token ‖ maxAmount. beforeHook requires single-call + default exec mode, target == token, value == 0, calldata exactly 68 bytes, selector == transfer, and amount <= max. ERC20PeriodTransferEnforcer already pins mode, target, selector and length, but I repeated all of it in the new enforcer as you suggested, so it stays sound on its own. The length pin also catches trailing bytes and transferFrom.

The compiler adds it to every erc20 grant with perTx and to the x402 top-ups. The co-signer is now only needed for the rules that are still off-chain (denylist, range checks on contract-call args).

Tests: Foundry unit + fuzz (batch, try mode, approve, transferFrom, same-shape selector, trailing bytes, wrong token, value), plus a Base Sepolia fork test through the deployed DelegationManager with no co-signer: exactly 2 USDC passes, 2.000001 reverts with amount-exceeded, and the daily cap still applies on top.

Deployed via CREATE2 on Base Sepolia: 0x4D5a6De281dfDC3d276A787a1fD517DBCe04936b

Design note: docs/decisions/0006-per-transfer-cap.md

Grants signed before this keep their old caveats, so the app tells those users to re-grant.

Also released in u/cyphos-guard/sdk 0.2.0 and the MCP server 0.1.2.

1

u/Mysterious-Day-9896 8d ago

Update, mostly thanks to this thread:

Per-payment cap is on-chain now. The gap someone pointed out here (an agent redeeming directly could spend the whole period budget in one transfer) is closed by a new ERC20PerTransferCapEnforcer; details in my reply to that comment and in the design note.

I attacked it on a live grant. A test agent with a stolen key ran 25 known attacks straight against the DelegationManager on Base Sepolia, skipping my SDK and service: one unit over the cap, paying itself, approve/permit/transferFrom, batch and try mode, re-delegating to itself without caveats, editing the signed caveats, forged x402 vault payments. 25/25 reverted, each with the expected reason, and a payment of exactly the cap still went through. Transactions.

The same catalog runs on a fork on every push, plus Halmos proofs and a mutation test (26 planted bugs, all caught).

If you can think of attack #26, there's an issue template for it, and a good one becomes a test.

1

u/Mysterious-Day-9896 8d ago edited 7d ago

And the first answer to "attack #26" came in. My very good friend balresch read the code, found a real gap and was kind enough to report it properly on GitHub (issue #2):

X402Vault.isValidSignature approves any agent-signed TransferWithAuthorization to a listed seller within the cap, for as long as the vault exists. It can't be switched off. Revoking a grant only called disableDelegation, which stops new top-ups but not payments from what's already in the vault. So a rest (e.g. a top-up whose settlement failed) stayed payable with the agent key after revoke, after a re-grant (each grant gets a new vault), and after the recovery page's emergency stop, which removes the passkey the app needs to withdraw.

Fix, same day:

  • revoke and re-grant add vault.withdraw(token, account, balance) for every non-empty vault in the same userOp as disableDelegation
  • the recovery page can empty vaults through the account with the recovery EOA after the stop; it only accepts contracts whose owner() is the account

Fork tests reproduce it first and failed before the fix. Severity was low: at most about one payment, and only to listed sellers. But it broke what "revoke" promises, and that matters more than the amount.

Left open on purpose: an owner-only disable() in the vault. It changes the bytecode and the CREATE2 address, so it would only help new grants, and with the sweep it would only block authorizations signed before the revoke, which expire within 10 minutes anyway.

Edit (25 Nov): that was wrong. The 10-minute limit is in the SDK, not in the vault, so a stolen agent key can pre-sign authorizations that stay valid much longer. They'd hit an empty vault after the sweep, but would work again on any money that reached that vault later. So I built disable() after all:

  • the vault has a one-way, owner-only disable(); once off, isValidSignature approves nothing, including authorizations signed earlier
  • revoke and re-grant switch the vault off and empty it in the same userOp; the recovery page does the same after the emergency stop
  • withdraw(token, to, type(uint256).max) takes the balance at execution time, so the race where a payment lands between reading the balance and withdrawing it is gone too
  • a re-grant skips any derived vault address that's already switched off

Fork tests that reproduced both gaps now pass, plus a Halmos proof and two new planted bugs in the mutation test. Vaults created before the change have no off switch: they're still emptied on revoke, and a re-grant gives the agent a new vault. Thanks again to balresch for the report.

1

u/Productiverobot 5d ago

Really clean architecture — moving the per-tx ERC-20 cap into a stateless enforcer instead of relying purely on a cosigner is a huge trust-model upgrade.

One complementary pattern worth considering specifically for your x402 API path when an agent runs tight loops (e.g., hundreds of $0.001–$0.01 tool calls):

Instead of having the agent trigger an ERC-1271 / EIP-3009 transfer authorization on every single HTTP request, your Hybrid DeleGator passkey vault can open a single pre-capped SHA-256 PayWord hash-chain channel once (locking maxSteps * unitAmount USDC against w_0 = SHA256^N(w_N) for a specific recipient + expiry), and hand the agent only the 32-byte hash chain seed — no EVM private key at all.

Why it composes nicely with ERC-7710:

  • Even under complete prompt injection or container compromise, the agent can mathematically never spend more than maxSteps * unitAmount, and strictly to the committed seller address.
  • Zero on-chain txs and zero cosigner round-trips during the session — each API call just reveals w_k (1 SHA-256 hash on the server, $0 gas), and unused USDC returns to the vault via refund() after expiry or closeAndRefund().
  • If the agent hits the cap, it goes right back to your passkey over-limit approval flow to open a fresh channel.

We just deployed an ownerless, 0%-fee reference contract for this on Base Mainnet (0x971BA814Df91c20DcA3aDd870C2B55d643965C50, Sourcify exact_match) and proposed it as a batch-settlement x402 extension (https://github.com/x402-foundation/x402/issues/4494). Could be an easy caveat-target to whitelist alongside direct EIP-3009 sellers.

1

u/[deleted] 10d ago

[removed] — view removed comment

1

u/Mysterious-Day-9896 10d ago

Thanks, good checklist. Mapping it to what's there:

Binding: the EIP-712 cosignature covers the DelegationManager, delegation hash, redeemer, keccak256 of the exact execution calldata (single-call mode only, so target + value + data), a salt and a deadline. The domain pins chain + enforcer. Replay protection is a per-DelegationManager "used digest" mapping on-chain rather than a sequential nonce, so each cosignature redeems once.

Service down or compromised: fail-closed, because no cosignature means no redemption. A compromised cosigner can only approve within the delegation's other on-chain caveats (period caps, allowlist, expiry). The passkey owner revokes via disableDelegation without the cosigner.

Recovery EOA: good nudge, added today. The static recovery page now has an emergency stop: it calls updateSigners(recovery, [], [], []), which deletes the passkey. Since the DelegationManager re-checks the delegator's ERC-1271 signature on every redemption, every passkey-signed delegation dies at once, with no delegation list and no service needed. Fork-tested: https://github.com/Phosse136/cyphos-guard/blob/main/apps/recovery/src/recovery.fork.test.ts

7212: HybridDeleGator uses P256SCLVerifierLib (precompile at 0x100, SCL fallback). On Base the precompile is live; on other chains the fallback works but costs a few hundred thousand gas per check instead of 3,450, and that hits every redemption since the signature is re-verified each time. Documented here: https://github.com/Phosse136/cyphos-guard/blob/main/docs/passkey-path.md