#!/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 PGPsigner="janoside" PGPpubkeyLink="https://github.com/janoside.gpg" PGPpubkeyFingerprint="F579929B39B119CC7B0BB71FB326ACF51F317B69" 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) # check if password protected isBitcoinWalletOff=$(sudo cat /mnt/hdd/${network}/${network}.conf | grep -c "^disablewallet=1") passwordInfo="" if [ "${isBitcoinWalletOff}" != "1" ]; then passwordInfo="Login is 'admin' with your Password B" fi 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 ${passwordInfo}\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 ${passwordInfo}\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" echo "# --> /home/btcrpcexplorer/.config/btc-rpc-explorer.env" # check if electrs is installed & running if [ "${ElectRS}" == "on" ]; then # CHECK THAT ELECTRS INDEX IS BUILD (WAITLOOP) # electrs listening in port 50001 means index is build isElectrumReady=$(netstat | grep -c "50001") if [ "${isElectrumReady}" == "0" ]; then echo "# electrs is ON but not ready .. might still building index - kick systemd service into fail/wait/restart" exit 1 fi echo "# electrs is ON .. and ready (${isElectrumReady})" # CHECK THAT ELECTRS IS PART OF CONFIG echo "# updating BTCEXP_ADDRESS_API=electrumx" sed -i 's/^BTCEXP_ADDRESS_API=.*/BTCEXP_ADDRESS_API=electrumx/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env else # ELECTRS=OFF --> MAKE SURE IT IS NOT CONNECTED echo "# updating BTCEXP_ADDRESS_API=none" sed -i 's/^BTCEXP_ADDRESS_API=.*/BTCEXP_ADDRESS_API=none/g' /home/btcrpcexplorer/.config/btc-rpc-explorer.env fi # UPDATE RPC PASSWORD RPCPASSWORD=$(cat /mnt/hdd/${network}/${network}.conf | grep "^rpcpassword=" | cut -d "=" -f2) echo "# updating BTCEXP_BITCOIND_PASS=${RPCPASSWORD}" sed -i "s/^BTCEXP_BITCOIND_PASS=.*/BTCEXP_BITCOIND_PASS=${RPCPASSWORD}/g" /home/btcrpcexplorer/.config/btc-rpc-explorer.env # WALLET PROTECTION (only if Bitcoin has wallet active protect BTC-RPC-Explorer with additional passwordB) isBitcoinWalletOff=$(cat /mnt/hdd/${network}/${network}.conf | grep -c "^disablewallet=1") if [ "${isBitcoinWalletOff}" == "1" ]; then echo "# updating BTCEXP_BASIC_AUTH_PASSWORD= --> no password needed because wallet is disabled" sed -i "s/^BTCEXP_BASIC_AUTH_PASSWORD=.*/BTCEXP_BASIC_AUTH_PASSWORD=/g" /home/btcrpcexplorer/.config/btc-rpc-explorer.env else echo "# updating BTCEXP_BASIC_AUTH_PASSWORD=${RPCPASSWORD} --> enable password to protect wallet" sed -i "s/^BTCEXP_BASIC_AUTH_PASSWORD=.*/BTCEXP_BASIC_AUTH_PASSWORD=${RPCPASSWORD}/g" /home/btcrpcexplorer/.config/btc-rpc-explorer.env fi exit 0 # exit with clean code 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 /home/admin/config.scripts/blitz.git-verify.sh \ "${PGPsigner}" "${PGPpubkeyLink}" "${PGPpubkeyFingerprint}" || exit 1 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 <