forked from ActorForth/bch-devsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·393 lines (325 loc) · 10.9 KB
/
setup
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
#!/usr/bin/env bash
set -e
# Ensure working directory is clean before setup
if [[ -f ".lock" ]]; then
echo "It appears you've already run this setup script before."
echo "Please run the ./clean script before trying to setup again."
exit 1
fi
BTK_SLEEP_TIME=6
BTK_BLOCKS=200
BTK_GENERATE_ADDRESS=bchreg:qzerfrqkvsclmrlx3hzl0j2ac2eh0k88wqa0efygtm
BTK_WIF=cUDjy47QLpAUyv1beivDPoYCHjCRVqwRMwCuSnkdF4teVFBDD1ip
BTK_ENV=$(uname)
BTK_NODE=0
BTK_REST=0
BTK_SLPDB=0
BTK_JS_API=0
BTK_NGINX=0
BTK_WALLET=0 # Under construction
BTK_NETWORK="bitcoin-bridging"
# Process user arguments, or display usage message
if [[ ! -z $1 ]]; then
for var in "$@"
do
[[ $var == "node" ]] && BTK_NODE=1
[[ $var == "rest" ]] && BTK_REST=1
[[ $var == "slpdb" ]] && BTK_SLPDB=1
if [[ $var == "wallet" ]]; then
BTK_WALLET=1
# BTK_NGINX=1
fi
if [[ $var == "js" ]]; then
BTK_JS_API=1
BTK_NGINX=1
fi
if [[ $var == "default" ]]; then
BTK_NODE=1
BTK_REST=1
BTK_SLPDB=1
BTK_NGINX=1
fi
if [[ $var == "all" ]]; then
BTK_NODE=1
BTK_REST=1
BTK_SLPDB=1
BTK_JS_API=1
BTK_WALLET=1
BTK_NGINX=1
echo "settingup all infrastructures"
fi
done
else
echo "usage: setup [command]"
echo "- node: Bitcoin Cash Node with ElectrumX indexing server"
echo "- rest: rest.bitcoin.com API service with OpenSight"
echo "- slpdb: SLPDB token indexing server"
echo "- js: JavaScript stack ready for BCH JS development"
echo "- wallet: Electron Cash SLP wallet GUI application"
echo "- all: all of the above"
exit 0
fi
echo "Setting up bch-toolkit..."
echo "Checking that necessary software is available: "
# Check that Docker and docker-compose are installed
echo "Looking for Docker..."
if [[ -x "$(command -v docker)" ]]; then
# Note: The following line may seem useless,
# however it causes the scirpt to error
# if A) Docker is not currently running, or
# B) permissions are not set correctly
# on Linux or FreeBSD host
BTK_DOCKER_STATUS=`docker ps`
echo "Docker found."
else
echo "Docker not found. Please install docker."
exit 1
fi
echo "Looking for docker-compose..."
if [[ -x "$(command -v docker-compose)" ]]; then
echo "Docker-compose found."
else
echo "Docker-compose not found. Please install docker-compose."
exit 1
fi
# Check and (if needed) create docker network for bch-toolkit
if [[ -z $(docker network ls --filter name=^${BTK_NETWORK}$ --format="{{ .Name }}") ]]; then
docker network create ${BTK_NETWORK}
fi
# Configure docker-compose files for the selected stack
if [[ $BTK_NODE == 1 ]]; then
# export COMPOSE_FILE="./dc_files/docker-compose.bitcoinunlimited.yml"
# export COMPOSE_FILE="./dc_files/docker-compose.bitcoincashnode.yml"
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.electrumx.yml"
# The next steps initiate the regtest node before proceeding to the
# final configurations (which includes REST, OpenSight, Wallet, etc)
# Save state of compose_file (before initilizing node)
# export PRE_NETWORK_FILE="$COMPOSE_FILE"
# Temporarily add network to docker-compose file
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.network.yml"
# Create temporary env file
# echo "COMPOSE_FILE=$COMPOSE_FILE" >> ".env"
# Initailze regtest node
docker pull zquestz/bitcoin-unlimited:latest
echo "Starting local node servers..."
docker-compose up -d bitcoind-regtest
sleep $BTK_SLEEP_TIME
# Generate N blocks
echo "Generating $BTK_BLOCKS initial blocks for regtest..."
# echo "Generating $BTK_BLOCKS blocks for regtest to address"
# echo $BTK_GENERATE_ADDRESS
docker-compose exec -T bitcoind-regtest bitcoin-cli generate $BTK_BLOCKS > /dev/null
sleep $BTK_SLEEP_TIME
# Stop the server(s)
docker-compose exec -T bitcoind-regtest bitcoin-cli stop
# docker-compose stop
# Remove temporary env file
# rm ".env"
# Reset the compose_file
# export COMPOSE_FILE="$PRE_NETWORK_FILE"
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.fulcrum.yml"
fi
if [[ $BTK_NGINX == 1 ]]; then
# Check that openssl is installed and available
echo "Looking for openssl..."
if [[ -x "$(command -v openssl)" ]]; then
echo "Openssl found."
else
echo "Openssl not found. Please install openssl."
exit 1
fi
# Create new SSL certificate/key pair to be used on nginx server
if [[ -d "./configs/cert" ]]; then
if [[ -f "./configs/cert/cert.key" && -f "./configs/cert/cert.crt" ]]; then
echo "OpenSSL certificates already exist."
else
echo "Removing previous certs directory"
rm -rf ./configs/cert
fi
fi
if [[ ! -d "./configs/cert" ]]; then
mkdir ./configs/cert
echo "Creating SSL certificate to use for nginx reverse proxy server."
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ./configs/cert/cert.key \
-out ./configs/cert/cert.crt \
-subj "/C=no/ST=no/L=no/O=no/OU=no/CN=no/emailAddress=no"
fi
export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.nginx.yml"
fi
if [[ $BTK_REST == 1 ]]; then
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.opensight.yml"
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.restapi.yml"
docker pull actorforth/opensight:latest
docker pull actorforth/rest.bitcoin.com:latest
fi
if [[ $BTK_JS_API == 1 ]]; then
export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.bch-api.yml"
fi
if [[ $BTK_SLPDB == 1 ]]; then
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.mongodb.yml"
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.slpdb.yml"
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.slpserve.yml"
docker pull mongo:4.2-bionic
docker pull actorforth/slpdb:latest
docker pull actorforth/slpserve:latest
fi
# Add network to docker-compose file
# export COMPOSE_FILE="$COMPOSE_FILE:./dc_files/docker-compose.network.yml"
# echo "COMPOSE_FILE=$COMPOSE_FILE" >> ".env"
touch ".lock"
if [[ $BTK_WALLET == 1 ]]; then
# Check that git is installed
echo "Looking for git..."
if [[ -x "$(command -v git)" ]]; then
echo "Git found."
else
echo "Git not found. Please install git."
exit 1
fi
# Check that python3 is installed
if [ -x "$(command -v python3)" ]; then
echo "Python3 found."
else
echo "Python (version 3) not found. Please install Python 3."
exit 1
fi
# This is ugly for now but should work, will clean up in future commit
echo "Looking for virtualenv..."
##
## Currently working on adding support for anaconda
##
# set +e
# BTK_VENV_VER=`python3 -m virtualenv --version | grep -o 20`
# set -e
#
# if [ -z "$BTK_VENV_VER" ]; then
# echo "Looking for Anaconda instead..."
# if [ -x "$(command -v conda)" ]; then
# BTK_CONDA=1
# echo "Anaconda found."
# fi
# fi
# TODO: Add anaconda support
# TODO: Throw error if virtualenv version too low
BTK_VENV_VER=`python3 -m virtualenv --version | grep -o 20`
if [[ $BTK_VENV_VER == 20 || $BTK_CONDA == 1 ]]; then
echo "Virtualenv found."
else
echo "Neither virtualenv nor conda could be found. Please install virtualenv."
exit 1
fi
# Linux hosts require qt5 library available
if [[ $BTK_ENV == "Linux" ]]; then
BTK_QT_FOUND=0
BTK_DEB=0
if [[ -x "$(command -v dpkg-query)" ]]; then
BTK_DEB=1
if dpkg-query -s qtchooser ; then
BTK_QT_FOUND=1
fi
elif [[ -x "$(command -v /usr/bin/qmake)" ]]; then
BTK_QT_FOUND=1
fi
if [[ $BTK_QT_FOUND == 1 ]]; then
echo "(Linux Host) Qt5 library found."
else
echo "(Linux Host) Your machine appears" \
"to be missing the Qt5 libraries necessary for" \
"running the Electrum Cash SLP wallet."
if [[ $BTK_DEB == 1 ]]; then
echo "On Ubuntu/Debian-based distributions," \
"it may be available in your package" \
"manager under the name 'qt5-default'."
exit 1
fi
fi
fi
# Check and (if necessary) download+setup the Electron Cash SLP wallet
if [ -d "./Electron-Cash-SLP" ]; then
echo "Electron Cash SLP already exists."
else
echo "Downloading Electron Cash SLP wallet..."
git clone https://github.com/simpleledger/Electron-Cash-SLP.git
fi
echo "Setting up python virtual environment..."
if [ ! -d "./Electron-Cash-SLP/venv" ]; then
cd ./Electron-Cash-SLP
if [[ $BTK_CONDA == 1 ]]; then
echo "This script doesn't currently work with conda."
exit 1
# conda create --name venv python=3.7 -y
else
python3 -m virtualenv ./venv
fi
source ./venv/bin/activate
python3 ./setup.py build
python3 ./setup.py install
cd ..
else
echo "Environment already exists."
fi
# Start the servers and get the genesis & verification block, and merkle root
if [ -f "./.env" ]; then
echo "Starting local node servers..."
docker-compose up -d
sleep $BTK_SLEEP_TIME
# Generate some blocks, and get their hashes/merkle roots
docker-compose exec -T bitcoind-regtest bitcoin-cli getblock 0 > ./Electron-Cash-SLP/genesis.txt
docker-compose exec -T bitcoind-regtest bitcoin-cli getblock 150 > ./Electron-Cash-SLP/fork.txt
docker-compose exec -T bitcoind-regtest bitcoin-cli getblock 200 > ./Electron-Cash-SLP/verification.txt
# Push hashes into environmental variables
cd ./Electron-Cash-SLP
BTK_GEN_HASH=$(grep \"hash\" genesis.txt | \
awk '{ print substr(substr($2, 2), 1, length($2)-3) }')
BTK_FOR_HASH=$(grep \"hash\" fork.txt | \
awk '{ print substr(substr($2, 2), 1, length($2)-3) }')
BTK_VER_MERK=$(grep merkle verification.txt | \
awk '{ print substr(substr($2, 2), 1, length($2)-3) }')
rm genesis.txt fork.txt verification.txt
# Patch the Electron-SLP-Cash files
echo "Patching Electon Cash SLP wallet files to work with regtest network..."
cd ./lib
rm ./servers_testnet.json
cat <<-EOF > "./servers_testnet.json"
{
"127.0.0.1": {
"s": "50002"
}
}
EOF
rm ./servers_slpdb_testnet.json
cat <<-EOF > "./servers_slpdb_testnet.json"
{
"127.0.0.1": {}
}
EOF
if [[ $BTK_ENV == "Darwin" || $BTK_ENV == "FreeBSD" ]]; then
sed -i "" "91s|GENESIS.*$|GENESIS = \"${BTK_GEN_HASH}|" networks.py
sed -i "" \
"98s|BITCOIN_CASH_FORK_BLOCK_HEIGHT =.*$|BITCOIN_CASH_FORK_BLOCK_HEIGHT = 150|" \
networks.py
sed -i "" \
"99s|BITCOIN_CASH_FORK_BLOCK_HASH =.*$|BITCOIN_CASH_FORK_BLOCK_HASH = \"${BTK_FOR_HASH}|" \
networks.py
sed -i "" \
"101s|VERIFICATION_BLOCK_MERKLE_ROOT =.*$|VERIFICATION_BLOCK_MERKLE_ROOT = \"${BTK_VER_MERK}|" \
networks.py
sed -i "" \
"102s|VERIFICATION_BLOCK_HEIGHT =.*$|VERIFICATION_BLOCK_HEIGHT = 200|" \
networks.py
fi
if [[ $BTK_ENV == "Linux" ]]; then
sed -i "91s|GENESIS.*$|GENESIS = \"${BTK_GEN_HASH}\"|" networks.py
sed -i "98s|BITCOIN_CASH_FORK_BLOCK_HEIGHT =.*$|BITCOIN_CASH_FORK_BLOCK_HEIGHT = 150|" networks.py
sed -i "99s|BITCOIN_CASH_FORK_BLOCK_HASH =.*$|BITCOIN_CASH_FORK_BLOCK_HASH = \"${BTK_FOR_HASH}|" networks.py
sed -i "101s|VERIFICATION_BLOCK_MERKLE_ROOT =.*$|VERIFICATION_BLOCK_MERKLE_ROOT = \"${BTK_VER_MERK}|" networks.py
sed -i "102s|VERIFICATION_BLOCK_HEIGHT =.*$|VERIFICATION_BLOCK_HEIGHT = 200|" networks.py
fi
cd ..
# echo $BTK_VER_MERK > merkle.txt
cd ..
docker-compose stop
fi
fi
echo "Done."