forked from axelarnetwork/tofnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·119 lines (93 loc) · 2.92 KB
/
entrypoint.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
#!/bin/bash
set -e
OK=0
ERR=1
# create: create a new mnemonic and export it to $EXPORT_PATH
create_mnemonic() {
echo "Creating mnemonic ..."
# check if mnemonic path exists
if [ -f "$TOFND_HOME/kvstore/kv/db" ]; then
echo "Skipping create because a kv-store was found at $TOFND_HOME"
return $ERR
fi
(echo ${PASSWORD} | tofnd ${ARGS} -m create) && echo "... ok" && return $OK
return $ERR
}
# import: import a mnemonic from $IMPORT_PATH
import_mnemonic() {
echo "Importing mnemonic ..."
if [ -f "$TOFND_HOME/kvstore/kv/db" ]; then
echo "Skipping import because a kv-store already exists at $TOFND_HOME"
return $ERR
fi
if [ ! -f "$IMPORT_PATH" ]; then \
echo "No import file found at $IMPORT_PATH"
return $ERR
fi
if [ -n "${NOPASSWORD}" ]; then \
echo "No password"
( cat $IMPORT_PATH | tofnd ${ARGS} -m import ) || return $ERR
else
echo "With password"
( (echo $PASSWORD && cat $IMPORT_PATH) | tofnd ${ARGS} -m import ) || return $ERR
fi
echo "... ok"
return $OK
}
# export: export the mnemonic to $EXPORT_PATH
export_mnemonic() {
echo "Exporting mnemonic ..."
echo ${PASSWORD} | tofnd ${ARGS} -m export || return $ERR
echo "... ok"
return $OK
}
# Get password from env var
EMPTY_STRING=""
PASSWORD="${PASSWORD:-$EMPTY_STRING}"
# set tofnd root. TOFND_HOME can be set to a different path by the user.
TOFND_HOME=${TOFND_HOME:-"./.tofnd"}
IMPORT_PATH=$TOFND_HOME/import
EXPORT_PATH=$TOFND_HOME/export
echo "Using tofnd root:" $TOFND_HOME
# gather user's args
# add '--no-password' flag to args if enabled
ARGS=${NOPASSWORD:+"--no-password"}
# add '--address' flag to args if enabled
ARGS+=${ADDRESS:+" --address ${ADDRESS}"}
# add '--port' flag to args if enabled
ARGS+=${PORT:+" --port ${PORT}"}
# check mnemonic arg
if [ -n "${MNEMONIC_CMD}" ]; then \
case ${MNEMONIC_CMD} in
# auto: try to set up tofnd and then spin up tofnd with the existing mnemonic.
# Order of set up: 1) import mnemonic, 2) create mnemonic.
# If 2) then move the mnemonic to $IMPORT_PATH so that tofnd will not complain
auto)
echo "Trying import" && import_mnemonic \
|| (echo "... skipping. Trying to create" && create_mnemonic && mv $EXPORT_PATH $IMPORT_PATH) \
|| echo "... skipping"
;;
existing)
;;
create)
create_mnemonic || exit $ERR
exit $OK
;;
import)
import_mnemonic || exit $ERR
exit $OK
;;
export)
export_mnemonic || exit $ERR
exit $OK
;;
*)
echo "Unknown command: ${MNEMONIC_CMD}"
exit $ERR
;;
esac
echo "Using existing mnemonic ..."
ARGS+=" -m existing"
fi
# execute tofnd daemon
exec echo ${PASSWORD} | tofnd ${ARGS} "$@"; \