fix(scripts): 使用项目目录的 ethers 计算地址
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
578a865c4d
commit
8465f53996
|
|
@ -386,52 +386,37 @@ derive_address() {
|
|||
|
||||
ADDRESS=""
|
||||
|
||||
# 方法 1: 使用 node + ethers
|
||||
if command -v node &> /dev/null; then
|
||||
log_debug "尝试使用 Node.js + ethers 计算地址"
|
||||
# 检查 node 是否可用
|
||||
if ! command -v node &> /dev/null; then
|
||||
log_error "Node.js 未安装,无法计算地址"
|
||||
echo "公钥 (hex): $PUBLIC_KEY"
|
||||
return
|
||||
fi
|
||||
|
||||
# 获取脚本所在目录,进入 blockchain-service 目录执行(那里有 ethers)
|
||||
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
local service_dir="$script_dir/../services/blockchain-service"
|
||||
|
||||
if [ -d "$service_dir/node_modules/ethers" ]; then
|
||||
log_debug "使用 blockchain-service 目录的 ethers"
|
||||
ADDRESS=$(cd "$service_dir" && node -e "
|
||||
const { computeAddress } = require('ethers');
|
||||
console.log(computeAddress('0x$PUBLIC_KEY'));
|
||||
" 2>/dev/null)
|
||||
else
|
||||
# 尝试直接执行
|
||||
log_debug "尝试直接使用 node + ethers"
|
||||
ADDRESS=$(node -e "
|
||||
try {
|
||||
const { computeAddress } = require('ethers');
|
||||
const address = computeAddress('0x$PUBLIC_KEY');
|
||||
console.log(address);
|
||||
} catch (e) {
|
||||
// ethers 可能未安装
|
||||
process.exit(1);
|
||||
}
|
||||
" 2>/dev/null) || ADDRESS=""
|
||||
const { computeAddress } = require('ethers');
|
||||
console.log(computeAddress('0x$PUBLIC_KEY'));
|
||||
" 2>/dev/null)
|
||||
fi
|
||||
|
||||
# 方法 2: 使用 Python + web3 (备选)
|
||||
if [ -z "$ADDRESS" ] && command -v python3 &> /dev/null; then
|
||||
log_debug "尝试使用 Python + eth_keys 计算地址"
|
||||
|
||||
ADDRESS=$(python3 -c "
|
||||
try:
|
||||
from eth_keys import keys
|
||||
from eth_utils import to_checksum_address
|
||||
public_key_bytes = bytes.fromhex('$PUBLIC_KEY')
|
||||
if len(public_key_bytes) == 65 and public_key_bytes[0] == 4:
|
||||
public_key_bytes = public_key_bytes[1:]
|
||||
pk = keys.PublicKey(public_key_bytes)
|
||||
print(pk.to_checksum_address())
|
||||
except Exception as e:
|
||||
pass
|
||||
" 2>/dev/null) || ADDRESS=""
|
||||
fi
|
||||
|
||||
# 方法 3: 使用 openssl + keccak256 (更复杂,暂不实现)
|
||||
|
||||
if [ -n "$ADDRESS" ]; then
|
||||
log_success "EVM 地址派生成功"
|
||||
echo " 地址: $ADDRESS"
|
||||
else
|
||||
log_warn "无法自动计算地址"
|
||||
echo ""
|
||||
echo "请手动从公钥计算 EVM 地址,或安装以下工具之一:"
|
||||
echo " - Node.js + ethers: npm install -g ethers"
|
||||
echo " - Python + eth_keys: pip install eth-keys eth-utils"
|
||||
echo ""
|
||||
log_error "计算地址失败"
|
||||
echo "公钥 (hex): $PUBLIC_KEY"
|
||||
fi
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue