-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbtc_prune_node_setup.sh
More file actions
141 lines (122 loc) · 4.23 KB
/
Copy pathbtc_prune_node_setup.sh
File metadata and controls
141 lines (122 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Function to generate random strings
generate_random_string() {
local length=$1
tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$length"
echo
}
# Function to generate a random port between 30000 and 39999
generate_random_port() {
shuf -i 30000-39999 -n 1
}
# Welcome message
echo "Welcome to Codono Bitcoin Prune Node Setup!"
echo "This script will guide you through setting up a Bitcoin node with pruning enabled."
read -p "Do you want to continue with the setup wizard? (y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Setup aborted."
exit 1
fi
# Load or generate RPC credentials
RPC_FILE="/opt/btc_rpc.yml"
if [ -f "$RPC_FILE" ]; then
echo "Loading existing RPC information..."
RPCUser=$(grep 'rpcuser:' "$RPC_FILE" | cut -d' ' -f2)
RPCPass=$(grep 'rpcpassword:' "$RPC_FILE" | cut -d' ' -f2)
RPCPort=$(grep 'rpcport:' "$RPC_FILE" | cut -d' ' -f2)
ACCESS_IP=$(grep 'access_ip:' "$RPC_FILE" | cut -d' ' -f2)
else
echo "Generating new RPC credentials..."
RPCUser=$(generate_random_string 16)
RPCPass=$(generate_random_string 32)
RPCPort=$(generate_random_port)
read -p "Enter the ACCESS_IP (default 0.0.0.0): " ACCESS_IP
ACCESS_IP=${ACCESS_IP:-0.0.0.0}
mkdir -p /opt
cat <<EOF > "$RPC_FILE"
rpcuser: $RPCUser
rpcpassword: $RPCPass
rpcport: $RPCPort
access_ip: $ACCESS_IP
EOF
fi
# Install dependencies
if ! command -v screen &> /dev/null; then
echo "Installing screen..."
sudo apt-get update
sudo apt-get install screen -y
fi
if ! command -v bitcoind &> /dev/null; then
echo "Installing Bitcoin Core..."
cd /opt/
wget https://bitcoin.org/bin/bitcoin-core-22.0/bitcoin-22.0-x86_64-linux-gnu.tar.gz
tar -xvf bitcoin-22.0-x86_64-linux-gnu.tar.gz
sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-22.0/bin/*
fi
# Start temporary bitcoind to create wallet
mkdir -p /root/.bitcoin
echo "Starting temporary bitcoind to create 'default' wallet..."
bitcoind -daemon -conf=/dev/null -rpcuser=$RPCUser -rpcpassword=$RPCPass -rpcport=$RPCPort -server=1 -datadir=/root/.bitcoin
# Wait for bitcoind to respond
TRIES=0
echo "Waiting for bitcoind to become ready..."
until bitcoin-cli -rpcuser=$RPCUser -rpcpassword=$RPCPass -rpcport=$RPCPort getblockchaininfo > /dev/null 2>&1 || [ $TRIES -ge 20 ]; do
sleep 3
echo "Attempt $((TRIES + 1))..."
TRIES=$((TRIES+1))
done
if [ $TRIES -ge 20 ]; then
echo "bitcoind did not start successfully. Check logs."
exit 1
fi
# Create the default wallet if it doesn't exist
if ! bitcoin-cli -rpcuser=$RPCUser -rpcpassword=$RPCPass -rpcport=$RPCPort listwallets | grep -q "default"; then
echo "Creating 'default' wallet..."
bitcoin-cli -rpcuser=$RPCUser -rpcpassword=$RPCPass -rpcport=$RPCPort createwallet "default"
else
echo "'default' wallet already exists."
fi
# Stop temporary bitcoind to reconfigure with prune mode
echo "Stopping temporary bitcoind..."
bitcoin-cli -rpcuser=$RPCUser -rpcpassword=$RPCPass -rpcport=$RPCPort stop
sleep 10
# Create bitcoin.conf with default wallet auto-loaded
echo "Creating bitcoin.conf..."
mkdir -p /root/.bitcoin
cat <<EOF > /root/.bitcoin/bitcoin.conf
rpcuser=$RPCUser
rpcpassword=$RPCPass
rpcport=$RPCPort
rpctimeout=5
rpcallowip=$ACCESS_IP
rpcbind=0.0.0.0
testnet=0
server=1
prune=550
addresstype=p2sh-segwit
wallet=default
EOF
# Start bitcoind in screen session
echo "Starting bitcoind in screen session with pruning and 'default' wallet..."
screen -S bitcoin_node -d -m bitcoind -conf=/root/.bitcoin/bitcoin.conf
# Setup Supervisor if needed
if [ ! -f /etc/supervisor/conf.d/bitcoin.conf ]; then
echo "Installing Supervisor..."
sudo apt-get update
sudo apt-get install supervisor -y
cat <<EOF > /etc/supervisor/conf.d/bitcoin.conf
[program:bitcoin]
command=/usr/local/bin/bitcoind -datadir=/root/.bitcoin -conf=/root/.bitcoin/bitcoin.conf
autostart=true
autorestart=true
stderr_logfile=/var/log/supervisor/bitcoin.err.log
stdout_logfile=/var/log/supervisor/bitcoin.out.log
EOF
echo "Reloading Supervisor..."
supervisorctl reload
fi
echo
echo "✅ Bitcoin node setup complete!"
echo "👉 You can reattach to the screen session with: screen -r bitcoin_node"
echo "🔐 Credentials saved at /opt/btc_rpc.yml — keep it safe and delete when done."