forked from jcnelson/naka3.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naka3.sh
executable file
·1349 lines (1135 loc) · 34.6 KB
/
naka3.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
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/bash
set -uoe pipefail
DEBUG=1
CONFIG="./config.sh"
PROGNAME="$0"
TEMPLATES="$(dirname "$PROGNAME")/conf.in"
function debug() {
if [ "$DEBUG" -ne 0 ]; then
echo "DEBG [$(date +%s.%N)] $1" >/dev/stderr
fi
}
function exit_error() {
echo "$1" >/dev/stderr
exit 1
}
# Get the path to the bitcoin PID file
# $1: config file
function get_bitcoind_pid_path() {
local conf_path="$1"
source "$conf_path"
echo "$(conf_get_bitcoind_data_dir)/bitcoin.pid"
}
# Get the path to the bitcoin log file
# $1: config file
function get_bitcoind_logfile_path() {
local conf_path="$1"
source "$conf_path"
echo "$(conf_get_bitcoind_data_dir)/bitcoin.log"
}
# Get the path to the signer PID file
# $1: config file
# $2: signer ID
function get_signer_pid_path() {
local conf_path="$1"
local signer_id="$2"
source "$conf_path"
echo "$(conf_get_basedir)/signer-${signer_id}.pid"
}
# Get the path to the signer log file
# $1: config file
# $2: signer ID
function get_signer_logfile_path() {
local conf_path="$1"
local signer_id="$2"
source "$conf_path"
echo "$(conf_get_basedir)/signer-${signer_id}.log"
}
# Get the path to the signer DB file
# $1: config file
# $2: signer ID
function get_signer_db_path() {
local conf_path="$1"
local signer_id="$2"
source "$conf_path"
echo "$(conf_get_stacks_signer_data_dir)/signer-${signer_id}.db"
}
# Get the path to the signer config template
function get_signer_template_path() {
echo "$TEMPLATES/signer.toml.in"
}
# Get the path to the signer config file
# $1: config file
# $2: signer ID
function get_signer_config_path() {
local conf_path="$1"
local signer_id="$2"
source "$conf_path"
echo "$(conf_get_stacks_signer_data_dir)/signer-${signer_id}.toml"
}
# Get the PoX address for the signer
# $1: config file
# $2: signer ID
function get_signer_pox_address() {
local conf_path="$1"
local signer_id="$2"
source "$conf_path"
conf_get_stacks_signer_pox_address
}
# Get the signer private key
# $1: config file
# $2: signer ID
function get_signer_private_key() {
local conf_path="$1"
local signer_id="$2"
source "$conf_path"
conf_get_stacks_signer_private_key
}
# Get the path to the node's PID file
# $1: config file
# $2: node ID
function get_node_pid_path() {
local conf_path="$1"
local node_id="$2"
source "$conf_path"
echo "$(conf_get_basedir)/node-${node_id}.pid"
}
# Get the path to the node log file
# $1: config file
# $2: node ID
function get_node_logfile_path() {
local conf_path="$1"
local node_id="$2"
source "$conf_path"
echo "$(conf_get_basedir)/node-${node_id}.log"
}
# Get the path to the node chainstate dir
# $1: config file
# $2: node ID
function get_node_data_dir() {
local conf_path="$1"
local node_id="$2"
source "$conf_path"
echo "$(conf_get_stacks_node_data_dir)/node-${node_id}"
}
# Get the path to the node config file
# $1: config file
# $2: node ID
function get_node_config_path() {
local conf_path="$1"
local node_id="$2"
source "$conf_path"
echo "$(conf_get_stacks_node_data_dir)/node-${node_id}.toml"
}
# Get the path to the node config template
function get_node_template_path() {
echo "$TEMPLATES/stacks-node.toml.in"
}
# Get the path to the node's event observer config template
function get_node_event_observer_template_path() {
echo "$TEMPLATES/stacks-node-event-observer.toml.in"
}
# Run bitcoind
# $1: p2p port
# $2: rpc port
# $3: data directory
# $4: logfile
# $5: pidfile
function run_bitcoind() {
local port="$1"
local rpcport="$2"
local datadir="$3"
local logfile="$4"
local pidfile="$5"
local pid
bitcoind \
--regtest \
--nodebug \
--nodebuglogfile \
--rest \
--txindex=1 \
--server=1 \
--listenonion=0 \
--rpcbind=0.0.0.0 \
--port="$port" \
--rpcport="$rpcport" \
--datadir="$datadir" \
--rpcuser="naka3" \
--rpcpassword="naka3" > "$logfile" 2>&1 &
pid="$!"
echo "$pid" > "$pidfile"
debug "Bitcoind started: pid $pid"
}
# Run a signer
# $1: signer config file
# $2: logfile
# $3: pidfile
function run_signer() {
local signer_conf="$1"
local logfile="$2"
local pidfile="$3"
local pid
set -e
BLOCKSTACK_DEBUG=1 STACKS_LOG_DEBUG=1 RUST_BACKTRACE=full stacks-signer check-config -c "$signer_conf" >/dev/null 2>&1 || exit_error "Invalid signer config"
set +e
BLOCKSTACK_DEBUG=1 STACKS_LOG_DEBUG=1 RUST_BACKTRACE=full stacks-signer run -c "$signer_conf" > "$logfile" 2>&1 &
pid="$!"
echo "$pid" > "$pidfile"
debug "Signer started: PID $pid"
}
# Run a node
# $1: node config file
# $2: logfile
# $3: pidfile
function run_node() {
local node_conf="$1"
local logfile="$2"
local pidfile="$3"
local pid
BLOCKSTACK_DEBUG=1 STACKS_LOG_DEBUG=1 RUST_BACKTRACE=full stacks-node start --config "$node_conf" > "$logfile" 2>&1 &
pid="$!"
echo "$pid" > "$pidfile"
debug "Node started: PID $pid"
}
# Start bitcoind
# Writes the PID to the given .pid file
# $1: config file
# $2: resume (true|false)
function start_bitcoind() {
local conf_path="$1"
local resume="$2"
local pid_path
local port
local rpcport
local datadir
local rc
source "$conf_path"
pid_path="$(get_bitcoind_pid_path "$conf_path")"
logfile="$(get_bitcoind_logfile_path "$conf_path")"
# NOTE: these functions are loaded from conf_path
port="$(conf_get_bitcoind_p2p_port)"
rpcport="$(conf_get_bitcoind_rpc_port)"
datadir="$(conf_get_bitcoind_data_dir)"
if [ -f "$pid_path" ]; then
echo "(already running $(cat "$pid_path"))"
return 0
fi
if [ -d "$datadir" ] && [[ "$resume" != "true" ]]; then
rm -rf "$datadir"
mkdir -p "$datadir"
fi
run_bitcoind "$port" "$rpcport" "$datadir" "$logfile" "$pid_path"
# wait for it to start up
while true; do
set +e
bitcoin-cli -rpcconnect="127.0.0.1:$rpcport" -rpcuser="naka3" -rpcpassword="naka3" ping 2>/dev/null
rc=$?
set -e
if [ $rc -eq 0 ]; then
break
fi
sleep 1
done
echo "PID $(cat "$pid_path")"
return 0
}
# Make a signer config file
# $1: config file
# $2: signer ID
# $3: template file
# prints the path
function make_signer_config() {
local conf_path="$1"
local signer_id="$2"
local template_path="$3"
local port
local rpcport
local key
local host
local signer_host
local signer_port
local dbfile
local signer_conf_path
if ! [ -f "$template_path" ]; then
exit_error "No such file or directory: $template_path"
fi
dbfile="$(get_signer_db_path "$conf_path" "$signer_id")"
signer_conf_path="$(get_signer_config_path "$conf_path" "$signer_id")"
source "$conf_path"
rm -f "$dbfile"
# NOTE: these functions are loaded from conf_path
port="$(conf_get_stacks_p2p_port)"
rpcport="$(conf_get_stacks_rpc_port)"
key="$(conf_get_stacks_signer_key)"
host="$(conf_get_stacks_host)"
signer_host="$(conf_get_stacks_signer_host)"
signer_port="$(conf_get_stacks_signer_port)"
sed -r \
-e "s!@@SIGNER_KEY@@!$key!g" \
-e "s!@@STACKS_HOST@@!$host!g" \
-e "s!@@STACKS_RPC_PORT@@!$rpcport!g" \
-e "s!@@SIGNER_DB@@!$dbfile!g" \
-e "s!@@SIGNER_HOST@@!$signer_host!g" \
-e "s!@@SIGNER_PORT@@!$signer_port!g" \
"$template_path" > "$signer_conf_path"
echo "$signer_conf_path"
return 0
}
# Start a signer
# Writes the PID to the given .pid file
# $1: config file
# $2: signer ID
# prints the result
function start_signer() {
local conf_path="$1"
local signer_id="$2"
local pid_path
local signer_config_template_path
local signer_config_path
pid_path="$(get_signer_pid_path "$conf_path" "$signer_id")"
logfile="$(get_signer_logfile_path "$conf_path" "$signer_id")"
signer_config_template_path="$(get_signer_template_path)"
signer_config_path="$(make_signer_config "$conf_path" "$signer_id" "$signer_config_template_path")"
if [ -f "$pid_path" ]; then
echo "(already running $(cat "$pid_path"))"
return 0
fi
run_signer "$signer_config_path" "$logfile" "$pid_path"
echo "PID $(cat "$pid_path")"
return 0
}
# Make a stacks node config file
# Call this _after_ generating signer config files
# $1: config file
# $2: node ID
# $3: template file
# $4: event observer template file
# $5: miner (true/false)
# $6: stacker (true/false)
# $7: associated signer IDs as a CSV (pass 'none' if there is none)
# prints the path
function make_node_config() {
local conf_path="$1"
local node_id="$2"
local template_path="$3"
local event_observer_template_path="$4"
local miner="$5"
local stacker="$6"
local signers="$7"
local port
local rpcport
local miner_key
local btc_host
local btc_port
local btc_rpcport
local node_conf_path
local datadir
local seed_host
local seed_port
local stacks_host
local signer_datadir
if ! [ -f "$template_path" ]; then
exit_error "No such file or directory: $template_path"
fi
datadir="$(get_node_data_dir "$conf_path" "$node_id")"
node_conf_path="$(get_node_config_path "$conf_path" "$node_id")"
source "$conf_path"
# NOTE: these functions are loaded from conf_path
port="$(conf_get_stacks_p2p_port)"
rpcport="$(conf_get_stacks_rpc_port)"
btc_host="$(conf_get_bitcoind_host)"
btc_port="$(conf_get_bitcoind_p2p_port)"
btc_rpcport="$(conf_get_bitcoind_rpc_port)"
seed_pubkey="$(conf_get_seed_pubkey)"
seed_host="$(conf_get_seed_host)"
seed_port="$(conf_get_seed_p2p_port)"
miner_key="$(conf_get_stacks_miner_key)"
stacks_host="$(conf_get_stacks_host)"
peer_key="$(conf_get_stacks_peer_key)"
sed -r \
-e "s!@@STACKS_RPC_PORT@@!$rpcport!g" \
-e "s!@@STACKS_P2P_PORT@@!$port!g" \
-e "s!@@STACKS_PUBLIC_HOST@@!$stacks_host!g" \
-e "s!@@SEED_NODE_HOST@@!$seed_host!g" \
-e "s!@@SEED_NODE_P2P_PORT@@!$seed_port!g" \
-e "s!@@SEED_NODE_PUBKEY@@!$seed_pubkey!g" \
-e "s!@@STACKS_MINER_KEY@@!$miner_key!g" \
-e "s!@@STACKS_PEER_KEY@@!$peer_key!g" \
-e "s!@@STACKS_MINER@@!$miner!g" \
-e "s!@@STACKS_STACKER@@!$stacker!g" \
-e "s!@@BITCOIN_HOST@@!$btc_host!g" \
-e "s!@@BITCOIN_P2P_PORT@@!$btc_port!g" \
-e "s!@@BITCOIN_RPC_PORT@@!$btc_rpcport!g" \
-e "s!@@STACKS_DATA_DIR@@!$datadir!g" \
"$template_path" > "$node_conf_path"
if [ "$signers" != "none" ]; then
# load up all signer endpoints
signer_datadir="$(conf_get_stacks_signer_data_dir)"
local signer_conf
local endpoint
local signer_host
local signer_port
local cur_signer
local remaining_signers
signers="$signers,"
while [ -n "$signers" ]; do
signers="${signers#,}"
remaining_signers="${signers#*,}"
cur_signer="${signers%"$remaining_signers"}"
cur_signer="${cur_signer%,}"
signers="$remaining_signers"
debug "Add signer '$cur_signer', remaining is '$signers'"
signer_conf="$signer_datadir/signer-${cur_signer}.toml"
# extract the endpoint
endpoint="$(grep -E "^endpoint[^=]*=" "$signer_conf" | \
sed -r 's/^endpoint[^=]*=[ ]*"(.+)"[ ]*$/\1/g')"
# extract host and port
signer_host="${endpoint%:*}"
signer_port="${endpoint#*:}"
if [ "$signer_host" = "0.0.0.0" ]; then
signer_host="127.0.0.1"
fi
sed -r \
-e "s!@@SIGNER_ENDPOINT@@!$signer_host:$signer_port!g" \
"$event_observer_template_path" >> "$node_conf_path"
done
fi
echo "$node_conf_path"
return 0
}
# Set a fault injection in the existing node config.
# $1: config file path
# $2: fault name
# $3: fault value
function add_node_fault_injection() {
local node_config_path="$1"
local fault_name="$2"
local fault_value="$3"
if ! [ -f "$node_config_path" ]; then
exit_error "No such file or directory: $conf_path"
fi
sed -i \
-e "s!# @@${fault_name}@@!${fault_name} = ${fault_value}!g" \
"$node_config_path"
return 0
}
# Start a stacks node.
# Call _after_ generating signer configs
# Writes the PID to the given .pid file
# $1: config file
# $2: node ID
# prints the result
function start_node() {
local conf_path="$1"
local node_id="$2"
local pid_path
local datadir
node_config_path="$(get_node_config_path "$conf_path" "$node_id")"
if ! [ -f "$node_config_path" ]; then
exit_error "ERROR: cannot start node $node_id: no such config file $node_config_path"
fi
pid_path="$(get_node_pid_path "$conf_path" "$node_id")"
logfile="$(get_node_logfile_path "$conf_path" "$node_id")"
datadir="$(get_node_data_dir "$conf_path" "$node_id")"
if [ -f "$pid_path" ]; then
echo "(already running $(cat "$pid_path"))"
return 0
fi
if [ -d "$datadir" ]; then
echo "rm -rf '$datadir'"
mkdir -p "$datadir"
fi
run_node "$node_config_path" "$logfile" "$pid_path"
echo "PID $(cat "$pid_path")"
return 0
}
# Stop a process
# $1: pid path
function stop_process() {
local pid_path="$1"
local rc
if ! [ -f "$pid_path" ]; then
echo "(not running)"
rm -f "$pid_path"
return 0
fi
pid="$(cat "$pid_path")"
set +e
kill -s SIGTERM "$pid" 2>/dev/null
rc=$?
if [ $rc -ne 0 ]; then
kill -s 0 "$pid" 2>/dev/null
rc=$?
if [ $rc -ne 0 ]; then
echo "(not running)"
rm -f "$pid_path"
set -e
return 0
fi
else
waitpid "$pid" 2>/dev/null
fi
set -e
rm -f "$pid_path"
echo "done"
}
# Stop bitcoind
# $1: conf file
function stop_bitcoind() {
local conf_path="$1"
local pid_path
local pid
local rc
source "$conf_path"
pid_path="$(get_bitcoind_pid_path "$conf_path")"
stop_process "$pid_path"
}
# Stop the signer
# $1: conf file
# $2: signer id
function stop_signer() {
local conf_path="$1"
local signer_id="$2"
local pid_path
local pid
source "$conf_path"
pid_path="$(get_signer_pid_path "$conf_path" "$signer_id")"
stop_process "$pid_path"
}
# Stop the node
# $1: conf file
# $2: node id
function stop_node() {
local conf_path="$1"
local node_id="$2"
local pid_path
local pid
source "$conf_path"
pid_path="$(get_node_pid_path "$conf_path" "$node_id")"
stop_process "$pid_path"
}
# Run a bitcoin-cli command
# $1: config path
# arguments are all passed to bitcoin-cli
function run_bitcoin_cli() {
local conf_path="$1"
local rpcport
source "$conf_path"
rpcport="$(conf_get_bitcoind_rpc_port)"
shift 1
bitcoin-cli -rpcconnect=127.0.0.1 -rpcport="$rpcport" -rpcuser=naka3 -rpcpassword=naka3 -rpcwallet=main "$@"
}
# Run a blockstack-cli command
# arguments are all passed to blockstack-cli
function run_blockstack_cli() {
blockstack-cli --testnet "$@"
}
# Tail a log file
# Keep trying even if it doesn't exist
# $1: logfile path
function follow_logs() {
local logfile="$1"
while true; do
if ! [ -f "$logfile" ]; then
echo "Waiting for $logfile to become available..."
sleep 1
continue
fi
tail -f "$logfile"
done
}
# Convert a byte stream to a hex string
# from https://stackoverflow.com/questions/9515007/linux-script-to-convert-byte-data-into-a-hex-string
function hex_encode() {
od -An -v -tx1 | tr -d ' \n'
}
# Decode b58 text into a hex byte stream
# $1: base58 text
# from https://github.com/grondilu/bitcoin-bash-tools
function b58_decode() {
local base58_chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxy"
local b58text="$1"
if [[ "$b58text" =~ ^(1*)([$base58_chars]+)$ ]]; then
dc -e "${BASH_REMATCH[1]//1/0P} 0${base58_chars//?/ds&1+} 0${BASH_REMATCH[2]//?/ 58*l&+}P" | hex_encode
else
return 1
fi
}
# Decode a base58 Bitcoin address into "$version_byte $hash_bytes"
# $1: address
# prints the above
function b58_address_decode() {
local addr="$1"
b58_decode "$addr" | sed -r 's/^([0-9a-f]{2})([0-9a-f]{40}).+$/\1 \2#/g' | tr '#' '\n'
}
# Make a signer's stacking transaction
# $1: config
# $2: signer ID
# $3: reward cycle
# $4: max amount
# $5: nonce
# $6: auth ID
function make_stacking_tx() {
local conf_file="$1"
local signer_id="$2"
local reward_cycle="$3"
local max_amount="$4"
local nonce="$5"
local auth_id="$6"
local signer_config_path
local pox_address
local signer_json
local signer_pubkey
local signer_signature
local signer_privkey
local address_parts
local address_version
local hash_bytes
local start_burn_block=$((reward_cycle * 20 + 1))
signer_config_path="$(get_signer_config_path "$conf_file" "$signer_id")"
signer_privkey="$(get_signer_private_key "$conf_file" "$signer_id")"
pox_address="$(get_signer_pox_address "$conf_file" "$signer_id")"
address_parts="$(b58_address_decode "$pox_address")"
address_version="$(echo "$address_parts" | cut -d ' ' -f 1)"
if [ "$address_version" != "6f" ]; then
exit_error "Only support testnet p2pkh"
fi
hash_bytes="$(echo "$address_parts" | cut -d ' ' -f 2)"
signer_json="$(stacks-signer generate-stacking-signature \
--pox-address "$pox_address" \
--reward-cycle "$reward_cycle" \
--config "$signer_config_path" \
--method "stack-stx" \
--period 12 \
--max-amount "$max_amount" \
--auth-id "$auth_id" \
| (
local line
local pubkey
local sig
while read -r line; do
if [[ "$line" =~ "Signer Public Key:" ]]; then
pubkey="$(echo "$line" | cut -d ':' -f 2 | sed -r 's/ //g')"
elif [[ "$line" =~ "Signer Key Signature:" ]]; then
sig="$(echo "$line" | cut -d ':' -f 2 | sed -r 's/ //g')"
fi
done
echo "{\"pubkey\": \"$pubkey\", \"sig\": \"$sig\"}"
)
)"
signer_pubkey="$(echo "$signer_json" | jq -r '.pubkey')"
signer_signature="$(echo "$signer_json" | jq -r '.sig')"
run_blockstack_cli contract-call \
"$signer_privkey" \
1000 \
"$nonce" \
"ST000000000000000000002AMW42H" \
"pox-4" \
"stack-stx" \
-e "u${max_amount}" \
-e "{ version: 0x00, hashbytes: 0x${hash_bytes} }" \
-e "u${start_burn_block}" \
-e "u12" \
-e "(some ${signer_signature})" \
-e "${signer_pubkey}" \
-e "u${max_amount}" \
-e "u${auth_id}"
}
# Get an account's nonce
# $1: config
# $2: address
function get_account_nonce() {
local conf_path="$1"
local address="$2"
local stacks_host
local stacks_port
local nonce
stacks_host="$(conf_get_seed_host)"
stacks_port="$(conf_get_seed_rpc_port)"
nonce="$(curl -sL "http://${stacks_host}:${stacks_port}/v2/accounts/${address}?proof=0" | jq -r '.nonce')"
echo "$nonce"
}
# Send a transaction
# $1: transaction hex
# $2: host
# $3: port
function send_tx() {
local tx="$1"
local stacks_host="$2"
local stacks_port="$3"
local content_length
content_length="${#tx}"
content_length=$((content_length / 2))
echo -n "$tx" | xxd -r -p | \
curl -sL -X POST -H "content-type: application/octet-stream" -H "content-length: $content_length" --data-binary @- "http://${stacks_host}:${stacks_port}/v2/transactions"
}
# Make a stx-transfer transaction.
# Automatically fetches the right nonce.
# The fee rate is hard-coded to 360, which is 2x a single-sig stx-transfer's length
# $1: config
# $2: private key
# $3: amount
# $4: recipient address
# $5: memo
function make_stx_transfer() {
local conf_file="$1"
local private_key="$2"
local amount="$3"
local recipient="$4"
local memo="$5"
local address
local nonce
address="$(run_blockstack_cli addresses "$private_key" | jq -r '.STX')"
nonce="$(get_account_nonce "$conf_file" "$address")"
run_blockstack_cli token-transfer \
"$private_key" \
"360" \
"$nonce" \
"$recipient" \
"$amount" \
"$memo"
}
# Continuously send a stx-transfer transaction from an address, for a given amount, to a given recipient
# $1: config
# $2: private key
# $3: amount
# $4: recipient address
# $5: sleep time between transmission
# $6: abort file -- if this file exists, then this loop breaks
function begin_stx_transfers() {
local conf_file="$1"
local private_key="$2"
local amount="$3"
local recipient="$4"
local sleep_time="$5"
local abort_file="$6"
local tx
local stacks_host
local stacks_port
local content_length
local address
local nonce
local response
address="$(run_blockstack_cli addresses "$private_key" | jq -r '.STX')"
nonce="$(get_account_nonce "$conf_file" "$address")"
stacks_host="$(conf_get_seed_host)"
stacks_port="$(conf_get_seed_rpc_port)"
while ! [ -f "$abort_file" ]; do
tx="$(run_blockstack_cli token-transfer \
"$private_key" \
"360" \
"$nonce" \
"$recipient" \
"$amount" \
"naka3")"
content_length="${#tx}"
content_length=$((content_length / 2))
set -e
response="$(send_tx "$tx" "$stacks_host" "$stacks_port")"
if [ -z "$(echo "$response" | jq -r '.error' 2>/dev/null)" ]; then
nonce=$((nonce + 1))
fi
set +e
sleep "$sleep_time"
done
}
# Stop a running begin_stx_transfers loop
# $1: abort file
function end_stx_transfers() {
local abort_file="$1"
touch "$abort_file"
}
# Print usage and exit
# $1: subcommand usage
function usage() {
local cmd="$1"
case "$cmd" in
bitcoind)
exit_error "Usage: $PROGNAME bitcoind start|resume|stop|mine|peer"
;;
bitcoin-cli)
exit_error "Usage: $PROGNAME bitcoin-cli [args...]"
;;
signer)
exit_error "Usage: $PROGNAME signer [signer_id] config|start|stop|logs|stack-tx"
;;
node)
exit_error "Usage: $PROGNAME node [node_id] config-miner|config-follower|config-miner-stacker|config-follower-stacker|miner-addr|start|stop|logs"
;;
tx)
exit_error "Usage: $PROGNAME tx transfer|begin-transfers|end-transfers [args...]"
;;
"$PROGNAME")
exit_error "Need a command"
;;
*)
exit_error "Unrecognized command '$cmd'. Options are bitcoind, bitcoin-cli, signer, node, tx"
;;
esac
}
function main() {
local cmd
set +ue
cmd="$1"
set -ue
debug "Command is '$cmd'"
case "$cmd" in
bitcoind)
local subcmd="$2"
debug "Subcmmand is '$subcmd'"
case "$subcmd" in
start)
echo -n "Starting bitcoind... "
start_bitcoind "$CONFIG" "false"
echo -n "Instantiating 'main' wallet... "
run_bitcoin_cli "$CONFIG" createwallet "main" true >/dev/null 2>&1
echo "OK"
;;
resume)
echo -n "Resuming bitcoind..."
start_bitcoind "$CONFIG" "true"
run_bitcoin_cli "$CONFIG" loadwallet "main" >/dev/null 2>&1
echo "OK"
;;
stop)
echo -n "Stopping bitcoind... "
stop_bitcoind "$CONFIG"
;;
mine)
set +ue
local num_blocks="$3"
local addr="$4"
if [ -z "$num_blocks" ] || [ -z "$addr" ]; then
echo >&2 "Number of blocks and/or address not given"
usage "bitcoind"
fi
set -ue
run_bitcoin_cli "$CONFIG" generatetoaddress "$num_blocks" "$addr"
;;
peer)
set +ue
local peer_addr="$3"
local peer_port="$4"
if [ -z "$peer_addr" ] || [ -z "$peer_port" ]; then
echo >&2 "Missing peer addr and/or port"
usage "bitcoind"
fi
set -ue
echo -n "Peering $peer_addr:$peer_port..."
run_bitcoin_cli "$CONFIG" addnode "$peer_addr:$peer_port" "add"
echo "OK"
;;
*)
usage "bitcoind"
;;
esac
;;
bitcoin-cli)
shift 1
run_bitcoin_cli "$CONFIG" "$@"
;;
signer)
set +ue
local signer_id="$2"
debug "Signer ID is '$signer_id'"
local subcmd="$3"
debug "Subcommand is '$subcmd'"
set -ue
if [ -z "$signer_id" ]; then
echo >&2 "Signer ID not given"
usage "signer"
fi
if [ -z "$subcmd" ]; then
echo >&2 "Subcommand not given"
usage "signer"
fi
case "$subcmd" in
config|configure|make-config)
echo -n "Making config for signer '$signer_id'... "
local signer_config_path