1+ #! /bin/bash
2+ set -e
3+
4+ echo " [initunlocklnd] Waiting 2 seconds for lnd..."
5+ sleep 2
6+
7+ # ensure that lnd is up and running before proceeding
8+ while
9+ CA_CERT=" $LND_DATA /tls.cert"
10+ LND_WALLET_DIR=" $LND_DATA /data/chain/$1 /$2 /"
11+ MACAROON_FILE=" $LND_DATA /admin.macaroon"
12+ MACAROON_HEADER=" r0ckstar:dev"
13+ if [ -f " $MACAROON_FILE " ]; then
14+ MACAROON_HEADER=" Grpc-Metadata-macaroon:$( xxd -p -c 10000 " $MACAROON_FILE " | tr -d ' ' ) "
15+ fi
16+
17+ STATUS_CODE=$( curl -s --cacert " $CA_CERT " -H $MACAROON_HEADER -o /dev/null -w " %{http_code}" $LND_REST_LISTEN_HOST /v1/getinfo)
18+ # if lnd is running it'll either return 200 if unlocked (noseedbackup=1) or 404 if it needs initialization/unlock
19+ if [ " $STATUS_CODE " == " 200" ] || [ " $STATUS_CODE " == " 404" ] ; then
20+ break
21+ # or 500 from version 0.13.1 onwards because it breaks with `wallet not created, create one to enable full RPC access` error
22+ elif [ " $STATUS_CODE " == " 500" ] ; then
23+ STATUS_CODE=$( curl -s --cacert " $CA_CERT " -H $MACAROON_HEADER $LND_REST_LISTEN_HOST /v1/state)
24+ if [ " $STATUS_CODE " == " {\" state\" :\" NON_EXISTING\" }" ] || [ " $STATUS_CODE " == " {\" state\" :\" LOCKED\" }" ] ; then
25+ break # wallet ready to be either created or unlocked
26+ fi
27+ # for {\"state\":\"UNLOCKED\"}" we will depend on that previous condition with STATUS_CODE 200 or 404
28+ # because even though wallet is unlocked, /v1/getinfo will still keep returning 500 until it's ready
29+
30+ echo " [initunlocklnd] Still waiting on LND, got response for wallet status: $STATUS_CODE ... waiting another 2 seconds..."
31+ sleep 2
32+ else
33+ echo " [initunlocklnd] LND still didn't start, got $STATUS_CODE status code back... waiting another 2 seconds..."
34+ sleep 2
35+ fi
36+ do true ; done
37+
38+ # read variables after we ensured that lnd is up
39+ CA_CERT=" $LND_DATA /tls.cert"
40+ LND_WALLET_DIR=" $LND_DATA /data/chain/$1 /$2 /"
41+ MACAROON_FILE=" $LND_DATA /admin.macaroon"
42+ MACAROON_HEADER=" r0ckstar:dev"
43+ if [ -f " $MACAROON_FILE " ]; then
44+ MACAROON_HEADER=" Grpc-Metadata-macaroon:$( xxd -p -c 10000 " $MACAROON_FILE " | tr -d ' ' ) "
45+ fi
46+
47+ WALLET_FILE=" $LND_WALLET_DIR /wallet.db"
48+ LNDUNLOCK_FILE=${WALLET_FILE/ wallet.db/ walletunlock.json}
49+ if [ -f " $WALLET_FILE " ]; then
50+ if [ ! -f " $LNDUNLOCK_FILE " ]; then
51+ echo " [initunlocklnd] WARNING: UNLOCK FILE DOESN'T EXIST! MIGRATE LEGACY INSTALLATION TO NEW VERSION ASAP"
52+ else
53+ echo " [initunlocklnd] Wallet and Unlock files are present... parsing wallet password and unlocking lnd"
54+
55+ # parse wallet password from unlock file
56+ WALLETPASS=$( jq -c -r ' .wallet_password' $LNDUNLOCK_FILE )
57+ # Nicolas deleted default password in some wallet unlock files, so we initializing default if password is empty
58+ [ " $WALLETPASS " == " " ] && WALLETPASS=" hellorockstar"
59+ # Corrected password (removing newlines before encoding).
60+ # previous versions will have a default wallet password including a line feed at the end "hellorockstar\n"
61+ # line feed hex code 0x0A. So we first try the password without the line feed if it fails we try it with
62+ # the older version.
63+ WALLETPASS_BASE64=$( echo $WALLETPASS | tr -d ' \n\r' | base64)
64+
65+ response=$( curl -s --cacert " $CA_CERT " -X POST -H " $MACAROON_HEADER " \
66+ -d ' { "wallet_password":"' $WALLETPASS_BASE64 ' " }' $LND_REST_LISTEN_HOST /v1/unlockwallet)
67+
68+ # Check for failure (e.g., incorrect password)
69+ if [[ " $response " == * " invalid" * ]]; then
70+ # If it fails, try the original password with linefeed
71+ WALLETPASS_BASE64_CURRENT=$( echo $WALLETPASS | base64)
72+
73+ # Now we change the password so that the line feed is removed.
74+ # The correct password is already written to the unlock file so we don't need
75+ # to change that. Moreover the changepassword call will change + unlock the wallet
76+ # there is no need to call unlockwallet after this call.
77+ change_password_response=$( curl -s --cacert " $CA_CERT " -X POST -H " $MACAROON_HEADER " \
78+ -d ' { "current_password":"' $WALLETPASS_BASE64_CURRENT ' ", "new_password":"' $WALLETPASS_BASE64 ' " }' \
79+ $LND_REST_LISTEN_HOST /v1/changepassword)
80+
81+ # make sure the log end with a newline.
82+ echo $change_password_response
83+
84+ echo -n " [initunlocklnd] Changed wallet password removing the \" line feed\" character at the end. "
85+ echo " The password can be found in $LNDUNLOCK_FILE "
86+ else
87+ echo " [initunlocklnd] Wallet unlocking failed, lnd returned: $response "
88+ exit 1
89+ fi
90+ fi
91+ else
92+ echo " [initunlocklnd] Wallet file doesn't exist. Initializing LND instance with new autogenerated password and seed"
93+
94+ # generate seed mnemonic
95+ GENSEED_RESP=$( curl -s --cacert " $CA_CERT " -X GET -H $MACAROON_HEADER $LND_REST_LISTEN_HOST /v1/genseed)
96+ CIPHER_ARRAY_EXTRACTED=$( echo $GENSEED_RESP | jq -c -r ' .cipher_seed_mnemonic' )
97+
98+ # using static default password per feedback, randomly generated password would still be stored in cleartext
99+ WALLETPASS=" hellorockstar"
100+
101+ # save all the the data to unlock file we'll use for future unlocks
102+ RESULTJSON=' {"wallet_password":"' $WALLETPASS ' ", "cipher_seed_mnemonic":' $CIPHER_ARRAY_EXTRACTED ' }'
103+ mkdir -p $LND_WALLET_DIR
104+ echo $RESULTJSON > $LNDUNLOCK_FILE
105+
106+ # previous versions will have a default wallet password including a line feed at the end "hellorockstar\n"
107+ # line feed hex code 0x0A.
108+ WALLETPASS_BASE64=$( echo $WALLETPASS | tr -d ' \n\r' | base64)
109+ INITWALLET_REQ=' {"wallet_password":"' $WALLETPASS_BASE64 ' ", "cipher_seed_mnemonic":' $CIPHER_ARRAY_EXTRACTED ' }'
110+
111+ # execute initwallet call
112+ curl -s --cacert " $CA_CERT " -X POST -H " $MACAROON_HEADER " -d " $INITWALLET_REQ " $LND_REST_LISTEN_HOST /v1/initwallet
113+ fi
114+
115+ # LND unlocked, now run Loop
116+
117+ if [ ! -z " $LND_HOST_FOR_LOOP " ]; then
118+ echo " [initunlocklnd] Preparing to start Loop"
119+
120+ if [ $LND_ENVIRONMENT == " regtest" ] || [ $LND_ENVIRONMENT == " signet" ]; then
121+ echo " [initunlocklnd] Loop can't be started for regtest and signet"
122+ elif [ -f " $MACAROON_FILE " ]; then
123+ sleep 10
124+
125+ echo " [initunlocklnd] Starting Loop"
126+ ./bin/loopd --network=$2 --lnd.macaroonpath=$MACAROON_FILE --lnd.host=$LND_HOST_FOR_LOOP --restlisten=0.0.0.0:8081 &
127+ else
128+ echo " [initunlocklnd] Loop can't be started without MACAROON"
129+ fi
130+ fi
0 commit comments