Browse Source

revised configure-bitcoin and ask_yn

main
deicidus 2 years ago
parent
commit
5130628449
  1. 30
      spells/cantrips/ask_Yn
  2. 35
      spells/cantrips/ask_yn
  3. 99
      spells/menu/configure-bitcoin

30
spells/cantrips/ask_Yn

@ -1,30 +0,0 @@
#!/usr/bin/env sh
# Define ask_Yn, which asks a yes or no question and saves the result in the given variable name (defaults to yes)
# Takes one arg, which is the variable name where the result will be saved
# No is 0 and Yes is 1
source say
ask_Yn() {
if [ -n "$2" ]; then
say_inline "$2 (Y/n): "
fi
STTY_CONFIG=$(stty -g) # Save current stty config
stty raw -echo # Disable echo
ANSWER=$(head -c 1) # Scrape terminal input
stty $STTY_CONFIG # Restore stty config
if [ "$ANSWER" != "${ANSWER#[Nn]}" ]; then
say "N"
export "${1}=0"
else
say "Y"
export "${1}=1"
fi
}
# Check if the script is being called from another script
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
# If not, call the function
ask_Yn "$@"
fi

35
spells/cantrips/ask_yn

@ -0,0 +1,35 @@
#!/bin/sh
# Define ask_yn, which asks a yes or no question and outputs the result as 1 for Yes and 0 for No.
# Takes two arguments:
# $1: The question to be asked.
# $2: The default answer, either "y" or "n".
ask_yn() {
question=$1
default_answer=$2
# Capitalize the default option correctly
if [ "$default_answer" = "n" ] || [ "$default_answer" = "N" ]; then
prompt="${question} (y/N): "
else
prompt="${question} (Y/n): "
fi
while true; do
printf "%s" "$prompt"
STTY_CONFIG=$(stty -g) # Save current stty config
stty raw -echo # Disable echo
ANSWER=$(head -c 1) # Scrape terminal input
stty $STTY_CONFIG # Restore stty config
case $ANSWER in
[Yy] ) echo "Y"; exit 1;; # Yes: exit with 1
[Nn] ) echo "N"; exit 0;; # No: exit with 0
"" ) if [ "$default_answer" = "n" ] || [ "$default_answer" = "N" ]; then echo "N"; exit 0; else echo "Y"; exit 1; fi;; # Default: exit with the default value
* ) printf "\nPlease answer Yes or No.\n";;
esac
done
}
ask_yn "$@"

99
spells/menu/configure-bitcoin

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# This script facilitates the setting of the configurations for thy Bitcoin system. # This spell facilitates the setting of the configurations for thy Bitcoin system.
# Function to check if a command needs to be run with sudo # Function to check if a command needs to be run with sudo
retry_with_sudo() { retry_with_sudo() {
@ -12,69 +12,74 @@ retry_with_sudo() {
fi fi
} }
# Default Bitcoin configuration # Source ask_yn script
bitcoin_conf="$HOME/.bitcoin/bitcoin.conf" . ask_yn
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 # Create bitcoin directory and configuration file if they do not exist
modify_bitcoin_conf() { mkdir -p "$HOME/.bitcoin"
local key=$1 touch "$HOME/.bitcoin/bitcoin.conf"
local value=$2
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 # Bitcoin defaults
} defaults="proxy=127.0.0.1:9050
listen=1
bind=127.0.0.1
disablewallet=1
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
BTC_LOGIN"
# Check for the bitcoin.conf file bitcoin_conf="$HOME/.bitcoin/bitcoin.conf"
if [ ! -f "$bitcoin_conf" ]; then
echo "Creating new bitcoin.conf at the default location with default settings..." # If the file was just created (is empty), write defaults
mkdir -p "$HOME/.bitcoin" if [ ! -s "$bitcoin_conf" ]; then
echo -e "$default_conf" > "$bitcoin_conf" echo "$defaults" > "$bitcoin_conf"
else else
printf "Found existing bitcoin.conf at $bitcoin_conf:\n\n" reset_conf=$(ask_yn "Would you like to reset the bitcoin.conf file to defaults?" "n")
cat "$bitcoin_conf" if [ $reset_conf -eq 1 ]; then
printf "\n" echo "$defaults" > "$bitcoin_conf"
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
fi fi
# Pruning # Function to modify bitcoin.conf using awk
printf "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history. (Y/n): " modify_bitcoin_conf() {
read -r enable_pruning local key=$1
enable_pruning=${enable_pruning:-Y} local value=$2
if [ "$enable_pruning" = "Y" ]; then awk -v key="$key" -v value="$value" 'BEGIN {found=0} !/^#/ && $1==key {found=1; print key"="value; next} {print} END {if (!found) print key"="value}' "$bitcoin_conf" > "$bitcoin_conf.tmp" && mv "$bitcoin_conf.tmp" "$bitcoin_conf" || retry_with_sudo mv "$bitcoin_conf.tmp" "$bitcoin_conf"
modify_bitcoin_conf "prune" "1" }
else
modify_bitcoin_conf "prune" "0"
fi
# Maximum memory # Storage amount and pruning question
printf "What is the maximum memory thou art willing to dedicate to Bitcoin in MB? (Type a number) [default: 300]: " printf "What is the maximum memory thou art willing to dedicate to Bitcoin in MB? (Type a number) [default: 300]: "
read -r max_mem read -r max_mem
max_mem=${max_mem:-300} max_mem=${max_mem:-300}
modify_bitcoin_conf "dbcache" "$max_mem" modify_bitcoin_conf "dbcache" "$max_mem"
# Data directory prune=$(ask_yn "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history." "y")
printf "Wouldst thou like to store the blockchain in the default location ($HOME/.bitcoin)? (Y/n): " if [ $prune -eq 1 ]; then
read -r default_datadir modify_bitcoin_conf "prune" "1"
default_datadir=${default_datadir:-Y} else
if [ "$default_datadir" = "N" ]; then modify_bitcoin_conf "prune" "0"
printf "Enter the path to thy desired data directory: "
read -r datadir
modify_bitcoin_conf "datadir" "$datadir"
fi fi
# Network selection # Network selection
printf "Art thou operating on the main Bitcoin network? (Y/n): " mainnet=$(ask_yn "Art thou operating on the main Bitcoin network?" "y")
read -r mainnet if [ $mainnet -eq 1 ]; then
mainnet=${mainnet:-Y}
if [ "$mainnet" = "Y" ]; then
modify_bitcoin_conf "testnet" "0" modify_bitcoin_conf "testnet" "0"
else else
modify_bitcoin_conf "testnet" "1" modify_bitcoin_conf "testnet" "1"
fi fi
echo "Thy Bitcoin configurations have been set. Fare thee well." # Blockchain storage location
default_data_dir="$HOME/.bitcoin/blocks"
modify_bitcoin_conf "datadir" "$default_data_dir"
alt_storage=$(ask_yn "Wouldst thou like to store the blockchain in the default location ($default_data_dir)?" "y")
if [ $alt_storage -eq 0 ]; then
printf "Please type the alternate path thou wouldst like to use for blockchain storage: "
read -r alt_path
modify_bitcoin_conf "datadir" "$alt_path"
fi
# Final confirmation and start
confirm=$(ask_yn "Dost thou wish to initiate Bitcoin with the specified configurations?" "y")
if [ $confirm -eq 1 ]; then
bitcoind -daemon
fi
Loading…
Cancel
Save