#!/bin/bash # https://github.com/janoside/btc-rpc-explorer # ~/.config/btc-rpc-explorer.env # https://github.com/janoside/btc-rpc-explorer/blob/master/.env-sample # command info if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then echo "# small config script to switch BTC-RPC-explorer on or off" echo "# bonus.btc-rpc-explorer.sh [status|on|off]" echo "# bonus.btc-rpc-explorer.sh prestart" exit 1 fi source /mnt/hdd/raspiblitz.conf ########################## # MENU ######################### # show info menu if [ "$1" = "menu" ]; then # get status echo "# collecting status info ... (please wait)" source <(sudo /home/admin/config.scripts/bonus.btc-rpc-explorer.sh status) # check if index is ready if [ "${isIndexed}" == "0" ]; then dialog --title " Blockchain Index Not Ready " --msgbox " The Blockchain Index is still getting built. ${indexInfo} This can take multiple hours. " 9 48 exit 0 fi # get network info localip=$(ip addr | grep 'state UP' -A2 | grep -E -v 'docker0|veth' | grep 'eth0\|wlan0\|enp0' | tail -n1 | awk '{print $2}' | cut -f1 -d'/') toraddress=$(sudo cat /mnt/hdd/tor/btc-rpc-explorer/hostname 2>/dev/null) fingerprint=$(openssl x509 -in /mnt/hdd/app-data/nginx/tls.cert -fingerprint -noout | cut -d"=" -f2) if [ "${runBehindTor}" = "on" ] && [ ${#toraddress} -gt 0 ]; then # TOR /home/admin/config.scripts/blitz.display.sh qr "${toraddress}" whiptail --title " BTC-RPC-Explorer " --msgbox "Open in your local web browser: http://${localip}:3020\n https://${localip}:3021 with Fingerprint: ${fingerprint}\n Login is 'admin' with your Password B\n Hidden Service address for TOR Browser (QR see LCD): ${toraddress} " 16 67 /home/admin/config.scripts/blitz.display.sh hide else # IP + Domain whiptail --title " BTC-RPC-Explorer " --msgbox "Open in your local web browser: http://${localip}:3020\n https://${localip}:3021 with Fingerprint: ${fingerprint}\n Login is 'admin' with your Password B\n Activate TOR to access the web block explorer from outside your local network. " 16 54 fi echo "please wait ..." exit 0 fi # add default value to raspi config if needed if ! grep -Eq "^BTCRPCexplorer=" /mnt/hdd/raspiblitz.conf; then echo "BTCRPCexplorer=off" >> /mnt/hdd/raspiblitz.conf fi # status if [ "$1" = "status" ]; then if [ "${BTCRPCexplorer}" = "on" ]; then echo "configured=1" # check indexing source <(sudo /home/admin/config.scripts/network.txindex.sh status) echo "isIndexed=${isIndexed}" echo "indexInfo='${indexInfo}'" # check for error isDead=$(sudo systemctl status btc-rpc-explorer | grep -c 'inactive (dead)') if [ ${isDead} -eq 1 ]; then echo "error='Service Failed'" exit 1 fi else echo "configured=0" fi exit 0 fi ########################## # PRESTART # - will be called as prestart by systemd service (as user btcrpcexplorer) ######################### if [ "$1" = "prestart" ]; then # users need to be `btcrpcexplorer` so that it can be run by systemd as prestart (no SUDO available) if [ "$USER" != "btcrpcexplorer" ]; then echo "# FAIL: run as user btcrpcexplorer" exit 1 fi echo "## btc-rpc-explorer.service PRESTART CONFIG" # UPDATE GENERAL CONFIG VALUES echo "# --> /home/btcrpcexplorer/.config/btc-rpc-explorer.env" RPCPASSWORD=$(cat /mnt/hdd/${network}/${network}.conf | grep "^rpcpassword=" | cut -d "=" -f2) echo "# updating BTCEXP_BITCOIND_PASS=${RPCPASSWORD}" sudo sed -i "s/^BTCEXP_BITCOIND_PASS=.*/BTCEXP_BITCOIND_PASS=${RPCPASSWORD}/g" /home/btcrpcexplorer/.config/btc-rpc-explorer.env # check if electrs is installed & running if [ "${ElectRS}" == "on" ]; then # CHECK THAT ELECTRS IS PART OF CONFIG echo "# TODO: check electrs config ..." # CHECK THAT ELECTRS INDEX IS BUILD # electrs listening in port 50001 means index is build echo "# electrs is on .. checking if index is build" echo "# waiting electrs on port 50001 (= index is ready)" while [ $(netstat | grep -c "50001") -eq 0 ]; do sleep 1 done echo "# electrs started, launching BTC-RPC-Explorer ..." fi exit 0 fi # stop service (for all calls below) echo "# making sure services are not running" sudo systemctl stop btc-rpc-explorer 2>/dev/null ########################## # ON ######################### # switch on if [ "$1" = "1" ] || [ "$1" = "on" ]; then echo "# *** INSTALL BTC-RPC-EXPLORER ***" isInstalled=$(sudo ls /etc/systemd/system/btc-rpc-explorer.service 2>/dev/null | grep -c 'btc-rpc-explorer.service') if [ ${isInstalled} -eq 0 ]; then # install nodeJS /home/admin/config.scripts/bonus.nodejs.sh on # make sure that txindex of blockchain is switched on /home/admin/config.scripts/network.txindex.sh on # add btcrpcexplorer user sudo adduser --disabled-password --gecos "" btcrpcexplorer # install btc-rpc-explorer cd /home/btcrpcexplorer sudo -u btcrpcexplorer git clone https://github.com/janoside/btc-rpc-explorer.git cd btc-rpc-explorer sudo -u btcrpcexplorer git reset --hard v3.2.0 sudo -u btcrpcexplorer npm install if ! [ $? -eq 0 ]; then echo "FAIL - npm install did not run correctly, aborting" exit 1 fi # prepare .env file echo "# getting RPC credentials from the ${network}.conf" RPC_USER=$(sudo cat /mnt/hdd/${network}/${network}.conf | grep rpcuser | cut -c 9-) PASSWORD_B=$(sudo cat /mnt/hdd/${network}/${network}.conf | grep rpcpassword | cut -c 13-) touch /home/admin/btc-rpc-explorer.env sudo chmod 600 /home/admin/btc-rpc-explorer.env || exit 1 cat > /home/admin/btc-rpc-explorer.env < /home/admin/btc-rpc-explorer.service <