raspiblitz/home.admin/config.scripts/lnd.unlock.sh

97 lines
2.8 KiB
Bash
Raw Normal View History

2020-05-28 01:15:47 +02:00
#!/bin/bash
2020-05-28 01:34:09 +02:00
if [ "$1" == "-h" ] || [ "$1" == "help" ]; then
2020-05-28 01:15:47 +02:00
echo "script to unlock LND wallet"
echo "lnd.unlock.sh [passwordC]"
exit 1
fi
2020-05-28 01:29:52 +02:00
# check if wallet is already unlocked
2020-05-28 01:41:45 +02:00
echo "# checking LND wallet ... (can take some time)"
2020-05-28 03:31:07 +02:00
walletUnlocked=$(echo "" | sudo -u bitcoin lncli unlock --stdin 2>&1 | grep -c "already unlocked")
2020-05-28 01:29:52 +02:00
if [ ${walletUnlocked} -eq 1 ]; then
echo "# OK LND wallet was already unlocked"
2020-05-28 01:36:35 +02:00
exit 0
2020-05-28 01:29:52 +02:00
fi
2020-05-28 01:15:47 +02:00
# 1. parameter
passwordC="$1"
# if no password check if stored for auto-unlock
if [ ${#passwordC} -eq 0 ]; then
autoUnlockExists=$(sudo ls /root/lnd.autounlock.pwd 2>/dev/null | grep -c "lnd.autounlock.pwd")
if [ ${autoUnlockExists} -eq 1 ]; then
echo "# using auto-unlock"
passwordC=$(sudo cat /root/lnd.autounlock.pwd)
fi
fi
# if still no password get from user
manualEntry=0
if [ ${#passwordC} -eq 0 ]; then
2020-05-28 02:50:30 +02:00
echo "# manual input"
2020-05-28 01:15:47 +02:00
manualEntry=1
passwordC=$(whiptail --passwordbox "\nEnter Password C to unlock wallet:\n" 9 52 "" --title " LND Wallet " --backtitle "RaspiBlitz" 3>&1 1>&2 2>&3)
fi
loopCount=0
while :
do
# TRY TO UNLOCK ...
loopCount=$(($loopCount +1))
2020-05-28 03:31:07 +02:00
echo "# calling: lncli unlock"
2020-05-28 02:56:21 +02:00
result=$(echo "$passwordC" | sudo -u bitcoin lncli unlock --recovery_window=5000 --stdin 2>&1)
2020-05-28 01:41:45 +02:00
wasUnlocked=$(echo "${result}" | grep -c 'successfully unlocked')
wrongPassword=$(echo "${result}" | grep -c 'invalid passphrase')
2020-05-28 01:39:33 +02:00
if [ ${wasUnlocked} -gt 0 ]; then
2020-05-28 01:15:47 +02:00
# SUCCESS UNLOCK
echo "# OK LND wallet unlocked"
2020-05-28 01:35:24 +02:00
exit 0
2020-05-28 01:15:47 +02:00
2020-05-28 01:39:33 +02:00
elif [ ${wrongPassword} -gt 0 ]; then
2020-05-28 01:15:47 +02:00
# WRONG PASSWORD
echo "# wrong password"
if [ ${manualEntry} -eq 1 ]; then
passwordC=$(whiptail --passwordbox "\nEnter Password C again:\n" 9 52 "" --title " Password was Wrong " --backtitle "RaspiBlitz - LND Wallet" 3>&1 1>&2 2>&3)
else
2020-05-28 01:36:35 +02:00
echo "error='wrong password'"
2020-05-28 01:35:24 +02:00
exit 1
2020-05-28 01:15:47 +02:00
fi
else
# UNKOWN RESULT
2020-05-28 01:29:52 +02:00
# check if wallet was unlocked anyway
walletUnlocked=$(echo "" | sudo -u bitcoin lncli unlock --stdin 3>&1 1>&2 2>&3 | grep -c "already unlocked")
if [ ${walletUnlocked} -eq 1 ]; then
echo "# OK LND wallet unlocked"
2020-05-28 01:35:24 +02:00
exit 0
2020-05-28 01:29:52 +02:00
fi
2020-05-28 01:15:47 +02:00
echo "# unkown error"
if [ ${manualEntry} -eq 1 ]; then
2020-05-28 12:48:28 +02:00
LND
whiptail --title " LND ERROR " --msgbox "${result}" --ok-button "Try CLI" 8 60
# fall back to direct CLI input
echo "Calling: lncli unlock"
echo "Please re-enter Password C:"
lncli unlock --recovery_window=5000
exit 1
2020-05-28 01:15:47 +02:00
else
# maybe lncli is waiting to get ready (wait and loop)
if [ ${loopCount} -gt 10 ]; then
echo "error='failed to unlock'"
2020-05-28 01:35:24 +02:00
exit 1
2020-05-28 01:15:47 +02:00
fi
sleep 2
fi
fi
done