-
Notifications
You must be signed in to change notification settings - Fork 0
/
unlock.sh
executable file
·65 lines (58 loc) · 2.2 KB
/
unlock.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
#!/usr/bin/env bash
# If wallet is already unlocked, short-circuit.
hash=`printf "$TYPED_PASSCODE" | sha256sum | cut -f1 -d' '`
if [ "$hash" = "$saved_hash" ]; then
# Make sure that TYPED_PASSCODE is exported so it can be used elsewhere.
export TYPED_PASSCODE=$TYPED_PASSCODE
printf "Wallet unlocked.\n"
return
fi
# Also short-circuit if we're using a hardcoded passcode for developer convenience.
saved_hash=`head -n 1 ~/.passcode-hash`
if [ "$saved_hash" = "4aa6892909e369933b9f1babc10519121e2dfd1042551f6b9bdd4eae51f1f0c2" ] ; then
printf "Using hard-coded passcode.\n"
export TYPED_PASSCODE="111111111111111111111"
return
fi
if [ "$1" = "--debug" ]; then printf "Saved passcode hash = $saved_hash.\n"; fi
printf "\n"
hash=""
reprompt="true"
trap 'reprompt="false"' INT
while :
do
printf "\033[0;33mEnter 21-char passcode to unlock wallet:\033[00m "
# Get input from user (and if CTRL+C is pressed, break immediately).
POSIXLY_CORRECT=1 read -s TYPED_PASSCODE
# If we got here because of CTRL+C, quit gracefully with warning.
if [ "$reprompt" = "false" ]; then
printf "\r\033[0;31mUnknown passcode makes wallet temporarily unusable for KERI tasks.\033[00m\n"
return
fi
# Evaluate by comparing hash of passcode to stored hash. If they match, then
# we can set TYPED_PASSCODE to what the user provided, and we know KERI will
# be able to unlock the wallet, so we're done prompting. Otherwise, prompt
# the user to try again.
hash=`printf "$TYPED_PASSCODE" | sha256sum | cut -f1 -d' '`
if [ "$1" = "--debug" ]; then printf "Hash of that passcode = $hash.\n"; fi
if [ "$hash" = "$saved_hash" ]; then
printf "\rWallet unlocked."
# Clear till end of line
tput el
printf "\n"
export TYPED_PASSCODE="$TYPED_PASSCODE"
break
else
len=${#TYPED_PASSCODE}
if [ $len -lt 5 ]; then
hint="${TYPED_PASSCODE:0:1}..."
else
hint="${TYPED_PASSCODE:0:2}...${TYPED_PASSCODE: -2:2}"
fi
printf "\r\033[0;31mPasscode %s (%d chars) doesn't match.\033[00m" $hint ${#TYPED_PASSCODE}
tput el
printf "\n"
fi
done
# Make sure CTRL+C is no longer trapped by the handler from this script.
trap - INT