An API reply is only text. Check it before a wallet signs it
A call is a target, call data and a value. How a pinned allowlist turns a tampered plan into a refusal, and what such checks still leave to a person.
An agent asks an API for a swap plan. A few hundred bytes of JSON come back, and a wallet is about to sign them. Between the API and the wallet sits a network, maybe a proxy, maybe a bug, maybe a compromised server. To the wallet, the reply is only text. This post looks at what a client can check on that text before anything is signed, and what it cannot.
A call is three things
Every call in a plan comes down to a target address, call data and a value:
{
"chainId": 8453,
"from": "0xWallet...",
"to": "0xTarget...",
"data": "0xa9059cbb000000...",
"value": "0"
}topicks the contract. If it is a contract account, its code runs.- The first four bytes of
datapick the function. They are the first four bytes of the Keccak-256 hash of the function signature.transfer(address,uint256)is0xa9059cbb;approve(address,uint256)is0x095ea7b3. valueis how much of the native coin the call sends.
What tampering looks like
A tampered reply does not need to be clever. It only needs to change one of these:
- Swap
tofor a contract the attacker controls. - Keep
to, but change the selector, for example to anapprovethat names the attacker as spender. - Swap the token being sold or bought for a look-alike.
- Move the call to a different chain, or a different
fromaddress.
None of these look wrong to a model reading a summary. Most of them look wrong to a list; the last section covers what a list does not catch.
Pin both, per chain
The defence is an allowlist shipped with the client, not fetched from the API it is checking: known contracts, known functions on each contract, and known tokens, for each chain. Before a plan reaches the model, it is matched against the list:
- Every call comes from the wallet the plan was made for.
- Every call runs on the chain the plan was made for.
- The main call goes to a pinned contract and a pinned function on it.
- Pinned tokens, such as USDC and the native and wrapped native coin, are checked against the list for that chain.
On any mismatch the whole plan becomes a refusal, with nothing to sign. Failing closed has a cost: when the service moves to a new contract, plans fail the check until the package is updated with the new address. That is the right way round. A stale list refuses a good plan; a missing list signs a bad one.
In Cleave Protocol's agent packages
Both of Cleave Protocol's agent packages run these checks: @cleave-protocol/ on npm and langchain-cleave on PyPI. Before a swap plan or a gas offer reaches the model, the package checks it against Cleave's contracts, functions and tokens, pinned per chain in the package. A plan that fails becomes an address_not_allowed refusal, with no calls and no typed data. A plan that passes keeps only its documented top-level fields.
Code that calls the API without these tools can still use the same checks. The npm package exposes screenPlanSwap and screenGetGas, and the Python package screen_plan_swap and screen_get_gas. Forward only what they return.
What the checks skip
An allowlist answers one question: is this call aimed at something known? It does not judge whether the trade is a good one. The checks do not cover:
- The price, the route or the amount received.
- The final recipient on some cross-chain routes. Where the funds go to a handler on the destination chain, a message delivered with the transfer decides who receives them, and the checks do not read that message.
- Sell tokens other than USDC and the chain's native and wrapped native coin. For a sell token outside the pinned set, verify the token contract on the planned chain. If you supplied a symbol, compare
send.tokenwith the contract address you intended. Before signing an approval, check its token contract, spender and amount, and compare the amount withpullsAtMost, the most the calls can take from the wallet. - The native messaging fee some cross-chain calls carry. The packages do not cap it independently. Review the total
valuethe wallet will send, including any native coin being sold. - Notices and other text fields, which are text from the API.
Before any wallet signs, show the user the warnings, the recipient, the amounts and the slippage, and get their confirmation. If the final recipient cannot be verified, stop before signing. A wallet that signs without a human looking should not be paired with these tools at all.
Sign after the checks pass and the user confirms the transaction details.
The checks, the refusal codes and what they leave out are on the Vercel AI SDK docs page.