|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +if [[ "$1" == "elements-cli" || "$1" == "elements-tx" || "$1" == "elementsd" || "$1" == "test_elements" ]]; then |
| 5 | + mkdir -p "$ELEMENTS_DATA" |
| 6 | + |
| 7 | + CONFIG_PREFIX="" |
| 8 | + CHAIN="$ELEMENTS_CHAIN" |
| 9 | + NL=$'\n' |
| 10 | + if [[ "$CHAIN" ]]; then |
| 11 | + CONFIG_PREFIX="chain=$CHAIN\n[$CHAIN]" |
| 12 | + elif [[ "${ELEMENTS_NETWORK}" == "regtest" ]]; then |
| 13 | + CHAIN="${ELEMENTS_REGTEST_CHAIN:-regtest}" |
| 14 | + elif [[ "${ELEMENTS_NETWORK}" == "testnet" ]]; then |
| 15 | + CHAIN="${ELEMENTS_TESTNET_CHAIN:-testnet}" |
| 16 | + elif [[ "${ELEMENTS_NETWORK}" == "mainnet" ]]; then |
| 17 | + CHAIN="${ELEMENTS_MAINNET_CHAIN:-liquidv1}" |
| 18 | + else |
| 19 | + CHAIN="" |
| 20 | + fi |
| 21 | + |
| 22 | + if [[ "$CHAIN" ]]; then |
| 23 | + CONFIG_PREFIX="chain=${CHAIN}${NL}[${CHAIN}]" |
| 24 | + fi |
| 25 | + |
| 26 | + if [[ "$ELEMENTS_WALLETDIR" ]] && [[ "$CHAIN" ]]; then |
| 27 | + |
| 28 | + WALLETDIR="$ELEMENTS_WALLETDIR/${CHAIN}" |
| 29 | + mkdir -p "$WALLETDIR" |
| 30 | + [[ -f "${WALLETDIR}/db.log" ]] && mkdir -p "${WALLETDIR}/${CHAIN}/wallets" && mv "${WALLETDIR}/db.log" "${WALLETDIR}/${CHAIN}/wallets/db.log" |
| 31 | + [[ -f "${WALLETDIR}/wallet.dat" ]] && mkdir -p "${WALLETDIR}/${CHAIN}/wallets" && mv "${WALLETDIR}/wallet.dat" "${WALLETDIR}/${CHAIN}/wallets/wallet.dat" |
| 32 | + [[ -d "${WALLETDIR}/database" ]] && mkdir -p "${WALLETDIR}/${CHAIN}/wallets" && mv "${WALLETDIR}/database" "${WALLETDIR}/${CHAIN}/wallets/database" |
| 33 | + chown -R elements:elements "$WALLETDIR" |
| 34 | + CONFIG_PREFIX="${CONFIG_PREFIX}${NL}walletdir=${WALLETDIR}/${CHAIN}/wallets${NL}" |
| 35 | + WALLETFILE="${WALLETDIR}/${CHAIN}/wallets/wallet.dat" |
| 36 | + if ! [[ -f "${WALLETFILE}" ]]; then |
| 37 | + echo "The wallet does not exists, creating it at ${WALLETDIR}..." |
| 38 | + gosu elements elements-wallet "-datadir=${WALLETDIR}" "-wallet=" create |
| 39 | + fi |
| 40 | + fi |
| 41 | + |
| 42 | + cat <<-EOF > "$ELEMENTS_DATA/elements.conf" |
| 43 | + ${CONFIG_PREFIX} |
| 44 | + printtoconsole=1 |
| 45 | + rpcallowip=::/0 |
| 46 | + ${ELEMENTS_EXTRA_ARGS} |
| 47 | + EOF |
| 48 | + chown elements:elements "$ELEMENTS_DATA/elements.conf" |
| 49 | + |
| 50 | + if [[ "${ELEMENTS_TORCONTROL}" ]]; then |
| 51 | + # Because elementsd only accept torcontrol= host as an ip only, we resolve it here and add to config |
| 52 | + TOR_CONTROL_HOST=$(echo ${ELEMENTS_TORCONTROL} | cut -d ':' -f 1) |
| 53 | + TOR_CONTROL_PORT=$(echo ${ELEMENTS_TORCONTROL} | cut -d ':' -f 2) |
| 54 | + if [[ "$TOR_CONTROL_HOST" ]] && [[ "$TOR_CONTROL_PORT" ]]; then |
| 55 | + TOR_IP=$(getent hosts $TOR_CONTROL_HOST | cut -d ' ' -f 1) |
| 56 | + echo "torcontrol=$TOR_IP:$TOR_CONTROL_PORT" >> "$ELEMENTS_DATA/elements.conf" |
| 57 | + echo "Added "torcontrol=$TOR_IP:$TOR_CONTROL_PORT" to $ELEMENTS_DATA/elements.conf" |
| 58 | + else |
| 59 | + echo "Invalid ELEMENTS_TORCONTROL" |
| 60 | + fi |
| 61 | + fi |
| 62 | + |
| 63 | + # ensure correct ownership and linking of data directory |
| 64 | + # we do not update group ownership here, in case users want to mount |
| 65 | + # a host directory and still retain access to it |
| 66 | + chown -R elements "$ELEMENTS_DATA" |
| 67 | + ln -sfn "$ELEMENTS_DATA" /home/elements/.elements |
| 68 | + chown -h elements:elements /home/elements/.elements |
| 69 | + |
| 70 | + exec gosu elements "$@" |
| 71 | +else |
| 72 | + exec "$@" |
| 73 | +fi |
0 commit comments