From 967bb7b3e6e8208d09c9eb595c4c505240d2a815 Mon Sep 17 00:00:00 2001 From: deicidus Date: Fri, 26 May 2023 01:34:26 -0700 Subject: [PATCH] revised configure-bitcoin --- spells/menu/bitcoin-menu | 6 ++-- spells/menu/configure-bitcoin | 65 +++++++++++++++++++---------------- spells/menu/uninstall-bitcoin | 0 3 files changed, 38 insertions(+), 33 deletions(-) mode change 100644 => 100755 spells/menu/uninstall-bitcoin diff --git a/spells/menu/bitcoin-menu b/spells/menu/bitcoin-menu index cf72a18..b68f26e 100755 --- a/spells/menu/bitcoin-menu +++ b/spells/menu/bitcoin-menu @@ -76,8 +76,8 @@ get_status() { display_menu() { title="${BOLD}${CYAN}Bitcoin: $(get_status)" blockchain_info="Display Blockchain Info%bitcoin-cli getblockchaininfo" - change_directory="Change Bitcoin Directory%change-bitcoin-directory" set_config="Configure Bitcoin%configure-bitcoin" + change_directory="Change Bitcoin Directory%change-bitcoin-directory" clear_cache="Clear Bitcoin Cache%# sometimes necessary when cache becomes corrupted" repair_permissions="Repair Permissions%repair-bitcoin-permissions # Sometimes necessary" exit_option="Exit%kill -2 $$" # Commands are run with 'eval' by the menu script, so we can't simply say 'exit'. The keyword $$ gets this scripts PID and the -2 code is SIGINT aka Ctrl-C @@ -86,10 +86,10 @@ display_menu() { install_bitcoin_option="Uninstall Bitcoin%uninstall-bitcoin" if is_bitcoin_running; then bitcoin_server_option="Stop Bitcoin Service%sudo systemctl stop bitcoind" - menu "$title" "$blockchain_info" "$change_directory" "$set_config" "$clear_cache" "$repair_permissions" "$bitcoin_server_option" "$install_bitcoin_option" "$exit_option" + menu "$title" "$blockchain_info" "$set_config" "$change_directory" "$clear_cache" "$repair_permissions" "$bitcoin_server_option" "$install_bitcoin_option" "$exit_option" else bitcoin_server_option="Start Bitcoin Service%sudo systemctl start bitcoind" - menu "$title" "$change_directory" "$set_config" "$clear_cache" "$repair_permissions" "$bitcoin_server_option" "$install_bitcoin_option" "$exit_option" + menu "$title" "$set_config" "$change_directory" "$clear_cache" "$repair_permissions" "$bitcoin_server_option" "$install_bitcoin_option" "$exit_option" fi else install_bitcoin_option="Install Bitcoin%install-bitcoin" diff --git a/spells/menu/configure-bitcoin b/spells/menu/configure-bitcoin index d8d1899..bca0633 100755 --- a/spells/menu/configure-bitcoin +++ b/spells/menu/configure-bitcoin @@ -12,47 +12,42 @@ retry_with_sudo() { fi } -# Create .bitcoin directory if it does not exist -mkdir -p "$HOME/.bitcoin" +# Default Bitcoin configuration bitcoin_conf="$HOME/.bitcoin/bitcoin.conf" -reset_conf="N" - -# If bitcoin.conf doesn't exist, create it. If it does exist, ask to reset it. -if [ ! -f "$bitcoin_conf" ]; then - touch "$bitcoin_conf" - reset_conf="Y" -else - printf "Would you like to reset bitcoin.conf to its default settings? (Y/n): " - read -r reset_conf_input - reset_conf=${reset_conf_input:-N} -fi +default_conf="proxy=127.0.0.1:9050\nlisten=1\nbind=127.0.0.1\ndisablewallet=1\nzmqpubrawblock=tcp://127.0.0.1:28332\nzmqpubrawtx=tcp://127.0.0.1:28333\nBTC_LOGIN" # Helper function to modify bitcoin.conf using awk modify_bitcoin_conf() { local key=$1 local value=$2 - awk -v key="$key" -v value="$value" '!/^#/ && $1==key {found=1; $3=value} 1; END {if (!found) print key"="value}' "$bitcoin_conf" > "$bitcoin_conf.tmp" && mv "$bitcoin_conf.tmp" "$bitcoin_conf" || retry_with_sudo awk -v key="$key" -v value="$value" '!/^#/ && $1==key {found=1; $3=value} 1; END {if (!found) print key"="value}' "$bitcoin_conf" | sudo tee "$bitcoin_conf" >/dev/null + awk -v key="$key" -v value="$value" 'BEGIN{FS=OFS="="} !/^#/ && $1==key {$2=value; found=1} 1; END {if (!found) print key"="value}' "$bitcoin_conf" > "$bitcoin_conf.tmp" && mv "$bitcoin_conf.tmp" "$bitcoin_conf" || retry_with_sudo awk -v key="$key" -v value="$value" 'BEGIN{FS=OFS="="} !/^#/ && $1==key {$2=value; found=1} 1; END {if (!found) print key"="value}' "$bitcoin_conf" | sudo tee "$bitcoin_conf" >/dev/null } -# Apply default settings if the file was newly created or if the user chose to reset it. -if [ "$reset_conf" = "Y" ]; then - modify_bitcoin_conf "proxy" "127.0.0.1:9050" - modify_bitcoin_conf "listen" "1" - modify_bitcoin_conf "bind" "127.0.0.1" - modify_bitcoin_conf "disablewallet" "1" - modify_bitcoin_conf "zmqpubrawblock" "tcp://127.0.0.1:28332" - modify_bitcoin_conf "zmqpubrawtx" "tcp://127.0.0.1:28333" - modify_bitcoin_conf "BTC_LOGIN" +# Check for the bitcoin.conf file +if [ ! -f "$bitcoin_conf" ]; then + echo "Creating new bitcoin.conf at the default location with default settings..." + mkdir -p "$HOME/.bitcoin" + echo -e "$default_conf" > "$bitcoin_conf" +else + printf "Found existing bitcoin.conf at $bitcoin_conf:\n\n" + cat "$bitcoin_conf" + printf "\n" + printf "Would you like to reset it to default settings? (Y/n): " + read -r reset_conf + reset_conf=${reset_conf:-Y} + if [ "$reset_conf" = "Y" ]; then + echo "Resetting bitcoin.conf to default settings..." + echo -e "$default_conf" > "$bitcoin_conf" + fi fi -# Storage amount and pruning question -printf "Wouldst thou like to operate bitcoin in pruned mode? (Y/n): " -read -r prune -prune=${prune:-N} -if [ "$prune" = "Y" ]; then - modify_bitcoin_conf "prune" "555" +# Pruning +printf "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history. (Y/n): " +read -r enable_pruning +enable_pruning=${enable_pruning:-Y} +if [ "$enable_pruning" = "Y" ]; then + modify_bitcoin_conf "prune" "1" else - echo "Creating archival node, good choice." modify_bitcoin_conf "prune" "0" fi @@ -62,6 +57,16 @@ read -r max_mem max_mem=${max_mem:-300} modify_bitcoin_conf "dbcache" "$max_mem" +# Data directory +printf "Wouldst thou like to store the blockchain in the default location ($HOME/.bitcoin)? (Y/n): " +read -r default_datadir +default_datadir=${default_datadir:-Y} +if [ "$default_datadir" = "N" ]; then + printf "Enter the path to thy desired data directory: " + read -r datadir + modify_bitcoin_conf "datadir" "$datadir" +fi + # Network selection printf "Art thou operating on the main Bitcoin network? (Y/n): " read -r mainnet diff --git a/spells/menu/uninstall-bitcoin b/spells/menu/uninstall-bitcoin old mode 100644 new mode 100755