BlazePhoenix › 工程 签名前先验证 calldata:拒绝被篡改兑换的检查 译文 · 如有冲突,以英文版本为准。 阅读英文原文 ›
签名之前,如何检查兑换的 calldata 确实按你的要求执行? 对即将签名的 calldata 运行 BlazePhoenix SDK 的 verifySwapExactIn。它会解码字节并与你的请求比对;收款地址被改或最低数额被调低,都会以 calldata_mismatch 被拒绝。
BlazePhoenix Engineering · article updated 2026-11-20 · site dataset through 2026-11-30 · 6 min · written from the deployed bytecode
概览 难度 中级 阅读时间 6 min 章节 6 更新于 2026-11-20
本页内容
1 检查比对什么 2 测试:改动一个字段 3 返回的结果 4 它在哪里自动运行,又在哪里由你运行 5 它不做什么 By Mitra (@Sigmacrit ) — anonymous developer of the BlazePhoenix protocol. The code is the résumé.
简短回答:BlazePhoenix SDK 导出的 verifySwapExactIn 会用 Router ABI 解码 swapExactIn 调用,并把每个字段与你传入的请求逐一比对。只要有任何不同,它就抛出代码为 calldata_mismatch 的 BlazeError,并列出每一处差异。它是纯函数:不联网,不用密钥。
本页说明它比对什么;在 Base 主网上的一次真实运行,其中 calldata 被编辑了两次、被拒绝了两次(10 USDC 换 AAPLc;未签名、未发送、无密钥);SDK 在哪里替你调用它;以及它不做什么。
检查比对什么 你把 calldata 和一个 SwapExpectation(即你所要求的内容)交给它。它解码 calldata 并检查以下字段,在抛出之前会收集所有不一致之处。
amountIn:编码的输入数额等于你要求的数额。 recipient:编码的收款地址等于你的地址。不区分大小写。 deadline:编码的 deadline 等于你计划中的 deadline。 userMinOut:至少为你的 minOutAtLeast,且绝不为零。 route:编码的路径把 tokenIn 连到 tokenOut。 如果你传入预览过的路径,编码的路径必须与该路径完全一致。 测试:改动一个字段 在你自己的 RPC 上构建计划,根据你自己的请求和计划构建 expectation,并检查未被改动的字节。然后编辑 plan.tx.data 中的收款地址字,再检查一次。下面的收款地址和演示地址只是占位符,不会发送任何内容。
把编码的最低数额降为 1,同样会被拒绝。每次编辑只改动 calldata 中恰好一个 32 字节的字,而这正是被篡改的前端或被调包的载荷会做的那种改动。
import { BlazePhoenix, verifySwapExactIn } from '@blazephoenix/sdk';
const USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
const AAPLc = '0xb200000000000000000000C2e324d24d7eEcd1fb';
const RECIPIENT = '0x1111111111111111111111111111111111111111'; // demo address
const blaze = new BlazePhoenix({ rpc: { base: 'YOUR_RPC' } });
const plan = await blaze.buildSwap({
chain: 'base',
tokenIn: USDC,
tokenOut: AAPLc,
amountIn: '10000000', // 10 USDC, 6 decimals
version: '1',
recipient: RECIPIENT,
from: 'YOUR_ADDRESS',
simulate: true,
slippageBps: 50,
blockNumber: 59967185n, // pin the read to one block
});
const want = {
tokenIn: USDC,
tokenOut: AAPLc,
amountIn: 10000000n,
recipient: RECIPIENT,
deadline: plan.deadline,
minOutAtLeast: plan.minOut,
};
verifySwapExactIn(plan.tx.data, want); // honest bytes: passes
// change the recipient word inside the calldata
const tampered = plan.tx.data.replace(
RECIPIENT.slice(2),
'2222222222222222222222222222222222222222',
);
try {
verifySwapExactIn(tampered, want);
} catch (e) {
console.log(e.code, e.message); // calldata_mismatch
}返回的结果 下面的输出来自已发布的 SDK 1.6.0,在 Base 主网上通过公共 RPC 运行,读取固定在区块 59967185。该计划报价 amountOut 2840251(0.02840251 AAPLc,精度为 8 位小数),minOut 为 2826050。
未被改动的 calldata 通过:检查直接返回,不抛出。 收款地址字从 0x1111111111111111111111111111111111111111 改为 0x2222222222222222222222222222222222222222。检查抛出 calldata_mismatch,附带这一行:recipient 0x2222222222222222222222222222222222222222 ≠ 0x1111111111111111111111111111111111111111。 最低数额从 2826050 改为 1。检查抛出 calldata_mismatch,附带这一行:userMinOut 1 < required 2826050。 它在哪里自动运行,又在哪里由你运行 buildSwap 只在 Quoter 2.x 路径上才会自行调用同一检查:该路径中由 Quoter 编码 calldata,SDK 再按你的请求逐字段核对。本次运行中版本是 1,那天 Base 上部署的唯一一代(请求版本 2 会返回 not_deployed, deployed: 1.0.0)。在该路径上,SDK 自己编码 calldata,计划会报告 encodedBy sdk。
因此在上面的运行中,检查是手动调用的。即使是 SDK 完成的编码,它的错误文本也以 Quoter calldata refused 开头,因为该文本是固定的。请在签名之前,对你手上的 calldata 运行它。
它不做什么 它只把 calldata 与你给出的请求比对,别无其他。如果你在 expectation 中省略 minOutAtLeast,取自计划本身的较低最低数额就会通过,所以请设置你自己的下限。它不模拟兑换,也不判断这笔兑换是否划算。
请对你即将签名的那个确切字符串运行它,并在签名之前立即运行。如果它抛出错误,就不要签名:它是失败即关闭的。想试用,运行 npm i @blazephoenix/sdk,其余工具请见 /agents。
不要轻信本页,请自行复现 上文的每一项主张都可以在链上核查。从这里开始:
Install @blazephoenix/sdk, build a plan on your own RPC, change the recipient word in plan.tx.data, then call verifySwapExactIn(data, want). Expect calldata_mismatch with a line naming the recipient. Lower the encoded minimum to 1 and expect calldata_mismatch with userMinOut 1 < required. Run it on the exact bytes you will sign.引用本文 采用 CC BY-NC-ND 4.0 许可:引用时请注明出处并附链接。商业使用和衍生版本须获得 BlazePhoenix 的书面许可;译文由 BlazePhoenix 提供。 Copy a ready-made citation:
BlazePhoenix (2026). 签名前先验证 calldata:拒绝被篡改兑换的检查. BlazePhoenix Engineering. https://blazephoenix.xyz/zh/learn/verify-the-calldata-before-you-sign/@misc{blazephoenix_verify_the_calldata_before_you_sign,
title = {签名前先验证 calldata:拒绝被篡改兑换的检查},
author = {BlazePhoenix},
year = {2026},
url = {https://blazephoenix.xyz/zh/learn/verify-the-calldata-before-you-sign/},
note = {Accessed: reproduce the claim with the command above}
}Writing an answer, a wiki entry or a paper? The claim above is reproducible against the chain before you quote it — which is the only sound basis for citing a technical source at all.
Contracts are verified on every chain we deploy to — addresses in the protocol manifest . Deeper formal treatment: the whitepaper (PDF) .
内容指纹 (SHA-256): 227030c6af4f6f1d79c634af5b423150c6fe04dd17276ca9d54e631025cb93a6 (针对英文原文,以其为准)重新计算:对文章的规范化 JSON(仅内容字段,键已排序,NFC,LF)取 SHA-256,方法见 /learn/hashes.json 。
© 2026 BlazePhoenix · CC BY-NC-ND 4.0
Questions, a partnership or a bug to report? Contact us or email contact@blazephoenix.xyz