-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodebuilder
executable file
·1334 lines (1230 loc) · 57.1 KB
/
nodebuilder
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# A minimally-interactive script for launching a Bitcoin Core node
# shellcheck disable=SC2155
set -o errexit
set -o nounset
check_dpkg_lock() {
if fuser /var/lib/dpkg/lock > /dev/null 2>&1 || fuser /var/lib/dpkg/lock-frontend > /dev/null 2>&1; then
throw_error 'Debian package manager (dpkg) is locked by another process. Try running: ps aux | grep -i apt'
fi
}
check_internet() {
check_internet_address=$1
readonly CHECK_INTERNET_PORT='443'
readonly CHECK_INTERNET_TIMEOUT='10'
# shellcheck disable=SC2015
command -v torsocks > /dev/null 2>&1 && [ "${TARGET_OPERATING_SYSTEM}" != 'Darwin' ] &&
torsocks curl --silent --output /dev/null --retry 2 --connect-timeout "${CHECK_INTERNET_TIMEOUT}" "https://${check_internet_address}:${CHECK_INTERNET_PORT}" > /dev/null ||
curl --silent --output /dev/null --retry 5 --connect-timeout "${CHECK_INTERNET_TIMEOUT}" "https://${check_internet_address}:${CHECK_INTERNET_PORT}" > /dev/null
}
clear_the_terminal() {
if command -v clear > /dev/null 2>&1; then
clear
elif command -v tput > /dev/null 2>&1; then
tput clear
else
printf '\033c'
fi
}
display_macos_warning() {
if [ "${TARGET_KERNEL}" = 'Darwin' ] && [ "${TARGET_ARCHITECTURE}" = 'arm64' ]; then
printf '%s\n%s\n' \
'WARNING: macOS has an unpatched security vulnerability called GoFetch.' \
' Avoid security-critical actions, such as securing significant funds.'
if [ "${unattended:-false}" = 'false' ] && ! is_running_in_ci; then
printf '%s' 'PRESS ENTER to continue or press Ctrl+C to exit... '
read -r _
fi
fi
}
ensure_curl_dependency() {
if command -v dnf > /dev/null 2>&1; then
sudo dnf install --allowerasing --assumeyes curl > /dev/null
elif ! command -v curl > /dev/null 2>&1; then
printf '%s' 'Installing curl depencency... '
case "${TARGET_OPERATING_SYSTEM}" in
alpine)
apk --quiet add curl
;;
debian | ubuntu)
sudo apt-get install -y curl > /dev/null
;;
fedora* | rhel | centos* | rocky | ol)
sudo dnf install --allowerasing --assumeyes curl > /dev/null
;;
gentoo)
emerge --jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail net-misc/curl
;;
arch | endeavouros | garuda | manjaro)
sudo pacman -Syu --needed --noconfirm curl > /dev/null
;;
suse | *opensuse*)
sudo zypper --non-interactive --quiet install curl > /dev/null
;;
clear-linux-os)
sudo swupd bundle-add curl > /dev/null
;;
*)
if command -v apk > /dev/null; then
apk --quiet add curl
elif command -v apt-get > /dev/null; then
sudo apt-get -qq install -y curl > /dev/null
elif command -v dnf > /dev/null; then
sudo dnf install --allowerasing --assumeyes curl > /dev/null
elif command -v pacman > /dev/null; then
sudo pacman -Syu --needed --noconfirm curl > /dev/null
elif command -v zypper > /dev/null; then
sudo zypper --non-interactive --quiet install curl > /dev/null
else
throw_error 'PACKAGE MANAGER NOT FOUND. Please open an issue at github com bitcoin-tools nodebuilder.'
fi
;;
esac
printf '%s\n' 'ok.'
fi
command -v curl > /dev/null 2>&1 || throw_error "Unable to install 'curl'. Please report this error on github com bitcoin-tools nodebuilder"
}
ensure_sudo_dependency() {
if ! command -v sudo > /dev/null 2>&1; then
printf '%s' 'Installing sudo depencency... '
case "${TARGET_OPERATING_SYSTEM}" in
alpine)
apk --quiet add sudo
;;
debian | ubuntu)
apt-get install -y sudo > /dev/null
;;
fedora* | rhel | centos* | rocky | ol)
dnf install --allowerasing --assumeyes sudo > /dev/null
;;
gentoo)
emerge --sync --quiet
emerge --jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail app-admin/sudo
;;
arch | endeavouros | garuda | manjaro)
pacman -Syu --needed --noconfirm sudo > /dev/null
;;
suse | *opensuse*)
zypper --non-interactive --quiet install sudo > /dev/null
;;
clear-linux-os)
swupd bundle-add sudo > /dev/null
;;
*)
if command -v apk > /dev/null; then
apk --quiet add sudo
elif command -v apt-get > /dev/null; then
apt-get -qq install -y sudo > /dev/null
elif command -v dnf > /dev/null; then
dnf install sudo > /dev/null
elif command -v pacman > /dev/null; then
pacman -Syu --needed --noconfirm sudo > /dev/null
elif command -v zypper > /dev/null; then
zypper --non-interactive --quiet install sudo > /dev/null
else
throw_error 'PACKAGE MANAGER NOT FOUND. Please open an issue at github com bitcoin-tools nodebuilder.'
fi
;;
esac
printf '%s\n' 'ok.'
fi
command -v sudo > /dev/null 2>&1 || throw_error "Unable to install 'sudo'. Please report this error on github com bitcoin-tools nodebuilder."
}
get_cursor_column() {
stored_stty_settings=$(stty -g)
# Set raw mode, no echo, immediate read
stty raw -echo min 0 time 10
# Send ANSII escape code to request cursor position
printf "\033[6n" > /dev/tty
# System will print the cursor's row and column
IFS=';' read -r _ stty_column
stty "${stored_stty_settings}"
printf '%s' "${stty_column}"
}
test_get_cursor_column() {
# Test when cursor is at the start of a line
printf '\n'
col=$(get_cursor_column)
[ "${col}" -ne 1 ] && throw_error "Failed: Expected 1, got ${col}"
# Test when cursor is at the end of the string 'test'
printf '\n%s' 'test'
col=$(get_cursor_column)
[ "${col}" -ne 5 ] && throw_error "Failed: Expected 5, got ${col}"
printf '%s\n' 'All tests passed'
return 0
}
get_free_space_in_mib() {
if [ "${TARGET_KERNEL}" = 'Darwin' ]; then
/bin/df -m "${HOME}" | awk '{print $4}' | sed 1d
else
df --output=avail --block-size='1MiB' "${HOME}" | sed 1d
fi
}
get_memory_metric_in_mib() {
metric_to_query="$1"
awk -v metric="${metric_to_query}" \
-v kib_mib_factor="${KIB_TO_MIB}" \
'$0 ~ metric { printf "%d \n", $2 / kib_mib_factor }' /proc/meminfo
}
get_operating_system() {
if [ "${TARGET_KERNEL}" = 'Darwin' ]; then
printf '%s\n' 'Darwin'
else
readonly OS_RELEASE_ID="$(grep '^ID=' /etc/os-release | cut -d= -f2)"
readonly OS_RELEASE_ID_LIKE="$(grep '^ID_LIKE=' /etc/os-release | cut -d= -f2)"
if [ -n "${OS_RELEASE_ID}" ] || [ -n "${OS_RELEASE_ID_LIKE}" ]; then
printf '%s\n' "${OS_RELEASE_ID_LIKE:-${OS_RELEASE_ID}}"
else
throw_error 'Failed to determine OS release type'
fi
fi
}
# shellcheck disable=SC2317
handle_error() {
log_error "Failed to run line $1."
handle_exit
exit 1
}
# shellcheck disable=SC2317
handle_exit() {
[ -n "${TEMP_DIRECTORY:-}" ] && rm -rf -- "${TEMP_DIRECTORY:?}"/
}
# shellcheck disable=SC2317
handle_sigint() {
printf '\n%s\n' 'Detected Ctrl+C. Exiting.'
handle_exit
exit 0
}
install_build_dependencies_apk() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_apk.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs apk --quiet add |
grep -v 'ICU with non-English locales' -A2 -B1 || true
}
install_build_dependencies_aptget() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_aptget.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs sudo DEBIAN_FRONTEND=noninteractive apt-get -qq install --assume-yes --no-install-recommends > /dev/null
# TODO: Remove gcc-12 installation on next line when Ubuntu updates the default version of gcc to support C++ 20 features. Bitcoin Core requires gcc with support for C++ 20 features.
DEBIAN_FRONTEND=noninteractive sudo apt-get -qq install --assume-yes --no-install-recommends gcc-12
}
install_build_dependencies_darwin() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_darwin.txt'
#TODO: wrap this command with torsocks-curl with curl fallback
sudo printf '' && NONINTERACTIVE=1 /bin/bash -c "$(curl --fail --silent --show-error --location --retry 5 \
https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" > /dev/null 2>&1
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs brew install --quiet > /dev/null
}
install_build_dependencies_dnf() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_dnf.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs sudo dnf install --assumeyes > /dev/null
}
install_build_dependencies_emerge() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_emerge.txt'
printf '\n'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
if ! printf '%s\n' "${dependencies}" | xargs emerge --autounmask-write \
--jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail; then
etc-update --automode -5
emerge --update --jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail --deep --newuse @world
# Run the install command again
printf '%s\n' "${dependencies}" | xargs emerge --autounmask-write \
--jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail
fi
}
install_build_dependencies_pacman() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_pacman.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs sudo pacman -Syu --needed --noconfirm --quiet > /dev/null 2> "${STDERR_COMPILE_LOG_FILE}"
grep -v 'skipping' "${STDERR_COMPILE_LOG_FILE}" >&2 || true
}
install_build_dependencies_swupd() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_swupd.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs sudo swupd bundle-add --quiet > /dev/null
}
install_build_dependencies_zypper() {
readonly BUILD_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/build_dependencies_zypper.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${BUILD_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${BUILD_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error "The list of dependencies is empty."
printf '%s\n' "${dependencies}" | xargs sudo zypper --non-interactive --quiet install
export CXX=g++-13
}
install_runtime_dependencies_apk() {
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_apk.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs sudo apk --quiet add
}
install_runtime_dependencies_aptget() {
check_dpkg_lock
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_aptget.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs sudo DEBIAN_FRONTEND=noninteractive apt-get -qq install --assume-yes --no-install-recommends > /dev/null
}
install_runtime_dependencies_darwin() {
if ! command -v git > /dev/null 2>&1; then
printf '%s\n%s' 'not found.' 'Installing git via the Xcode Command Line Tools... '
# These steps were taken from: https github com/Homebrew/install/blob/aceed88a4a062e2b41dc40a7428c71309fce14c9/install.sh#L831
# TODO: find a way to install only the git binary, not all the Xcode tools. Unattended git installation shouldn't take 20+ lines of code.
readonly CLT_INSTALL_IN_PROGRESS='/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress'
sudo touch "${CLT_INSTALL_IN_PROGRESS}"
readonly CLT_LABEL_COMMAND="/usr/sbin/softwareupdate -l | grep -B 1 -E 'Command Line Tools' | awk -F'*' '/^ *\\*/ {print \$2}' | sed -e 's/^ *Label: //' -e 's/^ *//' | sort -V | tail -n1 | tr -d '\n'"
readonly CLT_LABEL="$(/bin/sh -c "${CLT_LABEL_COMMAND}")"
if [ -n "${CLT_LABEL}" ]; then
sudo /usr/sbin/softwareupdate -i "${CLT_LABEL}" > /dev/null
else
throw_error 'Failed to install git. Cannot find a compatible Xcode Command Line Tools package.'
fi
sudo rm "${CLT_INSTALL_IN_PROGRESS}"
if command -v git > /dev/null 2>&1; then
printf '%s\n' 'ok.'
elif [ "${unattended}" = 'true' ]; then
throw_error 'Failed to install Xcode Command Line Tools. Try re-running without -u/--unattended or manually installing git.'
else
printf '%s\n%s\n' 'failed.' 'Installing the Command Line Tools (expect a popup window).'
/usr/bin/xcode-select --install
printf '%s\n' "PRESS ENTER after you've completed the installation via the popup window."
read -r _
if ! command -v git > /dev/null 2>&1; then
throw_error 'Failed to install git via the Xcode Command Line Tools. Try manually installing git'
fi
fi
fi
}
install_runtime_dependencies_dnf() {
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_dnf.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs sudo dnf --assumeyes --quiet install > /dev/null
}
install_runtime_dependencies_emerge() {
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_emerge.txt'
printf '\n'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs emerge --autounmask-write --jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail
}
install_runtime_dependencies_pacman() {
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_pacman.txt'
readonly STDERR_DEPENDENCIES_LOG_FILE="${TEMP_DIRECTORY}/stderr_runtime_dependencies_log"
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs sudo pacman -Syu --needed --noconfirm --quiet > /dev/null 2> "${STDERR_DEPENDENCIES_LOG_FILE}"
grep -v 'skipping' "${STDERR_DEPENDENCIES_LOG_FILE}" >&2 || true
rm "${STDERR_DEPENDENCIES_LOG_FILE}"
}
install_runtime_dependencies_swupd() {
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_swupd.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs sudo swupd bundle-add --quiet > /dev/null
}
install_runtime_dependencies_zypper() {
readonly RUNTIME_DEPENDENCIES_URL='https://github.com/bitcoin-tools/nodebuilder/raw/v1.5.0/resources/dependencies/runtime_dependencies_zypper.txt'
command -v torsocks > /dev/null 2>&1 &&
dependencies=$(torsocks curl --fail --silent --show-error --location --retry 2 "${RUNTIME_DEPENDENCIES_URL}") ||
dependencies=$(curl --fail --silent --show-error --location --retry 5 "${RUNTIME_DEPENDENCIES_URL}")
[ -z "${dependencies:-}" ] && throw_error 'The list of dependencies is empty.'
printf '%s\n' "${dependencies}" | xargs sudo zypper --non-interactive --quiet install > /dev/null
}
install_system_updates_apk() {
apk update --quiet && apk upgrade --quiet
}
install_system_updates_aptget() {
check_dpkg_lock
readonly STDERR_INSTALL_LOG_FILE="${TEMP_DIRECTORY}/stderr_install.log"
sudo apt-get -qq update && sudo NEEDRESTART_MODE=a apt-get -qq dist-upgrade --assume-yes > /dev/null 2> "${STDERR_INSTALL_LOG_FILE}"
grep -v 'apt-utils' "${STDERR_INSTALL_LOG_FILE}" >&2 || true
rm "${STDERR_INSTALL_LOG_FILE}"
}
install_system_updates_dnf() {
sudo dnf clean all && sudo dnf --assumeyes --quiet upgrade > /dev/null
}
install_system_updates_emerge() {
readonly GENTOO_EBUILD_REPOSITORY='/var/db/repos/gentoo'
printf '\n'
[ -d "${GENTOO_EBUILD_REPOSITORY}" ] || mkdir -p "${GENTOO_EBUILD_REPOSITORY}"
emerge --depclean --quiet
emerge --update --jobs "$(nproc)" --load-average "$(($(nproc) + 1))" --quiet --quiet-build --quiet-fail --deep --newuse @world
emerge --depclean --quiet
}
install_system_updates_pacman() {
sudo pacman -Syu --noconfirm --quiet > /dev/null
}
install_system_updates_swupd() {
sudo swupd update --quiet > /dev/null
}
install_system_updates_zypper() {
sudo zypper --non-interactive --quiet dist-upgrade > /dev/null
}
is_running_in_ci() {
if [ "${CI:-false}" = 'true' ]; then
return 0
else
return 1 #1=false
fi
}
is_running_in_container() {
if [ -f /proc/1/cgroup ] && [ "$(grep -c 'docker\|lxc\|actions_job' /proc/1/cgroup)" -gt 0 ]; then
return 0
elif [ -f /proc/1/mountinfo ] && [ "$(grep -c 'docker\|lxc\|actions_job' /proc/1/mountinfo)" -gt 0 ]; then
return 0
else
return 1 #1=false
fi
}
is_valid_bitcoin_version() {
[ -z "$1" ] && throw_error 'No arguemnt passed into is_valid_bitcoin_version().'
bitcoin_version_to_check="$1"
for version in ${VALID_BITCOIN_VERSION_LIST}; do
if [ "${bitcoin_version_to_check}" = "${version}" ]; then
return 0
fi
done
return 1
}
log_error() {
printf '\n'
printf '%s\n' "[$(date +'%Y-%m-%dT%H:%M:%S')] ERROR: $*" >&2
}
print_usage() {
printf '%s\n\n' "Usage: $0 [options]"
printf '%s\n' 'Options:'
printf '%s\n' '-b, --bitcoin-version Specify the Bitcoin version'
printf '%s\n' '-c, --compile Build Bitcoin from source'
printf '%s\n' '-h, --help Display this help message'
printf '%s\n' '-p, --prune Set a prune value in MiB'
printf '%s\n' '-t, --test Run unit tests on functions'
printf '%s\n' '-u, --unattended Run in non-interactive mode'
}
run_unit_tests() {
printf '%s' 'Testing get_cursor_column...'
test_get_cursor_column
printf '%s\n' 'PASS'
return 0
}
set_bitcoin_core_option() {
option="$1"
value="$2"
if [ -f "${bitcoin_core_config_file:?}" ]; then
sed -i.bak "/^${option}=/d" "${bitcoin_core_config_file:?}"
rm "${bitcoin_core_config_file:?}.bak"
else
install -m 600 /dev/null "${bitcoin_core_config_file:?}"
fi
printf '%s\n' "${option}=${value}" >> "${bitcoin_core_config_file:?}"
}
# This function sets a Bitcoin Core option in the configuration file if it's not already set.
set_if_unset_bitcoin_core_option() {
option="$1"
value="$2"
if [ ! -f "${bitcoin_core_config_file:?}" ]; then
install -m 600 /dev/null "${bitcoin_core_config_file:?}"
fi
if [ "$(grep -c -i "^${option}=" "${bitcoin_core_config_file:?}")" = 0 ]; then
printf '%s\n' "${option}=${value}" >> "${bitcoin_core_config_file:?}"
fi
}
throw_error() {
if [ $# -gt 1 ]; then
log_error "$@"
elif [ $# -eq 1 ]; then
log_error "$1"
else
log_error "Unknown error thrown at line ${LINENO:-}"
fi
handle_exit
exit 1
}
# This list excludes vulnerable versions, e.g. CVE-2015-6031 and CVE-2018-17144
readonly VALID_BITCOIN_VERSION_LIST='0.9.5 0.10.0 0.10.1 0.10.2 0.10.3 0.10.4 \
0.11.0 0.11.1 0.11.2 0.12.0 0.12.1 0.13.0 0.13.1 0.13.2 \0.14.3 0.15.2 \
0.16.3 0.17.0 0.17.0.1 0.17.1 0.17.2 0.18.0 0.18.1 0.19.0 0.19.0.1 0.19.1 \
0.20.0 0.20.1 0.20.2 0.21.0 0.21.1 0.21.2 22.0 22.1 23.0 23.1 23.2 24.0 \
24.0.1 24.1 24.2 25.0 25.1 25.2 26.0 26.1 27.0'
target_bitcoin_version='27.0'
compile_bitcoin='false'
prune_value='-1'
unattended='false'
unit_tests='false'
while [ $# -gt 0 ]; do
case "$1" in
-b | --bitcoin-version)
if [ $# -eq 1 ]; then
throw_error '-b/--bitcoin-version requires an argument.'
fi
if is_valid_bitcoin_version "$2"; then
target_bitcoin_version="$2"
else
throw_error "The Bitcoin version '$2' is not valid. Please use a value such as '27.0' from https://bitcoincore.org/bin/."
fi
shift
;;
-c | --compile)
compile_bitcoin='true'
;;
-h | --help)
print_usage
exit 0
;;
-p | --prune)
if [ $# -eq 1 ]; then
throw_error '-p/--prune requires an argument'
fi
case $2 in
*[!0-9]*) # if $2 contains any non-numeric characters
throw_error "The prune value '$2' must be a positive integer or zero to disable pruning"
;;
*) ;;
esac
if [ "$2" -gt 0 ] && [ "$2" -lt 550 ]; then
throw_error "The prune value '$2' must be at least 550 (MiB) or zero to disable pruning"
fi
prune_value="$2"
shift
;;
-t | --test)
unit_tests='true'
;;
-u | --unattended)
unattended='true'
;;
*)
printf '%s\n%s\n' "Error: '$1' is invalid." 'Use -h or --help for available options.'
exit 1
;;
esac
shift
done
[ "${unit_tests}" = 'true' ] && run_unit_tests
trap 'if [ $? -eq 0 ]; then handle_exit; else handle_error ${LINENO:-}; fi' EXIT
trap handle_sigint INT
readonly BYTES_TO_KIB=1024
readonly KIB_TO_MIB="${BYTES_TO_KIB}"
readonly MIB_TO_GIB="${BYTES_TO_KIB}"
readonly TEMP_DIRECTORY="$(mktemp -d)"
readonly TARGET_ARCHITECTURE="$(uname -m)"
readonly TARGET_KERNEL="$(uname -s)"
readonly TARGET_OPERATING_SYSTEM="$(get_operating_system)"
readonly USER_DATA_DIRECTORY="${XDG_DATA_HOME:-${HOME}/.local/share}"
readonly SHORTCUT_IMAGE_FILE="${USER_DATA_DIRECTORY}/images/bitcoin128.png"
readonly SHORTCUT_IMAGE_SOURCE="https://github.com/bitcoin/bitcoin/raw/v${target_bitcoin_version}/share/pixmaps/bitcoin128.png"
ensure_sudo_dependency
if is_running_in_container; then
printf '%s\n' 'Detected: running in a container.'
fi
if is_running_in_ci ||
is_running_in_container ||
sudo --validate --noninteractive > /dev/null 2>&1; then
:
else
printf '%s\n' 'Please enter your sudo password if requested.'
sudo --validate
fi
clear_the_terminal
case "${TARGET_KERNEL}" in
Linux)
printf '%s\n' 'Detected: running on Linux.'
readonly TARGET_BITCOIN_OPERATING_SYSTEM='linux-gnu'
readonly BITCOIN_DATA_DIRECTORY="${HOME}/.bitcoin"
ensure_curl_dependency
printf '%s' 'Checking internet status... '
( (check_internet 1.1.1.1 ||
check_internet 1.1.0.0 ||
check_internet 9.9.9.9 ||
check_internet 9.9.9.11) &&
check_internet bitcoincore.org) ||
throw_error 'Check for active internet failed.'
if ! is_running_in_ci; then
printf '%s\n%s' 'ok.' 'Performing a system upgrade... '
case "${TARGET_OPERATING_SYSTEM}" in
alpine)
install_system_updates_apk
;;
debian | ubuntu)
install_system_updates_aptget
;;
fedora* | rhel | centos* | rocky | ol)
install_system_updates_dnf
;;
gentoo)
install_system_updates_emerge
;;
arch | endeavouros | garuda | manjaro)
install_system_updates_pacman
;;
suse | *opensuse*)
install_system_updates_zypper
;;
clear-linux-os)
install_system_updates_swupd
;;
*)
if command -v apk > /dev/null; then
install_system_updates_apk
elif command -v apt-get > /dev/null; then
install_system_updates_aptget
elif command -v dnf > /dev/null; then
install_system_updates_dnf
elif command -v emerge > /dev/null; then
install_system_updates_emerge
elif command -v pacman > /dev/null; then
install_system_updates_pacman
elif command -v swupd > /dve/null; then
install_system_updates_swupd
elif command -v zypper > /dev/null; then
install_system_updates_zypper
else
throw_error 'This version of Linux is not supported.'
fi
;;
esac
fi
# Stop running if the Docker container base image is outdated
if [ -f /var/run/reboot-required ] && is_running_in_container; then
throw_error 'The Docker base image is outdated. Please open an issue at github.com/bitcoin-tools/nodebuilder.'
# Reboot the system unless it's running in CI/CD
elif [ -f /var/run/reboot-required ] && ! is_running_in_ci; then
printf '\n%s\n%s\n' 'REBOOT REQUIRED to upgrade the following:' "$(cat /var/run/reboot-required.pkgs)"
if [ "${unattended}" = 'false' ]; then
printf '\n%s' 'PRESS ENTER to reboot or press Ctrl+C to exit... '
read -r _
fi
printf '%s\n' 'Rebooting.'
sudo reboot
exit 0
fi
printf '%s\n' 'ok.'
printf '%s' 'Ensuring runtime dependencies... '
case "${TARGET_OPERATING_SYSTEM}" in
alpine)
install_runtime_dependencies_apk
;;
debian | ubuntu)
install_runtime_dependencies_aptget
;;
fedora* | rhel | centos* | rocky | ol)
install_runtime_dependencies_dnf
;;
gentoo)
install_runtime_dependencies_emerge
;;
arch | endeavouros | garuda | manjaro)
install_runtime_dependencies_pacman
;;
suse | *opensuse*)
install_runtime_dependencies_zypper
;;
clear-linux-os)
install_runtime_dependencies_swupd
;;
*)
install_runtime_command_function=''
for package_manager in \
apk \
apt-get \
dnf \
emerge \
pacman \
swupd \
zypper; do
if command -v "${package_manager}" > /dev/null; then
install_runtime_command_function="install_runtime_dependencies_$(echo "${package_manager}" | tr -d '-')"
break
fi
done
if [ -n "${install_runtime_command_function}" ]; then
"${install_runtime_command_function}"
else
throw_error 'Unknown package manager. This version of Linux is not supported.'
fi
;;
esac
printf '%s\n' 'ok.'
;;
Darwin)
readonly TARGET_BITCOIN_OPERATING_SYSTEM='apple-darwin'
readonly BITCOIN_DATA_DIRECTORY="${HOME}/Library/Application Support/Bitcoin"
printf '%s\n%s' 'Detected: running on macOS.' 'Checking internet status... '
( (check_internet 1.1.1.1 ||
check_internet 1.1.0.0 ||
check_internet 9.9.9.9 ||
check_internet 9.9.9.11) &&
check_internet bitcoincore.org) ||
throw_error 'Check for active internet failed.'
printf '%s\n' 'ok.'
display_macos_warning
printf '%s' 'Checking for git... '
install_runtime_dependencies_darwin
printf '%s\n' 'ok.'
;;
MINGW*)
throw_error 'Windows is not supported. Instead, use WSL (Windows Subsystem for Linux).'
;;
*)
throw_error 'Your operating system is not supported.'
;;
esac
printf '%s' "Checking for Bitcoin Core ${target_bitcoin_version}... "
if command -v bitcoind > /dev/null 2>&1; then
current_bitcoin_version="$(bitcoind --version 2> /dev/null | head -1 | cut -c23-)"
else
current_bitcoin_version=''
fi
# If version is like 'xx.yy', pad it as 'xx.yy.0'
case "${current_bitcoin_version}" in
*[.]*[.]*)
current_bitcoin_version_padded="${current_bitcoin_version}"
;;
*)
current_bitcoin_version_padded="${current_bitcoin_version}.0"
;;
esac
case "${target_bitcoin_version}" in
*[.]*[.]*)
target_bitcoin_version_padded="${target_bitcoin_version}"
;;
*)
target_bitcoin_version_padded="${target_bitcoin_version}.0"
;;
esac
if [ "${current_bitcoin_version_padded}" = "${target_bitcoin_version_padded}" ]; then
printf '%s\n' 'ok.'
elif [ "${compile_bitcoin:-false}" = 'true' ] ||
[ "${TARGET_OPERATING_SYSTEM}" = 'alpine' ] ||
[ "${TARGET_OPERATING_SYSTEM}" = 'gentoo' ]; then
printf '%s\n %s' 'no.' 'Ensuring compile dependencies... '
readonly COMPILE_DIRECTORY="${TEMP_DIRECTORY}/compile_bitcoin"
readonly STDERR_COMPILE_LOG_FILE="${TEMP_DIRECTORY}/stderr_install.log"
case "${TARGET_OPERATING_SYSTEM}" in
alpine)
install_build_dependencies_apk
;;
debian | ubuntu)
install_build_dependencies_aptget
;;
fedora* | rhel | centos* | rocky | ol)
install_build_dependencies_dnf
;;
gentoo)
install_build_dependencies_emerge
;;
arch | endeavouros | garuda | manjaro)
install_build_dependencies_pacman
;;
suse | *opensuse*)
install_build_dependencies_zypper
;;
clear-linux-os)
install_build_dependencies_swupd
;;
Darwin)
install_build_dependencies_darwin
;;
*)
install_build_command_function=''
for package_manager in \
apk \
apt-get \
dnf \
emerge \
pacman \
swupd \
zypper; do
if command -v "${package_manager}" > /dev/null; then
install_build_command_function="install_build_dependencies_$(echo "${package_manager}" | tr -d '-')"
break
fi
done
if [ -n "${install_build_command_function}" ]; then
"${install_build_command_function}"
else
printf '%s\n %s\n' 'failed.' 'Unable to determine build dependencies. Attempting to compile without verifying dependencies.'
fi
;;
esac
printf '%s\n %s' 'ok.' 'Downloading Bitcoin source code... '
readonly BITCOIN_CORE_REPO='https://github.com/bitcoin/bitcoin.git'
# shellcheck disable=SC2015
command -v torsocks > /dev/null 2>&1 && [ "${TARGET_KERNEL}" != 'Darwin' ] &&
torsocks git clone --branch "v${target_bitcoin_version}" --depth 1 --quiet -c advice.detachedHead=false "${BITCOIN_CORE_REPO}" "${COMPILE_DIRECTORY}" ||
git clone --branch "v${target_bitcoin_version}" --depth 1 --quiet -c advice.detachedHead=false "${BITCOIN_CORE_REPO}" "${COMPILE_DIRECTORY}"
cd "${COMPILE_DIRECTORY}"/
printf '%s\n %s' 'ok.' 'Analyzing hardware configuration... '
./autogen.sh > /dev/null 2> "${STDERR_COMPILE_LOG_FILE}"
grep -v 'build-aux' "${STDERR_COMPILE_LOG_FILE}" >&2 || true
printf '%s\n %s' 'ok.' 'Configuring the build environment... '
if [ "${TARGET_KERNEL}" = 'Darwin' ]; then
./configure CC=clang CXX=clang++ --without-bdb --enable-suppress-external-warnings > /dev/null
else
./configure --without-bdb --enable-suppress-external-warnings > /dev/null
fi
printf '%s\n %s' 'ok.' 'Compiling source code, please wait... '
if [ "${TARGET_OPERATING_SYSTEM}" = 'Darwin' ]; then
compile_jobs_count="$(($(sysctl -n hw.physicalcpu) + 1))"
else
compile_jobs_count="$(($(nproc) + 1))"
fi
make --jobs "${compile_jobs_count}" > /dev/null 2>&1
printf '%s\n %s' 'ok.' 'Running compile checks, please wait... '
make --jobs "${compile_jobs_count}" check > /dev/null 2> "${STDERR_COMPILE_LOG_FILE}"
# exclude the two lines before and after 'Ran 3 tests in ' in make check's stdout
sed -n '1N;2N;/Ran 3 tests in /{N;N;d;};P;N;D' "${STDERR_COMPILE_LOG_FILE}" >&2
printf '%s\n %s' 'ok.' 'Installing Bitcoin Core... '
make install > /dev/null 2>&1 || sudo make install > /dev/null
printf '%s\n' 'ok.'
cd - > /dev/null
rm "${STDERR_COMPILE_LOG_FILE}"
rm -rf "${COMPILE_DIRECTORY:?}"/
current_bitcoin_version="${target_bitcoin_version}"
else
printf '%s\n' 'no.'
readonly BITCOIN_CORE_EXTRACT_DIR="${TEMP_DIRECTORY}/bitcoin-core"
readonly BITCOIN_INSTALL_BIN_SOURCE="${BITCOIN_CORE_EXTRACT_DIR}/bin"
readonly BITCOIN_INSTALL_LIB_SOURCE="${BITCOIN_CORE_EXTRACT_DIR}/lib"
readonly BITCOIN_INSTALL_INCLUDE_SOURCE="${BITCOIN_CORE_EXTRACT_DIR}/include"
readonly BITCOIN_INSTALL_MAN_SOURCE="${BITCOIN_CORE_EXTRACT_DIR}/share/man/man1"
readonly BITCOIN_INSTALL_DESTINATION='/usr/local'
readonly BITCOIN_INSTALL_BIN_DESTINATION="${BITCOIN_INSTALL_DESTINATION}/bin"
readonly BITCOIN_INSTALL_LIB_DESTINATION="${BITCOIN_INSTALL_DESTINATION}/lib"
readonly BITCOIN_INSTALL_INCLUDE_DESTINATION="${BITCOIN_INSTALL_DESTINATION}/include"
readonly BITCOIN_INSTALL_MAN_DESTINATION="${BITCOIN_INSTALL_DESTINATION}/share/man/man1"
readonly BITCOIN_TARBALL_FILENAME="bitcoin-${target_bitcoin_version}-${TARGET_ARCHITECTURE}-${TARGET_BITCOIN_OPERATING_SYSTEM}.tar.gz"
readonly BITCOIN_TARBALL_DESTINATION_PATH="${HOME}/Downloads/${BITCOIN_TARBALL_FILENAME}"
if ! [ -f "${BITCOIN_TARBALL_DESTINATION_PATH}" ]; then
readonly BITCOIN_SOURCE="https://bitcoincore.org/bin/bitcoin-core-${target_bitcoin_version}"
readonly BITCOIN_TARBALL_FILE_SOURCE="${BITCOIN_SOURCE}/${BITCOIN_TARBALL_FILENAME}"
readonly BITCOIN_HASH_FILENAME='SHA256SUMS'
readonly BITCOIN_HASH_FILE_SOURCE="${BITCOIN_SOURCE}/${BITCOIN_HASH_FILENAME}"
readonly GPG_SIGNATURES_FILENAME='SHA256SUMS.asc'
readonly GPG_SIGNATURES_FILE_SOURCE="${BITCOIN_SOURCE}/${GPG_SIGNATURES_FILENAME}"
readonly GPG_GOOD_SIGNATURES_REQUIRED='7'
readonly GUIX_SIGS_REPO='https://github.com/bitcoin-core/guix.sigs.git'
readonly BITCOIN_TARBALL_TEMPORARY_DIRECTORY="${TEMP_DIRECTORY}/${BITCOIN_TARBALL_FILENAME}"
readonly BITCOIN_HASH_FILE="${TEMP_DIRECTORY}/${BITCOIN_HASH_FILENAME}"
readonly GPG_SIGNATURES_FILE="${TEMP_DIRECTORY}/${GPG_SIGNATURES_FILENAME}"
readonly GUIX_SIGS_TEMPORARY_DIRECTORY="${TEMP_DIRECTORY}/guix.sigs"
readonly GUIX_SIGS_DESTINATION_DIRECTORY="${HOME}/Downloads/guix.sigs"
printf ' %s' 'Downloading Bitcoin Core... '
if command -v torsocks > /dev/null 2>&1 && [ "${TARGET_KERNEL}" != 'Darwin' ]; then
torsocks curl --fail --silent --show-error --location --retry 2 --output "${BITCOIN_HASH_FILE}" "${BITCOIN_HASH_FILE_SOURCE}" ||
curl --fail --silent --show-error --location --retry 5 --output "${BITCOIN_HASH_FILE}" "${BITCOIN_HASH_FILE_SOURCE}"
torsocks curl --fail --silent --show-error --location --retry 2 --output "${GPG_SIGNATURES_FILE}" "${GPG_SIGNATURES_FILE_SOURCE}" ||
curl --fail --silent --show-error --location --retry 5 --output "${GPG_SIGNATURES_FILE}" "${GPG_SIGNATURES_FILE_SOURCE}"
torsocks curl --fail --silent --show-error --location --retry 2 --output "${BITCOIN_TARBALL_TEMPORARY_DIRECTORY}" "${BITCOIN_TARBALL_FILE_SOURCE}" ||
curl --fail --silent --show-error --location --retry 5 --output "${BITCOIN_TARBALL_TEMPORARY_DIRECTORY}" "${BITCOIN_TARBALL_FILE_SOURCE}"
else
curl --fail --silent --show-error --location --retry 5 --output "${BITCOIN_HASH_FILE}" "${BITCOIN_HASH_FILE_SOURCE}"
curl --fail --silent --show-error --location --retry 5 --output "${GPG_SIGNATURES_FILE}" "${GPG_SIGNATURES_FILE_SOURCE}"
curl --fail --silent --show-error --location --retry 5 --output "${BITCOIN_TARBALL_TEMPORARY_DIRECTORY}" "${BITCOIN_TARBALL_FILE_SOURCE}"
fi
printf '%s\n' 'ok.'
printf ' %s' 'Validating the checksum... '
cd "${TEMP_DIRECTORY}"/
if command -v sha256sum > /dev/null; then
readonly SHA256_CHECK="$(grep "${BITCOIN_TARBALL_FILENAME}" "${BITCOIN_HASH_FILENAME}" | sha256sum --check 2> /dev/null)"
elif command -v shasum > /dev/null; then
readonly SHA256_CHECK="$(grep "${BITCOIN_TARBALL_FILENAME}" "${BITCOIN_HASH_FILENAME}" | shasum -a 256 --check 2> /dev/null)"
else
throw_error 'Either sha256sum or shasum must on PATH. Please open an issue at github com bitcoin-tools nodebuilder.'
fi
cd "${HOME}"/
case "${SHA256_CHECK}" in
*'OK'*) ;;
*)
throw_error "INVALID CHECKSUM. The download has failed. This script cannot continue due to security concerns. Please review the temporary file ${TEMP_DIRECTORY}/${BITCOIN_HASH_FILE}."
;;
esac
printf '%s\n' 'ok.'
printf ' %s' 'Validating the signatures... '
if [ -d "${GUIX_SIGS_DESTINATION_DIRECTORY}"/ ]; then
gpg --quiet --import "${GUIX_SIGS_DESTINATION_DIRECTORY}"/builder-keys/*.gpg
else
# shellcheck disable=SC2015
command -v torsocks > /dev/null 2>&1 && [ "${TARGET_KERNEL}" != 'Darwin' ] &&
torsocks git clone --depth 1 --quiet "${GUIX_SIGS_REPO}" "${GUIX_SIGS_TEMPORARY_DIRECTORY}" ||
git clone --depth 1 --quiet "${GUIX_SIGS_REPO}" "${GUIX_SIGS_TEMPORARY_DIRECTORY}"
gpg --quiet --import "${GUIX_SIGS_TEMPORARY_DIRECTORY}"/builder-keys/*.gpg
fi
readonly GPG_GOOD_SIGNATURE_COUNT="$(gpg --verify "${GPG_SIGNATURES_FILE}" 2>&1 | grep -c '^gpg: Good signature from ')"
pgrep '^gpg-agent$' > /dev/null && gpgconf --kill gpg-agent
pgrep '^keyboxd$' > /dev/null && gpgconf --kill keyboxd
if [ "${GPG_GOOD_SIGNATURE_COUNT}" -ge "${GPG_GOOD_SIGNATURES_REQUIRED}" ]; then
printf '%s\n' "${GPG_GOOD_SIGNATURE_COUNT} good."
[ -d "$(dirname "${BITCOIN_TARBALL_DESTINATION_PATH}")" ] || mkdir -p "$(dirname "${BITCOIN_TARBALL_DESTINATION_PATH}")"
mv "${BITCOIN_TARBALL_TEMPORARY_DIRECTORY}" "${BITCOIN_TARBALL_DESTINATION_PATH}"
[ -d "${GUIX_SIGS_DESTINATION_DIRECTORY}"/ ] || mv "${GUIX_SIGS_TEMPORARY_DIRECTORY}" "${GUIX_SIGS_DESTINATION_DIRECTORY}"
rm "${BITCOIN_HASH_FILE}"
rm "${GPG_SIGNATURES_FILE}"
else
throw_error "INVALID SIGNATURES. The download has failed. This script cannot continue due to security concerns. Please review the temporary file ${TEMP_DIRECTORY}/${GPG_SIGNATURES_FILE}."
fi
fi
printf ' %s' 'Extracting Bitcoin Core... '
[ -d "${BITCOIN_CORE_EXTRACT_DIR}"/ ] || mkdir "${BITCOIN_CORE_EXTRACT_DIR}"
tar -xzf "${BITCOIN_TARBALL_DESTINATION_PATH}" -C "${BITCOIN_CORE_EXTRACT_DIR}"/ --strip-components=1
printf '%s\n' 'ok.'
printf ' %s' 'Running the unit tests... '
if [ "${TARGET_ARCHITECTURE}" = 'arm64' ]; then
codesign -s - "${BITCOIN_INSTALL_BIN_SOURCE}"/test_bitcoin
fi
readonly UNIT_TEST_RESPONSE="$("${BITCOIN_INSTALL_BIN_SOURCE}"/test_bitcoin 2>&1)"
case "${UNIT_TEST_RESPONSE}" in
*'No errors detected'*) ;;
*)
printf '\n%s\n' "${UNIT_TEST_RESPONSE}"
throw_error 'Unit tests failed.'
;;
esac
printf '%s\n %s' 'ok.' 'Installing Bitcoin Core... '
[ -d "${BITCOIN_INSTALL_LIB_DESTINATION}" ] ||
mkdir -p "${BITCOIN_INSTALL_LIB_DESTINATION}" 2> /dev/null ||
sudo mkdir "${BITCOIN_INSTALL_LIB_DESTINATION}" ||
throw_error "Unable to create directory ${BITCOIN_INSTALL_LIB_DESTINATION}."
case "${TARGET_KERNEL}" in
Linux)
sudo cp "${BITCOIN_INSTALL_LIB_SOURCE}"/libbitcoinconsensus.so.0.0.0 "${BITCOIN_INSTALL_LIB_DESTINATION}"/libbitcoinconsensus.so.0.0.0