Bytecode Checker CLI
🔍 检查链下合约代码和链上合约代码是否一致的 CLI 工具
功能特性
- ✅ 支持检查单个合约、指定网络或所有合约
- ✅ 智能识别 Constructor 参数差异和 Immutable 变量差异
- ✅ 自动移除元数据进行比较
- ✅ 生成详细的 JSON 报告
- ✅ 支持增量更新报告
一、发布 NPM 包
1. 准备发布
cd evm-checkcode-cli
npm install
npm run build
2. 配置 NPM 账户
npm login
3. 发布到 NPM
npm pack --dry-run
npm publish
npm publish --access public
4. 更新版本
npm version patch
npm version minor
npm version major
npm publish
二、使用 NPM 包
1. 安装
在你的 Hardhat 项目中安装:
npm install --save-dev bytecode-check
或全局安装:
npm install -g bytecode-check
2. 创建配置文件
在项目根目录创建 contractInfo.json 文件:
{
"eth": {
"Vault": "0x80aaf2e4636c510e067a5d300d8bafd48027addf",
"VaultCrossChainRelay": "0x060194eec4556096baaabd6bf553d2658d6a66ab"
},
"bsc": {
"Vault": "0x2cb7d2603a5f43b9fe79e98f09fe3eec40b6765d",
"VaultCrossChainRelay": "0x23ae3a565e0896866e7725fe6d49fd777359c162"
}
}
格式说明:
- 第一层 key 是网络名称(必须与
hardhat.config.js 中的网络名称一致)
- 第二层 key 是合约名称(必须与编译的合约名称一致)
- value 是合约地址
3. 配置 Hardhat
确保 hardhat.config.js 中配置了相应的网络:
module.exports = {
networks: {
eth: {
url: "https://eth-mainnet.g.alchemy.com/v2/YOUR-API-KEY",
},
bsc: {
url: "https://bsc-dataseed.binance.org/",
}
}
};
4. 编译合约
npx hardhat compile
5. 执行检查
npx bytecode-check check
npx bytecode-check check --contract Vault
npx bytecode-check check --network eth
npx bytecode-check check --config ./config/contracts.json
npx bytecode-check check --output ./reports/result.json
6. 命令选项
--contract <name> | -c | 指定要检查的合约名称 | - |
--network <name> | -n | 指定要检查的网络名称 | - |
--config <path> | - | 指定配置文件路径 | ./contractInfo.json |
--output <path> | -o | 指定输出报告文件路径 | ./bytecode-check-report.json |
三、输出示例
✓ 已加载配置文件: /path/to/contractInfo.json
配置文件: /path/to/contractInfo.json
网络数量: 2
- eth: 2 个合约
- bsc: 2 个合约
ℹ 检查所有合约...
✓ 检查完成,共 4 个合约
================================================================================
字节码检查报告
================================================================================
总计检查: 4
✓ 匹配: 4
✗ 不匹配: 0
⚠ 错误: 0
✅ 匹配的合约:
--------------------------------------------------------------------------------
Vault (eth): 完全匹配
VaultCrossChainRelay (eth): Constructor参数差异
Vault (bsc): 完全匹配
VaultCrossChainRelay (bsc): 完全匹配
✓ 报告已保存: /path/to/bytecode-check-report.json
四、报告文件格式
生成的 bytecode-check-report.json 文件格式:
{
"eth": {
"Vault": {
"contract": "Vault",
"address": "0x80aaf2e4636c510e067a5d300d8bafd48027addf",
"matches": true,
"error": null,
"reason": "完全匹配",
"onChainBytecodeLength": 12345,
"localBytecodeLength": 12345
}
},
"bsc": {
"Vault": {
"contract": "Vault",
"address": "0x2cb7d2603a5f43b9fe79e98f09fe3eec40b6765d",
"matches": true,
"error": null,
"reason": "完全匹配",
"onChainBytecodeLength": 12345,
"localBytecodeLength": 12345
}
}
}
五、常见问题
Q: 提示找不到配置文件怎么办?
A: 确保在项目根目录创建了 contractInfo.json 文件,或使用 --config 参数指定路径。
Q: 提示不是 Hardhat 项目怎么办?
A: 确保在 Hardhat 项目根目录下运行命令,并且存在 hardhat.config.js 或 hardhat.config.ts 文件。
Q: 如何处理 Constructor 参数差异?
A: Constructor 参数差异是正常的,工具会智能识别并标记为"匹配"。
许可证
MIT
作者
kiro