Skip to content

Instantly share code, notes, and snippets.

@mrfelton
Last active July 30, 2019 07:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrfelton/db071f859a657b695914e585453febc4 to your computer and use it in GitHub Desktop.
Save mrfelton/db071f859a657b695914e585453febc4 to your computer and use it in GitHub Desktop.
Shell script to unlock lnd on system startup
#!/bin/bash
# Do nothing if the WALLET_PASSWORD var is not set.
if [ -z "$PASSWORD" ]; then
echo "[lnd_unlock] Please set PASSWORD in order to unlock wallet automatically."
s6-pause
fi
# output script content for easier debugging.
# set -x
# exit from script if error was raised.
set -e
# Add vim to get xxd
# FIXME: move this to a custom docker image.
apk add vim
# Add curl
apk add curl
# Add s6-pause
apk add s6-portable-utils
# return is used within bash function in order to return the value.
return() {
echo "$1"
}
# set_default function gives the ability to move the setting of default
# env variable from docker file to the script thereby giving the ability to the
# user override it durin container start.
set_default() {
# docker initialized env variables with blank string and we can't just
# use -z flag as usually.
BLANK_STRING='""'
VARIABLE="$1"
DEFAULT="$2"
if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then
if [ -z "$DEFAULT" ]; then
error "You should specify default variable"
else
VARIABLE="$DEFAULT"
fi
fi
return "$VARIABLE"
}
# Set default variables if needed.
NETWORK=$(set_default "$NETWORK" "testnet")
CHAIN=$(set_default "$CHAIN" "bitcoin")
LND_DIR=$(set_default "$LND_DIR" "$HOME/.lnd")
if [ ! -f "$LND_DIR/data/chain/$CHAIN/$NETWORK/admin.macaroon" ]; then
echo "[lnd_unlock] File not found: $LND_DIR/data/chain/$CHAIN/$NETWORK/admin.macaroon"
s6-pause
fi
echo "[lnd_unlock] Waiting 2 seconds for lnd..."
sleep 2;
echo "[lnd_unlock] Unlocking wallet..."
BASE64=`echo -n "$PASSWORD"|base64 | tr -d '\n\r'`
MACAROON_HEADER="Grpc-Metadata-macaroon: $(xxd -ps -u -c 1000 "$LND_DIR/data/chain/$CHAIN/$NETWORK/admin.macaroon")"
curl --silent --output /dev/null --show-error -X POST --cacert "$LND_DIR/tls.cert" --header $MACAROON_HEADER https://localhost:8080/v1/unlockwallet \
-d '{ "wallet_password": "'$BASE64'" }'
# Keep the process alive until we receive an exit signal.
s6-pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment