
TL;DR
- Problem
- AI agents are starting to pay for APIs, data and compute. NEAR had no machine-native way to charge per request, and an agent holding a normal wallet key can be prompt-injected into draining it.
- My role
- Solo builder at a hackathon: the product idea, the protocol and contract design, the app, the guided tour and the submission.
- What I did
- Built an x402-compatible payment scheme for NEAR, an Agent Firewall in Rust (per-call cap, daily budget, payee allowlist, kill switch), an MCP server, and an app that shows every step of a paid call and lets you attack it.
- Outcome
- Live at ali-near.vercel.app. On NEAR testnet, a paid call went through and three attacks were refused by the contract, each with its own error.
The goal: let an agent spend, never let it drain
Giving an AI agent money today usually means giving it a wallet key. If the agent reads a poisoned web page or gets stuck in a loop, that key can empty the wallet. I wanted the human to set the rules once and have the chain enforce them, so even a fully jailbroken agent can only do what the policy allows.
"Let your AI agent spend. Never let it drain."
One-liner, from the Ali hackathon submission
The product problem: nobody can see a payment protocol
Ali's core is invisible: an HTTP 402 response, a contract check, a token transfer and a verification. Hackathon judges have little time per project, and many will never set up a wallet. A strong protocol that nobody understands in that time loses. So I treated understanding as the main product problem, with the same care I would give an activation flow.
Four decisions that made it safe and understandable
Give the agent a budget, not a wallet
The bet
The safest key is one that can only do one thing. NEAR already has that built in: function-call access keys scoped to one contract and one method.
What happened
The agent pays from a vault whose contract checks, in order, that the wallet is not paused, the call is under the per-call cap, the payee is on the allowlist and the spend fits today's budget. The agent's only key can call pay and nothing else. If a token transfer fails, a callback refunds the budget.
Be compatible with the standard, not a new one
The bet
Developers adopt what they already know. x402, the open standard built on HTTP 402 Payment Required, exists on other chains, so Ali should speak it rather than invent a format.
What happened
One Express middleware turns any API into a paid one, and it answers in both x402 v1 and v2. What is specific to NEAR is the scheme: the agent pays on chain first, through its firewall, and the proof is the transaction hash with a one-time nonce that the API checks.
Show the attacks, don't claim safety
The bet
"Secure" is a claim everyone makes. Watching an attack get refused is proof.
What happened
The app has three red attack cards. Fake prompt: a poisoned news answer tells the agent to pay a drainer account, refused because the payee is not allowed. Drain all: the agent tries to send its whole balance, refused by the per-call cap. Spam 30 calls: a runaway loop, stopped by the daily budget. A pause switch stops everything, and a replayed payment proof fails because each nonce works once.
A 2-minute tour with one idea per chapter
The bet
People understand a system faster by tapping through it than by reading about it. If each tap is a real request, the tour is also a live demo.
What happened
The tour has 9 short chapters, from "AI agents need to pay for things" through "Give the agent a budget, not your wallet", the three attacks and "You can stop everything". You tap a card and a live view shows the agent, the firewall and the API. Each tap runs a real request on a fresh test budget. It is the same idea as getting a new user to the aha moment as fast as possible: here, the moment the firewall says no.
Making it usable by real agents
A demo is not enough if real agents can't use it. Ali ships an MCP server, so Claude Desktop, Claude Code, Cursor or any MCP client becomes an agent with a budget. The model never holds a key: it asks the app to pay, and every payment still goes through the firewall. When a prompt injection tells the model to open a paid link that pays an attacker, the firewall refuses.
The playground has three modes so nobody is blocked: Practice (instant, shared budget), a Demo wallet that simulates the full owner flow in one click, and a real NEAR testnet wallet for people who want to sign every transaction.
Results
| Test on NEAR testnet | Result |
|---|---|
| Paid API call over x402 v2 | Went through |
| Prompt injection (pay an attacker) | Refused: payee not allowed |
| Drain the whole balance | Refused: per-call limit |
| Runaway loop | Stopped after 3 calls: daily limit |
| Call on a paused wallet | Refused: paused |
| Automated tests | 58 TypeScript, 22 Rust (9 + 13) |
One honest gap: signing in with a real MyNearWallet or Meteor account in the browser was not verified by me. The same transactions were sent by a scripted wallet.
What I learned
Technical products have an activation problem too. The protocol work took most of the hours, but the tour is what makes the work legible to a judge or a developer in two minutes. One idea per chapter and a real request behind every tap turned out to be a good rule for explaining anything complex.
I also learned that the best safety design reuses what the platform already gives you. NEAR's scoped keys and named accounts did more for safety and clarity than any custom layer I could have added in a hackathon.
Hiring a growth PM who ships? I take products from a one-line pitch to live, with data at every step.
Get in touch