fix(scripts): 修改热钱包初始化脚本直接调用mpc-system

- 改为直接调用 mpc-system account-service (端口 4000)
- 使用 snake_case API 格式 (threshold_n, threshold_t, require_delegate)
- 系统热钱包设置 require_delegate: false (不需要用户持有share)
- 更新状态查询路径为 /api/v1/mpc/sessions/{sessionId}

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-15 08:44:45 -08:00
parent 9531405c9b
commit fce8a919ff
1 changed files with 31 additions and 27 deletions

View File

@ -6,8 +6,8 @@
# 用途: 创建系统热钱包的 MPC 密钥,用于提现转账 # 用途: 创建系统热钱包的 MPC 密钥,用于提现转账
# #
# 前提条件: # 前提条件:
# 1. mpc-service 正在运行 (默认端口 3006) # 1. mpc-system account-service 正在运行 (默认端口 4000)
# 2. mpc-system 正在运行且所有 party 已启动 # 2. mpc-system session-coordinator 和 server-party 已启动
# 3. jq 已安装 (用于解析 JSON) # 3. jq 已安装 (用于解析 JSON)
# 4. curl 已安装 # 4. curl 已安装
# #
@ -18,13 +18,13 @@
# -u, --username 热钱包用户名 (默认: system-hot-wallet) # -u, --username 热钱包用户名 (默认: system-hot-wallet)
# -n, --threshold-n 总 party 数量 (默认: 3) # -n, --threshold-n 总 party 数量 (默认: 3)
# -t, --threshold-t 签名门限值 (默认: 2) # -t, --threshold-t 签名门限值 (默认: 2)
# -h, --host mpc-service 地址 (默认: http://localhost:3006) # -h, --host mpc-system account-service 地址 (默认: http://localhost:4000)
# -v, --verbose 显示详细调试信息 # -v, --verbose 显示详细调试信息
# --help 显示帮助 # --help 显示帮助
# #
# 示例: # 示例:
# ./init-hot-wallet.sh # ./init-hot-wallet.sh
# ./init-hot-wallet.sh -u my-hot-wallet -h http://192.168.1.111:3013 # ./init-hot-wallet.sh -u my-hot-wallet -h http://192.168.1.111:4000
# ./init-hot-wallet.sh --username prod-hot-wallet --threshold-n 3 --threshold-t 2 # ./init-hot-wallet.sh --username prod-hot-wallet --threshold-n 3 --threshold-t 2
# #
# ============================================================================= # =============================================================================
@ -43,7 +43,7 @@ NC='\033[0m' # No Color
USERNAME="system-hot-wallet" USERNAME="system-hot-wallet"
THRESHOLD_N=3 THRESHOLD_N=3
THRESHOLD_T=2 THRESHOLD_T=2
MPC_HOST="http://localhost:3006" MPC_HOST="http://localhost:4000"
VERBOSE=false VERBOSE=false
# 日志函数 # 日志函数
@ -99,17 +99,17 @@ check_dependencies() {
# 检查服务连通性 # 检查服务连通性
check_service() { check_service() {
log_info "检查 MPC 服务连通性..." log_info "检查 MPC account-service 连通性..."
local health_url="$MPC_HOST/api/v1/health" local health_url="$MPC_HOST/health"
log_debug "请求: GET $health_url" log_debug "请求: GET $health_url"
local response local response
response=$(curl -s -w "\n%{http_code}" --connect-timeout 5 "$health_url" 2>/dev/null) || { response=$(curl -s -w "\n%{http_code}" --connect-timeout 5 "$health_url" 2>/dev/null) || {
log_error "无法连接到 MPC 服务: $MPC_HOST" log_error "无法连接到 MPC account-service: $MPC_HOST"
echo "" echo ""
echo "请检查:" echo "请检查:"
echo " 1. mpc-service 是否正在运行" echo " 1. mpc-system account-service 是否正在运行 (端口 4000)"
echo " 2. 地址和端口是否正确" echo " 2. 地址和端口是否正确"
echo " 3. 网络是否可达" echo " 3. 网络是否可达"
exit 1 exit 1
@ -122,11 +122,11 @@ check_service() {
log_debug "响应内容: $body" log_debug "响应内容: $body"
if [ "$http_code" != "200" ]; then if [ "$http_code" != "200" ]; then
log_error "MPC 服务响应异常 (HTTP $http_code)" log_error "MPC account-service 响应异常 (HTTP $http_code)"
exit 1 exit 1
fi fi
log_success "MPC 服务连接正常" log_success "MPC account-service 连接正常"
} }
# 解析参数 # 解析参数
@ -184,15 +184,17 @@ validate_params() {
} }
# 创建 Keygen 会话 # 创建 Keygen 会话
# 直接调用 mpc-system account-service API (使用 snake_case)
create_keygen_session() { create_keygen_session() {
log_info "创建 Keygen 会话..." log_info "创建 Keygen 会话..."
local url="$MPC_HOST/api/v1/mpc/keygen" local url="$MPC_HOST/api/v1/mpc/keygen"
# mpc-system 使用 snake_case: threshold_n, threshold_t, require_delegate
local payload="{ local payload="{
\"username\": \"$USERNAME\", \"username\": \"$USERNAME\",
\"thresholdN\": $THRESHOLD_N, \"threshold_n\": $THRESHOLD_N,
\"thresholdT\": $THRESHOLD_T, \"threshold_t\": $THRESHOLD_T,
\"requireDelegate\": true \"require_delegate\": false
}" }"
log_debug "请求: POST $url" log_debug "请求: POST $url"
@ -214,17 +216,16 @@ create_keygen_session() {
fi fi
# 检查是否有错误 # 检查是否有错误
local error_msg=$(echo "$KEYGEN_RESPONSE" | jq -r '.message // .error // empty') local error_msg=$(echo "$KEYGEN_RESPONSE" | jq -r '.error // empty')
if [ -n "$error_msg" ] && [ "$error_msg" != "null" ]; then if [ -n "$error_msg" ] && [ "$error_msg" != "null" ]; then
local status_code=$(echo "$KEYGEN_RESPONSE" | jq -r '.statusCode // empty') log_error "API 错误: $error_msg"
if [ -n "$status_code" ] && [ "$status_code" != "200" ] && [ "$status_code" != "201" ]; then exit 1
log_error "API 错误: $error_msg"
exit 1
fi
fi fi
SESSION_ID=$(echo "$KEYGEN_RESPONSE" | jq -r '.sessionId') # mpc-system 返回 snake_case: session_id
SESSION_ID=$(echo "$KEYGEN_RESPONSE" | jq -r '.session_id')
local status=$(echo "$KEYGEN_RESPONSE" | jq -r '.status') local status=$(echo "$KEYGEN_RESPONSE" | jq -r '.status')
local selected_parties=$(echo "$KEYGEN_RESPONSE" | jq -r '.selected_parties | join(", ")')
if [ "$SESSION_ID" == "null" ] || [ -z "$SESSION_ID" ]; then if [ "$SESSION_ID" == "null" ] || [ -z "$SESSION_ID" ]; then
log_error "创建 Keygen 会话失败" log_error "创建 Keygen 会话失败"
@ -233,8 +234,9 @@ create_keygen_session() {
fi fi
log_success "会话已创建" log_success "会话已创建"
echo " 会话 ID: $SESSION_ID" echo " 会话 ID: $SESSION_ID"
echo " 状态: $status" echo " 状态: $status"
echo " 选中 Party: $selected_parties"
} }
# 等待 Keygen 完成 # 等待 Keygen 完成
@ -249,13 +251,15 @@ wait_for_keygen() {
local spinner_idx=0 local spinner_idx=0
while [ $attempt -lt $max_attempts ]; do while [ $attempt -lt $max_attempts ]; do
local url="$MPC_HOST/api/v1/mpc/keygen/$SESSION_ID/status" # mpc-system 状态查询 API: /api/v1/mpc/sessions/{sessionId}
local url="$MPC_HOST/api/v1/mpc/sessions/$SESSION_ID"
STATUS_RESPONSE=$(curl -s -X GET "$url" --connect-timeout 10) STATUS_RESPONSE=$(curl -s -X GET "$url" --connect-timeout 10)
log_debug "轮询响应: $STATUS_RESPONSE" log_debug "轮询响应: $STATUS_RESPONSE"
STATUS=$(echo "$STATUS_RESPONSE" | jq -r '.status') STATUS=$(echo "$STATUS_RESPONSE" | jq -r '.status')
PUBLIC_KEY=$(echo "$STATUS_RESPONSE" | jq -r '.publicKey // empty') # mpc-system 返回 snake_case: public_key
PUBLIC_KEY=$(echo "$STATUS_RESPONSE" | jq -r '.public_key // empty')
# 只在状态变化时输出 # 只在状态变化时输出
if [ "$STATUS" != "$last_status" ]; then if [ "$STATUS" != "$last_status" ]; then
@ -304,9 +308,9 @@ get_public_key() {
if [ -z "$PUBLIC_KEY" ] || [ "$PUBLIC_KEY" == "null" ]; then if [ -z "$PUBLIC_KEY" ] || [ "$PUBLIC_KEY" == "null" ]; then
# 再次获取状态以确保拿到公钥 # 再次获取状态以确保拿到公钥
local url="$MPC_HOST/api/v1/mpc/keygen/$SESSION_ID/status" local url="$MPC_HOST/api/v1/mpc/sessions/$SESSION_ID"
STATUS_RESPONSE=$(curl -s -X GET "$url") STATUS_RESPONSE=$(curl -s -X GET "$url")
PUBLIC_KEY=$(echo "$STATUS_RESPONSE" | jq -r '.publicKey') PUBLIC_KEY=$(echo "$STATUS_RESPONSE" | jq -r '.public_key')
fi fi
if [ -z "$PUBLIC_KEY" ] || [ "$PUBLIC_KEY" == "null" ]; then if [ -z "$PUBLIC_KEY" ] || [ "$PUBLIC_KEY" == "null" ]; then