-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinstall_stacks.sh
185 lines (158 loc) · 7.16 KB
/
install_stacks.sh
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
#!/bin/env bash
REQUIRED_DIRS=(
/etc/stacks-blockchain
/stacks-blockchain
)
for DIR in "${REQUIRED_DIRS[@]}"; do
if [ ! -d "${DIR}" ]; then
echo "[ install_stacks.sh ] - Creating missing dir: $DIR"
sudo mkdir -p "${DIR}"
fi
done
echo "[ install_stacks.sh ] - Cloning stacks-blockchain from https://github.com/stacks-network/stacks-blockchain"
git clone --depth 1 --branch master https://github.com/stacks-network/stacks-blockchain "${HOME}/stacks-blockchain" && cd "${HOME}/stacks-blockchain/testnet/stacks-node" || exit 1
echo "[ install_stacks.sh ] - Build and install stacks-blockchain binary -> /usr/local/bin/stacks-node"
cargo build --features monitoring_prom,slog_json --release --bin stacks-node
sudo cp -a "${HOME}/stacks-blockchain/target/release/stacks-node" "/usr/local/bin/stacks-node"
echo "[ install_stacks.sh ] - Creating stacks user/group and setting filesytem permissions"
sudo useradd stacks
sudo chown -R stacks:stacks /stacks-blockchain/
PRIV_KEY="privateKey from npx keychain"
WIF="privateKey from npx keychain"
BTC_ADDRESS="btcAddress from npx keychain"
echo "[ install_stacks.sh ] - Installing required js modules"
cd "${HOME}" && npm install @stacks/cli shx rimraf
echo "[ install_stacks.sh ] - Generating keychain file?"
CMD="sudo ls /root/keychain.json 2> /dev/null 1>/dev/null"
eval "${CMD}"
CHECK_KEYCHAIN_FILE="${?}"
if [ ${CHECK_KEYCHAIN_FILE} != "0" ]; then
echo "[ install_stacks.sh ] - Creating keychain file -> /root/keychain.json"
sudo bash -c 'npx @stacks/cli make_keychain 2>/dev/null > /root/keychain.json'
SCAN_TIME_EPOCH=$(($(date +%s) - 3600)) # scan from 1 hour ago
else
echo "[ install_stacks.sh ] - Using existing keychain file -> /root/keychain.json"
SCAN_TIME_EPOCH=$((1)) # scan from genesis
fi
PRIV_KEY=$(sudo cat /root/keychain.json | jq .keyInfo.privateKey | tr -d '"')
WIF=$(sudo cat /root/keychain.json | jq .keyInfo.wif | tr -d '"')
BTC_ADDRESS=$(sudo cat /root/keychain.json | jq .keyInfo.btcAddress | tr -d '"')
STX_ADDRESS=$(sudo cat /root/keychain.json | jq .keyInfo.address | tr -d '"')
if [ ! -f "/bitcoin/wallet.dat" ]; then
echo "[ install_stacks.sh ] - Creating bitcoin wallet"
bitcoin-cli \
-rpcconnect=127.0.0.1 \
-rpcport=8332 \
-rpcuser=btcuser \
-rpcpassword=btcpass \
createwallet "miner" \
false \
false \
"" \
false \
false \
true
echo "[ install_stacks.sh ] - Importing btc address ${BTC_ADDRESS}"
bitcoin-cli \
-rpcconnect=127.0.0.1 \
-rpcport=8332 \
-rpcuser=btcuser \
-rpcpassword=btcpass \
importmulti "[{ \"scriptPubKey\": { \"address\": \"${BTC_ADDRESS}\" }, \"timestamp\":${SCAN_TIME_EPOCH}, \"keys\": [ \"${WIF}\" ]}]" "{\"rescan\": true}"
fi
echo "[ install_stacks.sh ] - Bitcoin address info for ${BTC_ADDRESS}"
bitcoin-cli \
-rpcconnect=127.0.0.1 \
-rpcport=8332 \
-rpcuser=btcuser \
-rpcpassword=btcpass \
getaddressinfo "${BTC_ADDRESS}"
echo "[ install_stacks.sh ] - Creating stacks config -> /etc/stacks-blockchain/Config.toml"
sudo bash -c 'cat <<EOF> /etc/stacks-blockchain/Config.toml
[node]
working_dir = "/stacks-blockchain"
rpc_bind = "0.0.0.0:20443" # to prevent external access, change to 127.0.0.1
p2p_bind = "0.0.0.0:20444" # to prevent external access, change to 127.0.0.1
bootstrap_node = "02196f005965cebe6ddc3901b7b1cc1aa7a88f305bb8c5893456b8f9a605923893@seed.mainnet.hiro.so:20444,02539449ad94e6e6392d8c1deb2b4e61f80ae2a18964349bc14336d8b903c46a8c@cet.stacksnodes.org:20444,02ececc8ce79b8adf813f13a0255f8ae58d4357309ba0cedd523d9f1a306fcfb79@sgt.stacksnodes.org:20444,0303144ba518fe7a0fb56a8a7d488f950307a4330f146e1e1458fc63fb33defe96@est.stacksnodes.org:20444"
seed = "PRIV_KEY"
local_peer_seed = "PRIV_KEY"
miner = true
mine_microblocks = false
[burnchain]
wallet_name = "miner"
chain = "bitcoin"
mode = "mainnet"
peer_host = "127.0.0.1"
username = "btcuser" # bitcoin rpc username from bitcoin config
password = "btcpass" # bitcoin rpc password from bitcoin config
rpc_port = 8332 # bitcoin rpc port from bitcoin config
peer_port = 8333 # bitcoin p2p port from bitcoin config
satoshis_per_byte = 100
burn_fee_cap = 450000
[miner]
mining_key = "PRIV_KEY"
activated_vrf_key_path = "/stacks-blockchain/saved_vrf_key.json"
[connection_options]
private_neighbors = false
EOF'
echo "[ install_stacks.sh ] - Updating /etc/stacks-blockchain/Config.toml with privateKey"
sudo sed -i -e "s|seed = \"PRIV_KEY\"|seed = \"${PRIV_KEY}\"|" /etc/stacks-blockchain/Config.toml
sudo sed -i -e "s|local_peer_seed = \"PRIV_KEY\"|local_peer_seed = \"${PRIV_KEY}\"|" /etc/stacks-blockchain/Config.toml
echo "[ install_stacks.sh ] - Creating systemd unit for stacks -> /etc/systemd/system/stacks.service"
sudo bash -c 'cat <<EOF> /etc/systemd/system/stacks.service
[Unit]
Description=Stacks Blockchain
Requires=bitcoin.service
After=bitcoin.service
ConditionFileIsExecutable=/usr/local/bin/stacks-node
ConditionPathExists=/stacks-blockchain/
ConditionFileNotEmpty=/etc/stacks-blockchain/Config.toml
[Service]
ExecStart=/bin/sh -c "/usr/local/bin/stacks-node start --config /etc/stacks-blockchain/Config.toml >> /stacks-blockchain/miner.log 2>&1"
ExecStartPost=/bin/sh -c "umask 022; sleep 2 && pgrep -f \"/usr/local/bin/stacks-node start --config /etc/stacks-blockchain/Config.toml\" > /run/stacks-blockchain/stacks.pid"
ExecStopPost=/bin/sh -c "if [ -f \"/run/stacks-blockchain/stacks.pid\" ]; then rm -f /run/stacks-blockchain/stacks.pid; fi"
# Process management
####################
Type=simple
PIDFile=/run/stacks-blockchain/stacks.pid
Restart=on-failure
TimeoutStopSec=600
KillSignal=SIGTERM
# Directory creation and permissions
####################################
# Run as stacks:stacks
User=stacks
Group=stacks
RuntimeDirectory=stacks-blockchain
RuntimeDirectoryMode=0710
# Hardening measures
####################
# Provide a private /tmp and /var/tmp.
PrivateTmp=true
# Mount /usr, /boot/ and /etc read-only for the process.
ProtectSystem=full
# Deny access to /home, /root and /run/user
ProtectHome=true
# Disallow the process and all of its children to gain
# new privileges through execve().
NoNewPrivileges=true
# Use a new /dev namespace only populated with API pseudo devices
# such as /dev/null, /dev/zero and /dev/random.
PrivateDevices=true
[Install]
WantedBy=multi-user.target
EOF'
echo "[ install_stacks.sh ] - Reloading systemd and starting stacks-blockchain service"
sudo systemctl daemon-reload
sudo systemctl enable stacks.service
sudo systemctl start stacks.service
echo "[ install_stacks.sh ] - Stacks Address: ${STX_ADDRESS}"
echo "[ install_stacks.sh ] - Bitcoin Address: ${BTC_ADDRESS}"
echo
echo "[ install_stacks.sh ] - *******************************************************************************"
echo "[ install_stacks.sh ] - ** Keychain file is stored at /root/keychain.json **"
echo "[ install_stacks.sh ] - ** Highly recommend that it be copied off the host to a secure location **"
echo "[ install_stacks.sh ] - *******************************************************************************"
echo
echo "[ install_stacks.sh ] - Tail the stacks-blockchain log: sudo tail -f /stacks-blockchain/miner.log"
exit 0