Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·263 lines (219 loc) · 8.66 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·263 lines (219 loc) · 8.66 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
#
# Bitcoin Core Node Setup Script
# This script helps you configure your Bitcoin node setup
# Based on RaspiBolt guide: https://raspibolt.org/guide/bitcoin/bitcoin-client.html#generate-access-credentials
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "=========================================="
echo "Bitcoin Core Docker Setup"
echo "=========================================="
echo ""
# Check if rpcauth.py exists
if [ ! -f "rpcauth.py" ]; then
echo -e "${RED}Error: rpcauth.py not found!${NC}"
echo "This script is required from Bitcoin Core."
exit 1
fi
# Make rpcauth.py executable
chmod +x rpcauth.py
# Check if .env already exists
if [ -f .env ]; then
read -p ".env file already exists. Do you want to overwrite it? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Setup cancelled. Using existing .env file."
exit 0
fi
fi
# Copy .env.template to .env
cp .env.template .env
echo "✓ Created .env file from template"
echo ""
# Prompt for external drive path
echo "Step 1: Configure External Drive Path"
echo "--------------------------------------"
read -p "Enter the full path to your external drive Bitcoin data directory (e.g., /mnt/external-drive/bitcoin-data): " DATA_PATH
if [ -z "$DATA_PATH" ]; then
echo "Error: Path cannot be empty"
exit 1
fi
# Update BITCOIN_DATA_PATH in .env
sed -i "s|BITCOIN_DATA_PATH=.*|BITCOIN_DATA_PATH=${DATA_PATH}|" .env
PARENT_DIR=$(dirname "${DATA_PATH}")
sed -i "s|MEMPOOL_DATA_PATH=.*|MEMPOOL_DATA_PATH=${PARENT_DIR}/mempool|" .env
echo "✓ Set data path to: ${DATA_PATH}"
echo "✓ Set mempool data path to: ${PARENT_DIR}/mempool"
echo ""
# Create directory if it doesn't exist
if [ ! -d "$DATA_PATH" ]; then
read -p "Directory doesn't exist. Do you want to create it? (Y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
mkdir -p "$DATA_PATH"
echo "✓ Created directory: ${DATA_PATH}"
# Set permissions
USER_ID=$(id -u)
GROUP_ID=$(id -g)
chown -R ${USER_ID}:${GROUP_ID} "$DATA_PATH" 2>/dev/null || {
echo "⚠ Could not set permissions automatically. You may need to run:"
echo " sudo chown -R ${USER_ID}:${GROUP_ID} ${DATA_PATH}"
}
fi
fi
mkdir -p "${DATA_PATH}/tor"
echo ""
# Network selection
echo "Step 2: Select Network"
echo "--------------------------------------"
echo "1) Mainnet (default)"
echo "2) Testnet"
echo "3) Regtest"
read -p "Select network (1-3, default: 1): " NETWORK_CHOICE
case $NETWORK_CHOICE in
2)
sed -i 's/RPC_PORT=.*/RPC_PORT=18332/' .env
sed -i 's/P2P_PORT=.*/P2P_PORT=18333/' .env
echo "✓ Configured for Testnet"
echo "⚠ Remember to uncomment 'testnet=1' in bitcoin.conf"
;;
3)
sed -i 's/RPC_PORT=.*/RPC_PORT=18443/' .env
sed -i 's/P2P_PORT=.*/P2P_PORT=18444/' .env
echo "✓ Configured for Regtest"
echo "⚠ Remember to uncomment 'regtest=1' in bitcoin.conf"
;;
*)
echo "✓ Configured for Mainnet"
;;
esac
echo ""
# Generate RPC credentials using Bitcoin Core's official rpcauth.py
echo "Step 3: Configure RPC Authentication"
echo "--------------------------------------"
echo "Generating secure RPC credentials using Bitcoin Core's rpcauth.py..."
echo ""
RPC_USER="bitcoin"
# Use rpcauth.py with --json flag to get structured output
# Pass empty password argument to auto-generate a secure password
RPC_OUTPUT=$(python3 rpcauth.py "${RPC_USER}" --json)
# Parse JSON output
RPC_PASSWORD=$(echo "${RPC_OUTPUT}" | python3 -c "import sys, json; print(json.load(sys.stdin)['password'])")
RPC_AUTH_LINE=$(echo "${RPC_OUTPUT}" | python3 -c "import sys, json; print(json.load(sys.stdin)['rpcauth'])")
echo "✓ Generated secure RPC credentials using official Bitcoin Core rpcauth.py"
echo ""
# Update RPC credentials in .env (replace existing placeholders)
sed -i "s|BITCOIN_RPC_USER=.*|BITCOIN_RPC_USER=${RPC_USER}|" .env
sed -i "s|BITCOIN_RPC_PASSWORD=.*|BITCOIN_RPC_PASSWORD=${RPC_PASSWORD}|" .env
echo "✓ Updated RPC credentials in .env file"
echo ""
# Create bitcoin.conf in the data directory
echo "Step 4: Creating bitcoin.conf"
echo "--------------------------------------"
BITCOIN_CONF="${DATA_PATH}/bitcoin.conf"
# Copy the template bitcoin.conf from the repo
cp bitcoin.conf "${BITCOIN_CONF}"
# Replace the placeholder rpcauth line with the generated one
sed -i "s|rpcauth=bitcoin:SETUP_SCRIPT_WILL_GENERATE_THIS|rpcauth=${RPC_AUTH_LINE}|" "${BITCOIN_CONF}"
chmod 600 "${BITCOIN_CONF}"
echo "✓ Created bitcoin.conf at: ${BITCOIN_CONF}"
echo ""
# Create electrs.conf in the electrs data directory
echo "Step 5: Creating electrs.conf"
echo "--------------------------------------"
# Get ELECTRS_DATA_PATH from .env file
ELECTRS_DATA_PATH=$(grep "^ELECTRS_DATA_PATH=" .env | cut -d'=' -f2)
if [ -z "$ELECTRS_DATA_PATH" ]; then
echo -e "${YELLOW}Warning: ELECTRS_DATA_PATH not found in .env file${NC}"
echo "Skipping electrs.conf creation"
else
ELECTRS_CONF="${ELECTRS_DATA_PATH}/electrs.toml"
# Create electrs data directory if it doesn't exist
if [ ! -d "$ELECTRS_DATA_PATH" ]; then
mkdir -p "$ELECTRS_DATA_PATH"
echo "✓ Created electrs data directory: ${ELECTRS_DATA_PATH}"
fi
mkdir -p "${ELECTRS_DATA_PATH}/tor/electrs_hidden_service"
# Copy the template electrs.toml from the repo
cp electrs/electrs.toml "${ELECTRS_CONF}"
chmod 600 "${ELECTRS_CONF}"
echo "✓ Created electrs.toml at: ${ELECTRS_CONF}"
fi
echo ""
# Create mempool data directories and DB credentials
echo "Step 6: Configuring Mempool"
echo "--------------------------------------"
MEMPOOL_DATA_PATH=$(grep "^MEMPOOL_DATA_PATH=" .env | cut -d'=' -f2)
if [ -z "$MEMPOOL_DATA_PATH" ]; then
echo -e "${YELLOW}Warning: MEMPOOL_DATA_PATH not found in .env file${NC}"
echo "Skipping Mempool data directory setup"
else
mkdir -p "${MEMPOOL_DATA_PATH}/mysql" "${MEMPOOL_DATA_PATH}/cache" \
"${MEMPOOL_DATA_PATH}/tor/mempool_hidden_service" \
"${MEMPOOL_DATA_PATH}/tor/amiexposed_hidden_service"
echo "✓ Created mempool data directories under: ${MEMPOOL_DATA_PATH}"
chown -R 1000:1000 "${MEMPOOL_DATA_PATH}" 2>/dev/null || {
echo "⚠ Could not set mempool directory ownership to 1000:1000. You may need to run:"
echo " sudo chown -R 1000:1000 ${MEMPOOL_DATA_PATH}"
}
MEMPOOL_DB_PASSWORD=$(openssl rand -hex 24)
MEMPOOL_DB_ROOT_PASSWORD=$(openssl rand -hex 24)
sed -i "s|MEMPOOL_DB_PASSWORD=.*|MEMPOOL_DB_PASSWORD=${MEMPOOL_DB_PASSWORD}|" .env
sed -i "s|MEMPOOL_DB_ROOT_PASSWORD=.*|MEMPOOL_DB_ROOT_PASSWORD=${MEMPOOL_DB_ROOT_PASSWORD}|" .env
echo "✓ Generated Mempool MariaDB credentials"
fi
echo ""
# Set secure permissions on .env
chmod 600 .env
echo "✓ Set secure permissions on .env file"
echo ""
# Summary
echo "=========================================="
echo "Setup Complete!"
echo "=========================================="
echo ""
echo -e "${GREEN}Configuration Summary:${NC}"
echo " Data Path: ${DATA_PATH}"
echo " Bitcoin Config: ${BITCOIN_CONF}"
if [ ! -z "$ELECTRS_DATA_PATH" ]; then
echo " Electrs Config: ${ELECTRS_DATA_PATH}/electrs.toml"
fi
if [ ! -z "$MEMPOOL_DATA_PATH" ]; then
echo " Mempool Data: ${MEMPOOL_DATA_PATH}"
fi
echo " RPC User: ${RPC_USER}"
echo ""
echo -e "${YELLOW}⚠ IMPORTANT - Save these credentials securely:${NC}"
echo " RPC Password: ${RPC_PASSWORD}"
echo " (This password is also saved in your .env file)"
echo ""
echo -e "${GREEN}Next Steps:${NC}"
echo " 1. Review bitcoin.conf if you want to customize settings"
echo " Location: ${BITCOIN_CONF}"
STEP=2
if [ ! -z "$ELECTRS_DATA_PATH" ]; then
echo " ${STEP}. Review electrs.toml if you want to customize Electrs settings"
echo " Location: ${ELECTRS_DATA_PATH}/electrs.toml"
STEP=$((STEP + 1))
fi
if [ ! -z "$MEMPOOL_DATA_PATH" ]; then
echo " ${STEP}. If this node already had bitcoin.conf, enable ZMQ there (see bitcoin.conf template)"
STEP=$((STEP + 1))
fi
echo " ${STEP}. Build the container(s): docker-compose build"
echo " $((STEP + 1)). Start the service(s): docker-compose up -d"
echo " $((STEP + 2)). Check logs: docker-compose logs -f"
echo " $((STEP + 3)). Use helper script: ./bitcoin-cli.sh getblockchaininfo"
echo ""
echo -e "${YELLOW}⚠ SECURITY NOTES:${NC}"
echo " - Keep your .env file secure (chmod 600 already applied)"
echo " - Never commit .env to version control"
echo " - Your RPC password has been securely generated using Bitcoin Core's rpcauth.py"
echo " - bitcoin.conf contains only the hash (HMAC-SHA256), not the plain password"
echo " - For more details, see: RPCAUTH.md"
echo ""