Skip to content

Commit ea3c3f5

Browse files
authored
Create btc_prune_node_setup.sh
1 parent fd74a05 commit ea3c3f5

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

node-setup/btc_prune_node_setup.sh

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/bin/bash
2+
3+
# Function to generate random strings
4+
generate_random_string() {
5+
local length=$1
6+
tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$length"
7+
echo
8+
}
9+
10+
# Function to generate a random port between 30000 and 39999
11+
generate_random_port() {
12+
shuf -i 30000-39999 -n 1
13+
}
14+
15+
# Welcome message
16+
echo "Welcome to Codono Bitcoin Prune Node Setup!"
17+
echo "This script will guide you through setting up a Bitcoin node with pruning enabled."
18+
echo "It will:"
19+
echo " - Check for existing RPC configuration and reuse it if available."
20+
echo " - Install necessary software (screen, Bitcoin Core) if not already installed."
21+
echo " - Configure Bitcoin Core with a random RPC port and user credentials."
22+
echo " - Start the Bitcoin node in a screen session for resume protection."
23+
echo " - Set up supervisor to manage the Bitcoin node service."
24+
echo " - Create a default wallet and test the connection."
25+
26+
# Prompt user to continue
27+
read -p "Do you want to continue with the setup wizard? (y/n): " -n 1 -r
28+
echo
29+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
30+
echo "Setup aborted."
31+
exit 1
32+
fi
33+
34+
# Check if btc_rpc.yml exists and load existing RPC information
35+
RPC_FILE="/opt/btc_rpc.yml"
36+
if [ -f "$RPC_FILE" ]; then
37+
echo "btc_rpc.yml exists. Loading existing RPC information..."
38+
RPCUser=$(grep 'rpcuser:' "$RPC_FILE" | cut -d' ' -f2)
39+
RPCPass=$(grep 'rpcpassword:' "$RPC_FILE" | cut -d' ' -f2)
40+
RPCPort=$(grep 'rpcport:' "$RPC_FILE" | cut -d' ' -f2)
41+
ACCESS_IP=$(grep 'access_ip:' "$RPC_FILE" | cut -d' ' -f2)
42+
else
43+
# Gather Information
44+
echo "Generating RPC credentials and port..."
45+
RPCUser=$(generate_random_string 16)
46+
RPCPass=$(generate_random_string 32)
47+
RPCPort=$(generate_random_port)
48+
49+
# Ask user for ACCESS_IP
50+
read -p "Enter the ACCESS_IP (or press Enter to use 0.0.0.0): " ACCESS_IP
51+
ACCESS_IP=${ACCESS_IP:-0.0.0.0}
52+
53+
echo "RPC User: $RPCUser"
54+
echo "RPC Password: $RPCPass"
55+
echo "RPC Port: $RPCPort"
56+
echo "Access IP: $ACCESS_IP"
57+
58+
# Save RPC information to btc_rpc.yml
59+
mkdir -p /opt
60+
cat <<EOF > "$RPC_FILE"
61+
rpcuser: $RPCUser
62+
rpcpassword: $RPCPass
63+
rpcport: $RPCPort
64+
access_ip: $ACCESS_IP
65+
EOF
66+
fi
67+
68+
# Check if screen is installed, and install it if not
69+
if ! command -v screen &> /dev/null; then
70+
echo "Screen is not installed. Installing screen..."
71+
sudo apt-get update
72+
sudo apt-get install screen -y
73+
else
74+
echo "Screen is already installed."
75+
fi
76+
77+
# Check if Bitcoin Core is installed, and install it if not
78+
if ! command -v bitcoind &> /dev/null; then
79+
echo "Bitcoin Core is not installed. Installing Bitcoin Core..."
80+
cd /opt/
81+
wget https://bitcoin.org/bin/bitcoin-core-22.0/bitcoin-22.0-x86_64-linux-gnu.tar.gz
82+
tar -xvf bitcoin-22.0-x86_64-linux-gnu.tar.gz
83+
sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-22.0/bin/*
84+
else
85+
echo "Bitcoin Core is already installed."
86+
fi
87+
88+
# Create default wallet if it doesn't exist
89+
if ! bitcoin-cli listwallets | grep -q "default"; then
90+
echo "Creating default wallet..."
91+
bitcoin-cli createwallet "default"
92+
else
93+
echo "Default wallet already exists."
94+
fi
95+
96+
# Create and configure bitcoin.conf
97+
echo "Configuring bitcoin.conf..."
98+
cd /root/.bitcoin/
99+
cat <<EOF > bitcoin.conf
100+
rpcuser=$RPCUser
101+
rpcpassword=$RPCPass
102+
rpcport=$RPCPort
103+
rpctimeout=5
104+
rpcallowip=$ACCESS_IP
105+
rpcbind=0.0.0.0
106+
testnet=0
107+
server=1
108+
prune=550
109+
addresstype=p2sh-segwit
110+
wallet=default
111+
#daemon=1
112+
EOF
113+
114+
# Start bitcoind in a screen session
115+
echo "Starting bitcoind in a screen session..."
116+
screen -S bitcoin_node -d -m bitcoind -server -rpcbind=0.0.0.0 -rpcport=$RPCPort -rpcallowip=$ACCESS_IP -rpcuser=$RPCUser -rpcpassword=$RPCPass -prune=550 -addresstype=p2sh-segwit
117+
118+
# Install and configure supervisor if not already configured
119+
if [ ! -f /etc/supervisor/conf.d/bitcoin.conf ]; then
120+
echo "Installing and configuring supervisor..."
121+
sudo apt-get update
122+
sudo apt-get install supervisor -y
123+
124+
cd /etc/supervisor/conf.d/
125+
cat <<EOF > bitcoin.conf
126+
[program:bitcoin]
127+
command=/usr/local/bin/bitcoind -datadir=/root/.bitcoin -conf=/root/.bitcoin/bitcoin.conf
128+
autostart=true
129+
autorestart=true
130+
stderr_logfile=/var/log/supervisor/bitcoin.err.log
131+
stdout_logfile=/var/log/supervisor/bitcoin.out.log
132+
EOF
133+
134+
# Reload supervisor
135+
echo "Reloading supervisor..."
136+
supervisorctl reload
137+
else
138+
echo "Supervisor configuration already exists."
139+
fi
140+
141+
# Testing Connection
142+
echo "Testing connection..."
143+
curl --data-binary '{"jsonrpc":"1.0","id":"curltext","method":"getblockchaininfo","params":[]}' -H 'content-type:text/plain;' http://$RPCUser:$RPCPass@127.0.0.1:$RPCPort/
144+
145+
# Creating a wallet if it doesn't exist
146+
if ! bitcoin-cli listwallets | grep -q "my_wallet"; then
147+
echo "Creating a wallet..."
148+
bitcoin-cli createwallet "my_wallet"
149+
else
150+
echo "Wallet 'my_wallet' already exists."
151+
fi
152+
153+
# Check if wallet is connected
154+
echo "Checking wallet connection..."
155+
bitcoin-cli getwalletinfo
156+
157+
echo "Bitcoin node setup complete! You can resume the screen session with 'screen -r bitcoin_node'. also credentials are stored in /opt/btc_rpc.yml save them some where safe and delete the file."

0 commit comments

Comments
 (0)