Skip to content

Commit

Permalink
fix: Use new-address command to replace the hardcoded iota_config fil…
Browse files Browse the repository at this point in the history
…es format (#5)

* fix: Use new-address command to replace the hardcoded iota_config file format

- feat: Backup iota_config and validator key pairs

* chore: update new-address command usage
  • Loading branch information
junwei0117 authored Feb 12, 2025
1 parent c38065e commit 23c3539
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 60 deletions.
8 changes: 4 additions & 4 deletions become_candidate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ if [ $? -ne 0 ]; then
exit 1
fi

if [ ! -f "./validator.info" ]; then
echo "Error: validator.info file not found"
if [ ! -f "./key-pairs/validator.info" ]; then
echo "Error: ./key-pairs/validator.info file not found"
exit 1
fi

cat ./validator.info
cat ./key-pairs/validator.info

read -p "Are your hostname, node info, etc. correct? [y/N] " response
if [[ ! $response =~ ^[Yy]$ ]]; then
Expand All @@ -26,5 +26,5 @@ if docker run --rm -v ./iota_config:/root/.iota/iota_config $IOTA_DOCKER_IMAGE /
sleep 2

echo "Sending request to be candidate..."
docker run --rm -v ./iota_config:/root/.iota/iota_config -v ./validator.info:/iota/validator.info $IOTA_DOCKER_IMAGE /bin/sh -c "/usr/local/bin/iota validator become-candidate /iota/validator.info"
docker run --rm -v ./iota_config:/root/.iota/iota_config -v ./key-pairs/validator.info:/iota/validator.info $IOTA_DOCKER_IMAGE /bin/sh -c "/usr/local/bin/iota validator become-candidate /iota/validator.info"
fi
87 changes: 31 additions & 56 deletions generate_validator_info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,45 @@
IOTA_DOCKER_IMAGE=${IOTA_TOOLS_DOCKER_IMAGE:-"iotaledger/iota-tools:testnet"}
API_URL=${IOTA_API_ENDPOINT:-"https://api.testnet.iota.cafe"}

echo "Will using ${API_URL} for iota_config API endpopint"

./check_iota_services.sh --api ${API_URL} --docker ${IOTA_DOCKER_IMAGE}
if [ $? -ne 0 ]; then
exit 1
fi

if ! command -v jq &> /dev/null; then
echo "jq is not installed. Installing jq..."
apt update && apt install -y jq
fi

if [ -d "./key-pairs" ]; then
read -p "Key Pairs Directory already exists. This will overwrite everything? [y/N] " response
if [ -d "./iota_config" ] || [ -d "./key-pairs" ]; then
read -p "./iota_config or key-pairs Directory already exists. This will overwrite everything? [y/N] " response
if [[ ! $response =~ ^[Yy]$ ]]; then
echo "Operation cancelled"
exit 1
fi

TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_DIR="key-pairs_backup_${TIMESTAMP}"
cp -r ./key-pairs "${BACKUP_DIR}"

rm -r ./iota_config
rm -r ./key-pairs
fi

echo "Will using ${API_URL} for iota_config API endpopint"

./check_iota_services.sh --api ${API_URL} --docker ${IOTA_DOCKER_IMAGE}
if [ $? -ne 0 ]; then
exit 1
fi

mkdir -p ./key-pairs
mkdir -p ./iota_config
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
mkdir -p "./tmp/key-pairs-for-making-info_${TIMESTAMP}"

KEYGEN_OUTPUT=$(docker run --rm ${IOTA_DOCKER_IMAGE} /bin/sh -c '/usr/local/bin/iota keytool generate ed25519 --json && cat *.key')

JSON_PART=$(echo "$KEYGEN_OUTPUT" | head -n-1)

IOTA_ADDRESS=$(echo "$JSON_PART" | jq -r '.iotaAddress')
PUBLIC_KEY=$(echo "$JSON_PART" | jq -r '.publicBase64Key')
ACCOUNT_BECH32_PRIVATE_KEY=$(echo "$KEYGEN_OUTPUT" | tail -n1)

cat > ./iota_config/client.yaml << EOF
---
keystore:
File: /root/.iota/iota_config/iota.keystore
envs:
- alias: custom
rpc: "${API_URL}"
ws: ~
basic_auth: ~
active_env: custom
active_address: "${IOTA_ADDRESS}"
EOF

cat > ./iota_config/iota.aliases << EOF
[
{
"alias": "flamboyant-hematite",
"public_key_base64": "${PUBLIC_KEY}"
}
]
EOF

cat > ./iota_config/iota.keystore << EOF
[
"${ACCOUNT_BECH32_PRIVATE_KEY}"
]
EOF
mkdir -p "./tmp/backup_${TIMESTAMP}"

NEW_ADDRESS_OUTPUT=$(docker run --rm -v ./iota_config:/root/.iota/iota_config "${IOTA_DOCKER_IMAGE}" /bin/sh -c '/usr/local/bin/iota client -y new-address --json 2>&1')

IOTA_ADDRESS=$(echo "$NEW_ADDRESS_OUTPUT" | sed -n '/{/,/}/p' | jq -r '.address')


if [ -z "$IOTA_ADDRESS" ] || [ "$IOTA_ADDRESS" = "null" ]; then
echo "Error: Failed to generate IOTA address"
exit 1
fi

cp -r ./iota_config "./tmp/backup_${TIMESTAMP}/iota_config"

read -p "Enter validator name: " NAME
read -p "Enter validator description: " DESCRIPTION
Expand All @@ -78,13 +52,14 @@ read -p "Enter hostname: " HOST_NAME
IMAGE_URL=${IMAGE_URL:-""}
PROJECT_URL=${PROJECT_URL:-""}

docker run --rm -v ./iota_config:/root/.iota/iota_config -v "./tmp/key-pairs-for-making-info_${TIMESTAMP}":/iota ${IOTA_DOCKER_IMAGE} /bin/sh -c "RUST_BACKTRACE=full /usr/local/bin/iota validator make-validator-info \"$NAME\" \"$DESCRIPTION\" \"$IMAGE_URL\" \"$PROJECT_URL\" \"$HOST_NAME\" 1000"
docker run --rm -v ./iota_config:/root/.iota/iota_config -v "./key-pairs":/iota ${IOTA_DOCKER_IMAGE} /bin/sh -c "RUST_BACKTRACE=full /usr/local/bin/iota validator make-validator-info \"$NAME\" \"$DESCRIPTION\" \"$IMAGE_URL\" \"$PROJECT_URL\" \"$HOST_NAME\" 1000"

if [ ! "$(ls -A ./key-pairs)" ]; then
echo "Error: Failed to generate validator info"
exit 1
fi

cp "./tmp/key-pairs-for-making-info_${TIMESTAMP}/account.key" ./key-pairs/account.key
cp "./tmp/key-pairs-for-making-info_${TIMESTAMP}/network.key" ./key-pairs/network.key
cp "./tmp/key-pairs-for-making-info_${TIMESTAMP}/protocol.key" ./key-pairs/protocol.key
cp "./tmp/key-pairs-for-making-info_${TIMESTAMP}/authority.key" ./key-pairs/authority.key
cp "./tmp/key-pairs-for-making-info_${TIMESTAMP}/validator.info" .
cp -r ./key-pairs "./tmp/backup_${TIMESTAMP}/key-pairs"

echo -e "\nPlease share the following information in Slack:"
echo -e "\nValidator Address: ${IOTA_ADDRESS}"
Expand Down

0 comments on commit 23c3539

Please sign in to comment.