-
Notifications
You must be signed in to change notification settings - Fork 174
/
cntools.sh
executable file
·3021 lines (2712 loc) · 137 KB
/
cntools.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
# shellcheck disable=SC1090,SC2086,SC2154,SC2034,SC2012
########## Global tasks ###########################################
tput smcup # Save screen
# General exit handler
cleanup() {
[[ -n $1 ]] && err=$1 || err=$?
tput rmcup # restore screen
tput cnorm # restore cursor
tput sgr0 # turn off all attributes
[[ -n ${exit_msg} ]] && echo -e "\n${exit_msg}\n" || echo -e "\nCNTools terminated, cleaning up...\n"
exit $err
}
trap cleanup HUP INT TERM
trap 'stty echo' EXIT
# Command : myExit [exit code] [message]
# Description : gracefully handle an exit and restore terminal to original state
myExit() {
exit_msg="$2"
cleanup "$1"
}
clear
usage() {
cat <<EOF
Usage: $(basename "$0") [-o]
CNTools - The Cardano SPOs best friend
-o Activate offline mode - run CNTools in offline mode without node access, a limited set of functions available
EOF
}
OFL_MODE="false"
while getopts :o opt; do
case ${opt} in
o )
OFL_MODE="true"
;;
\? )
myExit 1 "$(usage)"
;;
esac
done
shift $((OPTIND -1))
# get common env variables
. "$(dirname $0)"/env
# get cntools config parameters
. "$(dirname $0)"/cntools.config
# get helper functions from library file
. "$(dirname $0)"/cntools.library
# create temporary directory if missing
mkdir -p "${TMP_FOLDER}" # Create if missing
if [[ ! -d "${TMP_FOLDER}" ]]; then
myExit 1 "${RED}ERROR${NC}: Failed to create directory for temporary files:\n${TMP_FOLDER}"
fi
# check for required command line tools
if ! need_cmd "curl" || \
! need_cmd "jq" || \
! need_cmd "bc" || \
! need_cmd "sed" || \
! need_cmd "awk" || \
! need_cmd "column"; then myExit 1 "${need_cmd_error}"
fi
# Do some checks when run in online mode
if [[ ${OFL_MODE} = "false" ]]; then
# check to see if there are any updates available
clear
say "CNTools version check...\n"
URL="https://raw.githubusercontent.com/cardano-community/guild-operators/master/scripts/cnode-helper-scripts"
if wget -q -T 10 -O "${TMP_FOLDER}"/cntools.library "${URL}/cntools.library"; then
GIT_MAJOR_VERSION=$(grep -r ^CNTOOLS_MAJOR_VERSION= "${TMP_FOLDER}"/cntools.library |sed -e "s#.*=##")
GIT_MINOR_VERSION=$(grep -r ^CNTOOLS_MINOR_VERSION= "${TMP_FOLDER}"/cntools.library |sed -e "s#.*=##")
GIT_PATCH_VERSION=$(grep -r ^CNTOOLS_PATCH_VERSION= "${TMP_FOLDER}"/cntools.library |sed -e "s#.*=##")
if [[ "$GIT_PATCH_VERSION" -eq 999 ]]; then
((GIT_MAJOR_VERSION++))
GIT_MINOR_VERSION=0
GIT_PATCH_VERSION=0
fi
if [[ "$CNTOOLS_PATCH_VERSION" -eq 999 ]]; then
# CNTools was updated using special 999 patch tag, apply correct version in cntools.library and update variables already sourced
sed -i "s/CNTOOLS_MAJOR_VERSION=[[:digit:]]\+/CNTOOLS_MAJOR_VERSION=$((++CNTOOLS_MAJOR_VERSION))/" "$CNODE_HOME/scripts/cntools.library"
sed -i "s/CNTOOLS_MINOR_VERSION=[[:digit:]]\+/CNTOOLS_MINOR_VERSION=0/" "$CNODE_HOME/scripts/cntools.library"
sed -i "s/CNTOOLS_PATCH_VERSION=[[:digit:]]\+/CNTOOLS_PATCH_VERSION=0/" "$CNODE_HOME/scripts/cntools.library"
# CNTOOLS_MAJOR_VERSION variable already updated in sed replace command
CNTOOLS_MINOR_VERSION=0
CNTOOLS_PATCH_VERSION=0
CNTOOLS_VERSION="${CNTOOLS_MAJOR_VERSION}.${CNTOOLS_MINOR_VERSION}.${CNTOOLS_PATCH_VERSION}"
fi
if [[ "${CNTOOLS_MAJOR_VERSION}" != "${GIT_MAJOR_VERSION}" || "${CNTOOLS_MINOR_VERSION}" != "${GIT_MINOR_VERSION}" || "${CNTOOLS_PATCH_VERSION}" != "${GIT_PATCH_VERSION}" ]]; then
say "A new version of CNTools is available" "log"
echo
say "Installed Version : ${CNTOOLS_VERSION}" "log"
say "Available Version : ${GREEN}${GIT_MAJOR_VERSION}.${GIT_MINOR_VERSION}.${GIT_PATCH_VERSION}${NC}" "log"
say "\nGo to Update section for upgrade\n\nAlternately, follow https://cardano-community.github.io/guild-operators/#/basics?id=pre-requisites to update cntools as well alongwith any other files"
waitForInput "press any key to proceed"
else
# check if CNTools was recently updated, if so show whats new
URL_DOCS="https://raw.githubusercontent.com/cardano-community/guild-operators/master/docs/Scripts"
if wget -q -T 10 -O "${TMP_FOLDER}"/cntools-changelog.md "${URL_DOCS}/cntools-changelog.md"; then
if ! cmp -s "${TMP_FOLDER}"/cntools-changelog.md "$CNODE_HOME/scripts/cntools-changelog.md"; then
# Latest changes not shown, show whats new and copy changelog
clear
say "~ CNTools - What's New ~"
if [[ ! -f "$CNODE_HOME/scripts/cntools-changelog.md" ]]; then
# special case for first installation or 5.0.0 upgrade, print release notes until previous major version
waitForInput "Press any key to show what's new in last major release, use 'q' to quit viewer"
sed -n "/\[${CNTOOLS_MAJOR_VERSION}\.${CNTOOLS_MINOR_VERSION}\.${CNTOOLS_PATCH_VERSION}\]/,/\[$((CNTOOLS_MAJOR_VERSION-1))\.[0-9]\.[0-9]\]/p" "${TMP_FOLDER}"/cntools-changelog.md | head -n -2 | less
else
# print release notes from current until previously installed version
waitForInput "Press any key to show what's new compared to currently installed release, use 'q' to quit viewer"
[[ $(cat "$CNODE_HOME/scripts/cntools-changelog.md") =~ \[([[:digit:]])\.([[:digit:]])\.([[:digit:]])\] ]]
sed -n "/\[${CNTOOLS_MAJOR_VERSION}\.${CNTOOLS_MINOR_VERSION}\.${CNTOOLS_PATCH_VERSION}\]/,/\[${BASH_REMATCH[1]}\.${BASH_REMATCH[2]}\.${BASH_REMATCH[3]}\]/p" "${TMP_FOLDER}"/cntools-changelog.md | head -n -2 | less
fi
cp "${TMP_FOLDER}"/cntools-changelog.md "$CNODE_HOME/scripts/cntools-changelog.md"
fi
else
say "\n${RED}ERROR${NC}: failed to download changelog from GitHub!\n"
waitForInput "press any key to proceed"
fi
fi
else
say "\n${RED}ERROR${NC}: failed to download cntools.library from GitHub, unable to perform version check!\n"
waitForInput "press any key to proceed"
fi
# Check if config is a valid json file
if ! jq -e . >/dev/null 2>&1 "${CONFIG}"; then
myExit 1 "${RED}ERROR${NC}: Please ensure that your config file is in JSON format\nConfig file: ${CONFIG}"
fi
# Verify that Prometheus is enabled in config file
prom_port=$(jq -r '.hasPrometheus[1] //empty' ${CONFIG} 2>/dev/null)
prom_host=$(jq -r '.hasPrometheus[0] //empty' ${CONFIG} 2>/dev/null)
if [[ -z "${prom_port}" ]]; then
myExit 1 "${RED}ERROR${NC}: Please ensure that hasPrometheus is enabled, if unsure - rerun \"<path>/prereqs.sh -s\" again - and it would overwrite the config file"
fi
# Get protocol parameters and save to ${TMP_FOLDER}/protparams.json
[[ -f "${TMP_FOLDER}"/protparams.json ]] && rm -f "${TMP_FOLDER}"/protparams.json 2>/dev/null
${CCLI} shelley query protocol-parameters ${PROTOCOL_IDENTIFIER} ${NETWORK_IDENTIFIER} >"${TMP_FOLDER}/protparams.json" 2>&1
if grep -q "Network.Socket.connect" "${TMP_FOLDER}/protparams.json"; then
myExit 1 "${ORANGE}WARN${NC}: node socket path wrongly configured or node not running, please verify that socket set in env file match what is used to run the node\n\n\
${BLUE}Re-run CNTools in offline mode with -o parameter if you want to access CNTools with limited functionality${NC}"
elif ! jq . "${TMP_FOLDER}/protparams.json" &>/dev/null; then
myExit 1 "${ORANGE}WARN${NC}: failed to query protocol parameters, ensure your node is running with correct genesis (the node needs to be in sync to 1 epoch after the hardfork)\n\n\
Error message: $(cat "${TMP_FOLDER}/protparams.json")\n\n\
${BLUE}Re-run CNTools in offline mode with -o parameter if you want to access CNTools with limited functionality${NC}"
fi
fi
# check if there are pools in need of KES key rotation
clear
kes_rotation_needed="no"
while IFS= read -r -d '' pool; do
if [[ -f "${pool}/${POOL_CURRENT_KES_START}" ]]; then
kesExpiration "$(cat "${pool}/${POOL_CURRENT_KES_START}")"
if [[ ${expiration_time_sec_diff} -lt ${KES_ALERT_PERIOD} ]]; then
kes_rotation_needed="yes"
say "\n** WARNING **\nPool ${GREEN}$(basename ${pool})${NC} in need of KES key rotation"
if [[ ${expiration_time_sec_diff} -lt 0 ]]; then
say "${RED}Keys expired!${NC} : ${RED}$(showTimeLeft ${expiration_time_sec_diff:1})${NC} ago"
else
say "Remaining KES periods : ${RED}${remaining_kes_periods}${NC}"
say "Time left : ${RED}$(showTimeLeft ${expiration_time_sec_diff})${NC}"
fi
elif [[ ${expiration_time_sec_diff} -lt ${KES_WARNING_PERIOD} ]]; then
kes_rotation_needed="yes"
say "\nPool ${GREEN}$(basename ${pool})${NC} soon in need of KES key rotation"
say "Remaining KES periods : ${ORANGE}${remaining_kes_periods}${NC}"
say "Time left : ${ORANGE}$(showTimeLeft ${expiration_time_sec_diff})${NC}"
fi
fi
done < <(find "${POOL_FOLDER}" -mindepth 1 -maxdepth 1 -type d -print0 | sort -z)
[[ ${kes_rotation_needed} = "yes" ]] && waitForInput "press any key to proceed"
# Verify if the combinator network is already on shelley and if so, the epoch of transition
if [[ "${PROTOCOL}" == "Cardano" ]]; then
shelleyTransitionEpoch=$(cat "${SHELLEY_TRANS_FILENAME}" 2>/dev/null)
if [[ -z "${shelleyTransitionEpoch}" ]]; then
clear
if [[ "${NETWORK_IDENTIFIER}" == "--mainnet" ]]; then
shelleyTransitionEpoch="208"
elif [[ ${OFL_MODE} = "false" ]]; then
getPromMetrics
slot_in_epoch=$(grep "cardano_node_ChainDB_metrics_slotInEpoch_int" "${TMP_FOLDER}"/prom_metrics | awk '{print $2}')
slot_num=$(grep "cardano_node_ChainDB_metrics_slotNum_int" "${TMP_FOLDER}"/prom_metrics | awk '{print $2}')
epoch=$(grep "cardano_node_ChainDB_metrics_epoch_int" "${TMP_FOLDER}"/prom_metrics | awk '{print $2}')
calc_slot=0
byron_epochs=${epoch}
shelley_epochs=0
while [[ ${byron_epochs} -ge 0 ]]; do
calc_slot=$(( (byron_epochs*byron_epoch_length) + (shelley_epochs*epoch_length) + slot_in_epoch ))
[[ ${calc_slot} -eq ${slot_num} ]] && break
((byron_epochs--))
((shelley_epochs++))
done
node_sync="NODE SYNC: $(printTable ',' "$(echo -e "Epoch,Slot in Epoch,Slot\n${epoch},${slot_in_epoch},${slot_num}")")"
if [[ ${calc_slot} -ne ${slot_num} ]]; then
myExit 1 "${ORANGE}WARN${NC}: Failed to calculate shelley transition epoch\n\n${node_sync}"
elif [[ ${shelley_epochs} -eq 0 ]]; then
myExit 1 "${ORANGE}WARN${NC}: The network has not reached the hard fork from Byron to shelley, please wait to use CNTools until your node is in shelley era\n\n${node_sync}"
else
shelleyTransitionEpoch=${byron_epochs}
fi
else
myExit 1 "${ORANGE}WARN${NC}: Offline mode enabled and config set to TestNet, please manually create and set shelley transition epoch in:\n${SHELLEY_TRANS_FILENAME}"
fi
echo "${shelleyTransitionEpoch}" > "${SHELLEY_TRANS_FILENAME}"
fi
fi
###################################################################
function main {
while true; do # Main loop
# Start with a clean slate after each completed or canceled command excluding protparams.json from purge
find "${TMP_FOLDER:?}" -type f -not -name 'protparams.json' -delete
clear
say "$(printf "%-52s %s" " >> CNTools $CNTOOLS_VERSION << " "A Guild Operators collaboration")" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say " Main Menu"
echo
say " ) Wallet - create, show, remove and protect wallets"
say " ) Funds - send, withdraw and delegate"
say " ) Pool - pool creation and management"
say " ) Blocks - show core node leader slots"
say " ) Update - update cntools script and library config files"
say " ) Backup - backup & restore of wallet/pool/config"
say " ) Refresh - reload home screen content"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say "$(printf "%84s" "Epoch $(getEpoch) - $(timeUntilNextEpoch) until next")"
if [[ ${OFL_MODE} = "true" ]]; then
say "What would you like to do?"
else
tip_diff=$(getSlotTipDiff)
slot_interval=$(slotInterval)
if [[ ${tip_diff} -le $(( slot_interval * 2 )) ]]; then
say "$(printf " %-20s %73s" "What would you like to do?" "Node Sync: ${GREEN}${tip_diff} :)${NC}")"
elif [[ ${tip_diff} -le $(( slot_interval * 3 )) ]]; then
say "$(printf " %-20s %73s" "What would you like to do?" "Node Sync: ${ORANGE}${tip_diff} :|${NC}")"
else
say "$(printf " %-20s %73s" "What would you like to do?" "Node Sync: ${RED}${tip_diff} :(${NC}")"
fi
fi
echo
case $(select_opt "[w] Wallet" "[f] Funds" "[p] Pool" "[b] Blocks" "[u] Update" "[z] Backup & Restore" "[r] Refresh" "[q] Quit") in
0) OPERATION="wallet" ;;
1) OPERATION="funds" ;;
2) OPERATION="pool" ;;
3) OPERATION="blocks" ;;
4) OPERATION="update" ;;
5) OPERATION="backup" ;;
6) continue ;;
7) myExit 0 "CNTools closed!" ;;
esac
case $OPERATION in
wallet)
clear
say " >> WALLET" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say " Wallet Management"
echo
say " ) New - create a new wallet"
say " ) List - list all available wallets in a compact view"
say " ) Show - show detailed view of a specific wallet"
say " ) Remove - remove a wallet"
say " ) Decrypt - remove write protection and decrypt wallet"
say " ) Encrypt - encrypt wallet keys and make all files immutable"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say " Select Wallet operation\n"
case $(select_opt "[n] New" "[l] List" "[s] Show" "[r] Remove" "[d] Decrypt" "[e] Encrypt" "[h] Home") in
0) SUBCOMMAND="new" ;;
1) SUBCOMMAND="list" ;;
2) SUBCOMMAND="show" ;;
3) SUBCOMMAND="remove" ;;
4) SUBCOMMAND="decrypt" ;;
5) SUBCOMMAND="encrypt" ;;
6) continue ;;
esac
case $SUBCOMMAND in
new)
clear
say " >> WALLET >> NEW" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
read -r -p "Name of new wallet: " wallet_name
# Remove unwanted characters from wallet name
wallet_name=${wallet_name//[^[:alnum:]]/_}
if [[ -z "${wallet_name}" ]]; then
say "${RED}ERROR${NC}: Empty wallet name, please retry!"
waitForInput && continue
fi
echo
mkdir -p "${WALLET_FOLDER}/${wallet_name}"
# Wallet key filenames
payment_sk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_PAY_SK_FILENAME}"
payment_vk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_PAY_VK_FILENAME}"
stake_vk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_STAKE_VK_FILENAME}"
stake_sk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_STAKE_SK_FILENAME}"
if [[ $(find "${WALLET_FOLDER}/${wallet_name}" -type f -printf '.' | wc -c) -gt 0 ]]; then
say "${RED}WARN${NC}: A wallet ${GREEN}$wallet_name${NC} already exists"
say " Choose another name or delete the existing one"
waitForInput && continue
fi
${CCLI} shelley address key-gen --verification-key-file "${payment_vk_file}" --signing-key-file "${payment_sk_file}"
${CCLI} shelley stake-address key-gen --verification-key-file "${stake_vk_file}" --signing-key-file "${stake_sk_file}"
getBaseAddress ${wallet_name}
getPayAddress ${wallet_name}
getRewardAddress ${wallet_name}
say "New Wallet : ${GREEN}${wallet_name}${NC}" "log"
say "Address : ${base_addr}" "log"
say "Enterprise Address : ${pay_addr}" "log"
say "\nYou can now send and receive ADA using the above addresses."
say "Note that Enterprise Address will not take part in staking."
say "Wallet will be automatically registered on chain if you\nchoose to delegate or pledge wallet when registering a stake pool."
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
waitForInput && continue
;; ###################################################################
list)
clear
say " >> WALLET >> LIST" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
if [[ ${OFL_MODE} = "true" ]]; then
say "${CYAN}OFFLINE MODE${NC}: CNTools started in offline mode, wallet balance not shown!"
fi
[[ ! "$(ls -A "${WALLET_FOLDER}")" ]] && echo && say "${ORANGE}No wallets available!${NC}" "log"
while IFS= read -r -d '' wallet; do
wallet_name=$(basename ${wallet})
enc_files=$(find "${wallet}" -mindepth 1 -maxdepth 1 -type f -name '*.gpg' -printf '.' | wc -c)
echo
if [[ ${enc_files} -gt 0 ]]; then
say "${GREEN}${wallet_name}${NC} (${ORANGE}encrypted${NC})" "log"
else
say "${GREEN}${wallet_name}${NC}" "log"
fi
getBaseAddress ${wallet_name}
getPayAddress ${wallet_name}
if [[ ${OFL_MODE} = "true" ]]; then
[[ -n ${base_addr} ]] && say "$(printf "%-15s : %s" "Address" "${base_addr}")" "log"
[[ -n ${pay_addr} ]] && say "$(printf "%-15s : %s" "Enterprise Addr" "${pay_addr}")" "log"
else
if [[ -n ${base_addr} ]]; then
getBalance ${base_addr}
say "$(printf "%-16s : %s" "Address" "${base_addr}")" "log"
say "$(printf "%-16s : ${CYAN}%s${NC} ADA" "Funds" "$(formatLovelace ${lovelace})")" "log"
fi
if [[ -n ${pay_addr} ]]; then
getBalance ${pay_addr}
if [[ ${lovelace} -gt 0 ]]; then
say "$(printf "%-16s : %s" "Enterprise Addr" "${pay_addr}")" "log"
say "$(printf "%-16s : ${CYAN}%s${NC} ADA" "Enterprise Funds" "$(formatLovelace ${lovelace})")" "log"
fi
fi
if [[ -z ${base_addr} && -z ${pay_addr} ]]; then
say "${RED}Not a supporeted wallet${NC} - genesis address?"
say "Use an external script to send funds to a CNTools compatible wallet"
continue
fi
getRewards ${wallet_name}
if [[ "${reward_lovelace}" -ge 0 ]]; then
say "$(printf "%-16s : ${CYAN}%s${NC} ADA" "Rewards" "$(formatLovelace ${reward_lovelace})")" "log"
delegation_pool_id=$(jq -r '.delegation // empty' <<< "${stakeAddressInfo}")
if [[ -n ${delegation_pool_id} ]]; then
unset poolName
while IFS= read -r -d '' pool; do
getPoolID "$(basename ${pool})"
if [[ "${pool_id_bech32}" = "${delegation_pool_id}" ]]; then
poolName=$(basename ${pool}) && break
fi
done < <(find "${POOL_FOLDER}" -mindepth 1 -maxdepth 1 -type d -print0 | sort -z)
say "${RED}Delegated to${NC} ${BLUE}${poolName}${NC} ${RED}(${delegation_pool_id})${NC}" "log"
fi
fi
fi
done < <(find "${WALLET_FOLDER}" -mindepth 1 -maxdepth 1 -type d -print0 | sort -z)
echo
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
waitForInput
;; ###################################################################
show)
clear
say " >> WALLET >> SHOW" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
if [[ ${OFL_MODE} = "true" ]]; then
say "${CYAN}OFFLINE MODE${NC}: CNTools started in offline mode, limited wallet info shown!\n"
fi
wallet_dirs=()
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
for dir in "${dirs[@]}"; do
enc_files=$(find "${WALLET_FOLDER}/${dir}" -mindepth 1 -maxdepth 1 -type f -name '*.gpg' -printf '.' | wc -c)
if ! getBaseAddress "${dir}" && ! getPayAddress "${dir}" && [[ ${enc_files} -eq 0 ]]; then continue; fi
wallet_dirs+=("${dir}")
done
if [[ ${#wallet_dirs[@]} -eq 0 ]]; then
echo
say "${ORANGE}No wallets available!${NC}" "log"
echo
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
waitForInput && continue
fi
say "Select Wallet:\n"
if ! selectDir "${wallet_dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
wallet_name=${dir_name}
enc_files=$(find "${WALLET_FOLDER}/${wallet_name}" -mindepth 1 -maxdepth 1 -type f -name '*.gpg' -printf '.' | wc -c)
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
if [[ ${enc_files} -gt 0 ]]; then
say "$(printf "%-8s ${GREEN}%s${NC} ${ORANGE}%s${NC}" "Wallet" "${wallet_name}" "(encrypted)")" "log"
else
say "$(printf "%-8s ${GREEN}%s${NC}" "Wallet" "${wallet_name}")" "log"
fi
getBaseAddress ${wallet_name}
getPayAddress ${wallet_name}
if [[ ${OFL_MODE} = "false" ]]; then
getBalance ${base_addr}
base_lovelace=${lovelace}
if [[ ${utx0_count} -gt 0 ]]; then
echo
say "${BLUE}UTxOs${NC}"
head -n 2 "${TMP_FOLDER}"/fullUtxo.out
head -n 10 "${TMP_FOLDER}"/balance.out
[[ ${utx0_count} -gt 10 ]] && say "... (top 10 UTx0 with most lovelace)"
fi
getBalance ${pay_addr}
pay_lovelace=${lovelace}
if [[ ${utx0_count} -gt 0 ]]; then
echo
say "${BLUE}Enterprise UTxOs${NC}"
head -n 2 "${TMP_FOLDER}"/fullUtxo.out
head -n 10 "${TMP_FOLDER}"/balance.out
[[ ${utx0_count} -gt 10 ]] && say "... (top 10 UTx0 with most lovelace)"
fi
fi
echo
say "$(printf "%-19s : %s" "Address" "${base_addr}")" "log"
if [[ ${OFL_MODE} = "false" ]]; then
say "$(printf "%-19s : ${CYAN}%s${NC} ADA" "Funds" "$(formatLovelace ${base_lovelace})")" "log"
getAddressInfo "${base_addr}"
say "$(printf "%-19s : %s" "Era" "$(jq -r '.era' <<< ${address_info})")" "log"
say "$(printf "%-19s : %s" "Encoding" "$(jq -r '.encoding' <<< ${address_info})")" "log"
fi
say "$(printf "%-19s : %s" "Enterprise Address" "${pay_addr}")" "log"
if [[ ${OFL_MODE} = "false" ]]; then
say "$(printf "%-19s : ${CYAN}%s${NC} ADA" "Enterprise Funds" "$(formatLovelace ${pay_lovelace})")" "log"
getRewards ${wallet_name}
if [[ "${reward_lovelace}" -ge 0 ]]; then
say "$(printf "%-19s : ${CYAN}%s${NC} ADA" "Rewards" "$(formatLovelace ${reward_lovelace})")" "log"
delegation_pool_id=$(jq -r '.delegation // empty' <<< "${stakeAddressInfo}")
if [[ -n ${delegation_pool_id} ]]; then
unset poolName
while IFS= read -r -d '' pool; do
getPoolID "$(basename ${pool})"
if [[ "${pool_id_bech32}" = "${delegation_pool_id}" ]]; then
poolName=$(basename ${pool}) && break
fi
done < <(find "${POOL_FOLDER}" -mindepth 1 -maxdepth 1 -type d -print0 | sort -z)
echo
say "${RED}Delegated to${NC} ${BLUE}${poolName}${NC} ${RED}(${delegation_pool_id})${NC}" "log"
fi
fi
fi
echo
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
waitForInput
;; ###################################################################
remove)
clear
say " >> WALLET >> REMOVE" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
if [[ ${OFL_MODE} = "true" ]]; then
say "${CYAN}OFFLINE MODE${NC}: CNTools started in offline mode, unable to verify wallet balance\n"
waitForInput "Press key 'c' to continue or any other key to return to home menu"
[[ ${key} != "c" ]] && continue
fi
echo
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
if [[ ${#dirs[@]} -eq 0 ]]; then
say "${ORANGE}WARN${NC}: No wallets available!"
waitForInput && continue
fi
say "Select Wallet:\n"
if ! selectDir "${dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
wallet_name="${dir_name}"
if [[ ${OFL_MODE} = "true" ]]; then
say "\nAre you sure to delete wallet?\n"
case $(select_opt "[y] Yes" "[n] No") in
0) safeDel "${WALLET_FOLDER:?}/${wallet_name}"
;;
1) say "skipped removal process for ${GREEN}$wallet_name${NC}"
;;
esac
waitForInput && continue
fi
if ! getBaseAddress ${wallet_name} && ! getPayAddress ${wallet_name}; then
say "${RED}WARN${NC}: unable to get address for wallet and do a balance check"
say "\nAre you sure to delete wallet anyway?\n"
case $(select_opt "[y] Yes" "[n] No") in
0) safeDel "${WALLET_FOLDER:?}/${wallet_name}"
;;
1) say "skipped removal process for ${GREEN}$wallet_name${NC}"
;;
esac
waitForInput && continue
fi
if [[ -n ${base_addr} ]]; then
getBalance ${base_addr}
base_lovelace=${lovelace}
else
base_lovelace=0
fi
if [[ -n ${pay_addr} ]]; then
getBalance ${pay_addr}
pay_lovelace=${lovelace}
else
pay_lovelace=0
fi
getRewards ${wallet_name}
if [[ ${base_lovelace} -eq 0 && ${pay_lovelace} -eq 0 && ${reward_lovelace} -le 0 ]]; then
say "INFO: This wallet appears to be empty"
say "${RED}WARN${NC}: Deleting this wallet is final and you can not recover it unless you have a backup\n"
say "Are you sure to delete wallet?\n"
case $(select_opt "[y] Yes" "[n] No") in
0) safeDel "${WALLET_FOLDER:?}/${wallet_name}"
;;
1) say "skipped removal process for ${GREEN}$wallet_name${NC}"
;;
esac
else
say "${RED}WARN${NC}: wallet not empty!"
[[ ${base_lovelace} -gt 0 ]] && say "Funds : ${CYAN}$(formatLovelace ${base_lovelace})${NC} ADA"
[[ ${pay_lovelace} -gt 0 ]] && say "Enterprise Funds : ${CYAN}$(formatLovelace ${base_lovelace})${NC} ADA"
[[ ${reward_lovelace} -gt 0 ]] && say "Rewards : ${CYAN}$(formatLovelace ${reward_lovelace})${NC} ADA"
echo
say "${RED}WARN${NC}: Deleting this wallet is final and you can not recover it unless you have a backup\n"
say "Are you sure to delete wallet?\n"
case $(select_opt "[y] Yes" "[n] No") in
0) safeDel "${WALLET_FOLDER:?}/${wallet_name}"
;;
1) say "skipped removal process for ${GREEN}$wallet_name${NC}"
;;
esac
fi
waitForInput
;; ###################################################################
decrypt)
clear
say " >> WALLET >> DECRYPT" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
protectionPreRequisites || continue
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
if [[ ${#dirs[@]} -eq 0 ]]; then
say "${ORANGE}WARN${NC}: No wallets available!"
say "first create a wallet"
waitForInput && continue
fi
say "Select Wallet:\n"
if ! selectDir "${dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
wallet_name="${dir_name}"
filesUnlocked=0
keysDecrypted=0
say "# Removing write protection from all wallet files" "log"
while IFS= read -r -d '' file; do
if [[ $(lsattr -R "$file") =~ -i- ]]; then
sudo chattr -i "${file}" && \
chmod 600 "${file}" && \
filesUnlocked=$((++filesUnlocked))
say "${file}"
fi
done < <(find "${WALLET_FOLDER}/${wallet_name}" -mindepth 1 -maxdepth 1 -type f -print0)
echo
say "# Decrypting GPG encrypted wallet files" "log"
echo
say "Wallet ${GREEN}${wallet_name}${NC} Password"
echo
if ! getPassword; then # $password variable populated by getPassword function
say "\n\n" && say "${RED}ERROR${NC}: password input aborted!"
waitForInput && continue
fi
while IFS= read -r -d '' file; do
decryptFile "${file}" "${password}" && \
chmod 600 "${file::-4}" && \
keysDecrypted=$((++keysDecrypted))
done < <(find "${WALLET_FOLDER}/${wallet_name}" -mindepth 1 -maxdepth 1 -type f -name '*.gpg' -print0)
unset password
echo
say "Wallet unprotected: ${GREEN}${wallet_name}${NC}" "log"
say "Files unlocked: ${filesUnlocked}" "log"
say "Files decrypted: ${keysDecrypted}" "log"
if [[ ${filesUnlocked} -ne 0 || ${keysDecrypted} -ne 0 ]]; then
echo
say "${ORANGE}Wallet files are now unprotected${NC}"
say "Use 'WALLET >> ENCRYPT' to re-lock"
fi
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
waitForInput
;; ###################################################################
encrypt)
clear
say " >> WALLET >> ENCRYPT" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
protectionPreRequisites || continue
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
if [[ ${#dirs[@]} -eq 0 ]]; then
say "${ORANGE}WARN${NC}: No wallets available!"
say "first create a wallet"
waitForInput && continue
fi
say "Select Wallet:\n"
if ! selectDir "${dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
wallet_name="${dir_name}"
filesLocked=0
keysEncrypted=0
say "# Encrypting sensitive wallet keys with GPG" "log"
echo
say "Wallet ${GREEN}${wallet_name}${NC} Password"
echo
if ! getPassword confirm; then # $password variable populated by getPassword function
say "\n\n" && say "${RED}ERROR${NC}: password input aborted!"
waitForInput && continue
fi
keyFiles=(
"${WALLET_FOLDER}/${wallet_name}/${WALLET_PAY_SK_FILENAME}"
"${WALLET_FOLDER}/${wallet_name}/${WALLET_STAKE_SK_FILENAME}"
)
for keyFile in "${keyFiles[@]}"; do
if [[ -f "${keyFile}" ]]; then
chmod 400 "${keyFile}" && \
encryptFile "${keyFile}" "${password}" && \
keysEncrypted=$((++keysEncrypted))
fi
done
unset password
echo
say "# Write protecting all wallet keys using 'chattr +i'" "log"
while IFS= read -r -d '' file; do
if [[ ${file} != *.addr && ! $(lsattr -R "$file") =~ -i- ]]; then
chmod 400 "${file}" && \
sudo chattr +i "${file}" && \
filesLocked=$((++filesLocked)) && \
say "${file}"
fi
done < <(find "${WALLET_FOLDER}/${wallet_name}" -mindepth 1 -maxdepth 1 -type f -print0)
echo
say "Wallet protected: ${GREEN}${wallet_name}${NC}" "log"
say "Files locked: ${filesLocked}" "log"
say "Files encrypted: ${keysEncrypted}" "log"
if [[ ${filesLocked} -ne 0 || ${keysEncrypted} -ne 0 ]]; then
echo
say "${BLUE}Wallet files are now protected${NC}"
say "Use 'WALLET >> DECRYPT' to unlock"
fi
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
waitForInput
;; ###################################################################
esac
;; ###################################################################
funds)
clear
say " >> FUNDS" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say " Handle Funds"
echo
say " 1) Send - send ADA from a local wallet to an address or a wallet"
say " 2) Delegate - delegate stake wallet to a pool"
say " 3) Withdraw - withdraw earned rewards to base address"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say " Select funds operation\n"
case $(select_opt "[s] Send" "[d] Delegate" "[w] Withdraw Rewards" "[h] Home") in
0) SUBCOMMAND="send" ;;
1) SUBCOMMAND="delegate" ;;
2) SUBCOMMAND="withdrawrewards" ;;
3) continue ;;
esac
case $SUBCOMMAND in
withdrawrewards)
clear
say " >> FUNDS >> WITHDRAW REWARDS" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
if [[ ${OFL_MODE} = "true" ]]; then
say "${RED}ERROR${NC}: CNTools started in offline mode, option not available!"
waitForInput && continue
fi
echo
wallet_dirs=()
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
wallet_count=${#dirs[@]}
[[ ${wallet_count} -le ${WALLET_SELECTION_FILTER_LIMIT} ]] && say "Balance checking wallets...\n"
for dir in "${dirs[@]}"; do
stake_sk_file="${WALLET_FOLDER}/${dir}/${WALLET_STAKE_SK_FILENAME}"
stake_vk_file="${WALLET_FOLDER}/${dir}/${WALLET_STAKE_VK_FILENAME}"
pay_payment_sk_file="${WALLET_FOLDER}/${dir}/${WALLET_PAY_SK_FILENAME}"
[[ ! -f "${stake_sk_file}" || ! -f "${stake_vk_file}" || ! -f "${pay_payment_sk_file}" ]] && continue
if [[ ${wallet_count} -le ${WALLET_SELECTION_FILTER_LIMIT} ]]; then
getRewards ${dir}
[[ ${reward_lovelace} -le 0 ]] && continue
wallet_dirs+=("${dir} (Rewards: ${CYAN}$(formatLovelace ${reward_lovelace})${NC} ADA)")
else
wallet_dirs+=("${dir}")
fi
done
if [[ ${#wallet_dirs[@]} -eq 0 ]]; then
say "${ORANGE}WARN${NC}: No wallets available that have rewards to withdraw!"
say "Encrypted wallets not checked, please decrypt wallet first if encrypted"
waitForInput && continue
fi
say "Select Wallet:\n"
if ! selectDir "${wallet_dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
wallet_name="$(echo ${dir_name} | cut -d' ' -f1)"
getBaseAddress ${wallet_name}
stake_addr_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_STAKE_ADDR_FILENAME}"
stake_sk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_STAKE_SK_FILENAME}"
stake_vk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_STAKE_VK_FILENAME}"
pay_payment_sk_file="${WALLET_FOLDER}/${wallet_name}/${WALLET_PAY_SK_FILENAME}"
getBalance ${base_addr}
getRewards ${wallet_name}
if [[ ${reward_lovelace} -le 0 ]]; then
say "Failed to locate any rewards associated with the chosen wallet, please try another one"
waitForInput && continue
elif [[ ${lovelace} -eq 0 ]]; then
say "${ORANGE}WARN${NC}: No funds in base address, please send funds to base address of wallet to cover withdraw transaction fee"
waitForInput && continue
fi
say "$(printf "%s\t${CYAN}%s${NC} ADA" "Funds" "$(formatLovelace ${lovelace})")" "log"
say "$(printf "%s\t${CYAN}%s${NC} ADA" "Rewards" "$(formatLovelace ${reward_lovelace})")" "log"
if ! withdrawRewards "${stake_vk_file}" "${stake_sk_file}" "${pay_payment_sk_file}" "${base_addr}" "${reward_addr}" ${reward_lovelace}; then
echo && say "${RED}ERROR${NC}: failure during withdrawal of rewards"
waitForInput && continue
fi
if ! waitNewBlockCreated; then
waitForInput && continue
fi
getBalance ${base_addr}
while [[ ${lovelace} -ne ${newBalance} ]]; do
echo
say "${ORANGE}WARN${NC}: Balance mismatch, transaction not included in latest block ($(formatLovelace ${lovelace}) != $(formatLovelace ${newBalance}))"
if ! waitNewBlockCreated; then
break
fi
getBalance ${base_addr}
done
if [[ ${lovelace} -ne ${newBalance} ]]; then
waitForInput && continue
fi
getRewards ${wallet_name}
echo
say "$(printf "%s\t${CYAN}%s${NC} ADA" "Funds" "$(formatLovelace ${lovelace})")" "log"
say "$(printf "%s\t${CYAN}%s${NC} ADA" "Rewards" "$(formatLovelace ${reward_lovelace})")" "log"
waitForInput
;; ###################################################################
send)
clear
say " >> FUNDS >> SEND" "log"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
if [[ ${OFL_MODE} = "true" ]]; then
say "${RED}ERROR${NC}: CNTools started in offline mode, option not available!"
waitForInput && continue
fi
echo
s_wallet_dirs=()
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
wallet_count=${#dirs[@]}
[[ ${wallet_count} -le ${WALLET_SELECTION_FILTER_LIMIT} ]] && say "Balance checking wallets...\n"
for dir in "${dirs[@]}"; do
s_payment_sk_file="${WALLET_FOLDER}/${dir}/${WALLET_PAY_SK_FILENAME}"
[[ ! -f "${s_payment_sk_file}" ]] && continue
if [[ ${wallet_count} -le ${WALLET_SELECTION_FILTER_LIMIT} ]]; then
getBaseAddress ${dir}
getPayAddress ${dir}
getBalance ${base_addr}
base_lovelace=${lovelace}
getBalance ${pay_addr}
pay_lovelace=${lovelace}
if [[ ${base_lovelace} -gt 0 && ${pay_lovelace} -gt 0 ]]; then
s_wallet_dirs+=("${dir} (Funds: ${CYAN}$(formatLovelace ${base_lovelace})${NC} ADA | Enterprise Funds: ${CYAN}$(formatLovelace ${pay_lovelace})${NC} ADA)")
elif [[ ${base_lovelace} -gt 0 ]]; then
s_wallet_dirs+=("${dir} (Funds: ${CYAN}$(formatLovelace ${base_lovelace})${NC} ADA)")
elif [[ ${pay_lovelace} -gt 0 ]]; then
s_wallet_dirs+=("${dir} (Enterprise Funds: ${CYAN}$(formatLovelace ${pay_lovelace})${NC} ADA)")
fi
else
s_wallet_dirs+=("${dir}")
fi
done
if [[ ${#s_wallet_dirs[@]} -eq 0 ]]; then
say "${ORANGE}WARN${NC}: No wallets available that have funds to send!"
say "Encrypted wallets not checked, please decrypt wallet first if encrypted"
waitForInput && continue
fi
say "Select Source Wallet:\n"
if ! selectDir "${s_wallet_dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
s_wallet="$(echo ${dir_name} | cut -d' ' -f1)"
getBaseAddress ${s_wallet}
getPayAddress ${s_wallet}
getBalance ${base_addr}
base_lovelace=${lovelace}
getBalance ${pay_addr}
pay_lovelace=${lovelace}
if [[ ${pay_lovelace} -gt 0 && ${base_lovelace} -gt 0 ]]; then
# Both payment and base address available with funds, let user choose what to use
say "Select source wallet address"
say "$(printf "%s\t\t${CYAN}%s${NC} ADA" "Funds :" "$(formatLovelace ${base_lovelace})")" "log"
say "$(printf "%s\t${CYAN}%s${NC} ADA" "Enterprise Funds :" "$(formatLovelace ${pay_lovelace})")" "log"
echo
case $(select_opt "[b] Base (default)" "[e] Enterprise" "[Esc] Cancel") in
0) s_addr="${base_addr}" ;;
1) s_addr="${pay_addr}" ;;
2) continue ;;
esac
elif [[ ${pay_lovelace} -gt 0 ]]; then
s_addr="${pay_addr}"
say "$(printf "%s\t${CYAN}%s${NC} ADA" "Enterprise Funds :" "$(formatLovelace ${pay_lovelace})")" "log"
elif [[ ${base_lovelace} -gt 0 ]]; then
s_addr="${base_addr}"
say "$(printf "%s\t\t${CYAN}%s${NC} ADA" "Funds :" "$(formatLovelace ${base_lovelace})")" "log"
else
say "${RED}ERROR${NC}: no funds available for wallet ${GREEN}${s_wallet}${NC}"
waitForInput && continue
fi
s_payment_sk_file="${WALLET_FOLDER}/${s_wallet}/${WALLET_PAY_SK_FILENAME}"
# Amount
echo
say " -- Amount to Send (in ADA) --"
echo
say "Valid entry: ${BLUE}Integer (e.g. 15) or Decimal (e.g. 956.1235) - commas allowed as thousand separator${NC}"
say " The string '${BLUE}all${NC}' to send all available funds in source wallet"
echo
say "Info: If destination and source wallet is the same and amount set to 'all',"
say " wallet will be defraged, ie converts multiple UTxO's to one"
echo
read -r -p "Amount (ADA): " amountADA
amountADA="${amountADA//,}"
if [[ "${amountADA}" != "all" ]]; then
if ! ADAtoLovelace "${amountADA}" >/dev/null; then
waitForInput && continue
fi
amountLovelace=$(ADAtoLovelace "${amountADA}")
echo
say "Fee payed by sender? [else amount sent is reduced]\n"
case $(select_opt "[y] Yes" "[n] No" "[Esc] Cancel") in
0) include_fee="no" ;;
1) include_fee="yes" ;;
2) continue ;;
esac
else
echo
getBalance ${s_addr}
amountLovelace=${lovelace}
say "ADA to send set to total supply: $(formatLovelace ${amountLovelace})" "log"
echo
include_fee="yes"
fi
# Destination
d_wallet=""
say "Is destination a local wallet or an address?\n"
case $(select_opt "[w] Wallet" "[a] Address" "[Esc] Cancel") in
0) d_wallet_dirs=()
if ! getDirs "${WALLET_FOLDER}"; then continue; fi # dirs() array populated with all wallet folders
for dir in "${dirs[@]}"; do
if getBaseAddress ${dir} || getPayAddress ${dir}; then
d_wallet_dirs+=("${dir}")
fi
done
say "Select Destination Wallet:\n"
if ! selectDir "${d_wallet_dirs[@]}"; then continue; fi # ${dir_name} populated by selectDir function
d_wallet="${dir_name}"
getBaseAddress ${d_wallet}
getPayAddress ${d_wallet}
if [[ -n "${base_addr}" && "${base_addr}" != "${s_addr}" && -n "${pay_addr}" && "${pay_addr}" != "${s_addr}" ]]; then
# Both base and enterprise address available, let user choose what to use
say "Select destination wallet address"
case $(select_opt "[b] Base (default)" "[e] Enterprise" "[Esc] Cancel") in
0) d_addr="${base_addr}" ;;
1) d_addr="${pay_addr}" ;;
2) continue ;;
esac
elif [[ -n "${base_addr}" && "${base_addr}" != "${s_addr}" ]]; then
d_addr="${base_addr}"
elif [[ -n "${pay_addr}" && "${pay_addr}" != "${s_addr}" ]]; then
d_addr="${pay_addr}"
elif [[ "${base_addr}" = "${s_addr}" || "${pay_addr}" = "${s_addr}" ]]; then
say "${RED}ERROR${NC}: sending to same address as source not supported"
waitForInput && continue
else
say "${RED}ERROR${NC}: no address found for wallet ${GREEN}${d_wallet}${NC} :("
waitForInput && continue
fi
;;
1) echo && read -r -p "Address: " d_addr ;;
2) continue ;;
esac
# Destination could be empty, if so without getting a valid address
if [[ -z ${d_addr} ]]; then
say "${RED}ERROR${NC}: destination address field empty"