raspiblitz/home.admin/BBopenChannel.sh

142 lines
4.1 KiB
Bash
Raw Normal View History

2018-08-09 01:01:15 +02:00
#!/bin/bash
_temp="./download/dialog.$$"
_error="./.error.out"
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
echo ""
echo "*** Precheck ***"
# check if chain is in sync
2018-12-05 15:26:06 +01:00
chainInSync=$(lncli --chain=${network} --network=${chain}net getinfo | grep '"synced_to_chain": true' -c)
2018-08-09 01:01:15 +02:00
if [ ${chainInSync} -eq 0 ]; then
echo "FAIL - 'lncli getinfo' shows 'synced_to_chain': false"
echo "Wait until chain is sync with LND and try again."
echo ""
exit 1
fi
# check available funding
2018-12-05 15:26:06 +01:00
confirmedBalance=$(lncli --chain=${network} --network=${chain}net walletbalance | grep '"confirmed_balance"' | cut -d '"' -f4)
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."
echo ""
exit 1
2018-08-09 01:01:15 +02:00
fi
# check number of connected peers
2018-12-05 15:26:06 +01:00
numConnectedPeers=$(lncli --chain=${network} --network=${chain}net listpeers | grep pub_key -c)
2018-08-09 01:01:15 +02:00
if [ ${numConnectedPeers} -eq 0 ]; then
echo "FAIL - no peers connected on lightning network"
echo "You can only open channels to peer nodes to connected to first."
echo "Use CONNECT peer option in main menu first."
echo ""
exit 1
fi
# let user pick a peer to open a channels with
OPTIONS=()
while IFS= read -r grepLine
do
pubKey=$(echo ${grepLine} | cut -d '"' -f4)
2018-08-09 01:07:39 +02:00
#echo "grepLine(${pubKey})"
OPTIONS+=(${pubKey} "")
2018-12-05 15:26:06 +01:00
done < <(lncli --chain=${network} --network=${chain}net listpeers | grep pub_key)
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
echo "Selected CANCEL"
echo ""
exit 1
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
2018-08-09 01:01:15 +02:00
_error="./.error.out"
2018-12-05 15:26:06 +01:00
lncli --chain=${network} openchannel --network=${chain}net ${CHOICE} 1 0 2>$_error
2018-08-09 01:01:15 +02:00
error=`cat ${_error}`
if [ $(echo "${error}" | grep "channel is too small" -c) -eq 1 ]; then
minSat=$(echo "${error}" | tr -dc '0-9')
fi
# let user enter a amount
2018-08-09 01:07:39 +02:00
l1="Amount in SATOSHI 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 $_temp
if [ ${#amount} -eq 0 ]; then
echo "FAIL - not a valid input (${amount})"
exit 1
fi
# build command
2018-12-05 15:26:06 +01:00
command="lncli --chain=${network} --network=${chain}net openchannel ${pubKey} ${amount} 0"
2018-08-09 01:01:15 +02:00
# info output
clear
echo "******************************"
echo "Open Channel"
echo "******************************"
echo ""
echo "COMMAND LINE: "
echo $command
echo ""
echo "RESULT:"
# execute command
result=$($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}"
2018-08-09 01:01:15 +02:00
echo ""
2018-08-09 01:25:52 +02:00
echo "Whats next? --> You need to wait 6 confirmations, for the channel to be ready."
2018-08-22 21:18:04 +02:00
fundingTX=$(echo "${result}" | grep 'funding_txid' | cut -d '"' -f4)
if [ "${network}" = "bitcoin" ]; then
if [ "${chain}" = "main" ]; then
2018-12-12 15:16:37 +01:00
echo "https://live.blockcypher.com/btc/tx/${fundingTX}"
2018-08-22 21:18:04 +02:00
else
2018-12-12 15:16:37 +01:00
echo "https://live.blockcypher.com/btc-testnet/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
echo ""