@ -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