2023-06-26 14:19:59 +00:00
#!/bin/sh
2024-06-05 08:00:58 +00:00
# Check Linux Distro Version - it can differ depending on the nwaku image used
OS = $( cat /etc/os-release)
if echo $OS | grep -q "Debian" ; then
echo "The operating system is Debian."
apt update
apt install -y dnsutils
apt install -y jq
elif echo $OS | grep -q "Alpine" ; then
echo "The operating system is Alpine."
apk add bind-tools
apk add jq
fi
2024-03-14 11:35:25 +00:00
if test -f .env; then
echo "Using .env file"
. $( pwd ) /.env
fi
2024-05-28 14:20:17 +00:00
IP = $( ip a | grep "inet " | grep -Fv 127.0.0.1 | sed 's/.*inet \([^/]*\).*/\1/' )
2024-03-14 11:35:25 +00:00
# Function to extract IP address from URL, resolve the IP and replace it in the original URL
get_ip_address_and_replace( ) {
local url = $1
local domain_name = $( echo $RPC_URL | awk -F[ /:] '{print $4}' )
local ip_address = $( dig +short $domain_name )
2024-06-05 08:00:58 +00:00
valid_rpc_url = " $( echo " $url " | sed " s/ $domain_name / $ip_address /g " ) "
2024-03-14 11:35:25 +00:00
echo $valid_rpc_url
}
# the format of the RPC URL is checked in the generateRlnKeystore command and hostnames are not valid
pattern = "^(https?):\/\/((localhost)|([\w_-]+(?:(?:\.[\w_-]+)+)))(:[0-9]{1,5})?([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])*"
# Perform regex matching
2024-06-05 08:00:58 +00:00
if echo " $RPC_URL " | grep -q " $pattern " ; then
2024-03-14 11:35:25 +00:00
echo "RPC URL is valid"
else
echo " RPC URL is invalid: $RPC_URL . Attempting to resolve hostname. "
resolved_rpc_url = " $( get_ip_address_and_replace $RPC_URL ) "
if [ -z " $resolved_rpc_url " ] ; then
echo -e " Failed to retrieve IP address for $RPC_URL \n "
else
echo -e " Resolved RPC URL for $RPC_URL : $resolved_rpc_url "
RPC_URL = " $resolved_rpc_url "
fi
fi
2024-10-10 06:54:04 +00:00
#Function to get the index of the container and use it to retrieve a private key to be used to generate the keystore, allowing for either dash or underscore container name format (for docker-compose backward compatibility)
2024-05-28 14:20:17 +00:00
get_private_key( ) {
# Read the JSON file
json_content = $( cat /shared/anvil-config.txt)
2024-06-05 08:00:58 +00:00
# Check if json_content has a value
if [ -z " $json_content " ] ; then
echo "Error: Failed to read the JSON file or the file is empty." >& 2
return 1
fi
2024-05-28 14:20:17 +00:00
# Extract private_keys json array using jq
private_keys = $( echo " $json_content " | jq -r '.private_keys[]' )
2024-10-10 06:54:04 +00:00
CNTR = ` dig -x $IP +short | cut -d'.' -f1`
INDEX = ` echo $CNTR | sed 's/.*[-_]\([0-9]*\)/\1/' `
2024-05-28 14:20:17 +00:00
2024-06-05 08:00:58 +00:00
if [ $? -ne 0 ] || [ -z " $INDEX " ] ; then
echo "Error: Failed to determine the replica index from IP." >& 2
return 1
fi
2024-05-28 14:20:17 +00:00
# iterate through list of private keys and get the one corresponding to the container index
# we need to iterate because array objects cannot be used in /bin/ash (Alpine) and a separate script would need to be called to use bash
current_index = 1
for key in $private_keys
do
if [ $current_index -eq $INDEX ] ; then
2024-06-05 08:00:58 +00:00
pk = $key
2024-05-28 14:20:17 +00:00
echo $key
break
fi
current_index = $(( current_index+1))
done
2024-06-05 08:00:58 +00:00
if [ -z " $pk " ] ; then
echo " Error: Failed to get private key for the container with index= $INDEX . " >& 2
return 1
fi
2024-05-28 14:20:17 +00:00
}
2024-03-14 11:35:25 +00:00
if test -f .$RLN_CREDENTIAL_PATH ; then
echo " $RLN_CREDENTIAL_PATH already exists. Use it instead of creating a new one. "
else
2024-05-28 14:20:17 +00:00
private_key = " $( get_private_key) "
echo " Private key: $private_key "
echo "Generating RLN keystore"
2024-03-14 11:35:25 +00:00
/usr/bin/wakunode generateRlnKeystore \
--rln-relay-eth-client-address= " $RPC_URL " \
2024-05-28 14:20:17 +00:00
--rln-relay-eth-private-key= $private_key \
2024-03-14 11:35:25 +00:00
--rln-relay-eth-contract-address= $RLN_CONTRACT_ADDRESS \
--rln-relay-cred-path= $RLN_CREDENTIAL_PATH \
--rln-relay-cred-password= $RLN_CREDENTIAL_PASSWORD \
2024-06-07 08:13:45 +00:00
--rln-relay-user-message-limit= $RLN_RELAY_MSG_LIMIT \
2024-06-20 13:39:43 +00:00
--rln-relay-epoch-sec= $RLN_RELAY_EPOCH_SEC \
2024-06-07 08:13:45 +00:00
--log-level= DEBUG \
2024-03-14 11:35:25 +00:00
--execute
fi
2023-06-26 14:19:59 +00:00
echo "I am a nwaku node"
RETRIES = ${ RETRIES : =10 }
while [ -z " ${ BOOTSTRAP_ENR } " ] && [ ${ RETRIES } -ge 0 ] ; do
2024-03-12 11:02:04 +00:00
BOOTSTRAP_ENR = $( wget -qO- http://bootstrap:8645/debug/v1/info --header= 'Content-Type:application/json' 2> /dev/null | sed 's/.*"enrUri":"\([^"]*\)".*/\1/' ) ;
2023-06-26 14:19:59 +00:00
echo " Bootstrap node not ready, retrying (retries left: ${ RETRIES } ) "
sleep 1
RETRIES = $(( $RETRIES - 1 ))
done
if [ -z " ${ BOOTSTRAP_ENR } " ] ; then
echo "Could not get BOOTSTRAP_ENR and none provided. Failing"
exit 1
fi
echo " Using bootstrap node: ${ BOOTSTRAP_ENR } "
exec /usr/bin/wakunode\
--relay= true\
2024-06-07 11:15:50 +00:00
--lightpush= true\
2023-07-05 10:14:03 +00:00
--max-connections= 250\
2023-10-31 15:17:35 +00:00
--rest= true\
--rest-address= 0.0.0.0\
2024-06-07 08:13:45 +00:00
--rest-port= 8645\
2023-10-31 15:17:35 +00:00
--rln-relay= true\
2024-03-14 11:35:25 +00:00
--rln-relay-dynamic= true\
--rln-relay-eth-client-address= " $RPC_URL " \
--rln-relay-eth-contract-address= $RLN_CONTRACT_ADDRESS \
--rln-relay-cred-path= $RLN_CREDENTIAL_PATH \
--rln-relay-cred-password= $RLN_CREDENTIAL_PASSWORD \
2024-06-07 08:13:45 +00:00
--rln-relay-tree-path= "rlnv2_tree1" \
--rln-relay-epoch-sec= $RLN_RELAY_EPOCH_SEC \
--rln-relay-user-message-limit= $RLN_RELAY_MSG_LIMIT \
2023-06-26 14:19:59 +00:00
--dns-discovery= true\
--discv5-discovery= true\
--discv5-enr-auto-update= True\
2024-05-28 14:20:17 +00:00
--log-level= DEBUG\
2023-06-26 14:19:59 +00:00
--metrics-server= True\
--metrics-server-address= 0.0.0.0\
--discv5-bootstrap-node= ${ BOOTSTRAP_ENR } \
2024-05-15 14:44:33 +00:00
--nat= extip:${ IP } \
--pubsub-topic= /waku/2/rs/66/0\
--cluster-id= 66