diff --git a/backend/scripts/init-hot-wallet.sh b/backend/scripts/init-hot-wallet.sh index fe4cdc9f..25344c52 100644 --- a/backend/scripts/init-hot-wallet.sh +++ b/backend/scripts/init-hot-wallet.sh @@ -386,37 +386,52 @@ derive_address() { ADDRESS="" - # 检查 node 是否可用 - if ! command -v node &> /dev/null; then - log_error "Node.js 未安装,无法计算地址" - echo "公钥 (hex): $PUBLIC_KEY" - return - fi + # 方法 1: 使用 node + ethers + if command -v node &> /dev/null; then + log_debug "尝试使用 Node.js + ethers 计算地址" - # 获取脚本所在目录,进入 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 " - const { computeAddress } = require('ethers'); - console.log(computeAddress('0x$PUBLIC_KEY')); - " 2>/dev/null) + try { + const { computeAddress } = require('ethers'); + const address = computeAddress('0x$PUBLIC_KEY'); + console.log(address); + } catch (e) { + // ethers 可能未安装 + process.exit(1); + } + " 2>/dev/null) || ADDRESS="" 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_error "计算地址失败" + log_warn "无法自动计算地址" + echo "" + echo "请手动从公钥计算 EVM 地址,或安装以下工具之一:" + echo " - Node.js + ethers: npm install -g ethers" + echo " - Python + eth_keys: pip install eth-keys eth-utils" + echo "" echo "公钥 (hex): $PUBLIC_KEY" fi }