forked from citadel-tech/coinswap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-setup
More file actions
executable file
·811 lines (701 loc) · 26.5 KB
/
docker-setup
File metadata and controls
executable file
·811 lines (701 loc) · 26.5 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
IMAGE_NAME="coinswap"
BITCOIN_IMAGE_NAME="bitcoin-mutinynet"
CONFIG_FILE="$SCRIPT_DIR/.docker-config"
VERSION_FILE="$SCRIPT_DIR/docker/bitcoin-mutinynet.version"
DEFAULT_BITCOIN_DATADIR="/home/coinswap/.bitcoin"
DEFAULT_BITCOIN_NETWORK="signet"
DEFAULT_BITCOIN_RPC_PORT="38332"
DEFAULT_BITCOIN_ZMQ_PORT="28332"
DEFAULT_MAKERD_PORT="6102"
DEFAULT_MAKERD_RPC_PORT="6103"
DEFAULT_TOR_SOCKS_PORT="9050"
DEFAULT_TOR_CONTROL_PORT="9051"
DEFAULT_RPC_USER="user"
DEFAULT_RPC_PASSWORD="password"
DEFAULT_TOR_AUTH=""
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
load_config() {
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
print_info "Loaded configuration from $CONFIG_FILE"
fi
}
save_config() {
cat > "$CONFIG_FILE" << EOF
# Coinswap Docker Configuration
BITCOIN_DATADIR="$BITCOIN_DATADIR"
BITCOIN_NETWORK="$BITCOIN_NETWORK"
BITCOIN_RPC_PORT="$BITCOIN_RPC_PORT"
BITCOIN_ZMQ_PORT="$BITCOIN_ZMQ_PORT"
BITCOIN_RPC_USER="$BITCOIN_RPC_USER"
BITCOIN_RPC_PASSWORD="$BITCOIN_RPC_PASSWORD"
MAKERD_PORT="$MAKERD_PORT"
MAKERD_RPC_PORT="$MAKERD_RPC_PORT"
USE_TAPROOT="$USE_TAPROOT"
TOR_SOCKS_PORT="$TOR_SOCKS_PORT"
TOR_CONTROL_PORT="$TOR_CONTROL_PORT"
TOR_AUTH_PASSWORD="$TOR_AUTH_PASSWORD"
USE_EXTERNAL_BITCOIND="$USE_EXTERNAL_BITCOIND"
USE_EXTERNAL_TOR="$USE_EXTERNAL_TOR"
EXTERNAL_BITCOIND_HOST="$EXTERNAL_BITCOIND_HOST"
EXTERNAL_TOR_HOST="$EXTERNAL_TOR_HOST"
BITCOIN_WALLET_NAME="$BITCOIN_WALLET_NAME"
EOF
print_success "Configuration saved to $CONFIG_FILE"
}
check_port() {
local port=$1
if nc -z localhost "$port" 2>/dev/null; then
return 1
else
return 0
fi
}
check_tor_running() {
if check_port 9050; then
return 1
else
return 0
fi
}
check_bitcoind_running() {
local port=${1:-18332}
if check_port "$port"; then
return 1
else
return 0
fi
}
setup_env() {
# load configuration
load_config
# set defaults for any unset variables
BITCOIN_DATADIR="${BITCOIN_DATADIR:-$DEFAULT_BITCOIN_DATADIR}"
BITCOIN_NETWORK="${BITCOIN_NETWORK:-$DEFAULT_BITCOIN_NETWORK}"
BITCOIN_RPC_PORT="${BITCOIN_RPC_PORT:-$DEFAULT_BITCOIN_RPC_PORT}"
BITCOIN_ZMQ_PORT="${BITCOIN_ZMQ_PORT:-$DEFAULT_BITCOIN_ZMQ_PORT}"
BITCOIN_RPC_USER="${BITCOIN_RPC_USER:-$DEFAULT_RPC_USER}"
BITCOIN_RPC_PASSWORD="${BITCOIN_RPC_PASSWORD:-$DEFAULT_RPC_PASSWORD}"
MAKERD_PORT="${MAKERD_PORT:-$DEFAULT_MAKERD_PORT}"
MAKERD_RPC_PORT="${MAKERD_RPC_PORT:-$DEFAULT_MAKERD_RPC_PORT}"
USE_TAPROOT="${USE_TAPROOT:-true}"
TOR_SOCKS_PORT="${TOR_SOCKS_PORT:-$DEFAULT_TOR_SOCKS_PORT}"
TOR_CONTROL_PORT="${TOR_CONTROL_PORT:-$DEFAULT_TOR_CONTROL_PORT}"
TOR_AUTH_PASSWORD="${TOR_AUTH_PASSWORD:-$DEFAULT_TOR_AUTH}"
USE_EXTERNAL_BITCOIND="${USE_EXTERNAL_BITCOIND:-false}"
USE_EXTERNAL_TOR="${USE_EXTERNAL_TOR:-false}"
BITCOIN_WALLET_NAME="${BITCOIN_WALLET_NAME:-coinswap-maker}"
# export variables for docker-compose
export IMAGE_NAME="${IMAGE_NAME}"
export BITCOIN_IMAGE_NAME="${BITCOIN_IMAGE_NAME}"
export BITCOIN_TAG="latest"
if [ -f "$VERSION_FILE" ]; then
export BITCOIN_TAG=$(grep "^TAG=" "$VERSION_FILE" | cut -d'=' -f2)
fi
export BITCOIN_RPC_PORT
export BITCOIN_ZMQ_PORT
export BITCOIN_RPC_USER
export BITCOIN_RPC_PASSWORD
export BITCOIN_RPC_AUTH="${BITCOIN_RPC_USER}:${BITCOIN_RPC_PASSWORD}"
export MAKERD_PORT
export MAKERD_RPC_PORT
export TOR_SOCKS_PORT
export TOR_CONTROL_PORT
export BITCOIN_WALLET_NAME
export BITCOIN_NETWORK_ARG=""
if [[ "$BITCOIN_NETWORK" == "signet" ]]; then
export BITCOIN_NETWORK_ARG="-signet=1"
else
export BITCOIN_NETWORK_ARG="-regtest=1"
fi
# makerd args
export MAKERD_EXTRA_ARGS=""
if [[ "$USE_TAPROOT" == "true" ]]; then
export MAKERD_EXTRA_ARGS="--taproot"
fi
# profiles and hosts
local profiles=""
if [[ "$USE_EXTERNAL_BITCOIND" == "true" ]]; then
export BITCOIN_RPC_HOST="$EXTERNAL_BITCOIND_HOST"
else
profiles="${profiles}internal-bitcoind,"
# use localhost since makerd runs with network_mode: host
export BITCOIN_RPC_HOST="localhost:$BITCOIN_RPC_PORT"
fi
if [[ "$USE_EXTERNAL_TOR" == "true" ]]; then
profiles="${profiles}external-tor"
else
profiles="${profiles}internal-tor"
TOR_AUTH_PASSWORD=$DEFAULT_TOR_AUTH
fi
export TOR_AUTH_PASSWORD
export COMPOSE_PROFILES="${profiles%,}"
}
# List wallets from a bitcoind instance
list_bitcoin_wallets() {
local rpc_host="$1"
local rpc_user="$2"
local rpc_pass="$3"
local response=$(curl -s --user "${rpc_user}:${rpc_pass}" \
--data-binary '{"jsonrpc":"1.0","method":"listwalletdir","params":[]}' \
-H 'content-type:text/plain;' \
"http://${rpc_host}/" 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$response" ]; then
return 1
fi
# Extract wallet names from JSON response
echo "$response" | grep -o '"name":"[^"]*"' | cut -d'"' -f4
}
# Prompt user to select or create a wallet
select_bitcoin_wallet() {
local rpc_host="$1"
local rpc_user="$2"
local rpc_pass="$3"
echo ""
print_info "Bitcoin Wallet Configuration"
echo "----------------------------------------"
# Try to list existing wallets
print_info "Fetching existing wallets from bitcoind..."
local wallets=$(list_bitcoin_wallets "$rpc_host" "$rpc_user" "$rpc_pass")
if [ -n "$wallets" ]; then
echo ""
echo "Existing wallets found:"
echo "----------------------------------------"
local i=1
local wallet_array=()
while IFS= read -r wallet; do
if [ -n "$wallet" ]; then
echo " $i) $wallet"
wallet_array+=("$wallet")
((i++))
fi
done <<< "$wallets"
echo ""
echo " $i) Create a new wallet"
echo ""
read -p "Select a wallet [1-$i]: " wallet_choice
if [ "$wallet_choice" -eq "$i" ] 2>/dev/null; then
# Create new wallet
read -p "Enter new wallet name: " new_wallet_name
while [ -z "$new_wallet_name" ]; do
print_error "Wallet name cannot be empty"
read -p "Enter new wallet name: " new_wallet_name
done
BITCOIN_WALLET_NAME="$new_wallet_name"
print_info "Will create new wallet: $BITCOIN_WALLET_NAME"
elif [ "$wallet_choice" -ge 1 ] && [ "$wallet_choice" -lt "$i" ] 2>/dev/null; then
BITCOIN_WALLET_NAME="${wallet_array[$((wallet_choice-1))]}"
print_info "Selected wallet: $BITCOIN_WALLET_NAME"
else
print_error "Invalid selection"
select_bitcoin_wallet "$rpc_host" "$rpc_user" "$rpc_pass"
return
fi
else
print_warning "Could not fetch wallet list from bitcoind."
print_info "This may be because bitcoind is not running or RPC is not accessible."
echo ""
read -p "Enter wallet name to use/create: " new_wallet_name
while [ -z "$new_wallet_name" ]; do
print_error "Wallet name cannot be empty"
read -p "Enter wallet name to use/create: " new_wallet_name
done
BITCOIN_WALLET_NAME="$new_wallet_name"
fi
}
configure_setup() {
echo ""
print_info "Coinswap Docker Configuration"
echo "============================================"
echo ""
print_info "Bitcoin Core Configuration"
echo "----------------------------------------"
read -p "Bitcoin data directory [${DEFAULT_BITCOIN_DATADIR}]: " input
BITCOIN_DATADIR="${input:-$DEFAULT_BITCOIN_DATADIR}"
echo ""
echo "Select Bitcoin network:"
echo "1) Mutinynet (signet - default)"
echo "2) Regtest (local testing)"
read -p "Network [1]: " network_choice
case "${network_choice:-1}" in
1) BITCOIN_NETWORK="signet"; BITCOIN_RPC_PORT="38332"; BITCOIN_ZMQ_PORT="28332" ;;
2) BITCOIN_NETWORK="regtest"; BITCOIN_RPC_PORT="18442"; BITCOIN_ZMQ_PORT="28332" ;;
*) BITCOIN_NETWORK="signet"; BITCOIN_RPC_PORT="38332"; BITCOIN_ZMQ_PORT="28332" ;;
esac
echo ""
echo "Select Bitcoin Node Source:"
echo ""
echo " 1) Connect to your existing bitcoind node (Recommended)"
echo " - Most sovereign option, requires your own node"
echo " - Must be configured per docs/bitcoind.md"
echo ""
echo " 2) Use pre-configured bitcoind Docker image"
echo " - Quick setup, runs locally in Docker"
echo ""
read -p "Select option [1]: " source_choice
source_choice=${source_choice:-1}
USE_EXTERNAL_BITCOIND="false"
configure_docker_node="false"
BITCOIN_WALLET_NAME=""
case "$source_choice" in
1)
echo ""
print_warning "Your node must be configured correctly as per: docs/bitcoind.md"
print_warning "Refrence bitcoin configuration: docs/bitcoin.conf"
echo ""
read -p "Are you sure your node is configured properly? [y/N]: " confirm_existing
if [[ ! "${confirm_existing}" =~ ^[Yy]$ ]]; then
print_info "Falling back to Docker image..."
configure_docker_node="true"
else
USE_EXTERNAL_BITCOIND="true"
read -p "Bitcoin RPC host [localhost:$BITCOIN_RPC_PORT]: " btc_host
EXTERNAL_BITCOIND_HOST="${btc_host:-localhost:$BITCOIN_RPC_PORT}"
read -p "Bitcoin RPC user [${DEFAULT_RPC_USER}]: " rpc_user
BITCOIN_RPC_USER="${rpc_user:-$DEFAULT_RPC_USER}"
read -p "Bitcoin RPC password [${DEFAULT_RPC_PASSWORD}]: " rpc_pass
BITCOIN_RPC_PASSWORD="${rpc_pass:-$DEFAULT_RPC_PASSWORD}"
read -p "Bitcoin ZMQ port [$BITCOIN_ZMQ_PORT]: " btc_zmq_port
BITCOIN_ZMQ_PORT="${btc_zmq_port:-$BITCOIN_ZMQ_PORT}"
select_bitcoin_wallet "$EXTERNAL_BITCOIND_HOST" "$BITCOIN_RPC_USER" "$BITCOIN_RPC_PASSWORD"
fi
;;
*)
configure_docker_node="true"
;;
esac
if [ "$configure_docker_node" == "true" ]; then
USE_EXTERNAL_BITCOIND="false"
echo ""
print_info "Using Docker Bitcoin image (will sync from scratch)."
read -p "Bitcoin RPC port [$BITCOIN_RPC_PORT]: " btc_port
BITCOIN_RPC_PORT="${btc_port:-$BITCOIN_RPC_PORT}"
read -p "Bitcoin ZMQ port [$BITCOIN_ZMQ_PORT]: " btc_zmq_port
BITCOIN_ZMQ_PORT="${btc_zmq_port:-$BITCOIN_ZMQ_PORT}"
read -p "Bitcoin RPC user [${DEFAULT_RPC_USER}]: " rpc_user
BITCOIN_RPC_USER="${rpc_user:-$DEFAULT_RPC_USER}"
read -p "Bitcoin RPC password [${DEFAULT_RPC_PASSWORD}]: " rpc_pass
BITCOIN_RPC_PASSWORD="${rpc_pass:-$DEFAULT_RPC_PASSWORD}"
echo ""
print_info "Wallet will be configured after bitcoind syncs."
read -p "Enter wallet name to create [coinswap-maker]: " wallet_name
BITCOIN_WALLET_NAME="${wallet_name:-coinswap-maker}"
fi
echo ""
print_info "Tor Configuration"
echo "----------------------------------------"
USE_EXTERNAL_TOR="false"
TOR_CONFIGURED="false"
if check_tor_running; then
print_info "Detected Tor running on port 9050"
echo ""
read -p "Do you want to use this existing Tor instance? [y/N]: " use_existing_tor
if [[ "${use_existing_tor}" =~ ^[Yy]$ ]]; then
echo ""
print_warning "============================================"
print_warning "Your Tor must be configured correctly for makerd!"
print_warning "Required: ControlPort enabled with authentication"
print_warning "See: docs/tor.md for configuration details"
print_warning "============================================"
echo ""
read -p "Are you sure your Tor is configured properly? [y/N]: " confirm_tor
if [[ "${confirm_tor}" =~ ^[Yy]$ ]]; then
USE_EXTERNAL_TOR="true"
TOR_CONFIGURED="true"
read -p "Tor SOCKS host [localhost:9050]: " tor_host
EXTERNAL_TOR_HOST="${tor_host:-localhost:9050}"
read -p "Tor Auth Password [${DEFAULT_TOR_AUTH}]: " tor_auth
TOR_AUTH_PASSWORD="${tor_auth:-$DEFAULT_TOR_AUTH}"
else
print_info "Using internal Tor container instead..."
fi
fi
fi
if [ "$TOR_CONFIGURED" == "false" ]; then
USE_EXTERNAL_TOR="false"
echo ""
if check_tor_running; then
print_warning "Default Tor port 9050 is already in use."
print_info "You must specify custom ports for the internal Tor container."
echo ""
read -p "Tor SOCKS port (must not be 9050): " tor_socks
while [ -z "$tor_socks" ] || [ "$tor_socks" == "9050" ]; do
print_error "Please enter a valid port that is not 9050"
read -p "Tor SOCKS port: " tor_socks
done
TOR_SOCKS_PORT="$tor_socks"
read -p "Tor Control port (must not be 9051): " tor_control
while [ -z "$tor_control" ] || [ "$tor_control" == "9051" ]; do
print_error "Please enter a valid port that is not 9051"
read -p "Tor Control port: " tor_control
done
TOR_CONTROL_PORT="$tor_control"
else
TOR_SOCKS_PORT="$DEFAULT_TOR_SOCKS_PORT"
TOR_CONTROL_PORT="$DEFAULT_TOR_CONTROL_PORT"
fi
print_info "Using internal Tor container with configuration:"
echo " SOCKS Port: ${TOR_SOCKS_PORT}"
echo " Control Port: ${TOR_CONTROL_PORT}"
echo " Auth Password: (empty)"
TOR_AUTH_PASSWORD=$DEFAULT_TOR_AUTH
fi
echo ""
print_info "Coinswap Maker Ports"
echo "----------------------------------------"
read -p "Makerd RPC port [${DEFAULT_MAKERD_RPC_PORT}]: " makerd_rpc
MAKERD_RPC_PORT="${makerd_rpc:-$DEFAULT_MAKERD_RPC_PORT}"
echo ""
echo "Select Swap Type:"
echo ""
echo " 1) Taproot (Recommended New Protocol)"
echo " - Contract Tx with [Musig2 + Taproot HTLC]. Cheaper swap fees, more private. "
echo ""
echo " 2) Legacy (Original Atomic Swap Protocol)"
echo " - Contract Tx with [2of2 Multisig + P2WSH HTLC]. Higher swap fees, less private."
echo -e "${YELLOW}NOTE:${NC}"
echo -e "${YELLOW} Currently, a maker can only perform Legacy or Taproot swaps, but cannot perform both.${NC}"
echo -e "${YELLOW} Although it can handle both taproot and legacy transactions for regular wallet operations.${NC}"
echo -e "${YELLOW} If you wanna serve both types of swap in the market, run two separate makers of each type.${NC}"
echo -e "${YELLOW} You can use the existing bitcoind Docker sockets for the second maker too to avoid multiple node IBD.${NC}"
read -p "Selected Option [1]: " swap_type
if [[ "${swap_type:-1}" == "1" ]]; then
USE_TAPROOT="true"
else
USE_TAPROOT="false"
fi
echo ""
print_info "Configuration Summary"
echo "----------------------------------------"
echo "Bitcoin Network: $BITCOIN_NETWORK"
echo "Bitcoin Data Dir: $BITCOIN_DATADIR"
echo "Bitcoin RPC Port: $BITCOIN_RPC_PORT"
echo "Bitcoin ZMQ Port: $BITCOIN_ZMQ_PORT"
echo "Bitcoin RPC User: $BITCOIN_RPC_USER"
echo "Bitcoin Wallet: $BITCOIN_WALLET_NAME"
echo "Use External Bitcoin: $USE_EXTERNAL_BITCOIND"
if [[ "$USE_EXTERNAL_BITCOIND" == "true" ]]; then
echo "External Bitcoin Host: $EXTERNAL_BITCOIND_HOST"
fi
echo "Use External Tor: $USE_EXTERNAL_TOR"
if [[ "$USE_EXTERNAL_TOR" == "true" ]]; then
echo "External Tor Host: $EXTERNAL_TOR_HOST"
else
echo "Tor SOCKS Port: $TOR_SOCKS_PORT"
echo "Tor Control Port: $TOR_CONTROL_PORT"
fi
echo "Tor Auth Password: $TOR_AUTH_PASSWORD"
echo "Makerd Port: $MAKERD_PORT"
echo "Makerd RPC Port: $MAKERD_RPC_PORT"
echo "Use Taproot: $USE_TAPROOT"
echo ""
read -p "Save this configuration? [Y/n]: " save_config_prompt
if [[ "${save_config_prompt:-Y}" =~ ^[Yy]$ ]]; then
save_config
fi
}
check_docker() {
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker is not running. Please start Docker first."
exit 1
fi
print_success "Docker is available and running"
}
# Build the Docker image
build_image() {
print_info "Building Coinswap Docker image..."
cd "$SCRIPT_DIR"
if docker build -f docker/Dockerfile -t "${IMAGE_NAME}:latest" .; then
print_success "Coinswap image built successfully"
else
print_error "Failed to build Coinswap image"
exit 1
fi
}
# Build the Bitcoin Docker image
build_bitcoin_image() {
print_info "Building Bitcoin Mutinynet Docker image..."
cd "$SCRIPT_DIR"
if [ -f "$VERSION_FILE" ]; then
local tag=$(grep "^TAG=" "$VERSION_FILE" | cut -d'=' -f2)
local f_amd64=$(grep "^FILENAME_AMD64=" "$VERSION_FILE" | cut -d'=' -f2)
local s_amd64=$(grep "^SHA256_AMD64=" "$VERSION_FILE" | cut -d'=' -f2)
local f_arm64=$(grep "^FILENAME_ARM64=" "$VERSION_FILE" | cut -d'=' -f2)
local s_arm64=$(grep "^SHA256_ARM64=" "$VERSION_FILE" | cut -d'=' -f2)
if docker build \
--build-arg TAG="$tag" \
--build-arg FILENAME_AMD64="$f_amd64" \
--build-arg SHA256_AMD64="$s_amd64" \
--build-arg FILENAME_ARM64="$f_arm64" \
--build-arg SHA256_ARM64="$s_arm64" \
-f docker/Dockerfile.bitcoin-mutinynet \
-t "${BITCOIN_IMAGE_NAME}:${tag}" \
-t "${BITCOIN_IMAGE_NAME}:latest" .; then
print_success "Bitcoin Mutinynet image built successfully"
else
print_error "Failed to build Bitcoin Mutinynet image"
exit 1
fi
else
print_error "Version file not found: $VERSION_FILE"
exit 1
fi
}
# Start the full stack using docker-compose
start_stack() {
local use_defaults="false"
while [[ $# -gt 0 ]]; do
case $1 in
-d|--default)
use_defaults="true"
shift
;;
*)
shift
;;
esac
done
# Load existing configuration or prompt for new one
load_config
# If no configuration exists, run configuration setup
if [[ -z "$BITCOIN_DATADIR" ]]; then
if [[ "$use_defaults" == "true" ]]; then
print_info "Using default configuration..."
else
configure_setup
fi
fi
setup_env
print_info "Starting Coinswap stack with docker-compose..."
print_info "Active Profiles: $COMPOSE_PROFILES"
cd "$SCRIPT_DIR"
if [[ "$USE_EXTERNAL_BITCOIND" != "true" ]]; then
print_info "Starting bitcoind and tor services..."
if [[ "$COMPOSE_PROFILES" == *"internal-tor"* ]]; then
docker compose up -d bitcoind tor
else
docker compose up -d bitcoind
fi
if [ $? -ne 0 ]; then
print_error "Failed to start Coinswap stack"
exit 1
fi
print_success "Bitcoin and Tor services started"
print_info "Services running:"
docker compose ps
echo ""
wait_for_ibd_complete
start_makerd
else
if docker compose up -d; then
start_makerd
else
print_error "Failed to start Coinswap stack"
exit 1
fi
fi
}
stop_stack() {
print_info "Stopping Coinswap stack..."
cd "$SCRIPT_DIR"
setup_env
docker compose down
print_success "Coinswap stack stopped"
}
show_logs() {
cd "$SCRIPT_DIR"
setup_env
if [ -n "$1" ]; then
docker compose logs -f "$1"
else
docker compose logs -f
fi
}
# Wait for Bitcoin IBD to complete
wait_for_ibd_complete() {
print_info "Waiting for Bitcoin node to sync (Initial Block Download)..."
print_warning "This may take approximately 1 hour or more depending on your connection."
echo ""
print_info "Showing bitcoind logs (press Ctrl+C to stop watching, sync will continue in background):"
echo "============================================"
docker compose logs -f bitcoind &
local logs_pid=$!
cleanup_logs() {
print_info "Killing bitcoind logs process (PID: $logs_pid)..."
if [ -n "$logs_pid" ]; then
kill -KILL "$logs_pid" 2>/dev/null || true
kill -KILL -"$logs_pid" 2>/dev/null || true
wait "$logs_pid" 2>/dev/null || true
logs_pid=""
print_info "Bitcoind logs was killed."
fi
}
trap 'cleanup_logs; trap - EXIT INT TERM; exit 1' INT TERM
trap 'cleanup_logs' EXIT
while true; do
sleep 30
if ! docker compose ps 2>/dev/null | grep -q "bitcoind"; then
cleanup_logs
trap - EXIT INT TERM
print_error "bitcoind container stopped unexpectedly"
exit 1
fi
local ibd_status=$(docker compose exec -T bitcoind bitcoin-cli \
-rpcuser="${BITCOIN_RPC_USER}" \
-rpcpassword="${BITCOIN_RPC_PASSWORD}" \
-rpcport="${BITCOIN_RPC_PORT}" \
getblockchaininfo 2>/dev/null | grep -o '"initialblockdownload":[^,]*' | cut -d':' -f2 | tr -d ' ')
print_info "Initial Block Download status: '$ibd_status'"
if [ "$ibd_status" == "false" ]; then
print_success "Bitcoin Initial Block Download complete!"
cleanup_logs
trap - EXIT INT TERM
return 0
fi
done
}
# Start makerd service
start_makerd() {
setup_env
print_info "Starting makerd service..."
if docker compose up -d; then
print_success "makerd started successfully!"
sleep 3
echo -e "${YELLOW}==============================================================${NC}"
echo -e "${YELLOW}CHECK LOGS below for the FIDELITY FUNDING ADDRESS! !${NC}"
echo -e "${YELLOW}--------------------------------------------------------------${NC}"
echo -e "${YELLOW}Fund the displayed wallet address with at least the mentioned amount of BTC."
echo -e "If you fund extra, that will be used for swap liquidity."
echo -e "Ensure you have enough Swap Liquidity to participate in the market."
echo -e ""
echo -e "If the maker server starts successfully, you will see a log like below:"
echo -e " [6102] Taproot maker setup completed"
echo -e " [6102] Taproot swap liquidity: 49540 sats, 0 ongoing swaps"
echo -e " [6102] RPC socket binding successful at 127.0.0.1:6103"
echo -e ""
echo -e "Press Ctrl+C to stop watching logs (this will keep the maker server running in the background)."
echo -e "To restart makerd later, run: ./docker-setup.sh restart"
echo -e "To view logs later, run: ./docker-setup.sh logs [service] or docker compose logs -f [service]"
echo -e "To show all logs, run: ./docker-setup.sh logs"
echo -e "To stop the stack, run: ./docker-setup.sh stop"
echo -e "Show all available commands: ./docker-setup.sh help"
echo -e "${YELLOW}==============================================================${NC}"
echo ""
docker compose logs -f makerd-internal makerd 2>/dev/null
else
print_error "Failed to start makerd service"
print_info "Closing all services..."
stop_stack
exit 1
fi
}
run_command() {
local cmd="$1"
shift
setup_env
docker run --rm -it --network coinswap-network "${IMAGE_NAME}:latest" "$cmd" "$@"
}
show_help() {
echo "Coinswap Docker Setup Script"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " configure Configure Coinswap Docker setup"
echo " build Build the Docker image"
echo " build-bitcoin Build the Bitcoin Mutinynet Docker image"
echo " start [options] Start the full Coinswap stack"
echo " Options:"
echo " -d, --default Use default configuration without prompting"
echo " stop Stop the Coinswap stack"
echo " restart Restart the Coinswap stack"
echo " logs [service] Show logs (optionally for specific service)"
echo " Services: bitcoind, tor, makerd"
echo " status Show status of running services"
echo " shell Open shell in a new container"
echo ""
echo "Individual application commands:"
echo " makerd [args] Run makerd with arguments"
echo " maker-cli [args] run maker-cli with arguments"
echo " taker [args] Run taker with arguments"
echo " bitcoin-cli [args] Run bitcoin-cli with arguments"
echo ""
echo "Examples:"
echo " $0 build"
echo " $0 start"
echo " $0 taker --help"
echo " $0 maker-cli ping"
echo " $0 logs makerd"
}
case "${1:-}" in
"configure")
configure_setup
;;
"build")
check_docker
build_image
;;
"build-bitcoin")
check_docker
build_bitcoin_image
;;
"start")
check_docker
start_stack "${@:2}"
;;
"stop")
stop_stack
;;
"restart")
stop_stack
sleep 2
start_stack
;;
"logs")
show_logs "$2"
;;
"status")
cd "$SCRIPT_DIR"
setup_env
docker compose ps
;;
"shell")
setup_env
docker run --rm -it --network coinswap-network "coinswap/${IMAGE_NAME}:latest" /bin/sh
;;
"makerd"|"maker-cli"|"taker"|"bitcoin-cli")
run_command "$@"
;;
"help"|"--help"|"-h"|"")
show_help
;;
*)
print_error "Unknown command: $1"
echo ""
show_help
exit 1
;;
esac