Skip to content

Instantly share code, notes, and snippets.

@janvojt
Last active April 15, 2022 20:03
Show Gist options
  • Save janvojt/21dbc491f6ea2dc8cfb787be2d415f7a to your computer and use it in GitHub Desktop.
Save janvojt/21dbc491f6ea2dc8cfb787be2d415f7a to your computer and use it in GitHub Desktop.
.idea/
*.iml
*~
#!/bin/bash
# LND wallet auto-unlock script
# always sleep for some time in case lnd was started at the same time with lnd-unlock
sleep 10
LN_ROOT=$HOME/.lnd
echo "Will use '${LN_ROOT}' as lnd data dir."
restlisten=localhost:8080
# read LND configuration
eval $(cat ${LN_ROOT}/lnd.conf | grep -v ^\\[ | grep -v "^#" | grep -v "^;" | sed -r ':a s/^([^=.]*)[.-]/\1_/; t a')
bitcoind_service_name=bitcoind
lnd_service_name=lnd
if [[ ${bitcoin_testnet} -eq 1 ]]; then
lnd_service_name=lnd-test
bitcoind_service_name=bitcoind-test
fi
# Delay is needed to make sure bitcoind and lnd are ready.
now=$(date +%s)
bitcoind_sleep=1200
bitcoind_start=$(date -d "$(systemctl status ${bitcoind_service_name} | grep since | egrep -o "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")" +%s)
bitcoind_ago=$(date -d "$bitcoind_sleep seconds ago" +%s)
bitcoind_to_sleep=0
if [[ ${bitcoind_start} -gt ${bitcoind_ago} ]]; then
bitcoind_to_sleep=$(echo "$bitcoind_sleep - ($now - $bitcoind_start)" | bc)
fi
lnd_sleep=60
lnd_start=$(date -d "$(systemctl status ${lnd_service_name} | grep since | egrep -o "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")" +%s)
lnd_ago=$(date -d "$lnd_sleep seconds ago" +%s)
echo "Lightning service started at ${lnd_start}."
lnd_to_sleep=0
if [[ ${lnd_start} -gt ${lnd_ago} ]]; then
lnd_to_sleep=$(echo "$lnd_sleep - ($now - $lnd_start)" | bc)
fi
to_sleep=0
if [[ ${bitcoind_to_sleep} -gt ${lnd_to_sleep} ]]; then
to_sleep=${bitcoind_to_sleep}
echo "Will wait for ${to_sleep} seconds for bitcoind to initialize completely."
else
to_sleep=${lnd_to_sleep}
echo "Will wait for ${to_sleep} seconds for lnd to initialize completely."
fi
sleep ${to_sleep}
out=""
while [ -z "$out" ]; do
{ out=$(curl -v \
-H "Grpc-Metadata-macaroon: $(xxd -ps -u -c 1000 ${LN_ROOT}/data/chain/bitcoin/*/admin.macaroon)" \
--cacert ${LN_ROOT}/tls.cert \
-d "{\"wallet_password\": \"$(cat $HOME/.lnd-wallet-password | tr -d '\n' | base64 -w0)\"}" \
https://${restlisten}/v1/unlockwallet \
| tee >(grep "wallet already unlocked") >&3); } 3>&1
sleep 300
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment