raspiblitz/home.admin/BBopenChannel.sh

231 lines
6.9 KiB
Bash
Raw Normal View History

2018-08-09 01:01:15 +02:00
#!/bin/bash
_temp=$(mktemp -p /dev/shm/)
_error=$(mktemp -p /dev/shm/)
2018-08-09 01:01:15 +02:00
2018-12-05 15:26:06 +01:00
# load raspiblitz config data (with backup from old config)
2019-02-02 23:49:04 +01:00
source /home/admin/raspiblitz.info
source /mnt/hdd/raspiblitz.conf
2018-12-05 15:26:06 +01:00
if [ ${#network} -eq 0 ]; then network=`cat .network`; fi
if [ ${#network} -eq 0 ]; then network="bitcoin"; fi
2018-12-05 15:26:06 +01:00
if [ ${#chain} -eq 0 ]; then
echo "gathering chain info ... please wait"
chain=$(${network}-cli getblockchaininfo | jq -r '.chain')
fi
2018-08-09 01:01:15 +02:00
source <(/home/admin/config.scripts/network.aliases.sh getvars $1 $2)
shopt -s expand_aliases
alias bitcoincli_alias="$bitcoincli_alias"
alias lncli_alias="$lncli_alias"
alias lightningcli_alias="$lightningcli_alias"
2021-05-22 20:33:37 +01:00
2018-08-09 01:01:15 +02:00
echo ""
echo "*** Precheck ***"
2021-05-22 20:33:37 +01:00
# PRECHECK) check if chain is in sync
if [ $LNTYPE = cln ];then
BLOCKHEIGHT=$($bitcoincli_alias getblockchaininfo|grep blocks|awk '{print $2}'|cut -d, -f1)
CLHEIGHT=$($lightningcli_alias getinfo | jq .blockheight)
2021-05-22 20:33:37 +01:00
if [ $BLOCKHEIGHT -eq $CLHEIGHT ];then
chainOutSync=0
else
chainOutSync=1
fi
elif [ $LNTYPE = lnd ];then
chainOutSync=$($lncli_alias getinfo | grep '"synced_to_chain": false' -c)
2021-05-22 20:33:37 +01:00
fi
if [ ${chainOutSync} -eq 1 ]; then
if [ $LNTYPE = cln ];then
echo "# FAIL PRECHECK - lncli getinfo shows 'synced_to_chain': false - wait until chain is sync "
else
echo "# FAIL PRECHECK - 'lightning-cli getinfo' blockheight is different from 'bitcoind getblockchaininfo' - wait until chain is sync "
fi
echo
echo "# PRESS ENTER to return to menu"
read key
2018-08-09 01:01:15 +02:00
exit 1
2021-05-22 20:33:37 +01:00
else
echo "# OK - the chain is synced"
2018-08-09 01:01:15 +02:00
fi
# check available funding
2021-05-22 20:33:37 +01:00
if [ $LNTYPE = cln ];then
for i in $($lightningcli_alias \
listfunds|jq .outputs[]|jq 'select(.status=="confirmed")'|grep value|awk '{print $2}'|cut -d, -f1);do
2021-05-22 20:33:37 +01:00
confirmedBalance=$((confirmedBalance+i))
done
elif [ $LNTYPE = lnd ];then
confirmedBalance=$($lncli_alias walletbalance | grep '"confirmed_balance"' | cut -d '"' -f4)
2021-05-22 20:33:37 +01:00
fi
2018-08-09 01:04:52 +02:00
if [ ${confirmedBalance} -eq 0 ]; then
echo "FAIL - You have 0 SATOSHI in your confirmed LND On-Chain Wallet."
echo "Please fund your on-chain wallet first and wait until confirmed."
2021-05-22 20:33:37 +01:00
echo
echo "Press ENTER to return to main menu."
read key
2018-08-09 01:04:52 +02:00
exit 1
2018-08-09 01:01:15 +02:00
fi
# check number of connected peers
2021-05-22 20:33:37 +01:00
if [ $LNTYPE = cln ];then
numConnectedPeers=$($lightningcli_alias listpeers | grep -c '"id":')
2021-05-22 20:33:37 +01:00
elif [ $LNTYPE = lnd ];then
numConnectedPeers=$($lncli_alias listpeers | grep pub_key -c)
2021-05-22 20:33:37 +01:00
fi
2018-08-09 01:01:15 +02:00
if [ ${numConnectedPeers} -eq 0 ]; then
2021-05-22 20:33:37 +01:00
echo "FAIL - no peers connected on the lightning network"
2018-08-09 01:01:15 +02:00
echo "You can only open channels to peer nodes to connected to first."
echo "Use CONNECT peer option in main menu first."
2021-05-22 20:33:37 +01:00
echo
echo "Press ENTER to return to main menu."
read key
2018-08-09 01:01:15 +02:00
exit 1
fi
# let user pick a peer to open a channels with
OPTIONS=()
2021-05-22 20:33:37 +01:00
if [ $LNTYPE = cln ];then
while IFS= read -r grepLine
do
pubKey=$(echo ${grepLine} | cut -d '"' -f4)
# echo "grepLine(${pubKey})"
OPTIONS+=(${pubKey} "")
done < <(lightningcli_alias listpeers | grep '"id":')
elif [ $LNTYPE = lnd ];then
while IFS= read -r grepLine
do
pubKey=$(echo ${grepLine} | cut -d '"' -f4)
# echo "grepLine(${pubKey})"
OPTIONS+=(${pubKey} "")
done < <(lncli_alias listpeers | grep pub_key)
fi
2018-08-09 01:01:15 +02:00
TITLE="Open (Payment) Channel"
MENU="\nChoose a peer you connected to, to open the channel with: \n "
pubKey=$(dialog --clear \
--title "$TITLE" \
--menu "$MENU" \
14 73 5 \
"${OPTIONS[@]}" \
2>&1 >/dev/tty)
clear
if [ ${#pubKey} -eq 0 ]; then
2021-05-22 20:33:37 +01:00
clear
echo
echo "no channel selected - returning to menu ..."
sleep 4
exit 1
2018-08-09 01:01:15 +02:00
fi
2018-08-09 01:25:52 +02:00
# find out what is the minimum amount
# TODO find a better way - also consider dust and channel reserve
2018-08-09 02:22:27 +02:00
# details see here: https://github.com/btcontract/lnwallet/issues/52
2018-08-09 01:01:15 +02:00
minSat=20000
2018-08-22 21:12:24 +02:00
if [ "${network}" = "bitcoin" ]; then
2019-07-25 23:39:37 +02:00
minSat=50000
2018-08-22 21:12:24 +02:00
fi
2021-05-22 20:33:37 +01:00
if [ $LNTYPE = lnd ];then
_error="./.error.out"
lncli_alias openchannel ${pubkey} 1 0 2>$_error
error=$(cat ${_error})
if [ $(echo "${error}" | grep "channel is too small" -c) -eq 1 ]; then
minSat=$(echo "${error}" | tr -dc '0-9')
fi
2018-08-09 01:01:15 +02:00
fi
2020-11-10 23:54:58 +00:00
# let user enter an amount
2021-05-22 20:33:37 +01:00
l1="Amount in satoshis to fund this channel:"
2018-08-09 01:01:15 +02:00
l2="min required : ${minSat}"
l3="max available : ${confirmedBalance}"
dialog --title "Funding of Channel" \
--inputbox "$l1\n$l2\n$l3" 10 60 2>$_temp
amount=$(cat $_temp | xargs | tr -dc '0-9')
shred -u $_temp
2018-08-09 01:01:15 +02:00
if [ ${#amount} -eq 0 ]; then
2020-02-17 14:40:31 +01:00
echo
echo "no valid amount entered - returning to menu ..."
sleep 4
2018-08-09 01:01:15 +02:00
exit 1
fi
2020-11-10 23:54:58 +00:00
# let user enter a confirmation target
l1=""
l2="Urgent = 1 / Economy = 20"
dialog --title "Set confirmation target" \
--inputbox "$l1\n$l2" 10 60 2>$_temp
conf_target=$(cat $_temp | xargs | tr -dc '0-9')
shred -u $_temp
if [ ${#conf_target} -eq 0 ]; then
echo
echo "no valid target entered - returning to menu ..."
sleep 4
exit 1
fi
2018-08-09 01:01:15 +02:00
# build command
2021-05-22 20:33:37 +01:00
if [ $LNTYPE = cln ];then
# fundchannel id amount [feerate] [announce] [minconf] [utxos] [push_msat] [close_to]
feerate=$($bitcoincli_alias estimatesmartfee $conf_target |grep feerate|awk '{print $2}'|cut -c 5-7|bc)
2021-05-22 20:33:37 +01:00
command="lightningcli_alias fundchannel ${pubKey} ${amount} $feerate"
elif [ $LNTYPE = lnd ];then
command="lncli_alias openchannel --conf_target=${conf_target} ${pubKey} ${amount} 0"
fi
2018-08-09 01:01:15 +02:00
# info output
clear
echo "******************************"
echo "Open Channel"
echo "******************************"
2021-05-22 20:33:37 +01:00
echo
2018-08-09 01:01:15 +02:00
echo "COMMAND LINE: "
echo $command
2021-05-22 20:33:37 +01:00
echo
2018-08-09 01:01:15 +02:00
echo "RESULT:"
# execute command
2021-05-22 20:33:37 +01:00
result=$(eval $command 2>$_error)
error=$(cat ${_error})
2018-08-09 01:25:52 +02:00
2018-08-09 01:28:53 +02:00
#echo "result(${result})"
#echo "error(${error})"
2018-08-09 01:01:15 +02:00
2018-08-09 01:25:52 +02:00
if [ ${#error} -gt 0 ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "FAIL"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "${error}"
2018-08-09 01:01:15 +02:00
else
echo "******************************"
echo "WIN"
echo "******************************"
2018-08-09 01:25:52 +02:00
echo "${result}"
2021-05-22 20:33:37 +01:00
echo
echo "What's next? --> You need to wait 3 confirmations for the channel to be ready."
if [ $LNTYPE = cln ];then
fundingTX=$(echo "${result}" | grep 'txid' | cut -d '"' -f4)
elif [ $LNTYPE = lnd ];then
fundingTX=$(echo "${result}" | grep 'funding_txid' | cut -d '"' -f4)
fi
echo
2018-08-22 21:18:04 +02:00
if [ "${network}" = "bitcoin" ]; then
if [ "${chain}" = "main" ]; then
2021-05-22 20:33:37 +01:00
#echo "https://live.blockcypher.com/btc/tx/${fundingTX}"
echo "https://mempool.space/tx/${fundingTX}"
elif [ "${chain}" = "test" ]||[ "${chain}" = "sig" ]; then
echo "https://mempool.space/${chain}net/tx/${fundingTX}"
fi
echo
echo "In the Tor Browser:"
if [ "${chain}" = "main" ]; then
echo "http://mempoolhqx4isw62xs7abwphsq7ldayuidyx2v2oethdhhj6mlo2r6ad.onion/tx/${fundingTX}"
elif [ "${chain}" = "test" ]||[ "${chain}" = "sig" ]; then
echo "http://mempoolhqx4isw62xs7abwphsq7ldayuidyx2v2oethdhhj6mlo2r6ad.onion/${chain}net/tx/${fundingTX}"
2018-08-22 21:18:04 +02:00
fi
fi
if [ "${network}" = "litecoin" ]; then
echo "https://live.blockcypher.com/ltc/tx/${fundingTX}/"
fi
2018-08-09 01:01:15 +02:00
fi
2021-05-22 20:33:37 +01:00
echo
2020-02-17 14:35:58 +01:00
echo "Press ENTER to return to main menu."
read key