#!/usr/bin/env bash

hostip='127.0.0.1'

# echo $hostip

# 当前mtool的工作目录
mtool_dir=`echo $PLATON_MTOOLDIR`
#echo $mtool_dir

# 验证节点配置文件路径
validator_config_path=$mtool_dir/validator/validator_config.json

# 节点目录
node_dir=~/platon-node

# 获取节点公钥
nodeid=$(cat $node_dir/data/nodeid)
if [ 0 -ne $? ]; then
    echo  -e "\033[;31mget node public key failed!!! \033[0m"
    exit 1
fi

# 获取 bls 公钥
blsPubKey=$(cat $node_dir/data/blspub)
if [ 0 -ne $? ]; then
    echo  -e "\033[;31mget bls public key failed!!! \033[0m"
    exit 1
fi

# 生成 validator_config.json
cat>$validator_config_path<<EOF
{
  "nodePublicKey": "${nodeid}",
  "blsPubKey": "${blsPubKey}",
  "nodeAddress": "http://${hostip}",
  "nodePort": "16789",
  "nodeRpcPort": "6789"
}
EOF

if [ 0 -ne $? ]; then
    echo  -e "\033[;31mgenerate validator_config.json failed!!! \033[0m"
    exit 1
fi

echo -e "\033[;32mvalidator conf success,file path:$validator_config_path\033[0m"
